diff --git a/docs/config/README.md b/docs/config/README.md index 8849003146..cb30fa1043 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -127,6 +127,13 @@ Provide config options to the used theme. The options will vary depending on the ## Markdown + +### markdown.link +- Type: `Object` +- Default: `{ blank: true }` + +Setting `markdown.link.blank` to `false` will cause all external links to open in the same window. + ### markdown.slugify - Type: `Function` diff --git a/docs/guide/markdown.md b/docs/guide/markdown.md index 8bb8e27b5d..4f5b5aa35a 100644 --- a/docs/guide/markdown.md +++ b/docs/guide/markdown.md @@ -48,11 +48,13 @@ Given the following directory structure: ### External Links -Outbound links automatically gets `target="_blank"`: +Outbound links automatically gets `target="_blank" rel="noopener noreferrer"`: - [vuejs.org](https://vuejs.org) - [VuePress on GitHub](https://github.com/vuejs/vuepress) +You could alter this this behavior by setting `config.markdown.link.blank = false`. See more [here](/config/#markdown-link). + ## Front Matter [YAML front matter](https://jekyllrb.com/docs/frontmatter/) is supported out of the box: diff --git a/lib/markdown/index.js b/lib/markdown/index.js index 0352781c45..9e664412ec 100644 --- a/lib/markdown/index.js +++ b/lib/markdown/index.js @@ -20,7 +20,9 @@ module.exports = ({ markdown = {}} = {}) => { // custom plugins .use(component) .use(highlightLines) - .use(convertRouterLink) + .use(convertRouterLink, Object.assign({ + blank: true + }, markdown.link)) .use(hoistScriptStyle) .use(containers) diff --git a/lib/markdown/link.js b/lib/markdown/link.js index 969fedde75..70a4df3fcd 100644 --- a/lib/markdown/link.js +++ b/lib/markdown/link.js @@ -1,8 +1,9 @@ // markdown-it plugin for: -// 1. adding target="_blank" to external links +// 1. adding target="_blank" and rel="noopener noreferrer" to external links +// (if opts.blank is true) // 2. converting internal links to -module.exports = md => { +module.exports = (md, opts) => { let hasOpenRouterLink = false md.renderer.rules.link_open = (tokens, idx, options, env, self) => { @@ -14,8 +15,10 @@ module.exports = md => { const isExternal = /^https?:/.test(href) const isSourceLink = /(\/|\.md|\.html)(#.*)?$/.test(href) if (isExternal) { - addAttr(token, 'target', '_blank') - addAttr(token, 'rel', 'noopener noreferrer') + if (opts.blank) { + addAttr(token, 'target', '_blank') + addAttr(token, 'rel', 'noopener noreferrer') + } } else if (isSourceLink) { hasOpenRouterLink = true tokens[idx] = toRouterLink(token, link)