jonschlinkert / gray-matter

Smarter YAML front matter parser, used by metalsmith, Gatsby, Netlify, Assemble, mapbox-gl, phenomic, vuejs vitepress, TinaCMS, Shopify Polaris, Ant Design, Astro, hashicorp, garden, slidev, saber, sourcegraph, and many others. Simple to use, and battle tested. Parses YAML by default but can also parse JSON Front Matter, Coffee Front Matter, TOML Front Matter, and has support for custom parsers. Please follow gray-matter's author: https://github.com/jonschlinkert
https://github.com/jonschlinkert
MIT License
3.97k stars 138 forks source link

Handle empty matter #65

Closed alexander-akait closed 6 years ago

alexander-akait commented 6 years ago

Input:

---
---

body{
    border: 1px solid red;
}

Maybe return file.empty option. Based on https://github.com/prettier/prettier/issues/4162#issuecomment-373735198

thomasify commented 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.

jonschlinkert commented 6 years ago

Why not just re-add empty front matter blocks when no data is returned?

alexander-akait commented 6 years ago

@jonschlinkert we have a lot of files, some contain matter blocks some not, we can't add matter blocks always

jonschlinkert commented 6 years ago

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.

jonschlinkert commented 6 years ago

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.

alexander-akait commented 6 years ago

@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?

thomasify commented 6 years ago

A work around is to add nonsense data. But it is nonsense data which is less optimal than leaving the empty blocks.

jonschlinkert commented 6 years ago

can we just add property (example empty) when we have empty matter?

that's a great idea, I'll push something up!

jonschlinkert commented 6 years ago

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?

doowb commented 6 years ago

I think the .isEmpty property is a good idea. I don't think .empty will be necessary.

alexander-akait commented 6 years ago

@jonschlinkert .isEmpty is good solution. Can you fix it?

jonschlinkert commented 6 years ago

.isEmpty is good solution.

Great, I'll push it up.

alexander-akait commented 6 years ago

@jonschlinkert friendly ping :+1:

jonschlinkert commented 6 years ago

Thanks for the reminder, I'll work on this today!

jonschlinkert commented 6 years ago

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