diff options
author | Robin Appelman <robin@icewind.nl> | 2017-08-27 19:11:48 +0300 |
---|---|---|
committer | Robin Appelman <robin@icewind.nl> | 2017-08-27 19:11:48 +0300 |
commit | 1fcbadb340a39a03fcdacb8f76b8e770c80b1bf0 (patch) | |
tree | 367a01c8f21351306e5e3570de1904d7ded7f9cb | |
parent | 224f192dd71c8bbfa4d92f9b6fb0278c7061db9a (diff) |
Transform hash urls to point to the slugified id
Signed-off-by: Robin Appelman <robin@icewind.nl>
-rw-r--r-- | js/Renderer.ts | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/js/Renderer.ts b/js/Renderer.ts index 8c82032..7b0aba8 100644 --- a/js/Renderer.ts +++ b/js/Renderer.ts @@ -82,11 +82,15 @@ export class Renderer { slugify: slugifyHeading }); this.md.use(iterator, 'url_new_win', 'link_open', (tokens: MarkdownIt.Token[], idx: number) => { - tokens[idx].attrPush(['target', '_blank']); - tokens[idx].attrPush(['rel', 'noopener']); + const href = tokens[idx].attrGet('href') as string; + if (href[0] !== '#') { + tokens[idx].attrPush(['target', '_blank']); + tokens[idx].attrPush(['rel', 'noopener']); + } + tokens[idx].attrSet('href', this.getLinkUrl(href)) }); this.md.use(iterator, 'internal_image_link', 'image', (tokens: MarkdownIt.Token[], idx: number) => { - tokens[idx].attrSet('src', this.getUrl(tokens[idx].attrGet('src') as string)); + tokens[idx].attrSet('src', this.getImageUrl(tokens[idx].attrGet('src') as string)); }); } @@ -99,7 +103,14 @@ export class Renderer { return text; } - getUrl(path: string): string { + getLinkUrl(path: string): string { + if (path[0] === '#') { + return '#' + slugifyHeading(path.substr(1)); + } + return path; + } + + getImageUrl(path: string): string { if (!path) { return path; } |