laobubu / HyperMD

A WYSIWYG Markdown Editor for browsers. Break the Wall between writing and previewing.
MIT License
1.47k stars 137 forks source link

Don't render text in [...] as links unless a link is provided #36

Open peey opened 6 years ago

peey commented 6 years ago

For instance, on here (and also in commonmark)

This:

[hi] [hello]

[hi]: http://google.com 

Renders as

[hi] [hello]

[hello] is not substituted because no link is provided. This is helpful when you're just using [...] in text without using links

But in HyperMD everything in [...] is treated as linked text. e.g

image

laobubu commented 6 years ago

To unstyle invalid bare links, the doc must be thoroughly parsed. However in CodeMirror, styling is an one-pass ( to keep the editor effective ). When a possible bare link appears , HyperMD assumes it's valid because it can't look ahead for the link definition footnotes.

However, this feature is still implementable by creating an add-on . The add-on shall check every visible bare links and unstyle the invalid ones via "markText" API . But this could be very slow, if the document is big.

Currently there's no plan to implement this.

peey commented 6 years ago

I was thinking that another strategy for this could be that we maintain a map of registered links where keys are the text within [...] and the link is specified.

Also maintain an index of all [.*] elements (let's call them tags, HyperMD must already do this since it styles them)

Now by default, assume that [...] doesn't refer to a link (because it's often used as a bracket in text).

On every text change, scan just the changed parts and update the map. Map updates wouldn't happen during regular text editing, this would only happen when you specifically write or edit a line which looks like the following

[tag]: some link

And if the map was updated, just go through the index in the background and update the required links (HyperMD must be already doing this too, since it provides a tooltip on hovering over the link)

However, I'm not actually familiar with structure of either CodeMirror or HyperMD so I don't know if this strategy is actually implementable. But if it's possible to just process the changed content instead of whole doc then it won't be necessary to scan the whole document every time