Skip to content

Commit ab19b13

Browse files
cheng-kangQingWei-Li
authored andcommittedNov 1, 2018
feat: add heading config id (#671)
1 parent 5fcf210 commit ab19b13

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed
 

Diff for: ‎docs/helpers.md

+6
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,9 @@ You will get `<a href="/demo/">link</a>`html. Do not worry, you can still set ti
9494
![logo](https://docsify.js.org/_media/icon.svg ':size=50x100')
9595
![logo](https://docsify.js.org/_media/icon.svg ':size=100')
9696
![logo](https://docsify.js.org/_media/icon.svg ':size=10%')
97+
98+
## Customise ID for headings
99+
100+
```md
101+
### 你好,世界! :id=hello-world
102+
```

Diff for: ‎src/core/render/compiler.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -190,26 +190,27 @@ export class Compiler {
190190
* @link https://github.com/markedjs/marked#overriding-renderer-methods
191191
*/
192192
origin.heading = renderer.heading = function (text, level) {
193-
const nextToc = {level, title: text}
193+
let {str, config} = getAndRemoveConfig(text)
194+
const nextToc = {level, title: str}
194195

195-
if (/{docsify-ignore}/g.test(text)) {
196-
text = text.replace('{docsify-ignore}', '')
197-
nextToc.title = text
196+
if (/{docsify-ignore}/g.test(str)) {
197+
str = str.replace('{docsify-ignore}', '')
198+
nextToc.title = str
198199
nextToc.ignoreSubHeading = true
199200
}
200201

201-
if (/{docsify-ignore-all}/g.test(text)) {
202-
text = text.replace('{docsify-ignore-all}', '')
203-
nextToc.title = text
202+
if (/{docsify-ignore-all}/g.test(str)) {
203+
str = str.replace('{docsify-ignore-all}', '')
204+
nextToc.title = str
204205
nextToc.ignoreAllSubs = true
205206
}
206207

207-
const slug = slugify(text)
208+
const slug = slugify(config.id || str)
208209
const url = router.toURL(router.getCurrentPath(), {id: slug})
209210
nextToc.slug = url
210211
_self.toc.push(nextToc)
211212

212-
return `<h${level} id="${slug}"><a href="${url}" data-id="${slug}" class="anchor"><span>${text}</span></a></h${level}>`
213+
return `<h${level} id="${slug}"><a href="${url}" data-id="${slug}" class="anchor"><span>${str}</span></a></h${level}>`
213214
}
214215
// Highlight code
215216
origin.code = renderer.code = function (code, lang = '') {

0 commit comments

Comments
 (0)