| 1 |
package extras |
| 2 |
|
| 3 |
import ( |
| 4 |
"slices" |
| 5 |
|
| 6 |
"github.com/yuin/goldmark" |
| 7 |
"github.com/yuin/goldmark/ast" |
| 8 |
"github.com/yuin/goldmark/parser" |
| 9 |
"github.com/yuin/goldmark/renderer" |
| 10 |
"github.com/yuin/goldmark/renderer/html" |
| 11 |
"github.com/yuin/goldmark/text" |
| 12 |
"github.com/yuin/goldmark/util" |
| 13 |
) |
| 14 |
|
| 15 |
type inlineTagDelimiterProcessor struct { |
| 16 |
InlineTag |
| 17 |
} |
| 18 |
|
| 19 |
func newInlineTagDelimiterProcessor(tag InlineTag) parser.DelimiterProcessor { |
| 20 |
return &inlineTagDelimiterProcessor{tag} |
| 21 |
} |
| 22 |
|
| 23 |
func (p *inlineTagDelimiterProcessor) IsDelimiter(b byte) bool { |
| 24 |
return b == p.Char |
| 25 |
} |
| 26 |
|
| 27 |
func (p *inlineTagDelimiterProcessor) CanOpenCloser(opener, closer *parser.Delimiter) bool { |
| 28 |
return opener.Char == closer.Char |
| 29 |
} |
| 30 |
|
| 31 |
func (p *inlineTagDelimiterProcessor) OnMatch(_ int) ast.Node { |
| 32 |
return newInlineTag(p.InlineTag) |
| 33 |
} |
| 34 |
|
| 35 |
type inlineTagParser struct { |
| 36 |
InlineTag |
| 37 |
} |
| 38 |
|
| 39 |
func newInlineTagParser(tag InlineTag) parser.InlineParser { |
| 40 |
return &inlineTagParser{InlineTag: tag} |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
func (s *inlineTagParser) Trigger() []byte { |
| 45 |
return []byte{s.Char} |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
func (s *inlineTagParser) Parse(_ ast.Node, block text.Reader, pc parser.Context) ast.Node { |
| 50 |
before := block.PrecendingCharacter() |
| 51 |
line, segment := block.PeekLine() |
| 52 |
|
| 53 |
|
| 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' |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
node := parser.ScanDelimiter(modifiedLine, before, s.Number, newInlineTagDelimiterProcessor(s.InlineTag)) |
| 63 |
if node == nil || node.OriginalLength > 2 || before == rune(s.Char) { |
| 64 |
return nil |
| 65 |
} |
| 66 |
node.Segment = segment.WithStop(segment.Start + node.OriginalLength) |
| 67 |
block.Advance(node.OriginalLength) |
| 68 |
pc.PushDelimiter(node) |
| 69 |
return node |
| 70 |
} |
| 71 |
|
| 72 |
type inlineTagHTMLRenderer struct { |
| 73 |
htmlTag string |
| 74 |
tagKind ast.NodeKind |
| 75 |
html.Config |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
func NewInlineTagHTMLRenderer(tag InlineTag, opts ...html.Option) renderer.NodeRenderer { |
| 80 |
r := &inlineTagHTMLRenderer{ |
| 81 |
htmlTag: tag.Html, |
| 82 |
tagKind: tag.TagKind, |
| 83 |
Config: html.NewConfig(), |
| 84 |
} |
| 85 |
for _, opt := range opts { |
| 86 |
opt.SetHTMLOption(&r.Config) |
| 87 |
} |
| 88 |
return r |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
func (r *inlineTagHTMLRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) { |
| 93 |
reg.Register(r.tagKind, r.renderInlineTag) |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
var inlineTagAttributeFilter = html.GlobalAttributeFilter |
| 98 |
|
| 99 |
|
| 100 |
func (r *inlineTagHTMLRenderer) renderInlineTag( |
| 101 |
w util.BufWriter, _ []byte, n ast.Node, entering bool, |
| 102 |
) (ast.WalkStatus, error) { |
| 103 |
if entering { |
| 104 |
_ = w.WriteByte('<') |
| 105 |
_, _ = w.WriteString(r.htmlTag) |
| 106 |
if n.Attributes() != nil { |
| 107 |
html.RenderAttributes(w, n, inlineTagAttributeFilter) |
| 108 |
} |
| 109 |
} else { |
| 110 |
_, _ = w.WriteString("</") |
| 111 |
_, _ = w.WriteString(r.htmlTag) |
| 112 |
} |
| 113 |
_ = w.WriteByte('>') |
| 114 |
return ast.WalkContinue, nil |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
type inlineExtension struct { |
| 119 |
conf Config |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
type Config struct { |
| 124 |
Superscript SuperscriptConfig |
| 125 |
Subscript SubscriptConfig |
| 126 |
Insert InsertConfig |
| 127 |
Mark MarkConfig |
| 128 |
Delete DeleteConfig |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
type SuperscriptConfig struct { |
| 133 |
Enable bool |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
type SubscriptConfig struct { |
| 138 |
Enable bool |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
type InsertConfig struct { |
| 143 |
Enable bool |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
type MarkConfig struct { |
| 148 |
Enable bool |
| 149 |
} |
| 150 |
|
| 151 |
type DeleteConfig struct { |
| 152 |
Enable bool |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
func New(config Config) goldmark.Extender { |
| 158 |
return &inlineExtension{ |
| 159 |
conf: config, |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
func (tag *inlineExtension) Extend(md goldmark.Markdown) { |
| 165 |
addTag := func(tag InlineTag) { |
| 166 |
md.Parser().AddOptions(parser.WithInlineParsers( |
| 167 |
util.Prioritized(newInlineTagParser(tag), tag.ParsePriority), |
| 168 |
)) |
| 169 |
md.Renderer().AddOptions(renderer.WithNodeRenderers( |
| 170 |
util.Prioritized(NewInlineTagHTMLRenderer(tag), tag.RenderPriority), |
| 171 |
)) |
| 172 |
} |
| 173 |
if tag.conf.Superscript.Enable { |
| 174 |
addTag(SuperscriptTag) |
| 175 |
} |
| 176 |
if tag.conf.Subscript.Enable { |
| 177 |
addTag(SubscriptTag) |
| 178 |
} |
| 179 |
if tag.conf.Insert.Enable { |
| 180 |
addTag(InsertTag) |
| 181 |
} |
| 182 |
if tag.conf.Mark.Enable { |
| 183 |
addTag(MarkTag) |
| 184 |
} |
| 185 |
if tag.conf.Delete.Enable { |
| 186 |
addTag(DeleteTag) |
| 187 |
} |
| 188 |
} |