Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global config for links attr #358

Merged
merged 2 commits into from
May 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<a>` tags that points to an external link. The default option will open external links in a new window.

### markdown.anchor

- Type: `Object`
Expand Down
4 changes: 3 additions & 1 deletion docs/guide/markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion lib/markdown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
7 changes: 4 additions & 3 deletions lib/markdown/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// 1. adding target="_blank" to external links
// 2. converting internal links to <router-link>

module.exports = md => {
module.exports = (md, externalAttrs) => {
let hasOpenRouterLink = false

md.renderer.rules.link_open = (tokens, idx, options, env, self) => {
Expand All @@ -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)
Expand Down