fix: parseLinks regex misses links at position 0 of string

The previous pattern [^\!]\[...\] required exactly one non-! character
before the opening bracket, so a markdown link at the very start of
anchors to start-of-string or a non-! character without consuming
a required preceding character.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Manuel Rüger 2026-03-13 01:21:19 +01:00
parent 594c1e4fa0
commit 2a505a24a8

View File

@ -185,8 +185,13 @@ func SubstituteLinks(markdown []byte, links []LinkSubstitution) []byte {
}
func parseLinks(markdown string) []markdownLink {
// Matches links but not inline images
re := regexp.MustCompile(`[^\!]\[.+\]\((([^\)#]+)?#?([^\)]+)?)\)`)
// Matches markdown links but not inline images (![ ... ]).
// Group 1: full link target (path + optional hash)
// Group 2: file path portion
// Group 3: hash portion
// The leading (?:^|[^!]) anchor prevents matching image syntax without
// consuming a character that belongs to a preceding link or word.
re := regexp.MustCompile(`(?:^|[^!])\[.+\]\((([^\)#]+)?#?([^\)]+)?)\)`)
matches := re.FindAllStringSubmatch(markdown, -1)
links := make([]markdownLink, len(matches))