From 2e1b80d59ac07d4ba84af76d447ae2c99bff45af Mon Sep 17 00:00:00 2001 From: tjex Date: Sat, 10 Feb 2024 19:45:39 +0100 Subject: [PATCH 1/3] accept tripple dash file URIs as valid links. resolves #250 --- internal/adapter/lsp/document.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/internal/adapter/lsp/document.go b/internal/adapter/lsp/document.go index 454681db..1b184a3b 100644 --- a/internal/adapter/lsp/document.go +++ b/internal/adapter/lsp/document.go @@ -228,9 +228,12 @@ func (d *document) DocumentLinks() ([]documentLink, error) { }) } - for _, match := range markdownLinkRegex.FindAllStringSubmatchIndex(line, -1) { - // Ignore embedded image, e.g. ![title](href.png) - if match[0] > 0 && line[match[0]-1] == '!' { + // extract link paths from [title](path) patterns + for _, match := range + markdownLinkRegex.FindAllStringSubmatchIndex(line, -1) { + // case: '!' ignores embedded image, e.g. ![title](href.png) + // case: '/' ignores tripple dash file URIs, e.g. file:///file.go + if match[0] > 0 && line[match[0]-1] == '!' || line[match[3]+8] == '/' { continue } @@ -242,6 +245,7 @@ func (d *document) DocumentLinks() ([]documentLink, error) { appendLink(href, match[0], match[1], false, false) } + for _, match := range wikiLinkRegex.FindAllStringSubmatchIndex(line, -1) { href := line[match[2]:match[3]] hasTitle := match[4] != -1 From 7d26750eaccba833740debde1f0f03635f345d94 Mon Sep 17 00:00:00 2001 From: tjex Date: Sat, 10 Feb 2024 19:55:14 +0100 Subject: [PATCH 2/3] fix if statement logic --- internal/adapter/lsp/document.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/internal/adapter/lsp/document.go b/internal/adapter/lsp/document.go index 1b184a3b..9285470f 100644 --- a/internal/adapter/lsp/document.go +++ b/internal/adapter/lsp/document.go @@ -7,12 +7,12 @@ import ( "strings" "unicode/utf16" + "github.com/tliron/glsp" + protocol "github.com/tliron/glsp/protocol_3_16" "github.com/zk-org/zk/internal/core" "github.com/zk-org/zk/internal/util" "github.com/zk-org/zk/internal/util/errors" strutil "github.com/zk-org/zk/internal/util/strings" - "github.com/tliron/glsp" - protocol "github.com/tliron/glsp/protocol_3_16" ) // documentStore holds opened documents. @@ -228,24 +228,25 @@ func (d *document) DocumentLinks() ([]documentLink, error) { }) } - // extract link paths from [title](path) patterns - for _, match := range - markdownLinkRegex.FindAllStringSubmatchIndex(line, -1) { - // case: '!' ignores embedded image, e.g. ![title](href.png) - // case: '/' ignores tripple dash file URIs, e.g. file:///file.go - if match[0] > 0 && line[match[0]-1] == '!' || line[match[3]+8] == '/' { + // extract link paths from [title](path) patterns + 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] == '/') { continue } href := line[match[4]:match[5]] - // Valid Markdown links are percent-encoded. + + // Decode the href if it's percent-encoded if decodedHref, err := url.PathUnescape(href); err == nil { href = decodedHref } + appendLink(href, match[0], match[1], false, false) } - for _, match := range wikiLinkRegex.FindAllStringSubmatchIndex(line, -1) { href := line[match[2]:match[3]] hasTitle := match[4] != -1 From aac560143bd74cd0bfafb372b140ab47d7ecf9ad Mon Sep 17 00:00:00 2001 From: tjex Date: Thu, 21 Mar 2024 13:44:44 +1100 Subject: [PATCH 3/3] 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 9285470f..4eff87b7 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