Skip to content

Commit 9ad2aa8

Browse files
authored
Use media links for img in post-process (#10515)
* use media links for img in post-process * do not visit text of anchors
1 parent efec0d3 commit 9ad2aa8

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

modules/markup/html.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ func (ctx *postProcessCtx) postProcess(rawHTML []byte) ([]byte, error) {
290290
}
291291

292292
for _, node := range nodes {
293-
ctx.visitNode(node)
293+
ctx.visitNode(node, true)
294294
}
295295

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

316-
func (ctx *postProcessCtx) visitNode(node *html.Node) {
316+
func (ctx *postProcessCtx) visitNode(node *html.Node, visitText bool) {
317317
// Add user-content- to IDs if they don't already have them
318318
for idx, attr := range node.Attr {
319319
if attr.Key == "id" && !(strings.HasPrefix(attr.Val, "user-content-") || blackfridayExtRegex.MatchString(attr.Val)) {
@@ -323,13 +323,37 @@ func (ctx *postProcessCtx) visitNode(node *html.Node) {
323323
// We ignore code, pre and already generated links.
324324
switch node.Type {
325325
case html.TextNode:
326-
ctx.textNode(node)
326+
if visitText {
327+
ctx.textNode(node)
328+
}
327329
case html.ElementNode:
328-
if node.Data == "a" || node.Data == "code" || node.Data == "pre" {
330+
if node.Data == "img" {
331+
attrs := node.Attr
332+
for idx, attr := range attrs {
333+
if attr.Key != "src" {
334+
continue
335+
}
336+
link := []byte(attr.Val)
337+
if len(link) > 0 && !IsLink(link) {
338+
prefix := ctx.urlPrefix
339+
if ctx.isWikiMarkdown {
340+
prefix = util.URLJoin(prefix, "wiki", "raw")
341+
}
342+
prefix = strings.Replace(prefix, "/src/", "/media/", 1)
343+
344+
lnk := string(link)
345+
lnk = util.URLJoin(prefix, lnk)
346+
link = []byte(lnk)
347+
}
348+
node.Attr[idx].Val = string(link)
349+
}
350+
} else if node.Data == "a" {
351+
visitText = false
352+
} else if node.Data == "code" || node.Data == "pre" {
329353
return
330354
}
331355
for n := node.FirstChild; n != nil; n = n.NextSibling {
332-
ctx.visitNode(n)
356+
ctx.visitNode(n, visitText)
333357
}
334358
}
335359
// ignore everything else

0 commit comments

Comments
 (0)