From 33c0e92138e85fe3ca1c54d9b5fd1286bfa85ab3 Mon Sep 17 00:00:00 2001 From: Joe Mooring Date: Sun, 22 Dec 2024 14:39:44 -0800 Subject: [PATCH] extras: Allow superscript to begin with plus, minus, or single quote Closes #30 --- extras/_test/superscript.txt | 21 +++++++++++++++++++++ extras/inline.go | 14 +++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/extras/_test/superscript.txt b/extras/_test/superscript.txt index 764cfc2..f51d96b 100644 --- a/extras/_test/superscript.txt +++ b/extras/_test/superscript.txt @@ -60,3 +60,24 @@ text[^1] text[^2] //- - - - - - - - -//

text[^1] text[^2]

//= = = = = = = = = = = = = = = = = = = = = = = =// + +10: Superscript is one of +, -, ' +//- - - - - - - - -// +x^+^ x^-^ x^'^ +//- - - - - - - - -// +

x+ x- x'

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

x+2 x-2 x'2

+//= = = = = = = = = = = = = = = = = = = = = = = =// + +12: Superscript ends with one of +, -, ' +//- - - - - - - - -// +x^2+^ x^2-^ x^2'^ +//- - - - - - - - -// +

x2+ x2- x2'

+//= = = = = = = = = = = = = = = = = = = = = = = =// diff --git a/extras/inline.go b/extras/inline.go index 26b0f12..e18526b 100644 --- a/extras/inline.go +++ b/extras/inline.go @@ -1,6 +1,8 @@ package extras import ( + "slices" + "github.com/yuin/goldmark" "github.com/yuin/goldmark/ast" "github.com/yuin/goldmark/parser" @@ -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 }