Skip to content

Commit 2e00f4c

Browse files
authored
feat: ignore to compiled link, fixed #203 (#204)
* feat: ignore to compiled link, fixed #203 * feat: add noCompileLinks, fixed #203 * fix: remove test code
1 parent 7fb5ce6 commit 2e00f4c

File tree

9 files changed

+156
-4
lines changed

9 files changed

+156
-4
lines changed

docs/configuration.md

+18
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,21 @@ window.$docsify = {
362362
routerMode: 'history' // default: 'hash'
363363
}
364364
```
365+
366+
## noCompileLinks
367+
368+
- type: `Array`
369+
370+
371+
Sometimes we do not want docsify to handle our links. See [#203](https://github.com/QingWei-Li/docsify/issues/203)
372+
373+
374+
```js
375+
window.$docsify = {
376+
noCompileLinks: [
377+
'/foo',
378+
'/bar/.*'
379+
]
380+
}
381+
```
382+

docs/de-de/configuration.md

+18
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,21 @@ window.$docsify = {
341341
externalLinkTarget: '_self' // default: '_blank'
342342
}
343343
```
344+
345+
346+
## noCompileLinks
347+
348+
- type: `Array`
349+
350+
351+
Sometimes we do not want docsify to handle our links. See [#203](https://github.com/QingWei-Li/docsify/issues/203)
352+
353+
354+
```js
355+
window.$docsify = {
356+
noCompileLinks: [
357+
'/foo',
358+
'/bar/.*'
359+
]
360+
}
361+
```

docs/de-de/helpers.md

+24
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,27 @@ Generelle Tipps wie:
2525
wird wie folgt gerendert:
2626

2727
?> *TODO* unit test
28+
29+
## Ignore to compile link
30+
31+
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
32+
33+
```md
34+
[link](/demo/)
35+
```
36+
37+
38+
It will be compiled to `<a href="/#/demo/">link</a>` and will be loaded `/demo/README.md`. Maybe you want to jump to `/demo/index.html`.
39+
40+
Now you can do that
41+
42+
```md
43+
[link](/demo/ ":ignore")
44+
```
45+
You will get `<a href="/demo/">link</a>`html. Do not worry, you can still set title for link.
46+
47+
```md
48+
[link](/demo/ ":ignore title")
49+
50+
<a href="/demo/" title="title">link</a>
51+
```

docs/helpers.md

+25
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,28 @@ General tips like:
2525
are rendered as:
2626

2727
?> *TODO* unit test
28+
29+
## Ignore to compile link
30+
31+
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
32+
33+
```md
34+
[link](/demo/)
35+
```
36+
37+
38+
It will be compiled to `<a href="/#/demo/">link</a>` and will be loaded `/demo/README.md`. Maybe you want to jump to `/demo/index.html`.
39+
40+
Now you can do that
41+
42+
```md
43+
[link](/demo/ ":ignore")
44+
```
45+
You will get `<a href="/demo/">link</a>`html. Do not worry, you can still set title for link.
46+
47+
```md
48+
[link](/demo/ ":ignore title")
49+
50+
<a href="/demo/" title="title">link</a>
51+
```
52+

docs/zh-cn/configuration.md

+18
Original file line numberDiff line numberDiff line change
@@ -352,3 +352,21 @@ window.$docsify = {
352352
externalLinkTarget: '_self' // default: '_blank'
353353
}
354354
```
355+
356+
357+
## noCompileLinks
358+
359+
- type: `Array`
360+
361+
362+
Sometimes we do not want docsify to handle our links. See [#203](https://github.com/QingWei-Li/docsify/issues/203)
363+
364+
365+
```js
366+
window.$docsify = {
367+
noCompileLinks: [
368+
'/foo',
369+
'/bar/.*'
370+
]
371+
}
372+
```

docs/zh-cn/helpers.md

+25
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,28 @@ docsify 扩展了一些 Markdown 语法,可以让文档更易读。
2424

2525
?> *TODO* 完善示例
2626

27+
28+
## Ignore to compile link
29+
30+
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
31+
32+
```md
33+
[link](/demo/)
34+
```
35+
36+
37+
It will be compiled to `<a href="/#/demo/">link</a>` and will be loaded `/demo/README.md`. Maybe you want to jump to `/demo/index.html`.
38+
39+
Now you can do that
40+
41+
```md
42+
[link](/demo/ ":ignore")
43+
```
44+
You will get `<a href="/demo/">link</a>`html. Do not worry, you can still set title for link.
45+
46+
```md
47+
[link](/demo/ ":ignore title")
48+
49+
<a href="/demo/" title="title">link</a>
50+
```
51+

src/core/config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ const config = merge({
2121
mergeNavbar: false,
2222
formatUpdated: '',
2323
externalLinkTarget: '_blank',
24-
routerModel: 'hash'
24+
routerModel: 'hash',
25+
noCompileLinks: []
2526
}, window.$docsify)
2627

2728
const script = document.currentScript ||

src/core/render/compiler.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { emojify } from './emojify'
77
import { isAbsolutePath, getPath } from '../router/util'
88
import { isFn, merge, cached } from '../util/core'
99

10+
const cachedLinks = {}
11+
1012
export class Compiler {
1113
constructor (config, router) {
1214
this.config = config
@@ -42,6 +44,19 @@ export class Compiler {
4244
})
4345
}
4446

47+
matchNotCompileLink(link) {
48+
const links = this.config.noCompileLinks
49+
50+
for (var i = 0; i < links.length; i++) {
51+
const n = links[i]
52+
const re = cachedLinks[n] || (cachedLinks[n] = new RegExp(`^${n}$`))
53+
54+
if (re.test(link)) {
55+
return link
56+
}
57+
}
58+
}
59+
4560
_initRenderer () {
4661
const renderer = new marked.Renderer()
4762
const { linkTarget, router, contentBase } = this
@@ -80,11 +95,16 @@ export class Compiler {
8095
}
8196
renderer.link = function (href, title, text) {
8297
let blank = ''
83-
if (!/:|(\/{2})/.test(href)) {
98+
99+
if (!/:|(\/{2})/.test(href)
100+
&& !_self.matchNotCompileLink(href)
101+
&& !/(\s?:ignore)(\s\S+)?$/.test(title)) {
84102
href = router.toURL(href, null, router.getCurrentPath())
85103
} else {
86104
blank = ` target="${linkTarget}"`
105+
title = title && title.replace(/:ignore/g, '').trim()
87106
}
107+
88108
if (title) {
89109
title = ` title="${title}"`
90110
}

src/core/router/util.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,19 @@ export function parseQuery (query) {
1616
query.split('&').forEach(function (param) {
1717
const parts = param.replace(/\+/g, ' ').split('=')
1818

19-
res[parts[0]] = decode(parts[1])
19+
res[parts[0]] = parts[1] && decode(parts[1])
2020
})
21+
2122
return res
2223
}
2324

2425
export function stringifyQuery (obj) {
2526
const qs = []
2627

2728
for (const key in obj) {
28-
qs.push(`${encode(key)}=${encode(obj[key])}`.toLowerCase())
29+
qs.push(obj[key]
30+
? `${encode(key)}=${encode(obj[key])}`.toLowerCase()
31+
: encode(key))
2932
}
3033

3134
return qs.length ? `?${qs.join('&')}` : ''

0 commit comments

Comments
 (0)