valeriangalliat / markdown-it-anchor

A markdown-it plugin that adds an `id` attribute to headings and optionally permalinks.
The Unlicense
292 stars 72 forks source link

anchor was not added to correct path #51

Closed xilibro closed 5 years ago

xilibro commented 5 years ago

My page url is : http://localhost:8080/#/component/srclock but the anchor link is : http://localhost:8080/#/ji-chu-yong-fa

'component/srclok' part gone .

What problem is it would be? Who can give me some tips。

valeriangalliat commented 5 years ago

Hi! This is because by default markdown-it-anchor sets the href of the title anchor to #slug, meaning that if your slug is ji-chu-yong-fa, the href will be #ji-chu-yong-fa. The browser will then replace the whole anchor part of the URL, losing the /components/srclock part.

This is the default way of browsers to link to a specific element of the page. If you use the anchor part of the URL for internal navigation purpose, you can't use anchors to link to specific element of the page, and you would need to re-implement this behavior yourself.

For example, you could can pass a custom permalinkHref function that would keep the /components/srclock par of the URL and append the slug to it, so you would end up with something like #/components/srclock#ji-chu-yong-fa. It's then your responsibility to have some code parsing the location.hash to extract the ji-chu-yong-fa part at the end and make the browser scroll tho the element that have this ID.

nagaozen commented 5 years ago

@valeriangalliat is right, it's not an issue. The solution involves passing a custom permalinkHref.