docs: add target blank for all links (#5073)
This commit is contained in:
@@ -3,6 +3,7 @@ const MarkdownIt = require('markdown-it');
|
||||
const markdownItAnchor = require('markdown-it-anchor');
|
||||
const frontMatter = require('front-matter');
|
||||
const highlight = require('./highlight');
|
||||
const linkOpen = require('./link-open');
|
||||
const cardWrapper = require('./card-wrapper');
|
||||
const { slugify } = require('transliteration');
|
||||
|
||||
@@ -58,6 +59,7 @@ module.exports = function(source) {
|
||||
|
||||
options = {
|
||||
wrapper,
|
||||
linkOpen: true,
|
||||
...options
|
||||
};
|
||||
|
||||
@@ -68,5 +70,9 @@ module.exports = function(source) {
|
||||
source = fm.body;
|
||||
}
|
||||
|
||||
if (options.linkOpen) {
|
||||
linkOpen(parser);
|
||||
}
|
||||
|
||||
return options.wrapper(parser.render(source), fm);
|
||||
};
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
// add target="_blank" to all links
|
||||
module.exports = function linkOpen(md) {
|
||||
const defaultRender =
|
||||
md.renderer.rules.link_open ||
|
||||
function(tokens, idx, options, env, self) {
|
||||
return self.renderToken(tokens, idx, options);
|
||||
};
|
||||
|
||||
md.renderer.rules.link_open = function(tokens, idx, options, env, self) {
|
||||
const aIndex = tokens[idx].attrIndex('target');
|
||||
|
||||
if (aIndex < 0) {
|
||||
tokens[idx].attrPush(['target', '_blank']); // add new attribute
|
||||
}
|
||||
|
||||
return defaultRender(tokens, idx, options, env, self);
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user