From 8e844649957f6b6957342e6383ca060381fb037e Mon Sep 17 00:00:00 2001 From: Jason Yu Date: Mon, 7 May 2018 19:43:42 +0100 Subject: [PATCH 1/2] added global config for links to opt out target _blank --- lib/markdown/index.js | 4 +++- lib/markdown/link.js | 11 +++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) 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) From 7662366a7761c82f7b41cecabc800427611efc59 Mon Sep 17 00:00:00 2001 From: Jason Yu Date: Mon, 7 May 2018 19:51:45 +0100 Subject: [PATCH 2/2] updated docs --- docs/config/README.md | 7 +++++++ docs/guide/markdown.md | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) 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: