Skip to content

Commit 056ab70

Browse files
committed
fix inline placement of display math
Signed-off-by: Andrew Thornton <art27@cantab.net>
1 parent 47127fe commit 056ab70

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

modules/markup/markdown/math/block_node.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,20 @@ import "github.com/yuin/goldmark/ast"
99
// Block represents a math Block
1010
type Block struct {
1111
ast.BaseBlock
12+
Dollars bool
13+
Indent int
14+
Closed bool
1215
}
1316

1417
// KindBlock is the node kind for math blocks
1518
var KindBlock = ast.NewNodeKind("MathBlock")
1619

1720
// NewBlock creates a new math Block
18-
func NewBlock() *Block {
19-
return &Block{}
21+
func NewBlock(dollars bool, indent int) *Block {
22+
return &Block{
23+
Dollars: dollars,
24+
Indent: indent,
25+
}
2026
}
2127

2228
// Dump dumps the block to a string

modules/markup/markdown/math/block_parser.go

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package math
66

77
import (
8+
"bytes"
9+
810
"github.com/yuin/goldmark/ast"
911
"github.com/yuin/goldmark/parser"
1012
"github.com/yuin/goldmark/text"
@@ -15,13 +17,6 @@ type blockParser struct {
1517
parseDollars bool
1618
}
1719

18-
type blockData struct {
19-
dollars bool
20-
indent int
21-
}
22-
23-
var blockInfoKey = parser.NewContextKey()
24-
2520
// NewBlockParser creates a new math BlockParser
2621
func NewBlockParser(parseDollarBlocks bool) parser.BlockParser {
2722
return &blockParser{
@@ -31,7 +26,7 @@ func NewBlockParser(parseDollarBlocks bool) parser.BlockParser {
3126

3227
// Open parses the current line and returns a result of parsing.
3328
func (b *blockParser) Open(parent ast.Node, reader text.Reader, pc parser.Context) (ast.Node, parser.State) {
34-
line, _ := reader.PeekLine()
29+
line, segment := reader.PeekLine()
3530
pos := pc.BlockOffset()
3631
if pos == -1 || len(line[pos:]) < 2 {
3732
return nil, parser.NoChildren
@@ -44,33 +39,57 @@ func (b *blockParser) Open(parent ast.Node, reader text.Reader, pc parser.Contex
4439
return nil, parser.NoChildren
4540
}
4641

47-
pc.Set(blockInfoKey, &blockData{dollars: dollars, indent: pos})
48-
node := NewBlock()
42+
node := NewBlock(dollars, pos)
43+
44+
// Now we need to check if the ending block is on the segment...
45+
endBytes := []byte{'\\', ']'}
46+
if dollars {
47+
endBytes = []byte{'$', '$'}
48+
}
49+
idx := bytes.Index(line[pos+2:], endBytes)
50+
if idx >= 0 {
51+
segment.Stop = segment.Start + idx + 2
52+
reader.Advance(segment.Len() - 1)
53+
segment.Start += 2
54+
node.Lines().Append(segment)
55+
node.Closed = true
56+
return node, parser.Close | parser.NoChildren
57+
}
58+
59+
reader.Advance(segment.Len() - 1)
60+
segment.Start += 2
61+
node.Lines().Append(segment)
4962
return node, parser.NoChildren
5063
}
5164

5265
// Continue parses the current line and returns a result of parsing.
5366
func (b *blockParser) Continue(node ast.Node, reader text.Reader, pc parser.Context) parser.State {
67+
block := node.(*Block)
68+
if block.Closed {
69+
return parser.Close
70+
}
71+
5472
line, segment := reader.PeekLine()
55-
data := pc.Get(blockInfoKey).(*blockData)
5673
w, pos := util.IndentWidth(line, 0)
5774
if w < 4 {
58-
if data.dollars {
75+
if block.Dollars {
5976
i := pos
6077
for ; i < len(line) && line[i] == '$'; i++ {
6178
}
6279
length := i - pos
6380
if length >= 2 && util.IsBlank(line[i:]) {
6481
reader.Advance(segment.Stop - segment.Start - segment.Padding)
82+
block.Closed = true
6583
return parser.Close
6684
}
6785
} else if len(line[pos:]) > 1 && line[pos] == '\\' && line[pos+1] == ']' && util.IsBlank(line[pos+2:]) {
6886
reader.Advance(segment.Stop - segment.Start - segment.Padding)
87+
block.Closed = true
6988
return parser.Close
7089
}
7190
}
7291

73-
pos, padding := util.IndentPosition(line, 0, data.indent)
92+
pos, padding := util.IndentPosition(line, 0, block.Indent)
7493
seg := text.NewSegmentPadding(segment.Start+pos, segment.Stop, padding)
7594
node.Lines().Append(seg)
7695
reader.AdvanceAndSetPadding(segment.Stop-segment.Start-pos-1, padding)
@@ -79,7 +98,7 @@ func (b *blockParser) Continue(node ast.Node, reader text.Reader, pc parser.Cont
7998

8099
// Close will be called when the parser returns Close.
81100
func (b *blockParser) Close(node ast.Node, reader text.Reader, pc parser.Context) {
82-
pc.Set(blockInfoKey, nil)
101+
// noop
83102
}
84103

85104
// CanInterruptParagraph returns true if the parser can interrupt paragraphs,

0 commit comments

Comments
 (0)