Prefix all user-generated IDs in markup (#9477)

* Prefix all user-generated IDs in markup
* Add user-content- to IDs in unit-tests
* fixup markdown_test.go
* update the hrefs for the wiki test
* Add blackfriday extension regex

Signed-off-by: jolheiser <john.olheiser@gmail.com>
This commit is contained in:
John Olheiser 2019-12-23 16:38:50 -06:00 committed by zeripath
parent 071e7c4f0a
commit e6ceb6880a
5 changed files with 24 additions and 24 deletions

View file

@ -58,6 +58,9 @@ var (
emailRegex = regexp.MustCompile("(?:\\s|^|\\(|\\[)([a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9]{2,}(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+)(?:\\s|$|\\)|\\]|\\.(\\s|$))")
linkRegex, _ = xurls.StrictMatchingScheme("https?://")
// blackfriday extensions create IDs like fn:user-content-footnote
blackfridayExtRegex = regexp.MustCompile(`[^:]*:user-content-`)
)
// CSS class for action keywords (e.g. "closes: #1")
@ -312,6 +315,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-") || blackfridayExtRegex.MatchString(attr.Val)) {
node.Attr[idx].Val = "user-content-" + attr.Val
}
}
// We ignore code, pre and already generated links.
switch node.Type {
case html.TextNode: