Skip to content

Commit

Permalink
extras: Allow superscript to begin with plus, minus, or single quote
Browse files Browse the repository at this point in the history
  • Loading branch information
jmooring committed Dec 23, 2024
1 parent ca07402 commit 007782b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
21 changes: 21 additions & 0 deletions extras/_test/superscript.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,24 @@ text[^1] text[^2]
//- - - - - - - - -//
<p>text[^1] text[^2]</p>
//= = = = = = = = = = = = = = = = = = = = = = = =//

10: Superscript is one of +, -, '
//- - - - - - - - -//
x^+^ x^-^ x^'^
//- - - - - - - - -//
<p>x<sup>+</sup> x<sup>-</sup> x<sup>'</sup></p>
//= = = = = = = = = = = = = = = = = = = = = = = =//

11: Superscript begins with one of +, -, '
//- - - - - - - - -//
x^+2^ x^-2^ x^'2^
//- - - - - - - - -//
<p>x<sup>+2</sup> x<sup>-2</sup> x<sup>'2</sup></p>
//= = = = = = = = = = = = = = = = = = = = = = = =//

12: Superscript ends with one of +, -, '
//- - - - - - - - -//
x^2+^ x^2-^ x^2'^
//- - - - - - - - -//
<p>x<sup>2+</sup> x<sup>2-</sup> x<sup>2'</sup></p>
//= = = = = = = = = = = = = = = = = = = = = = = =//
14 changes: 13 additions & 1 deletion extras/inline.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package extras

import (
"slices"

"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
Expand Down Expand Up @@ -47,7 +49,17 @@ func (s *inlineTagParser) Trigger() []byte {
func (s *inlineTagParser) Parse(_ ast.Node, block text.Reader, pc parser.Context) ast.Node {
before := block.PrecendingCharacter()
line, segment := block.PeekLine()
node := parser.ScanDelimiter(line, before, s.Number, newInlineTagDelimiterProcessor(s.inlineTag))

// Issue 30
lineClone := slices.Clone(line)
if s.inlineTag.TagKind == kindSuperscript && len(line) > s.Number {
switch line[s.Number] {
case byte('+'), byte('-'), byte('\''):
lineClone[s.Number] = byte('z') // replace symbols with any letter
}
}

node := parser.ScanDelimiter(lineClone, before, s.Number, newInlineTagDelimiterProcessor(s.inlineTag))
if node == nil || node.OriginalLength > 2 || before == rune(s.Char) {
return nil
}
Expand Down

0 comments on commit 007782b

Please sign in to comment.