yzhang-gh / vscode-markdown

Markdown All in One
https://marketplace.visualstudio.com/items?itemName=yzhang.markdown-all-in-one
MIT License
2.84k stars 322 forks source link

File contents destroyed, overwritten by Table of Contents #1264

Open ibkevg opened 1 year ago

ibkevg commented 1 year ago

I have plugin version 3.5.1. Twice now I have experienced md file content corruption caused by a list being misidentified by the plugin as a TOC, wiped out and replaced by a TOC. The problem I'm experiencing sounds similar to the one described in issue 683: https://github.com/yzhang-gh/vscode-markdown/issues/683

I found on the plugin's vscode page this comment: "In case you are seeing unexpected TOC recognition, you can add a <!-- no toc --> comment above the list." So if I've understood right, I would have to pre-emptively place this tag infront of every list I create otherwise I risk file corruption caused by unexpected TOC recognition(?)

I'd like to suggest instead that placement of table of contents not be done automatically but instead require the insertion of a tag, for example,

This would be much much safer.

I generally use markdown files for taking notes, making lists as I develop. Lots of headings, often with lists inside. The first time I experienced corruption, I noticed it in time and was able to undo and then play around trying to figure out how to convince the auto-detecter that my list was just a list and not some auto-generated TOC. In this case, replacing "-" list items with "*" list items seemed to work (I tried all kinds of other fairly random formatting changes, to no avail.) However, I just discovered that the problem happened again and I'm not sure how long ago the corruption took place. Unfortunately, the original list is long gone now so I can't send it to you for analysis.

ibkevg commented 1 year ago

I would like to add that aside from this issue I've been using your plugin for a quite a while and it's been very useful. In particular, previewing an md with embedded latex works really well

yzhang-gh commented 1 year ago

Thanks for the feedback.

So if I've understood right, I would have to pre-emptively place this tag in front of every list I create otherwise I risk file corruption caused by unexpected TOC recognition(?)

You will see a line of text "table of contents (out-of-date)" right before the list if it is recognized as a TOC. To be safe, you can disable toc.updateOnSave and update TOC manually when you need.

I'd like to suggest instead that placement of table of contents not be done automatically but instead require the insertion of a tag, for example,

This would be much much safer.

The TOC auto-detection is a feature designed at the right beginning of this extension, to avoid extra tags. I had updated the rules of TOC detection to reduce false alarms a few times so your case won't be common, e.g., the first line of the list should match the first heading, and the overall similarity should be larger than 0.5.

ibkevg commented 1 year ago

The TOC auto-detection is a feature designed at the right beginning of this extension, to avoid extra tags.

I'll likely have to turn off the updateOnSave feature - I agree it's pretty handy but when it fails, it fails big (otherwise, to be safe, I'd have to put the not-a-TOC tag infront of every list in my document.)

But maybe there is another solution. In both cases that caused corruption, the TOC was somehow determined to be in the middle of my document - which is a weird place to put a TOC. So maybe a reasonable restriction on the TOC auto-detect algorithm would be to look only for an existing TOC in the area prior to the first heading? Perhaps controlled by an option?

yzhang-gh commented 1 year ago

but when it fails, it fails big

Personally I use git to manage my notes so it won't cause trouble for me, but I see your concern.

So maybe a reasonable restriction on the TOC auto-detect algorithm would be to look only for an existing TOC in the area prior to the first heading? Perhaps controlled by an option?

I kind of agree with you. We can show a warning before updating a (not so sure) TOC in an unusual place.

sator-imaging commented 3 months ago

upvote!

Sometimes I want to write "selective" toc in markdown. Option to disable toc by per file basis is really really helpful!!