Skip to content

Commit a288b49

Browse files
committed
Simplify rendering inline block of HTML code blocks.
1 parent f347858 commit a288b49

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

modules/markup/markdown/math/inline_block_node.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
// InlineBlock represents inline math e.g. $$...$$
1111
type InlineBlock struct {
1212
Inline
13+
ExtraClass string
1314
}
1415

1516
// InlineBlock implements InlineBlock.
@@ -27,5 +28,6 @@ func (n *InlineBlock) Kind() ast.NodeKind {
2728
func NewInlineBlock() *InlineBlock {
2829
return &InlineBlock{
2930
Inline{},
31+
"display",
3032
}
3133
}

modules/markup/markdown/math/inline_parser.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ func (parser *inlineParser) Parse(parent ast.Node, block text.Reader, pc parser.
9797
break
9898
}
9999
suceedingCharacter := line[pos]
100-
if !isPunctuation(suceedingCharacter) && !(suceedingCharacter == ' ') && !isBracket(suceedingCharacter) {
100+
// check valid ending character
101+
if !isPunctuation(suceedingCharacter) &&
102+
!(suceedingCharacter == ' ') &&
103+
!(suceedingCharacter == '\n') &&
104+
!isBracket(suceedingCharacter) {
101105
return nil
102106
}
103107
if line[ender-1] != '\\' {

modules/markup/markdown/math/inline_renderer.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package math
55

66
import (
77
"bytes"
8+
"strings"
89

910
"github.com/yuin/goldmark/ast"
1011
"github.com/yuin/goldmark/renderer"
@@ -21,11 +22,11 @@ func NewInlineRenderer() renderer.NodeRenderer {
2122

2223
func (r *InlineRenderer) renderInline(w util.BufWriter, source []byte, n ast.Node, entering bool) (ast.WalkStatus, error) {
2324
if entering {
24-
if _, ok := n.(*InlineBlock); ok {
25-
_, _ = w.WriteString(`<pre class="code-block is-loading"><code class="chroma language-math display">`)
26-
} else {
27-
_, _ = w.WriteString(`<code class="language-math is-loading">`)
25+
extraClass := ""
26+
if block, ok := n.(*InlineBlock); ok {
27+
extraClass = strings.TrimSpace(block.ExtraClass) + " "
2828
}
29+
_, _ = w.WriteString(`<code class="language-math ` + extraClass + `is-loading">`)
2930
for c := n.FirstChild(); c != nil; c = c.NextSibling() {
3031
segment := c.(*ast.Text).Segment
3132
value := util.EscapeHTML(segment.Value(source))
@@ -40,11 +41,7 @@ func (r *InlineRenderer) renderInline(w util.BufWriter, source []byte, n ast.Nod
4041
}
4142
return ast.WalkSkipChildren, nil
4243
}
43-
if _, ok := n.(*InlineBlock); ok {
44-
_, _ = w.WriteString(`</code></pre>`)
45-
} else {
46-
_, _ = w.WriteString(`</code>`)
47-
}
44+
_, _ = w.WriteString(`</code>`)
4845
return ast.WalkContinue, nil
4946
}
5047

modules/markup/markdown/math/math.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ func (e *Extension) Extend(m goldmark.Markdown) {
9696
util.Prioritized(NewInlineBracketParser(), 501),
9797
}
9898
if e.parseDollarInline {
99-
inlines = append(inlines, util.Prioritized(NewInlineDualDollarParser(), 501),
100-
util.Prioritized(NewInlineDollarParser(), 502))
99+
inlines = append(inlines, util.Prioritized(NewInlineDollarParser(), 503),
100+
util.Prioritized(NewInlineDualDollarParser(), 502))
101101
}
102102
m.Parser().AddOptions(parser.WithInlineParsers(inlines...))
103103

0 commit comments

Comments
 (0)