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

Home Page:https://github.com/jonschlinkert

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Handle empty matter

alexander-akait opened this issue · comments

Input:

---
---

body{
    border: 1px solid red;
}

Maybe return file.empty option.
Based on prettier/prettier#4162 (comment)

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 👍

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