Skip to content

Commit ab277a4

Browse files
authored
extras: Allow superscript to begin with plus, minus, or single quote
This is admittedly a trick, but it is (a) isolated to superscript parsing, and (b) only applicable to the plus, minus, and single quote symbols. The goldmark parser.ScanDelimiter function receives the line, but doesn't pass it on to anything else. So all we're doing is replacing the plus, minus, or single quote symbol with the letter "z" if the symbol is the first character after the delimiter. Using the letter "z" was an arbitrary choice. Closes #30
1 parent ecb5d4b commit ab277a4

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

‎extras/_test/superscript.txt‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,24 @@ text[^1] text[^2]
6060
//- - - - - - - - -//
6161
<p>text[^1] text[^2]</p>
6262
//= = = = = = = = = = = = = = = = = = = = = = = =//
63+
64+
10: Superscript is one of +, -, '
65+
//- - - - - - - - -//
66+
x^+^ x^-^ x^'^
67+
//- - - - - - - - -//
68+
<p>x<sup>+</sup> x<sup>-</sup> x<sup>'</sup></p>
69+
//= = = = = = = = = = = = = = = = = = = = = = = =//
70+
71+
11: Superscript begins with one of +, -, '
72+
//- - - - - - - - -//
73+
x^+2^ x^-2^ x^'2^
74+
//- - - - - - - - -//
75+
<p>x<sup>+2</sup> x<sup>-2</sup> x<sup>'2</sup></p>
76+
//= = = = = = = = = = = = = = = = = = = = = = = =//
77+
78+
12: Superscript ends with one of +, -, '
79+
//- - - - - - - - -//
80+
x^2+^ x^2-^ x^2'^
81+
//- - - - - - - - -//
82+
<p>x<sup>2+</sup> x<sup>2-</sup> x<sup>2'</sup></p>
83+
//= = = = = = = = = = = = = = = = = = = = = = = =//

‎extras/inline.go‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package extras
22

33
import (
4+
"slices"
5+
46
"github.com/yuin/goldmark"
57
"github.com/yuin/goldmark/ast"
68
"github.com/yuin/goldmark/parser"
@@ -47,7 +49,17 @@ func (s *inlineTagParser) Trigger() []byte {
4749
func (s *inlineTagParser) Parse(_ ast.Node, block text.Reader, pc parser.Context) ast.Node {
4850
before := block.PrecendingCharacter()
4951
line, segment := block.PeekLine()
50-
node := parser.ScanDelimiter(line, before, s.Number, newInlineTagDelimiterProcessor(s.inlineTag))
52+
53+
// Issue 30
54+
modifiedLine := slices.Clone(line)
55+
if s.inlineTag.TagKind == kindSuperscript && len(line) > s.Number {
56+
symbols := []byte{'+', '-', '\''}
57+
if slices.Contains(symbols, line[s.Number]) {
58+
modifiedLine[s.Number] = 'z' // replace with any letter or number
59+
}
60+
}
61+
62+
node := parser.ScanDelimiter(modifiedLine, before, s.Number, newInlineTagDelimiterProcessor(s.inlineTag))
5163
if node == nil || node.OriginalLength > 2 || before == rune(s.Char) {
5264
return nil
5365
}

0 commit comments

Comments
 (0)