Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefix all user-generated IDs in markup #9477

Merged
merged 7 commits into from
Dec 23, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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="#${node.attr('id')}"><span class="octicon octicon-link"></span></a>`);
jolheiser marked this conversation as resolved.
Show resolved Hide resolved
});
});

Expand Down