Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle empty matter #65

Closed
alexander-akait opened this issue Mar 16, 2018 · 15 comments
Closed

Handle empty matter #65

alexander-akait opened this issue Mar 16, 2018 · 15 comments

Comments

@alexander-akait
Copy link

Input:

---
---

body{
    border: 1px solid red;
}

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

@thomasify
Copy link

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
Copy link
Owner

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

@alexander-akait
Copy link
Author

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

@jonschlinkert
Copy link
Owner

jonschlinkert commented Mar 16, 2018

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
Copy link
Owner

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
Copy link
Author

@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
Copy link

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

@jonschlinkert
Copy link
Owner

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

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

@jonschlinkert
Copy link
Owner

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
Copy link
Collaborator

doowb commented Mar 23, 2018

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

@alexander-akait
Copy link
Author

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

@jonschlinkert
Copy link
Owner

.isEmpty is good solution.

Great, I'll push it up.

@alexander-akait
Copy link
Author

@jonschlinkert friendly ping 👍

@jonschlinkert
Copy link
Owner

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

jonschlinkert added a commit that referenced this issue Apr 2, 2018
jonschlinkert added a commit that referenced this issue Apr 2, 2018
- closes #65
- upgraded to es6
@jonschlinkert jonschlinkert mentioned this issue Apr 2, 2018
@jonschlinkert
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants