mirror of https://github.com/jlelse/GoBlog
Use custom renderer for images too
This commit is contained in:
parent
dbbc498994
commit
da18db0f74
51
markdown.go
51
markdown.go
|
@ -38,7 +38,7 @@ func initMarkdown() {
|
||||||
emoji.WithEmojis(emojiGoLib()),
|
emoji.WithEmojis(emojiGoLib()),
|
||||||
),
|
),
|
||||||
// Links
|
// Links
|
||||||
newLinkExtension(),
|
newCustomExtension(),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -65,25 +65,26 @@ func emojiGoLib() definition.Emojis {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Links
|
// Links
|
||||||
type linkExtension struct{}
|
type customExtension struct{}
|
||||||
|
|
||||||
func newLinkExtension() goldmark.Extender {
|
func newCustomExtension() goldmark.Extender {
|
||||||
return &linkExtension{}
|
return &customExtension{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *linkExtension) Extend(m goldmark.Markdown) {
|
func (l *customExtension) Extend(m goldmark.Markdown) {
|
||||||
m.Renderer().AddOptions(renderer.WithNodeRenderers(
|
m.Renderer().AddOptions(renderer.WithNodeRenderers(
|
||||||
util.Prioritized(newLinkRenderer(), 500),
|
util.Prioritized(newLinkRenderer(), 500),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
type linkRenderer struct{}
|
type customRenderer struct{}
|
||||||
|
|
||||||
func (l *linkRenderer) RegisterFuncs(r renderer.NodeRendererFuncRegisterer) {
|
func (c *customRenderer) RegisterFuncs(r renderer.NodeRendererFuncRegisterer) {
|
||||||
r.Register(ast.KindLink, l.renderLink)
|
r.Register(ast.KindLink, c.renderLink)
|
||||||
|
r.Register(ast.KindImage, c.renderImage)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *linkRenderer) renderLink(w util.BufWriter, _ []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
func (c *customRenderer) renderLink(w util.BufWriter, _ []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
||||||
n := node.(*ast.Link)
|
n := node.(*ast.Link)
|
||||||
if entering {
|
if entering {
|
||||||
// Make URL absolute if it's relative
|
// Make URL absolute if it's relative
|
||||||
|
@ -112,6 +113,34 @@ func (l *linkRenderer) renderLink(w util.BufWriter, _ []byte, node ast.Node, ent
|
||||||
return ast.WalkContinue, nil
|
return ast.WalkContinue, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func newLinkRenderer() renderer.NodeRenderer {
|
func (c *customRenderer) renderImage(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
||||||
return &linkRenderer{}
|
if !entering {
|
||||||
|
return ast.WalkContinue, nil
|
||||||
|
}
|
||||||
|
n := node.(*ast.Image)
|
||||||
|
// Make URL absolute if it's relative
|
||||||
|
destination := string(util.URLEscape(n.Destination, true))
|
||||||
|
if strings.HasPrefix(destination, "/") {
|
||||||
|
destination = appConfig.Server.PublicAddress + destination
|
||||||
|
}
|
||||||
|
_, _ = w.WriteString("<a href=\"")
|
||||||
|
_, _ = w.Write(util.EscapeHTML([]byte(destination)))
|
||||||
|
_, _ = w.WriteString("\">")
|
||||||
|
_, _ = w.WriteString("<img src=\"")
|
||||||
|
_, _ = w.Write(util.EscapeHTML([]byte(destination)))
|
||||||
|
_, _ = w.WriteString(`" alt="`)
|
||||||
|
_, _ = w.Write(util.EscapeHTML(n.Text(source)))
|
||||||
|
_ = w.WriteByte('"')
|
||||||
|
_, _ = w.WriteString(" loading=\"lazy\"")
|
||||||
|
if n.Title != nil {
|
||||||
|
_, _ = w.WriteString(` title="`)
|
||||||
|
_, _ = w.Write(n.Title)
|
||||||
|
_ = w.WriteByte('"')
|
||||||
|
}
|
||||||
|
_, _ = w.WriteString("></a>")
|
||||||
|
return ast.WalkSkipChildren, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func newLinkRenderer() renderer.NodeRenderer {
|
||||||
|
return &customRenderer{}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue