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

Why's markdown-toc modifying my front-matter? #151

Open
maxime1992 opened this issue Sep 9, 2019 · 6 comments
Open

Why's markdown-toc modifying my front-matter? #151

maxime1992 opened this issue Sep 9, 2019 · 6 comments
Labels

Comments

@maxime1992
Copy link

I would expect markdown-toc to only modify what's inside the <!-- toc --><!-- tocstop --> block.

And from the README:

Won't mangle front-matter, or mistake front-matter properties for headings like other TOC generators

But when I run that on a blog post written in markdown and using a front-matter header to pass metadata, here's a (breaking ⚠️) update:

image

I tried to use the flag --no-stripHeadingTags but nothing changed and I don't think it's related to that.

@doowb
Copy link
Collaborator

doowb commented Sep 10, 2019

@maxime1992 thanks for the issue. I'll try to reproduce this when I have a chance.

@doowb doowb added the bug label Sep 10, 2019
@maxime1992
Copy link
Author

@doowb
Copy link
Collaborator

doowb commented Sep 17, 2019

I looked into this some and the reason the front-matter is being re-formatted is because markdown-toc uses gray-matter to parse out the front-matter before generating the TOC. When the -i flag is used to insert the TOC into the file, gray-matter is used again to stringify the front matter back into the file. js-yaml is used for the stringifying and outputs the values in the format from your screenshot.

I think to fix this, we need to update gray-matter with the following:

  • return the parsed front-matter and delimiters on the res object
  • add option to use the original front-matter (or a passed in string for front-matter) when stringifying

Then we can update markdown-toc with the new features in gray-matter to stringify using the original front-matter since this library doesn't modify it at all.

The other option is to parse out the front-matter manually in this repository and concat it back together with the updated contents, but there are some edge cases that gray-matter already handles and I'm not sure what exactly to look for.

If I can get to it this weekend, I'll try to make the to get this working. If anyone else gets to it first, I'm happy to review PRs :)

@dylmye
Copy link

dylmye commented Oct 29, 2019

@doowb any update on this? :) I think we need to update gray-matter anyway to fix #138

@chalin
Copy link

chalin commented May 10, 2024

I'm hitting this issue now as well.

In addition, when there's front matter some of the files, markdown-toc adds an extra blank line at the end every time I run it.

@chalin
Copy link

chalin commented May 10, 2024

The comment in the README.md stating that markdown-toc doesn't mangle front-matter should be removed until this issue is fixed IMHO.

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

No branches or pull requests

4 participants