From 6071748dc8b66e0b6bf3508c51fee2730faceb66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mickae=CC=88l=20Menu?= Date: Sun, 14 Mar 2021 22:23:15 +0100 Subject: [PATCH] Fix crash when parsing a link snippet in an inline node --- CHANGELOG.md | 8 +++++++- adapter/markdown/markdown.go | 23 +++++++++++++++-------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 35828a8..0532a0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,13 @@ All notable changes to this project will be documented in this file. - +## Unreleased + +### Fixed + +* Looking for mentions of a note with a title containing double quotes. +* Crash when parsing certain link snippets. + ## 0.2.0 diff --git a/adapter/markdown/markdown.go b/adapter/markdown/markdown.go index 06fcdb7..b3270da 100644 --- a/adapter/markdown/markdown.go +++ b/adapter/markdown/markdown.go @@ -216,7 +216,7 @@ func parseLinks(root ast.Node, source []byte) ([]note.Link, error) { case *ast.Link: href := string(link.Destination) if href != "" { - snippet, snStart, snEnd := extractLines(n.Parent(), source) + snippet, snStart, snEnd := extractLines(n, source) links = append(links, note.Link{ Title: string(link.Text(source)), Href: href, @@ -230,7 +230,7 @@ func parseLinks(root ast.Node, source []byte) ([]note.Link, error) { case *ast.AutoLink: if href := string(link.URL(source)); href != "" && link.AutoLinkType == ast.AutoLinkURL { - snippet, snStart, snEnd := extractLines(n.Parent(), source) + snippet, snStart, snEnd := extractLines(n, source) links = append(links, note.Link{ Title: string(link.Label(source)), Href: href, @@ -252,13 +252,20 @@ func extractLines(n ast.Node, source []byte) (content string, start, end int) { if n == nil { return } - segs := n.Lines() - if segs.Len() == 0 { - return + switch n.Type() { + case ast.TypeInline: + return extractLines(n.Parent(), source) + + case ast.TypeBlock: + segs := n.Lines() + if segs.Len() == 0 { + return + } + start = segs.At(0).Start + end = segs.At(segs.Len() - 1).Stop + content = string(source[start:end]) } - start = segs.At(0).Start - end = segs.At(segs.Len() - 1).Stop - content = string(source[start:end]) + return }