Skip to content

Fix markdown preview $$ support #31514

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

Merged
merged 3 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions modules/markup/markdown/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,14 @@ func TestMathBlock(t *testing.T) {
"$a$ ($b$) [$c$] {$d$}",
`<p><code class="language-math is-loading">a</code> (<code class="language-math is-loading">b</code>) [$c$] {$d$}</p>` + nl,
},
{
"$$a$$ test",
`<p><code class="language-math display is-loading">a</code> test</p>` + nl,
},
{
"test $$a$$",
`<p>test <code class="language-math display is-loading">a</code></p>` + nl,
},
}

for _, test := range testcases {
Expand Down
6 changes: 6 additions & 0 deletions modules/markup/markdown/math/block_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ func (b *blockParser) Open(parent ast.Node, reader text.Reader, pc parser.Contex
}
idx := bytes.Index(line[pos+2:], endBytes)
if idx >= 0 {
// for case $$ ... $$ any other text
for i := pos + idx + 4; i < len(line); i++ {
if line[i] != ' ' && line[i] != '\n' {
return nil, parser.NoChildren
}
}
segment.Stop = segment.Start + idx + 2
reader.Advance(segment.Len() - 1)
segment.Start += 2
Expand Down
31 changes: 31 additions & 0 deletions modules/markup/markdown/math/inline_block_node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package math

import (
"github.com/yuin/goldmark/ast"
)

// InlineBlock represents inline math e.g. $$...$$
type InlineBlock struct {
Inline
}

// InlineBlock implements InlineBlock.
func (n *InlineBlock) InlineBlock() {}

// KindInlineBlock is the kind for math inline block
var KindInlineBlock = ast.NewNodeKind("MathInlineBlock")

// Kind returns KindInlineBlock
func (n *InlineBlock) Kind() ast.NodeKind {
return KindInlineBlock
}

// NewInlineBlock creates a new ast math inline block node
func NewInlineBlock() *InlineBlock {
return &InlineBlock{
Inline{},
}
}
30 changes: 26 additions & 4 deletions modules/markup/markdown/math/inline_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,20 @@ var defaultInlineDollarParser = &inlineParser{
end: []byte{'$'},
}

var defaultDualDollarParser = &inlineParser{
start: []byte{'$', '$'},
end: []byte{'$', '$'},
}

// NewInlineDollarParser returns a new inline parser
func NewInlineDollarParser() parser.InlineParser {
return defaultInlineDollarParser
}

func NewInlineDualDollarParser() parser.InlineParser {
return defaultDualDollarParser
}

var defaultInlineBracketParser = &inlineParser{
start: []byte{'\\', '('},
end: []byte{'\\', ')'},
Expand All @@ -38,7 +47,7 @@ func NewInlineBracketParser() parser.InlineParser {

// Trigger triggers this parser on $ or \
func (parser *inlineParser) Trigger() []byte {
return parser.start[0:1]
return parser.start
}

func isPunctuation(b byte) bool {
Expand Down Expand Up @@ -88,7 +97,11 @@ func (parser *inlineParser) Parse(parent ast.Node, block text.Reader, pc parser.
break
}
suceedingCharacter := line[pos]
if !isPunctuation(suceedingCharacter) && !(suceedingCharacter == ' ') && !isBracket(suceedingCharacter) {
// check valid ending character
if !isPunctuation(suceedingCharacter) &&
!(suceedingCharacter == ' ') &&
!(suceedingCharacter == '\n') &&
!isBracket(suceedingCharacter) {
return nil
}
if line[ender-1] != '\\' {
Expand All @@ -101,12 +114,21 @@ func (parser *inlineParser) Parse(parent ast.Node, block text.Reader, pc parser.

block.Advance(opener)
_, pos := block.Position()
node := NewInline()
var node ast.Node
if parser == defaultDualDollarParser {
node = NewInlineBlock()
} else {
node = NewInline()
}
segment := pos.WithStop(pos.Start + ender - opener)
node.AppendChild(node, ast.NewRawTextSegment(segment))
block.Advance(ender - opener + len(parser.end))

trimBlock(node, block)
if parser == defaultDualDollarParser {
trimBlock(&(node.(*InlineBlock)).Inline, block)
} else {
trimBlock(node.(*Inline), block)
}
return node
}

Expand Down
7 changes: 6 additions & 1 deletion modules/markup/markdown/math/inline_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ func NewInlineRenderer() renderer.NodeRenderer {

func (r *InlineRenderer) renderInline(w util.BufWriter, source []byte, n ast.Node, entering bool) (ast.WalkStatus, error) {
if entering {
_, _ = w.WriteString(`<code class="language-math is-loading">`)
extraClass := ""
if _, ok := n.(*InlineBlock); ok {
extraClass = "display "
}
_, _ = w.WriteString(`<code class="language-math ` + extraClass + `is-loading">`)
for c := n.FirstChild(); c != nil; c = c.NextSibling() {
segment := c.(*ast.Text).Segment
value := util.EscapeHTML(segment.Value(source))
Expand All @@ -43,4 +47,5 @@ func (r *InlineRenderer) renderInline(w util.BufWriter, source []byte, n ast.Nod
// RegisterFuncs registers the renderer for inline math nodes
func (r *InlineRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
reg.Register(KindInline, r.renderInline)
reg.Register(KindInlineBlock, r.renderInline)
}
3 changes: 2 additions & 1 deletion modules/markup/markdown/math/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ func (e *Extension) Extend(m goldmark.Markdown) {
util.Prioritized(NewInlineBracketParser(), 501),
}
if e.parseDollarInline {
inlines = append(inlines, util.Prioritized(NewInlineDollarParser(), 501))
inlines = append(inlines, util.Prioritized(NewInlineDollarParser(), 503),
util.Prioritized(NewInlineDualDollarParser(), 502))
}
m.Parser().AddOptions(parser.WithInlineParsers(inlines...))

Expand Down