-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/main'
- Loading branch information
Showing
2 changed files
with
34 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import MarkdownIt from 'markdown-it' | ||
|
||
export function getMarkdownRenderer() { | ||
const markdown = new MarkdownIt({ | ||
linkify: true, | ||
}) | ||
|
||
// source: https://github.com/markdown-it/markdown-it/blob/master/docs/architecture.md#renderer | ||
|
||
// Remember old renderer, if overridden, or proxy to default renderer | ||
const defaultRender = | ||
markdown.renderer.rules.link_open || | ||
function (tokens, idx, options, env, self) { | ||
return self.renderToken(tokens, idx, options) | ||
} | ||
|
||
markdown.renderer.rules.link_open = function (tokens, idx, options, env, self) { | ||
// If you are sure other plugins can't add `target` - drop check below | ||
const aIndex = tokens[idx].attrIndex('target') | ||
|
||
if (aIndex < 0) { | ||
tokens[idx].attrPush(['target', '_blank']) // add new attribute | ||
} else { | ||
tokens[idx].attrs[aIndex][1] = '_blank' // replace value of existing attr | ||
} | ||
|
||
// pass token to default renderer. | ||
return defaultRender(tokens, idx, options, env, self) | ||
} | ||
|
||
return markdown | ||
} |