Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEATURE: Add anchor links to headings (#12379)
- Loading branch information
Showing
7 changed files
with
66 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
app/assets/javascripts/pretty-text/engines/discourse-markdown/anchor.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
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); | ||
} | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2ad9b3f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This commit has been mentioned on Discourse Meta. There might be relevant details there:
https://meta.discourse.org/t/using-to-make-title-bold-makes-it-a-link/184454/2