feat: ignore to compiled link, fixed #203 (#204)

* feat: ignore to compiled link, fixed #203

* feat: add noCompileLinks, fixed #203

* fix: remove test code
This commit is contained in:
cinwell.li 2017-07-10 22:38:26 +08:00 committed by GitHub
commit 2e00f4c993
9 changed files with 156 additions and 4 deletions

View file

@ -21,7 +21,8 @@ const config = merge({
mergeNavbar: false,
formatUpdated: '',
externalLinkTarget: '_blank',
routerModel: 'hash'
routerModel: 'hash',
noCompileLinks: []
}, window.$docsify)
const script = document.currentScript ||

View file

@ -7,6 +7,8 @@ import { emojify } from './emojify'
import { isAbsolutePath, getPath } from '../router/util'
import { isFn, merge, cached } from '../util/core'
const cachedLinks = {}
export class Compiler {
constructor (config, router) {
this.config = config
@ -42,6 +44,19 @@ export class Compiler {
})
}
matchNotCompileLink(link) {
const links = this.config.noCompileLinks
for (var i = 0; i < links.length; i++) {
const n = links[i]
const re = cachedLinks[n] || (cachedLinks[n] = new RegExp(`^${n}$`))
if (re.test(link)) {
return link
}
}
}
_initRenderer () {
const renderer = new marked.Renderer()
const { linkTarget, router, contentBase } = this
@ -80,11 +95,16 @@ export class Compiler {
}
renderer.link = function (href, title, text) {
let blank = ''
if (!/:|(\/{2})/.test(href)) {
if (!/:|(\/{2})/.test(href)
&& !_self.matchNotCompileLink(href)
&& !/(\s?:ignore)(\s\S+)?$/.test(title)) {
href = router.toURL(href, null, router.getCurrentPath())
} else {
blank = ` target="${linkTarget}"`
title = title && title.replace(/:ignore/g, '').trim()
}
if (title) {
title = ` title="${title}"`
}

View file

@ -16,8 +16,9 @@ export function parseQuery (query) {
query.split('&').forEach(function (param) {
const parts = param.replace(/\+/g, ' ').split('=')
res[parts[0]] = decode(parts[1])
res[parts[0]] = parts[1] && decode(parts[1])
})
return res
}
@ -25,7 +26,9 @@ export function stringifyQuery (obj) {
const qs = []
for (const key in obj) {
qs.push(`${encode(key)}=${encode(obj[key])}`.toLowerCase())
qs.push(obj[key]
? `${encode(key)}=${encode(obj[key])}`.toLowerCase()
: encode(key))
}
return qs.length ? `?${qs.join('&')}` : ''