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

Use media links for img in post-process #10515

Merged
merged 3 commits into from
Feb 28, 2020
Merged
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
Prev Previous commit
Next Next commit
do not visit text of anchors
  • Loading branch information
zeripath committed Feb 27, 2020
commit 2d52f02566e9f100e26a8b85c1ba96a58e946404
14 changes: 9 additions & 5 deletions modules/markup/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func (ctx *postProcessCtx) postProcess(rawHTML []byte) ([]byte, error) {
}

for _, node := range nodes {
ctx.visitNode(node)
ctx.visitNode(node, true)
}

// Create buffer in which the data will be placed again. We know that the
Expand All @@ -313,7 +313,7 @@ func (ctx *postProcessCtx) postProcess(rawHTML []byte) ([]byte, error) {
return res, nil
}

func (ctx *postProcessCtx) visitNode(node *html.Node) {
func (ctx *postProcessCtx) visitNode(node *html.Node, visitText bool) {
// 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)) {
Expand All @@ -323,7 +323,9 @@ func (ctx *postProcessCtx) visitNode(node *html.Node) {
// We ignore code, pre and already generated links.
switch node.Type {
case html.TextNode:
ctx.textNode(node)
if visitText {
ctx.textNode(node)
}
case html.ElementNode:
if node.Data == "img" {
attrs := node.Attr
Expand All @@ -345,11 +347,13 @@ func (ctx *postProcessCtx) visitNode(node *html.Node) {
}
node.Attr[idx].Val = string(link)
}
} else if node.Data == "a" || node.Data == "code" || node.Data == "pre" {
} else if node.Data == "a" {
visitText = false
} else if node.Data == "code" || node.Data == "pre" {
return
}
for n := node.FirstChild; n != nil; n = n.NextSibling {
ctx.visitNode(n)
ctx.visitNode(n, visitText)
}
}
// ignore everything else
Expand Down