summaryrefslogtreecommitdiffstats
path: root/markup/goldmark/blockquotes/blockquotes.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2024-08-30 10:58:43 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2024-08-31 17:21:31 +0200
commitf738669a4d09ca04619f4d0f89d90c9b414e9f2e (patch)
tree5e05f8a849d921dc4380ac8552fd90891331b04a /markup/goldmark/blockquotes/blockquotes.go
parentb63f24adc78e09c1044b448daa7c6cb375cebee6 (diff)
downloadhugo-f738669a4d09ca04619f4d0f89d90c9b414e9f2e.tar.gz
hugo-f738669a4d09ca04619f4d0f89d90c9b414e9f2e.zip
Add Markdown render hooks for tables
Fixes #9316 Fixes #12811
Diffstat (limited to 'markup/goldmark/blockquotes/blockquotes.go')
-rw-r--r--markup/goldmark/blockquotes/blockquotes.go101
1 files changed, 10 insertions, 91 deletions
diff --git a/markup/goldmark/blockquotes/blockquotes.go b/markup/goldmark/blockquotes/blockquotes.go
index d26c92669..a261ec4fe 100644
--- a/markup/goldmark/blockquotes/blockquotes.go
+++ b/markup/goldmark/blockquotes/blockquotes.go
@@ -16,10 +16,8 @@ package blockquotes
import (
"regexp"
"strings"
- "sync"
"github.com/gohugoio/hugo/common/herrors"
- htext "github.com/gohugoio/hugo/common/text"
"github.com/gohugoio/hugo/common/types/hstring"
"github.com/gohugoio/hugo/markup/converter/hooks"
"github.com/gohugoio/hugo/markup/goldmark/internal/render"
@@ -71,70 +69,36 @@ func (r *htmlRenderer) renderBlockquote(w util.BufWriter, src []byte, node ast.N
return ast.WalkContinue, nil
}
- pos := ctx.PopPos()
- text := ctx.Buffer.Bytes()[pos:]
- ctx.Buffer.Truncate(pos)
+ text := ctx.PopRenderedString()
ordinal := ctx.GetAndIncrementOrdinal(ast.KindBlockquote)
- texts := string(text)
typ := typeRegular
- alertType := resolveGitHubAlert(texts)
+ alertType := resolveGitHubAlert(string(text))
if alertType != "" {
typ = typeAlert
}
renderer := ctx.RenderContext().GetRenderer(hooks.BlockquoteRendererType, typ)
if renderer == nil {
- return r.renderBlockquoteDefault(w, n, texts)
+ return r.renderBlockquoteDefault(w, n, text)
}
if typ == typeAlert {
// Trim preamble: <p>[!NOTE]<br>\n but preserve leading paragraph.
// We could possibly complicate this by moving this to the parser, but
// keep it simple for now.
- texts = "<p>" + texts[strings.Index(texts, "\n")+1:]
- }
-
- var sourceRef []byte
-
- // Extract a source sample to use for position information.
- if nn := n.FirstChild(); nn != nil {
- var start, stop int
- for i := 0; i < nn.Lines().Len() && i < 2; i++ {
- line := nn.Lines().At(i)
- if i == 0 {
- start = line.Start
- }
- stop = line.Stop
- }
- // We do not mutate the source, so this is safe.
- sourceRef = src[start:stop]
+ text = "<p>" + text[strings.Index(text, "\n")+1:]
}
bqctx := &blockquoteContext{
- page: ctx.DocumentContext().Document,
- pageInner: r.getPageInner(ctx),
+ BaseContext: render.NewBaseContext(ctx, renderer, n, src, nil, ordinal),
typ: typ,
alertType: alertType,
- text: hstring.RenderedString(texts),
- sourceRef: sourceRef,
- ordinal: ordinal,
+ text: hstring.RenderedString(text),
AttributesHolder: attributes.New(n.Attributes(), attributes.AttributesOwnerGeneral),
}
- bqctx.createPos = func() htext.Position {
- if resolver, ok := renderer.(hooks.ElementPositionResolver); ok {
- return resolver.ResolvePosition(bqctx)
- }
-
- return htext.Position{
- Filename: ctx.DocumentContext().Filename,
- LineNumber: 1,
- ColumnNumber: 1,
- }
- }
-
cr := renderer.(hooks.BlockquoteRenderer)
err := cr.RenderBlockquote(
@@ -143,24 +107,12 @@ func (r *htmlRenderer) renderBlockquote(w util.BufWriter, src []byte, node ast.N
bqctx,
)
if err != nil {
- return ast.WalkContinue, herrors.NewFileErrorFromPos(err, bqctx.createPos())
+ return ast.WalkContinue, herrors.NewFileErrorFromPos(err, bqctx.Position())
}
return ast.WalkContinue, nil
}
-func (r *htmlRenderer) 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
-}
-
// Code borrowed from goldmark's html renderer.
func (r *htmlRenderer) renderBlockquoteDefault(
w util.BufWriter, n ast.Node, text string,
@@ -180,19 +132,11 @@ func (r *htmlRenderer) renderBlockquoteDefault(
}
type blockquoteContext struct {
- page any
- pageInner any
+ hooks.BaseContext
+
text hstring.RenderedString
- typ string
- sourceRef []byte
alertType string
- ordinal int
-
- // This is only used in error situations and is expensive to create,
- // so delay creation until needed.
- pos htext.Position
- posInit sync.Once
- createPos func() htext.Position
+ typ string
*attributes.AttributesHolder
}
@@ -205,35 +149,10 @@ func (c *blockquoteContext) AlertType() string {
return c.alertType
}
-func (c *blockquoteContext) Page() any {
- return c.page
-}
-
-func (c *blockquoteContext) PageInner() any {
- return c.pageInner
-}
-
func (c *blockquoteContext) Text() hstring.RenderedString {
return c.text
}
-func (c *blockquoteContext) Ordinal() int {
- return c.ordinal
-}
-
-func (c *blockquoteContext) Position() htext.Position {
- c.posInit.Do(func() {
- c.pos = c.createPos()
- })
- return c.pos
-}
-
-func (c *blockquoteContext) PositionerSourceTarget() []byte {
- return c.sourceRef
-}
-
-var _ hooks.PositionerSourceTargetProvider = (*blockquoteContext)(nil)
-
// https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#alerts
// Five types:
// [!NOTE], [!TIP], [!WARNING], [!IMPORTANT], [!CAUTION]