From aac560143bd74cd0bfafb372b140ab47d7ecf9ad Mon Sep 17 00:00:00 2001 From: tjex Date: Thu, 21 Mar 2024 13:44:44 +1100 Subject: [PATCH] fix: lsp, validate file URI links with scheme+regex --- internal/adapter/lsp/document.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/internal/adapter/lsp/document.go b/internal/adapter/lsp/document.go index 9285470..4eff87b 100644 --- a/internal/adapter/lsp/document.go +++ b/internal/adapter/lsp/document.go @@ -162,6 +162,7 @@ func (d *document) LookForward(pos protocol.Position, length int) string { var wikiLinkRegex = regexp.MustCompile(`\[?\[\[(.+?)(?: *\| *(.+?))?\]\]`) var markdownLinkRegex = regexp.MustCompile(`\[([^\]]+?[^\\])\]\((.+?[^\\])\)`) +var fileURIregex = regexp.MustCompile(`file:///`) // LinkFromRoot returns a Link to this document from the root of the given // notebook. @@ -229,14 +230,24 @@ func (d *document) DocumentLinks() ([]documentLink, error) { } // extract link paths from [title](path) patterns + // note: match[0:1] is the entire match, match[2:3] is the contents of + // brackets, match[4:5] is contents of parentheses for _, match := range markdownLinkRegex.FindAllStringSubmatchIndex(line, -1) { - // Ignore embedded images ![title](file.png) and tripple dash file - // URIs [title](file:///file.go) - if (match[0] > 0 && line[match[0]-1] == '!') || - (match[0] > 0 && line[match[3]+8] == '/') { + + // Ignore embedded images ![title](file.png) + if match[0] > 0 && line[match[0]-1] == '!' { continue } + // ignore tripple dash file URIs [title](file:///foo.go) + if match[5]-match[4] >= 8 { + linkURL := line[match[4]:match[5]] + fileURIresult := linkURL[:8] + if fileURIregex.MatchString(fileURIresult) { + continue + } + } + href := line[match[4]:match[5]] // Decode the href if it's percent-encoded