Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions modules/markup/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,12 @@ func (ctx *postProcessCtx) postProcess(rawHTML []byte) ([]byte, error) {
}

func (ctx *postProcessCtx) visitNode(node *html.Node) {
// Add user-content- to IDs if they don't already have them
for idx, attr := range node.Attr {
if attr.Key == "id" && !strings.HasPrefix(attr.Val, "user-content-") {
node.Attr[idx].Val = "user-content-" + attr.Val
}
}
// We ignore code, pre and already generated links.
switch node.Type {
case html.TextNode:
Expand Down
15 changes: 2 additions & 13 deletions web_src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2472,21 +2472,10 @@ $(document).ready(() => {

// Set anchor.
$('.markdown').each(function () {
const headers = {};
$(this).find('h1, h2, h3, h4, h5, h6').each(function () {
let node = $(this);
const val = encodeURIComponent(node.text().toLowerCase().replace(/[^\u00C0-\u1FFF\u2C00-\uD7FF\w\- ]/g, '').replace(/[ ]/g, '-'));
let name = val;
if (headers[val] > 0) {
name = `${val}-${headers[val]}`;
}
if (headers[val] === undefined) {
headers[val] = 1;
} else {
headers[val] += 1;
}
node = node.wrap(`<div id="${name}" class="anchor-wrap" ></div>`);
node.append(`<a class="anchor" href="#${name}"><span class="octicon octicon-link"></span></a>`);
node = node.wrap('<div class="anchor-wrap"></div>');
node.append(`<a class="anchor" href="#${encodeURIComponent(node.attr('id'))}"><span class="octicon octicon-link"></span></a>`);
});
});

Expand Down