package extension
import (
"github.com/yuin/goldmark"
gast "github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/extension/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/renderer/html"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)
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 {
}
var defaultStrikethroughParser = &strikethroughParser {}
func NewStrikethroughParser () parser .InlineParser {
return defaultStrikethroughParser
}
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 > 2 || 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 ) {
}
type StrikethroughHTMLRenderer struct {
html .Config
}
func NewStrikethroughHTMLRenderer (opts ...html .Option ) renderer .NodeRenderer {
r := &StrikethroughHTMLRenderer {
Config : html .NewConfig (),
}
for _ , opt := range opts {
opt .SetHTMLOption (&r .Config )
}
return r
}
func (r *StrikethroughHTMLRenderer ) RegisterFuncs (reg renderer .NodeRendererFuncRegisterer ) {
reg .Register (ast .KindStrikethrough , r .renderStrikethrough )
}
var StrikethroughAttributeFilter = html .GlobalAttributeFilter
func (r *StrikethroughHTMLRenderer ) renderStrikethrough (
w util .BufWriter , source []byte , n gast .Node , entering bool ) (gast .WalkStatus , error ) {
if entering {
if n .Attributes () != nil {
_, _ = w .WriteString ("<del" )
html .RenderAttributes (w , n , StrikethroughAttributeFilter )
_ = w .WriteByte ('>' )
} else {
_, _ = w .WriteString ("<del>" )
}
} else {
_, _ = w .WriteString ("</del>" )
}
return gast .WalkContinue , nil
}
type strikethrough struct {
}
var Strikethrough = &strikethrough {}
func (e *strikethrough ) Extend (m goldmark .Markdown ) {
m .Parser ().AddOptions (parser .WithInlineParsers (
util .Prioritized (NewStrikethroughParser (), 500 ),
))
m .Renderer ().AddOptions (renderer .WithNodeRenderers (
util .Prioritized (NewStrikethroughHTMLRenderer (), 500 ),
))
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .