From 34b620e43f977f57a440ddac62e9824dcba7389c Mon Sep 17 00:00:00 2001 From: Jason Yu Date: Mon, 7 May 2018 20:07:50 +0100 Subject: [PATCH 1/2] added external links attrs --- lib/markdown/index.js | 5 ++++- lib/markdown/link.js | 7 ++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/markdown/index.js b/lib/markdown/index.js index 0352781c45..e945e550df 100644 --- a/lib/markdown/index.js +++ b/lib/markdown/index.js @@ -20,7 +20,10 @@ module.exports = ({ markdown = {}} = {}) => { // custom plugins .use(component) .use(highlightLines) - .use(convertRouterLink) + .use(convertRouterLink, Object.assign({ + target: '_blank', + rel: 'noopener noreferrer' + }, markdown.externalLinks)) .use(hoistScriptStyle) .use(containers) diff --git a/lib/markdown/link.js b/lib/markdown/link.js index 969fedde75..2ea7dfb920 100644 --- a/lib/markdown/link.js +++ b/lib/markdown/link.js @@ -2,7 +2,7 @@ // 1. adding target="_blank" to external links // 2. converting internal links to -module.exports = md => { +module.exports = (md, externalAttrs) => { let hasOpenRouterLink = false md.renderer.rules.link_open = (tokens, idx, options, env, self) => { @@ -14,8 +14,9 @@ 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') + Object.entries(externalAttrs).forEach(([key, val]) => { + addAttr(token, key, val) + }) } else if (isSourceLink) { hasOpenRouterLink = true tokens[idx] = toRouterLink(token, link) From 7a7c35701424ba01a40322b2ddb86345702608c6 Mon Sep 17 00:00:00 2001 From: Jason Yu Date: Mon, 7 May 2018 20:12:04 +0100 Subject: [PATCH 2/2] added 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..2c4cd8370b 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -134,6 +134,13 @@ Provide config options to the used theme. The options will vary depending on the Function for transforming header texts into slugs. This affects the ids/links generated for header anchors, table of contents and sidebar links. +### markdown.externalLinks + +- Type: `Object` +- Default: `{ target: '_blank', rel: 'noopener noreferrer' }` + +The key and value pair will be added to `` tags that points to an external link. The default option will open external links in a new window. + ### markdown.anchor - Type: `Object` diff --git a/docs/guide/markdown.md b/docs/guide/markdown.md index 8bb8e27b5d..ac883440a6 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 can customize the attributes added to external links by setting `config.markdown.externalLinks`. See more [here](/config/#markdown-externalLinks). + ## Front Matter [YAML front matter](https://jekyllrb.com/docs/frontmatter/) is supported out of the box: