From aa6ff5896bdcefddeb57eebbef0d8ae8b9470456 Mon Sep 17 00:00:00 2001 From: jaanonim Date: Fri, 25 Aug 2023 11:32:39 +0200 Subject: [PATCH] support for many links in the same line --- src/EditorSuggester.ts | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/EditorSuggester.ts b/src/EditorSuggester.ts index 32843c9..cd77dc3 100644 --- a/src/EditorSuggester.ts +++ b/src/EditorSuggester.ts @@ -29,19 +29,23 @@ export class EditorSuggester extends EditorSuggest { .getLine(cursor.line) .substring(0, cursor.ch); - const match = currentContent.match(linkRegex)?.first() ?? ""; - if (match) { - console.log(match); - return { - end: cursor, - start: { - line: cursor.line, - ch: currentContent.lastIndexOf(match), - }, - query: match, - }; - } - return null; + const matches = currentContent.match(linkRegex); + if (!matches) return null; + return matches?.reduce((prev, match) => { + if (match && prev === null) { + const end = currentContent.lastIndexOf(match); + if (end === 0 || currentContent.charAt(end - 1) !== "[") + return { + end: cursor, + start: { + line: cursor.line, + ch: end, + }, + query: match, + }; + } + return null; + }, null); } getSuggestions( context: EditorSuggestContext