Skip to content

Commit

Permalink
feat: ignore to compiled link, fixed #203 (#204)
Browse files Browse the repository at this point in the history
* feat: ignore to compiled link, fixed #203

* feat: add noCompileLinks, fixed #203

* fix: remove test code
  • Loading branch information
QingWei-Li authored Jul 10, 2017
1 parent 7fb5ce6 commit 2e00f4c
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 4 deletions.
18 changes: 18 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/.*'
]
}
```

18 changes: 18 additions & 0 deletions docs/de-de/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/.*'
]
}
```
24 changes: 24 additions & 0 deletions docs/de-de/helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<a href="/#/demo/">link</a>` 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 `<a href="/demo/">link</a>`html. Do not worry, you can still set title for link.

```md
[link](/demo/ ":ignore title")

<a href="/demo/" title="title">link</a>
```
25 changes: 25 additions & 0 deletions docs/helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<a href="/#/demo/">link</a>` 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 `<a href="/demo/">link</a>`html. Do not worry, you can still set title for link.

```md
[link](/demo/ ":ignore title")

<a href="/demo/" title="title">link</a>
```

18 changes: 18 additions & 0 deletions docs/zh-cn/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/.*'
]
}
```
25 changes: 25 additions & 0 deletions docs/zh-cn/helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<a href="/#/demo/">link</a>` 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 `<a href="/demo/">link</a>`html. Do not worry, you can still set title for link.

```md
[link](/demo/ ":ignore title")

<a href="/demo/" title="title">link</a>
```

3 changes: 2 additions & 1 deletion src/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const config = merge({
mergeNavbar: false,
formatUpdated: '',
externalLinkTarget: '_blank',
routerModel: 'hash'
routerModel: 'hash',
noCompileLinks: []
}, window.$docsify)

const script = document.currentScript ||
Expand Down
22 changes: 21 additions & 1 deletion src/core/render/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}"`
}
Expand Down
7 changes: 5 additions & 2 deletions src/core/router/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@ 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
}

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('&')}` : ''
Expand Down

0 comments on commit 2e00f4c

Please sign in to comment.