支持锚点链接

This commit is contained in:
purocean
2019-02-22 15:09:41 +08:00
parent a9a5c2ee3b
commit e7296cfd4b
2 changed files with 11 additions and 1 deletions
+10
View File
@@ -117,6 +117,16 @@ export default {
for (let ele of document.querySelectorAll('code[class^="language-"]')) {
HighlightLineNumber.lineNumbersBlock(ele)
}
for (let ele of document.getElementsByTagName('a')) {
const href = ele.getAttribute('href')
if (href && href.startsWith('#')) {
ele.onclick = () => {
document.getElementById(href.replace(/^#/, '')).scrollIntoView()
return false
}
}
}
}, 500, {leading: true})
this.render()
+1 -1
View File
@@ -166,7 +166,7 @@ export default (md, o) => {
header.attrSet('id', slug)
}
buffer = `<li><a href="#${slug}" onclick="document.getElementById('${slug}').scrollIntoView(); return false;">`
buffer = `<li><a href="#${slug}">`
buffer += typeof options.format === 'function' ? options.format(heading.content) : heading.content
buffer += `</a>`
i++