markdown-toc icon indicating copy to clipboard operation
markdown-toc copied to clipboard

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

Open maxime1992 opened this issue 5 years ago • 6 comments

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 :warning:) update:

image

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

maxime1992 avatar Sep 09 '19 06:09 maxime1992

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

doowb avatar Sep 10 '19 17:09 doowb

Thanks @doowb

You can give it a go with this file:

https://raw.githubusercontent.com/maxime1992/my-dev.to/master/blog-posts/enigma-part-2/enigma-part-2.md

maxime1992 avatar Sep 11 '19 08:09 maxime1992

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 :)

doowb avatar Sep 17 '19 00:09 doowb

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

dylmye avatar Oct 29 '19 14:10 dylmye

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 avatar May 10 '24 21:05 chalin

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

chalin avatar May 10 '24 22:05 chalin