Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accept triple slash file URIs (file:///...) as valid links #391

Merged
merged 5 commits into from
Mar 29, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions internal/adapter/lsp/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
tjex marked this conversation as resolved.
Show resolved Hide resolved
)

// documentStore holds opened documents.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -228,17 +229,32 @@ 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 image, e.g. ![title](href.png)

// 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]]
// 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)
}

Expand Down