Skip to content

Commit

Permalink
feat: support generic markdown file path reference (#509)
Browse files Browse the repository at this point in the history
  • Loading branch information
ulivz committed May 27, 2018
1 parent 5fcac1b commit 292e4bc
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 6 deletions.
36 changes: 30 additions & 6 deletions lib/markdown/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// 1. adding target="_blank" to external links
// 2. converting internal links to <router-link>

const indexRE = /(.*)(index|readme).md(#?.*)$/i

module.exports = (md, externalAttrs) => {
let hasOpenRouterLink = false
let hasOpenExternalLink = false
Expand Down Expand Up @@ -37,15 +39,28 @@ module.exports = (md, externalAttrs) => {
const links = md.__data.links || (md.__data.links = [])
links.push(to)

to = to
.replace(/\.md$/, '.html')
.replace(/\.md(#.*)$/, '.html$1')
// normalize links to README/index
if (/^index|readme\.html/i.test(to)) {
to = '/'
const indexMatch = to.match(indexRE)
if (indexMatch) {
const [, path, , hash] = indexMatch
to = path + hash
} else {
to = to
.replace(/\.md$/, '.html')
.replace(/\.md(#.*)$/, '.html$1')
}

// relative path usage.
if (!to.startsWith('/')) {
to = ensureBeginningDotSlash(to)
}

// markdown-it encodes the uri
link[1] = decodeURI(to)

// export the router links for testing
const routerLinks = md.__data.routerLinks || (md.__data.routerLinks = [])
routerLinks.push(to)

return Object.assign({}, token, {
tag: 'router-link'
})
Expand All @@ -65,3 +80,12 @@ module.exports = (md, externalAttrs) => {
return self.renderToken(tokens, idx, options)
}
}

const beginningSlashRE = /^\.\//

function ensureBeginningDotSlash (path) {
if (beginningSlashRE.test(path)) {
return path
}
return './' + path
}
25 changes: 25 additions & 0 deletions test/markdown/__snapshots__/link.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`link should render external links correctly 1`] = `
<p>
<a href="https://vuejs.org/" target="_blank" rel="noopener noreferrer">vue
<OutboundLink/>
</a>
</p>
`;

exports[`link should render external links correctly 2`] = `
<p>
<a href="http://vuejs.org/" target="_blank" rel="noopener noreferrer">vue
<OutboundLink/>
</a>
</p>
`;

exports[`link should render external links correctly 3`] = `
<p>
<a href="https://google.com" target="_blank" rel="noopener noreferrer">some <strong>link</strong> with <code>code</code>
<OutboundLink/>
</a>
</p>
`;
77 changes: 77 additions & 0 deletions test/markdown/link.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Md } from './util'
import link from '@/markdown/link.js'
import { dataReturnable } from '@/markdown/index.js'

const mdL = Md().use(link, {
target: '_blank',
rel: 'noopener noreferrer'
})

dataReturnable(mdL)

const internalLinkAsserts = {
// START abosolute path usage
'/': '/',

'/foo/': '/foo/',
'/foo/#hash': '/foo/#hash',

'/foo/two.md': '/foo/two.html',
'/foo/two.html': '/foo/two.html',
// END abosolute path usage

// START relative path usage
'README.md': './',
'./README.md': './',

'index.md': './',
'./index.md': './',

'one.md': './one.html',
'./one.md': './one.html',

'foo/README.md': './foo/',
'./foo/README.md': './foo/',

'foo/README.md#hash': './foo/#hash',
'./foo/README.md#hash': './foo/#hash',

'../README.md': './../',
'../README.md#hash': './../#hash',

'../foo.md': './../foo.html',
'../foo.md#hash': './../foo.html#hash',

'../foo/one.md': './../foo/one.html',
'../foo/one.md#hash': './../foo/one.html#hash'
// END relative path usage
}

const externalLinks = [
'[vue](https://vuejs.org/)',
'[vue](http://vuejs.org/)',
'[some **link** with `code`](https://google.com)' // #496
]

describe('link', () => {
test('should convert internal links to router links correctly', () => {
for (const before in internalLinkAsserts) {
const input = `[${before}](${before})`
const output = mdL.render(input)
const after = getCompiledLink(output)
expect(after).toBe(internalLinkAsserts[before])
}
})

test('should render external links correctly', () => {
for (const link of externalLinks) {
const { html } = mdL.render(link)
expect(html).toMatchSnapshot()
}
})
})

function getCompiledLink (output) {
const { data: { routerLinks }} = output
return routerLinks[0]
}

0 comments on commit 292e4bc

Please sign in to comment.