Skip to content

Commit

Permalink
add Parser.RegisterInline(byte, func) to register inline parsers (for #…
Browse files Browse the repository at this point in the history
  • Loading branch information
kjk committed Sep 12, 2023
1 parent 531d2d7 commit 14b07df
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
31 changes: 31 additions & 0 deletions md_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,34 @@ func TestMd(t *testing.T) {
}
}
}

func hashtag(p *parser.Parser, data []byte, offset int) (int, ast.Node) {
data = data[offset:]
i := 0
n := len(data)
for i < n && !parser.IsSpace(data[i]) {
i++
}
if i == 0 {
return 0, nil
}
link := &ast.Link{
Destination: append([]byte("/search?q=%23"), data[1:i]...),
Title: data[0:i],
}
ast.AppendChild(link, &ast.Text{Leaf: ast.Leaf{Literal: data[0:i]}})
return i + 1, link
}

func TestInlineParser(t *testing.T) {
md := []byte(`#Haiku`)
p := parser.New()
p.RegisterInline('#', hashtag)
html := ToHTML(md, p, nil)

r := `<p><a href="/search?q=%23Haiku" title="#Haiku">#Haiku</a></p>
`
if r != string(html) {
t.Errorf("`%s`\n!=\n`%s`\n", string(html), r)
}
}
6 changes: 6 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ func NewWithExtensions(extension Extensions) *Parser {
return &p
}

func (p *Parser) RegisterInline(n byte, fn inlineParser) inlineParser {
prev := p.inlineCallback[n]
p.inlineCallback[n] = fn
return prev
}

func (p *Parser) getRef(refid string) (ref *reference, found bool) {
if p.ReferenceOverride != nil {
r, overridden := p.ReferenceOverride(refid)
Expand Down

0 comments on commit 14b07df

Please sign in to comment.