Goldmark extension for HTML mark tags
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
2.9 KiB

package mark
import (
"git.jlel.se/jlelse/goldmark-mark/ast"
"github.com/yuin/goldmark"
gast "github.com/yuin/goldmark/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 markDelimiterProcessor struct {
}
func (p *markDelimiterProcessor) IsDelimiter(b byte) bool {
return b == '='
}
func (p *markDelimiterProcessor) CanOpenCloser(opener, closer *parser.Delimiter) bool {
return opener.Char == closer.Char
}
func (p *markDelimiterProcessor) OnMatch(consumes int) gast.Node {
return ast.NewMark()
}
var defaultMarkDelimiterProcessor = &markDelimiterProcessor{}
type markParser struct {
}
var defaultMarkParser = &markParser{}
// NewMarkParser return a new InlineParser that parses
// mark expressions.
func NewMarkParser() parser.InlineParser {
return defaultMarkParser
}
func (s *markParser) Trigger() []byte {
return []byte{'='}
}
func (s *markParser) Parse(parent gast.Node, block text.Reader, pc parser.Context) gast.Node {
before := block.PrecendingCharacter()
line, segment := block.PeekLine()
node := parser.ScanDelimiter(line, before, 2, defaultMarkDelimiterProcessor)
if node == nil {
return nil
}
node.Segment = segment.WithStop(segment.Start + node.OriginalLength)
block.Advance(node.OriginalLength)
pc.PushDelimiter(node)
return node
}
func (s *markParser) CloseBlock(parent gast.Node, pc parser.Context) {
// nothing to do
}
// MarkHTMLRenderer is a renderer.NodeRenderer implementation that
// renders Mark nodes.
type MarkHTMLRenderer struct {
html.Config
}
// NewMarkHTMLRenderer returns a new MarkHTMLRenderer.
func NewMarkHTMLRenderer(opts ...html.Option) renderer.NodeRenderer {
r := &MarkHTMLRenderer{
Config: html.NewConfig(),
}
for _, opt := range opts {
opt.SetHTMLOption(&r.Config)
}
return r
}
// RegisterFuncs implements renderer.NodeRenderer.RegisterFuncs.
func (r *MarkHTMLRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
reg.Register(ast.KindMark, r.renderMark)
}
// MarkAttributeFilter defines attribute names which dd elements can have.
var MarkAttributeFilter = html.GlobalAttributeFilter
func (r *MarkHTMLRenderer) renderMark(w util.BufWriter, source []byte, n gast.Node, entering bool) (gast.WalkStatus, error) {
if entering {
if n.Attributes() != nil {
_, _ = w.WriteString("<mark")
html.RenderAttributes(w, n, MarkAttributeFilter)
_ = w.WriteByte('>')
} else {
_, _ = w.WriteString("<mark>")
}
} else {
_, _ = w.WriteString("</mark>")
}
return gast.WalkContinue, nil
}
type mark struct {
}
// Mark is an extension that allow you to use mark expression like '~~text~~' .
var Mark = &mark{}
func (e *mark) Extend(m goldmark.Markdown) {
m.Parser().AddOptions(parser.WithInlineParsers(
util.Prioritized(NewMarkParser(), 500),
))
m.Renderer().AddOptions(renderer.WithNodeRenderers(
util.Prioritized(NewMarkHTMLRenderer(), 500),
))
}