mirror of https://github.com/mautrix/go.git
77 lines
2.2 KiB
Go
77 lines
2.2 KiB
Go
// Copyright (c) 2024 Tulir Asokan
|
|
//
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
package mdext
|
|
|
|
import (
|
|
"github.com/yuin/goldmark"
|
|
gast "github.com/yuin/goldmark/ast"
|
|
"github.com/yuin/goldmark/extension"
|
|
"github.com/yuin/goldmark/extension/ast"
|
|
"github.com/yuin/goldmark/parser"
|
|
"github.com/yuin/goldmark/renderer"
|
|
"github.com/yuin/goldmark/text"
|
|
"github.com/yuin/goldmark/util"
|
|
)
|
|
|
|
var ShortStrike goldmark.Extender = &shortStrikeExtender{length: 1}
|
|
var LongStrike goldmark.Extender = &shortStrikeExtender{length: 2}
|
|
|
|
type shortStrikeExtender struct {
|
|
length int
|
|
}
|
|
|
|
func (s *shortStrikeExtender) Extend(m goldmark.Markdown) {
|
|
m.Parser().AddOptions(parser.WithInlineParsers(
|
|
util.Prioritized(&strikethroughParser{length: s.length}, 500),
|
|
))
|
|
m.Renderer().AddOptions(renderer.WithNodeRenderers(
|
|
util.Prioritized(extension.NewStrikethroughHTMLRenderer(), 500),
|
|
))
|
|
}
|
|
|
|
type strikethroughDelimiterProcessor struct{}
|
|
|
|
func (p *strikethroughDelimiterProcessor) IsDelimiter(b byte) bool {
|
|
return b == '~'
|
|
}
|
|
|
|
func (p *strikethroughDelimiterProcessor) CanOpenCloser(opener, closer *parser.Delimiter) bool {
|
|
return opener.Char == closer.Char
|
|
}
|
|
|
|
func (p *strikethroughDelimiterProcessor) OnMatch(consumes int) gast.Node {
|
|
return ast.NewStrikethrough()
|
|
}
|
|
|
|
var defaultStrikethroughDelimiterProcessor = &strikethroughDelimiterProcessor{}
|
|
|
|
type strikethroughParser struct {
|
|
length int
|
|
}
|
|
|
|
func (s *strikethroughParser) Trigger() []byte {
|
|
return []byte{'~'}
|
|
}
|
|
|
|
func (s *strikethroughParser) Parse(parent gast.Node, block text.Reader, pc parser.Context) gast.Node {
|
|
before := block.PrecendingCharacter()
|
|
line, segment := block.PeekLine()
|
|
node := parser.ScanDelimiter(line, before, 1, defaultStrikethroughDelimiterProcessor)
|
|
if node == nil || node.OriginalLength != s.length || before == '~' {
|
|
return nil
|
|
}
|
|
|
|
node.Segment = segment.WithStop(segment.Start + node.OriginalLength)
|
|
block.Advance(node.OriginalLength)
|
|
pc.PushDelimiter(node)
|
|
return node
|
|
}
|
|
|
|
func (s *strikethroughParser) CloseBlock(parent gast.Node, pc parser.Context) {
|
|
// nothing to do
|
|
}
|