mathiassoeholm / markdown-link-updater

20 stars 9 forks source link

Mangled links #15

Closed jgillich closed 2 years ago

jgillich commented 2 years ago

Sample file from this repo

Moving that file to a sub-directory mangles the links

| **Used by**              | [Automatic Machine Pistol](../Automatic_Machine_Pistol.md) (AMP), [Repeate../Repeater.md.md), [Suppre../Suppressor.mdsor.md), ../Cycler.mdCycler.m../Gauss.mdss](Gauss.m../Punisher.mder](Punisher.md)../Mini-Chaingun.md](Mini-Chaingun.md) |

Haven't been able to figure out why but perhaps you have an idea.

mathiassoeholm commented 2 years ago

Thanks for submitting this, and I'm sorry for the super slow reply 😬

It appears to get mangled because you have multiple links on the same line. This is of course something I would like to support, so I will try to fix it when I have time :-)

I've added an initial failing unit test here with a smaller example: https://github.com/mathiassoeholm/markdown-link-updater/pull/17/files

jgillich commented 2 years ago

I think I just figured out why this is happening: https://github.com/mathiassoeholm/markdown-link-updater/blob/main/src/pure-get-edits.ts#L146

The ranges are generated before editing, but once you replace a link, the positions of all other links on the same line change (and that isn't being accounted for). That's why it gets worse and worse for each link

jgillich commented 2 years ago

If you're busy just let me know and I'll submit a fix

mathiassoeholm commented 2 years ago

You're absolutely right :-)

All edits are generated by the pureGetEdits function and are only applied afterward. This is also what makes the code easy to test, because pureGetEdits has no side effects, but only returns information about what needs to be done.

If edits occur on the same line, they should account for the edit that comes before it.

Feel free to submit a fix, that can make this test pass: https://github.com/mathiassoeholm/markdown-link-updater/pull/17/files (assuming I used the correct range value on line 668 🤞).

If it's too much trouble, I can give it a go within the next two weeks :-)