2019-11-27 07:42:36 -05:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package goldmark
|
|
|
|
|
|
|
|
import (
|
2021-02-23 12:04:05 -05:00
|
|
|
"bytes"
|
2021-07-15 02:46:54 -04:00
|
|
|
"strings"
|
2021-02-23 12:04:05 -05:00
|
|
|
|
2022-03-10 02:19:03 -05:00
|
|
|
"github.com/gohugoio/hugo/common/types/hstring"
|
2019-11-27 07:42:36 -05:00
|
|
|
"github.com/gohugoio/hugo/markup/converter/hooks"
|
2022-03-09 12:26:32 -05:00
|
|
|
"github.com/gohugoio/hugo/markup/goldmark/goldmark_config"
|
2022-12-03 06:33:48 -05:00
|
|
|
"github.com/gohugoio/hugo/markup/goldmark/images"
|
2022-02-17 07:04:00 -05:00
|
|
|
"github.com/gohugoio/hugo/markup/goldmark/internal/render"
|
|
|
|
"github.com/gohugoio/hugo/markup/internal/attributes"
|
2019-11-27 07:42:36 -05:00
|
|
|
|
|
|
|
"github.com/yuin/goldmark"
|
|
|
|
"github.com/yuin/goldmark/ast"
|
|
|
|
"github.com/yuin/goldmark/renderer"
|
|
|
|
"github.com/yuin/goldmark/renderer/html"
|
|
|
|
"github.com/yuin/goldmark/util"
|
|
|
|
)
|
|
|
|
|
2020-03-14 10:43:10 -04:00
|
|
|
var _ renderer.SetOptioner = (*hookedRenderer)(nil)
|
2019-11-27 07:42:36 -05:00
|
|
|
|
2022-03-09 12:26:32 -05:00
|
|
|
func newLinkRenderer(cfg goldmark_config.Config) renderer.NodeRenderer {
|
2020-03-14 10:43:10 -04:00
|
|
|
r := &hookedRenderer{
|
2022-03-09 12:26:32 -05:00
|
|
|
linkifyProtocol: []byte(cfg.Extensions.LinkifyProtocol),
|
2019-11-27 07:42:36 -05:00
|
|
|
Config: html.Config{
|
|
|
|
Writer: html.DefaultWriter,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2022-03-09 12:26:32 -05:00
|
|
|
func newLinks(cfg goldmark_config.Config) goldmark.Extender {
|
|
|
|
return &links{cfg: cfg}
|
2019-11-27 07:42:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type linkContext struct {
|
2022-03-17 17:03:27 -04:00
|
|
|
page any
|
2024-04-11 11:46:18 -04:00
|
|
|
pageInner any
|
2019-11-27 07:42:36 -05:00
|
|
|
destination string
|
|
|
|
title string
|
2022-03-10 02:19:03 -05:00
|
|
|
text hstring.RenderedString
|
2019-12-18 11:23:09 -05:00
|
|
|
plainText string
|
2022-12-03 06:33:48 -05:00
|
|
|
*attributes.AttributesHolder
|
2019-11-27 07:42:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx linkContext) Destination() string {
|
|
|
|
return ctx.destination
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ctx linkContext) Page() any {
|
2019-11-27 07:42:36 -05:00
|
|
|
return ctx.page
|
|
|
|
}
|
|
|
|
|
2024-04-11 11:46:18 -04:00
|
|
|
func (ctx linkContext) PageInner() any {
|
|
|
|
return ctx.pageInner
|
|
|
|
}
|
|
|
|
|
2022-03-10 02:19:03 -05:00
|
|
|
func (ctx linkContext) Text() hstring.RenderedString {
|
2019-11-27 07:42:36 -05:00
|
|
|
return ctx.text
|
|
|
|
}
|
|
|
|
|
2019-12-18 11:23:09 -05:00
|
|
|
func (ctx linkContext) PlainText() string {
|
|
|
|
return ctx.plainText
|
|
|
|
}
|
|
|
|
|
2019-11-27 07:42:36 -05:00
|
|
|
func (ctx linkContext) Title() string {
|
|
|
|
return ctx.title
|
|
|
|
}
|
|
|
|
|
2022-12-03 06:33:48 -05:00
|
|
|
type imageLinkContext struct {
|
|
|
|
linkContext
|
|
|
|
ordinal int
|
|
|
|
isBlock bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx imageLinkContext) IsBlock() bool {
|
|
|
|
return ctx.isBlock
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx imageLinkContext) Ordinal() int {
|
|
|
|
return ctx.ordinal
|
|
|
|
}
|
|
|
|
|
2020-03-14 10:43:10 -04:00
|
|
|
type headingContext struct {
|
2022-03-17 17:03:27 -04:00
|
|
|
page any
|
2024-04-11 11:46:18 -04:00
|
|
|
pageInner any
|
2020-03-14 10:43:10 -04:00
|
|
|
level int
|
|
|
|
anchor string
|
2022-03-10 02:19:03 -05:00
|
|
|
text hstring.RenderedString
|
2020-03-14 10:43:10 -04:00
|
|
|
plainText string
|
2022-02-17 07:04:00 -05:00
|
|
|
*attributes.AttributesHolder
|
2020-03-14 10:43:10 -04:00
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ctx headingContext) Page() any {
|
2020-03-14 10:43:10 -04:00
|
|
|
return ctx.page
|
|
|
|
}
|
|
|
|
|
2024-04-11 11:46:18 -04:00
|
|
|
func (ctx headingContext) PageInner() any {
|
|
|
|
return ctx.pageInner
|
|
|
|
}
|
|
|
|
|
2020-03-14 10:43:10 -04:00
|
|
|
func (ctx headingContext) Level() int {
|
|
|
|
return ctx.level
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx headingContext) Anchor() string {
|
|
|
|
return ctx.anchor
|
|
|
|
}
|
|
|
|
|
2022-03-10 02:19:03 -05:00
|
|
|
func (ctx headingContext) Text() hstring.RenderedString {
|
2020-03-14 10:43:10 -04:00
|
|
|
return ctx.text
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx headingContext) PlainText() string {
|
|
|
|
return ctx.plainText
|
|
|
|
}
|
|
|
|
|
|
|
|
type hookedRenderer struct {
|
2022-03-09 12:26:32 -05:00
|
|
|
linkifyProtocol []byte
|
2019-11-27 07:42:36 -05:00
|
|
|
html.Config
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func (r *hookedRenderer) SetOption(name renderer.OptionName, value any) {
|
2019-11-27 07:42:36 -05:00
|
|
|
r.Config.SetOption(name, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterFuncs implements NodeRenderer.RegisterFuncs.
|
2020-03-14 10:43:10 -04:00
|
|
|
func (r *hookedRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
|
2019-11-27 07:42:36 -05:00
|
|
|
reg.Register(ast.KindLink, r.renderLink)
|
2021-07-15 02:46:54 -04:00
|
|
|
reg.Register(ast.KindAutoLink, r.renderAutoLink)
|
2019-11-27 07:42:36 -05:00
|
|
|
reg.Register(ast.KindImage, r.renderImage)
|
2020-03-14 10:43:10 -04:00
|
|
|
reg.Register(ast.KindHeading, r.renderHeading)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *hookedRenderer) renderImage(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
|
|
|
n := node.(*ast.Image)
|
2022-02-17 07:04:00 -05:00
|
|
|
var lr hooks.LinkRenderer
|
2020-03-14 10:43:10 -04:00
|
|
|
|
2022-02-17 07:04:00 -05:00
|
|
|
ctx, ok := w.(*render.Context)
|
2020-03-14 10:43:10 -04:00
|
|
|
if ok {
|
2022-02-17 07:04:00 -05:00
|
|
|
h := ctx.RenderContext().GetRenderer(hooks.ImageRendererType, nil)
|
|
|
|
ok = h != nil
|
|
|
|
if ok {
|
|
|
|
lr = h.(hooks.LinkRenderer)
|
|
|
|
}
|
2020-03-14 10:43:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
2021-07-15 03:05:13 -04:00
|
|
|
return r.renderImageDefault(w, source, node, entering)
|
2020-03-14 10:43:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if entering {
|
|
|
|
// Store the current pos so we can capture the rendered text.
|
2022-02-17 07:04:00 -05:00
|
|
|
ctx.PushPos(ctx.Buffer.Len())
|
2020-03-14 10:43:10 -04:00
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
|
|
|
|
2022-02-17 07:04:00 -05:00
|
|
|
pos := ctx.PopPos()
|
2022-02-16 07:44:09 -05:00
|
|
|
text := ctx.Buffer.Bytes()[pos:]
|
|
|
|
ctx.Buffer.Truncate(pos)
|
2020-03-14 10:43:10 -04:00
|
|
|
|
2022-12-03 06:33:48 -05:00
|
|
|
var (
|
|
|
|
isBlock bool
|
|
|
|
ordinal int
|
|
|
|
)
|
|
|
|
if b, ok := n.AttributeString(images.AttrIsBlock); ok && b.(bool) {
|
|
|
|
isBlock = true
|
|
|
|
}
|
|
|
|
if n, ok := n.AttributeString(images.AttrOrdinal); ok {
|
|
|
|
ordinal = n.(int)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We use the attributes to signal from the parser whether the image is in
|
|
|
|
// a block context or not.
|
|
|
|
// We may find a better way to do that, but for now, we'll need to remove any
|
|
|
|
// internal attributes before rendering.
|
|
|
|
attrs := r.filterInternalAttributes(n.Attributes())
|
|
|
|
|
2022-02-17 07:04:00 -05:00
|
|
|
err := lr.RenderLink(
|
2023-02-25 03:24:59 -05:00
|
|
|
ctx.RenderContext().Ctx,
|
2020-03-14 10:43:10 -04:00
|
|
|
w,
|
2022-12-03 06:33:48 -05:00
|
|
|
imageLinkContext{
|
|
|
|
linkContext: linkContext{
|
|
|
|
page: ctx.DocumentContext().Document,
|
2024-04-11 11:46:18 -04:00
|
|
|
pageInner: r.getPageInner(ctx),
|
2022-12-03 06:33:48 -05:00
|
|
|
destination: string(n.Destination),
|
|
|
|
title: string(n.Title),
|
|
|
|
text: hstring.RenderedString(text),
|
|
|
|
plainText: string(n.Text(source)),
|
|
|
|
AttributesHolder: attributes.New(attrs, attributes.AttributesOwnerGeneral),
|
|
|
|
},
|
|
|
|
ordinal: ordinal,
|
|
|
|
isBlock: isBlock,
|
2020-03-14 10:43:10 -04:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return ast.WalkContinue, err
|
|
|
|
}
|
|
|
|
|
2024-04-11 11:46:18 -04:00
|
|
|
func (r *hookedRenderer) getPageInner(rctx *render.Context) any {
|
|
|
|
pid := rctx.PeekPid()
|
|
|
|
if pid > 0 {
|
|
|
|
if lookup := rctx.DocumentContext().DocumentLookup; lookup != nil {
|
|
|
|
if v := rctx.DocumentContext().DocumentLookup(pid); v != nil {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rctx.DocumentContext().Document
|
|
|
|
}
|
|
|
|
|
2022-12-03 06:33:48 -05:00
|
|
|
func (r *hookedRenderer) filterInternalAttributes(attrs []ast.Attribute) []ast.Attribute {
|
|
|
|
n := 0
|
|
|
|
for _, x := range attrs {
|
|
|
|
if !bytes.HasPrefix(x.Name, []byte(internalAttrPrefix)) {
|
|
|
|
attrs[n] = x
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return attrs[:n]
|
|
|
|
}
|
|
|
|
|
2019-11-27 07:42:36 -05:00
|
|
|
// Fall back to the default Goldmark render funcs. Method below borrowed from:
|
|
|
|
// https://github.com/yuin/goldmark/blob/b611cd333a492416b56aa8d94b04a67bf0096ab2/renderer/html/html.go#L404
|
2021-07-15 03:05:13 -04:00
|
|
|
func (r *hookedRenderer) renderImageDefault(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
|
|
|
if !entering {
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
|
|
|
n := node.(*ast.Image)
|
|
|
|
_, _ = w.WriteString("<img src=\"")
|
|
|
|
if r.Unsafe || !html.IsDangerousURL(n.Destination) {
|
|
|
|
_, _ = w.Write(util.EscapeHTML(util.URLEscape(n.Destination, true)))
|
|
|
|
}
|
|
|
|
_, _ = w.WriteString(`" alt="`)
|
2023-05-30 07:21:45 -04:00
|
|
|
_, _ = w.Write(nodeToHTMLText(n, source))
|
2021-07-15 03:05:13 -04:00
|
|
|
_ = w.WriteByte('"')
|
|
|
|
if n.Title != nil {
|
|
|
|
_, _ = w.WriteString(` title="`)
|
|
|
|
r.Writer.Write(w, n.Title)
|
2019-11-27 07:42:36 -05:00
|
|
|
_ = w.WriteByte('"')
|
2021-07-15 03:05:13 -04:00
|
|
|
}
|
2022-12-03 06:33:48 -05:00
|
|
|
if n.Attributes() != nil {
|
|
|
|
attrs := r.filterInternalAttributes(n.Attributes())
|
|
|
|
attributes.RenderASTAttributes(w, attrs...)
|
|
|
|
}
|
2021-07-15 03:05:13 -04:00
|
|
|
if r.XHTML {
|
|
|
|
_, _ = w.WriteString(" />")
|
2019-11-27 07:42:36 -05:00
|
|
|
} else {
|
2021-07-15 03:05:13 -04:00
|
|
|
_, _ = w.WriteString(">")
|
2019-11-27 07:42:36 -05:00
|
|
|
}
|
2021-07-15 03:05:13 -04:00
|
|
|
return ast.WalkSkipChildren, nil
|
2019-11-27 07:42:36 -05:00
|
|
|
}
|
|
|
|
|
2020-03-14 10:43:10 -04:00
|
|
|
func (r *hookedRenderer) renderLink(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
|
|
|
n := node.(*ast.Link)
|
2022-02-17 07:04:00 -05:00
|
|
|
var lr hooks.LinkRenderer
|
2019-11-27 07:42:36 -05:00
|
|
|
|
2022-02-17 07:04:00 -05:00
|
|
|
ctx, ok := w.(*render.Context)
|
2019-11-27 07:42:36 -05:00
|
|
|
if ok {
|
2022-02-17 07:04:00 -05:00
|
|
|
h := ctx.RenderContext().GetRenderer(hooks.LinkRendererType, nil)
|
|
|
|
ok = h != nil
|
|
|
|
if ok {
|
|
|
|
lr = h.(hooks.LinkRenderer)
|
|
|
|
}
|
2019-11-27 07:42:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
2021-07-15 03:05:13 -04:00
|
|
|
return r.renderLinkDefault(w, source, node, entering)
|
2019-11-27 07:42:36 -05:00
|
|
|
}
|
|
|
|
|
2019-12-19 11:43:07 -05:00
|
|
|
if entering {
|
|
|
|
// Store the current pos so we can capture the rendered text.
|
2022-02-17 07:04:00 -05:00
|
|
|
ctx.PushPos(ctx.Buffer.Len())
|
2019-11-27 07:42:36 -05:00
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
|
|
|
|
2022-02-17 07:04:00 -05:00
|
|
|
pos := ctx.PopPos()
|
2022-02-16 07:44:09 -05:00
|
|
|
text := ctx.Buffer.Bytes()[pos:]
|
|
|
|
ctx.Buffer.Truncate(pos)
|
2019-12-18 11:23:09 -05:00
|
|
|
|
2022-02-17 07:04:00 -05:00
|
|
|
err := lr.RenderLink(
|
2023-02-25 03:24:59 -05:00
|
|
|
ctx.RenderContext().Ctx,
|
2019-11-27 07:42:36 -05:00
|
|
|
w,
|
|
|
|
linkContext{
|
2022-12-03 06:33:48 -05:00
|
|
|
page: ctx.DocumentContext().Document,
|
2024-04-11 11:46:18 -04:00
|
|
|
pageInner: r.getPageInner(ctx),
|
2022-12-03 06:33:48 -05:00
|
|
|
destination: string(n.Destination),
|
|
|
|
title: string(n.Title),
|
|
|
|
text: hstring.RenderedString(text),
|
|
|
|
plainText: string(n.Text(source)),
|
|
|
|
AttributesHolder: attributes.Empty,
|
2019-11-27 07:42:36 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2019-12-19 11:43:07 -05:00
|
|
|
return ast.WalkContinue, err
|
2020-03-14 10:43:10 -04:00
|
|
|
}
|
2019-11-27 07:42:36 -05:00
|
|
|
|
2021-07-15 03:05:13 -04:00
|
|
|
// Fall back to the default Goldmark render funcs. Method below borrowed from:
|
|
|
|
// https://github.com/yuin/goldmark/blob/b611cd333a492416b56aa8d94b04a67bf0096ab2/renderer/html/html.go#L404
|
|
|
|
func (r *hookedRenderer) renderLinkDefault(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
|
|
|
n := node.(*ast.Link)
|
|
|
|
if entering {
|
|
|
|
_, _ = w.WriteString("<a href=\"")
|
|
|
|
if r.Unsafe || !html.IsDangerousURL(n.Destination) {
|
|
|
|
_, _ = w.Write(util.EscapeHTML(util.URLEscape(n.Destination, true)))
|
|
|
|
}
|
|
|
|
_ = w.WriteByte('"')
|
|
|
|
if n.Title != nil {
|
|
|
|
_, _ = w.WriteString(` title="`)
|
|
|
|
r.Writer.Write(w, n.Title)
|
|
|
|
_ = w.WriteByte('"')
|
|
|
|
}
|
|
|
|
_ = w.WriteByte('>')
|
|
|
|
} else {
|
|
|
|
_, _ = w.WriteString("</a>")
|
|
|
|
}
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
|
|
|
|
2021-07-15 02:46:54 -04:00
|
|
|
func (r *hookedRenderer) renderAutoLink(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
|
|
|
if !entering {
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
n := node.(*ast.AutoLink)
|
2022-02-17 07:04:00 -05:00
|
|
|
var lr hooks.LinkRenderer
|
2021-07-15 02:46:54 -04:00
|
|
|
|
2022-02-17 07:04:00 -05:00
|
|
|
ctx, ok := w.(*render.Context)
|
2021-07-15 02:46:54 -04:00
|
|
|
if ok {
|
2022-02-17 07:04:00 -05:00
|
|
|
h := ctx.RenderContext().GetRenderer(hooks.LinkRendererType, nil)
|
|
|
|
ok = h != nil
|
|
|
|
if ok {
|
|
|
|
lr = h.(hooks.LinkRenderer)
|
|
|
|
}
|
2021-07-15 02:46:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
2021-07-15 03:05:13 -04:00
|
|
|
return r.renderAutoLinkDefault(w, source, node, entering)
|
2021-07-15 02:46:54 -04:00
|
|
|
}
|
|
|
|
|
2022-03-09 12:26:32 -05:00
|
|
|
url := string(r.autoLinkURL(n, source))
|
2021-07-15 02:46:54 -04:00
|
|
|
label := string(n.Label(source))
|
|
|
|
if n.AutoLinkType == ast.AutoLinkEmail && !strings.HasPrefix(strings.ToLower(url), "mailto:") {
|
|
|
|
url = "mailto:" + url
|
|
|
|
}
|
|
|
|
|
2022-02-17 07:04:00 -05:00
|
|
|
err := lr.RenderLink(
|
2023-02-25 03:24:59 -05:00
|
|
|
ctx.RenderContext().Ctx,
|
2021-07-15 02:46:54 -04:00
|
|
|
w,
|
|
|
|
linkContext{
|
2022-12-03 06:33:48 -05:00
|
|
|
page: ctx.DocumentContext().Document,
|
2024-04-11 11:46:18 -04:00
|
|
|
pageInner: r.getPageInner(ctx),
|
2022-12-03 06:33:48 -05:00
|
|
|
destination: url,
|
|
|
|
text: hstring.RenderedString(label),
|
|
|
|
plainText: label,
|
|
|
|
AttributesHolder: attributes.Empty,
|
2021-07-15 02:46:54 -04:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return ast.WalkContinue, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fall back to the default Goldmark render funcs. Method below borrowed from:
|
|
|
|
// https://github.com/yuin/goldmark/blob/5588d92a56fe1642791cf4aa8e9eae8227cfeecd/renderer/html/html.go#L439
|
2021-07-15 03:05:13 -04:00
|
|
|
func (r *hookedRenderer) renderAutoLinkDefault(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
2021-07-15 02:46:54 -04:00
|
|
|
n := node.(*ast.AutoLink)
|
|
|
|
if !entering {
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
2022-03-09 12:26:32 -05:00
|
|
|
|
2021-07-15 02:46:54 -04:00
|
|
|
_, _ = w.WriteString(`<a href="`)
|
2022-03-09 12:26:32 -05:00
|
|
|
url := r.autoLinkURL(n, source)
|
2021-07-15 02:46:54 -04:00
|
|
|
label := n.Label(source)
|
|
|
|
if n.AutoLinkType == ast.AutoLinkEmail && !bytes.HasPrefix(bytes.ToLower(url), []byte("mailto:")) {
|
|
|
|
_, _ = w.WriteString("mailto:")
|
|
|
|
}
|
|
|
|
_, _ = w.Write(util.EscapeHTML(util.URLEscape(url, false)))
|
|
|
|
if n.Attributes() != nil {
|
|
|
|
_ = w.WriteByte('"')
|
|
|
|
html.RenderAttributes(w, n, html.LinkAttributeFilter)
|
|
|
|
_ = w.WriteByte('>')
|
|
|
|
} else {
|
|
|
|
_, _ = w.WriteString(`">`)
|
|
|
|
}
|
|
|
|
_, _ = w.Write(util.EscapeHTML(label))
|
|
|
|
_, _ = w.WriteString(`</a>`)
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
|
|
|
|
2022-03-09 12:26:32 -05:00
|
|
|
func (r *hookedRenderer) autoLinkURL(n *ast.AutoLink, source []byte) []byte {
|
|
|
|
url := n.URL(source)
|
|
|
|
if len(n.Protocol) > 0 && !bytes.Equal(n.Protocol, r.linkifyProtocol) {
|
|
|
|
// The CommonMark spec says "http" is the correct protocol for links,
|
|
|
|
// but this doesn't make much sense (the fact that they should care about the rendered output).
|
|
|
|
// Note that n.Protocol is not set if protocol is provided by user.
|
|
|
|
url = append(r.linkifyProtocol, url[len(n.Protocol):]...)
|
|
|
|
}
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
|
2020-03-14 10:43:10 -04:00
|
|
|
func (r *hookedRenderer) renderHeading(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
|
|
|
n := node.(*ast.Heading)
|
2022-02-17 07:04:00 -05:00
|
|
|
var hr hooks.HeadingRenderer
|
2019-11-27 07:42:36 -05:00
|
|
|
|
2022-02-17 07:04:00 -05:00
|
|
|
ctx, ok := w.(*render.Context)
|
2019-11-27 07:42:36 -05:00
|
|
|
if ok {
|
2022-02-17 07:04:00 -05:00
|
|
|
h := ctx.RenderContext().GetRenderer(hooks.HeadingRendererType, nil)
|
|
|
|
ok = h != nil
|
|
|
|
if ok {
|
|
|
|
hr = h.(hooks.HeadingRenderer)
|
|
|
|
}
|
2019-11-27 07:42:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
2021-07-15 03:05:13 -04:00
|
|
|
return r.renderHeadingDefault(w, source, node, entering)
|
2019-11-27 07:42:36 -05:00
|
|
|
}
|
|
|
|
|
2019-12-18 11:23:09 -05:00
|
|
|
if entering {
|
|
|
|
// Store the current pos so we can capture the rendered text.
|
2022-02-17 07:04:00 -05:00
|
|
|
ctx.PushPos(ctx.Buffer.Len())
|
2019-11-27 07:42:36 -05:00
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
|
|
|
|
2022-02-17 07:04:00 -05:00
|
|
|
pos := ctx.PopPos()
|
2022-02-16 07:44:09 -05:00
|
|
|
text := ctx.Buffer.Bytes()[pos:]
|
|
|
|
ctx.Buffer.Truncate(pos)
|
2020-03-14 10:43:10 -04:00
|
|
|
// All ast.Heading nodes are guaranteed to have an attribute called "id"
|
|
|
|
// that is an array of bytes that encode a valid string.
|
|
|
|
anchori, _ := n.AttributeString("id")
|
|
|
|
anchor := anchori.([]byte)
|
2019-12-18 11:23:09 -05:00
|
|
|
|
2022-02-17 07:04:00 -05:00
|
|
|
err := hr.RenderHeading(
|
2023-02-25 03:24:59 -05:00
|
|
|
ctx.RenderContext().Ctx,
|
2019-11-27 07:42:36 -05:00
|
|
|
w,
|
2020-03-14 10:43:10 -04:00
|
|
|
headingContext{
|
2021-02-22 05:27:14 -05:00
|
|
|
page: ctx.DocumentContext().Document,
|
2024-04-11 11:46:18 -04:00
|
|
|
pageInner: r.getPageInner(ctx),
|
2021-02-22 05:27:14 -05:00
|
|
|
level: n.Level,
|
|
|
|
anchor: string(anchor),
|
2022-03-10 02:19:03 -05:00
|
|
|
text: hstring.RenderedString(text),
|
2021-02-22 05:27:14 -05:00
|
|
|
plainText: string(n.Text(source)),
|
2022-02-17 07:04:00 -05:00
|
|
|
AttributesHolder: attributes.New(n.Attributes(), attributes.AttributesOwnerGeneral),
|
2019-11-27 07:42:36 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2019-12-18 11:23:09 -05:00
|
|
|
return ast.WalkContinue, err
|
2019-11-27 07:42:36 -05:00
|
|
|
}
|
|
|
|
|
2021-07-15 03:05:13 -04:00
|
|
|
func (r *hookedRenderer) renderHeadingDefault(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
|
|
|
n := node.(*ast.Heading)
|
|
|
|
if entering {
|
|
|
|
_, _ = w.WriteString("<h")
|
|
|
|
_ = w.WriteByte("0123456"[n.Level])
|
|
|
|
if n.Attributes() != nil {
|
2022-02-17 07:04:00 -05:00
|
|
|
attributes.RenderASTAttributes(w, node.Attributes()...)
|
2021-07-15 03:05:13 -04:00
|
|
|
}
|
|
|
|
_ = w.WriteByte('>')
|
|
|
|
} else {
|
|
|
|
_, _ = w.WriteString("</h")
|
|
|
|
_ = w.WriteByte("0123456"[n.Level])
|
|
|
|
_, _ = w.WriteString(">\n")
|
|
|
|
}
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
|
|
|
|
2022-03-09 12:26:32 -05:00
|
|
|
type links struct {
|
|
|
|
cfg goldmark_config.Config
|
|
|
|
}
|
2019-11-27 07:42:36 -05:00
|
|
|
|
|
|
|
// Extend implements goldmark.Extender.
|
|
|
|
func (e *links) Extend(m goldmark.Markdown) {
|
|
|
|
m.Renderer().AddOptions(renderer.WithNodeRenderers(
|
2022-03-09 12:26:32 -05:00
|
|
|
util.Prioritized(newLinkRenderer(e.cfg), 100),
|
2019-11-27 07:42:36 -05:00
|
|
|
))
|
|
|
|
}
|
2023-05-30 07:21:45 -04:00
|
|
|
|
|
|
|
// Borrowed from Goldmark.
|
|
|
|
func nodeToHTMLText(n ast.Node, source []byte) []byte {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
for c := n.FirstChild(); c != nil; c = c.NextSibling() {
|
|
|
|
if s, ok := c.(*ast.String); ok && s.IsCode() {
|
|
|
|
buf.Write(s.Text(source))
|
|
|
|
} else if !c.HasChildren() {
|
|
|
|
buf.Write(util.EscapeHTML(c.Text(source)))
|
2023-11-07 22:47:15 -05:00
|
|
|
if t, ok := c.(*ast.Text); ok && t.SoftLineBreak() {
|
|
|
|
buf.WriteByte('\n')
|
|
|
|
}
|
2023-05-30 07:21:45 -04:00
|
|
|
} else {
|
|
|
|
buf.Write(nodeToHTMLText(c, source))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|