Closed alexander-akait closed 6 years ago
Specifically we would need the front matter to be empty to tell Jekyll that it should process the file. If it is stripped away by formatting then Jekyll copies the file verbatim.
Why not just re-add empty front matter blocks when no data is returned?
@jonschlinkert we have a lot of files, some contain matter blocks some not, we can't add matter blocks always
we can't add matter blocks always
We could possibly add an option for this, but the implementor (prettier) would still need to allow users to pass options to gray-matter.
Fwiw, we can't make this default behavior since it's not desirable behavior for any other implementation I've seen besides this use case.
Here is an example of how I personally would handle it (in prettier I guess, if they decide this should be handled there. if they don't we can discuss other options):
const str = `---
---
This is content`;
const file = matter(str);
console.log(file);
if (file.matter === '') {
file.content = str;
}
console.log(file);
edited: I updated the example.
actually, we might need to do a little more than what I did in my example since users often add comments to front-matter. Instead of if (file.matter === ''
) we might need to check length of file.data
keys or something.
@jonschlinkert we don't add new options, all should work with zero configuration,when we parse files without matter we always have empty
data, can we just add property
(example empty
) when we have empty matter?
A work around is to add nonsense data. But it is nonsense data which is less optimal than leaving the empty blocks.
can we just add property (example empty) when we have empty matter?
that's a great idea, I'll push something up!
Okay, after playing around with this. I propose we add an .isEmpty
property on the result. This way you can use the original string if the front-matter is empty. We can also add an .empty
property, as suggested, but it seems that it would be redundant.
thoughts?
I think the .isEmpty
property is a good idea. I don't think .empty
will be necessary.
@jonschlinkert .isEmpty
is good solution. Can you fix it?
.isEmpty is good solution.
Great, I'll push it up.
@jonschlinkert friendly ping :+1:
Thanks for the reminder, I'll work on this today!
Published to npm and git tagged as 4.0. file.isEmpty
is not a boolean. Let me know if you have questions or problems.
Thanks for the issue
Input:
Maybe return
file.empty
option. Based on https://github.com/prettier/prettier/issues/4162#issuecomment-373735198