From 2e00f4c993a5b1e05f63e26e208174d69a939a56 Mon Sep 17 00:00:00 2001 From: "cinwell.li" Date: Mon, 10 Jul 2017 22:38:26 +0800 Subject: [PATCH] feat: ignore to compiled link, fixed #203 (#204) * feat: ignore to compiled link, fixed #203 * feat: add noCompileLinks, fixed #203 * fix: remove test code --- docs/configuration.md | 18 ++++++++++++++++++ docs/de-de/configuration.md | 18 ++++++++++++++++++ docs/de-de/helpers.md | 24 ++++++++++++++++++++++++ docs/helpers.md | 25 +++++++++++++++++++++++++ docs/zh-cn/configuration.md | 18 ++++++++++++++++++ docs/zh-cn/helpers.md | 25 +++++++++++++++++++++++++ src/core/config.js | 3 ++- src/core/render/compiler.js | 22 +++++++++++++++++++++- src/core/router/util.js | 7 +++++-- 9 files changed, 156 insertions(+), 4 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 1e76bf906..1c0b680a3 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -362,3 +362,21 @@ window.$docsify = { routerMode: 'history' // default: 'hash' } ``` + +## noCompileLinks + +- type: `Array` + + +Sometimes we do not want docsify to handle our links. See [#203](https://github.com/QingWei-Li/docsify/issues/203) + + +```js +window.$docsify = { + noCompileLinks: [ + '/foo', + '/bar/.*' + ] +} +``` + diff --git a/docs/de-de/configuration.md b/docs/de-de/configuration.md index 5c3b57ff8..ebf8920a7 100644 --- a/docs/de-de/configuration.md +++ b/docs/de-de/configuration.md @@ -341,3 +341,21 @@ window.$docsify = { externalLinkTarget: '_self' // default: '_blank' } ``` + + +## noCompileLinks + +- type: `Array` + + +Sometimes we do not want docsify to handle our links. See [#203](https://github.com/QingWei-Li/docsify/issues/203) + + +```js +window.$docsify = { + noCompileLinks: [ + '/foo', + '/bar/.*' + ] +} +``` diff --git a/docs/de-de/helpers.md b/docs/de-de/helpers.md index 453a67a67..5a9e4775f 100644 --- a/docs/de-de/helpers.md +++ b/docs/de-de/helpers.md @@ -25,3 +25,27 @@ Generelle Tipps wie: wird wie folgt gerendert: ?> *TODO* unit test + +## Ignore to compile link + +Some time we will put some other relative path to the link, you have to need to tell docsify you don't need to compile this link. For example + +```md +[link](/demo/) +``` + + +It will be compiled to `link` and will be loaded `/demo/README.md`. Maybe you want to jump to `/demo/index.html`. + +Now you can do that + +```md +[link](/demo/ ":ignore") +``` +You will get `link`html. Do not worry, you can still set title for link. + +```md +[link](/demo/ ":ignore title") + +link +``` diff --git a/docs/helpers.md b/docs/helpers.md index fe636b2ec..aec77661e 100644 --- a/docs/helpers.md +++ b/docs/helpers.md @@ -25,3 +25,28 @@ General tips like: are rendered as: ?> *TODO* unit test + +## Ignore to compile link + +Some time we will put some other relative path to the link, you have to need to tell docsify you don't need to compile this link. For example + +```md +[link](/demo/) +``` + + +It will be compiled to `link` and will be loaded `/demo/README.md`. Maybe you want to jump to `/demo/index.html`. + +Now you can do that + +```md +[link](/demo/ ":ignore") +``` +You will get `link`html. Do not worry, you can still set title for link. + +```md +[link](/demo/ ":ignore title") + +link +``` + diff --git a/docs/zh-cn/configuration.md b/docs/zh-cn/configuration.md index 274fd174f..6363858f3 100644 --- a/docs/zh-cn/configuration.md +++ b/docs/zh-cn/configuration.md @@ -352,3 +352,21 @@ window.$docsify = { externalLinkTarget: '_self' // default: '_blank' } ``` + + +## noCompileLinks + +- type: `Array` + + +Sometimes we do not want docsify to handle our links. See [#203](https://github.com/QingWei-Li/docsify/issues/203) + + +```js +window.$docsify = { + noCompileLinks: [ + '/foo', + '/bar/.*' + ] +} +``` diff --git a/docs/zh-cn/helpers.md b/docs/zh-cn/helpers.md index ec8b76e61..ab41b3901 100644 --- a/docs/zh-cn/helpers.md +++ b/docs/zh-cn/helpers.md @@ -24,3 +24,28 @@ docsify 扩展了一些 Markdown 语法,可以让文档更易读。 ?> *TODO* 完善示例 + +## Ignore to compile link + +Some time we will put some other relative path to the link, you have to need to tell docsify you don't need to compile this link. For example + +```md +[link](/demo/) +``` + + +It will be compiled to `link` and will be loaded `/demo/README.md`. Maybe you want to jump to `/demo/index.html`. + +Now you can do that + +```md +[link](/demo/ ":ignore") +``` +You will get `link`html. Do not worry, you can still set title for link. + +```md +[link](/demo/ ":ignore title") + +link +``` + diff --git a/src/core/config.js b/src/core/config.js index 65f727ab9..95f81e0bc 100644 --- a/src/core/config.js +++ b/src/core/config.js @@ -21,7 +21,8 @@ const config = merge({ mergeNavbar: false, formatUpdated: '', externalLinkTarget: '_blank', - routerModel: 'hash' + routerModel: 'hash', + noCompileLinks: [] }, window.$docsify) const script = document.currentScript || diff --git a/src/core/render/compiler.js b/src/core/render/compiler.js index aafaa90bd..313d50782 100644 --- a/src/core/render/compiler.js +++ b/src/core/render/compiler.js @@ -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}"` } diff --git a/src/core/router/util.js b/src/core/router/util.js index e2c9bc7ce..80cc50704 100644 --- a/src/core/router/util.js +++ b/src/core/router/util.js @@ -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('&')}` : ''