Skip to content

How to extend inline markdown parsing? #291

@kensanata

Description

@kensanata

I'd be interested in parsing [[links]] and #hashtags but don't know where to start.

My naïve approach would be something like the following for hashtags (untested, since it doesn't compile). I'm pretty new to Go so I'm guessing the reason this fails is because I have no access to Parser.inlineCallback. I think the best solution would be to add something to Options that would allow passing a additional values for inlineCallback?

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],
	}
	return i + 1, link
}

// renderHtml renders the Page.Body to HTML and sets Page.Html.
func (p *Page) renderHtml() {
	parser := parser.New()
	parser.inlineCallback['#'] = hashtag
	maybeUnsafeHTML := markdown.ToHTML(p.Body, parser, nil)
	p.Html = sanitizeBytes(maybeUnsafeHTML)
	p.Language = language(p.plainText())
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions