Skip to content

Conversation

@kensanata
Copy link
Contributor

I have added hashtags to my Markdown. When rendered, these produce a search link. My problem was that sometimes the link text contained something that looked like a hashtag. Specifically: [oh #ok](ok) should produce <a href="ok">oh #ok</a></p> and not <a href="ok">oh <a class="tag" href="/search/?q=%23ok">#ok</a></a></p>.

I can fix this if p.InsideLink is accessible to my code:

// hashtag returns an inline parser function. This indirection is
// required because we want to receive an array of hashtags found.
// The hashtags in the array keep their case.
func hashtag() (func(p *parser.Parser, data []byte, offset int) (int, ast.Node), *[]string) {
	hashtags := make([]string, 0)
	return func(p *parser.Parser, data []byte, offset int) (int, ast.Node) {
		if p.InsideLink { // HERE
			return 0, nil
		}
		data = data[offset:]
		i := 0
		n := len(data)
		for i < n && !parser.IsSpace(data[i]) {
			i++
		}
		if i <= 1 {
			return 0, nil
		}
		hashtags = append(hashtags, string(data[1:i]))
		link := &ast.Link{
			AdditionalAttributes: []string{`class="tag"`},
			Destination:          append([]byte("/search/?q=%23"), data[1:i]...),
		}
		text := bytes.ReplaceAll(data[0:i], []byte("_"), []byte(" "))
		ast.AppendChild(link, &ast.Text{Leaf: ast.Leaf{Literal: text}})
		return i, link
	}, &hashtags
}

@kjk kjk merged commit 7a1f277 into gomarkdown:master Feb 7, 2025
1 check passed
@kjk
Copy link
Contributor

kjk commented Feb 7, 2025

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants