mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 09:42:07 +08:00
30 lines
903 B
JavaScript
30 lines
903 B
JavaScript
export function setup(helper) {
|
|
helper.registerPlugin((md) => {
|
|
md.core.ruler.push("anchor", (state) => {
|
|
for (let idx = 0; idx < state.tokens.length; idx++) {
|
|
if (state.tokens[idx].type !== "heading_open") {
|
|
continue;
|
|
}
|
|
|
|
const linkOpen = new state.Token("link_open", "a", 1);
|
|
const linkClose = new state.Token("link_close", "a", -1);
|
|
|
|
const slug = state.tokens[idx + 1].content
|
|
.toLowerCase()
|
|
.replace(/\s+/g, "-")
|
|
.replace(/[^\w\-]+/g, "")
|
|
.replace(/\-\-+/g, "-")
|
|
.replace(/^-+/, "")
|
|
.replace(/-+$/, "");
|
|
|
|
linkOpen.attrSet("name", slug);
|
|
linkOpen.attrSet("class", "anchor");
|
|
linkOpen.attrSet("href", "#" + slug);
|
|
|
|
state.tokens[idx + 1].children.unshift(linkClose);
|
|
state.tokens[idx + 1].children.unshift(linkOpen);
|
|
}
|
|
});
|
|
});
|
|
}
|