From b6f6a37cbdc2025cf43103ba2837ae624af10c2c Mon Sep 17 00:00:00 2001 From: BamBamboozled <100744411+BamBamboozled@users.noreply.github.com> Date: Thu, 17 Mar 2022 12:55:31 -0700 Subject: [PATCH] Add support for references when no [] exists (#144781) * add single reference linking regex * add less invasive regex * add lookahead to exclude [tag]: case * add tests for references when no [] exists * revert integration test script * make test description clearer * remove vim swap file (whoops) Co-authored-by: Jayce Co-authored-by: Jefferson Chen --- .../src/features/documentLinkProvider.ts | 6 ++--- .../src/test/documentLinkProvider.test.ts | 27 +++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/extensions/markdown-language-features/src/features/documentLinkProvider.ts b/extensions/markdown-language-features/src/features/documentLinkProvider.ts index ee72de7ccb0..9c028de7d90 100644 --- a/extensions/markdown-language-features/src/features/documentLinkProvider.ts +++ b/extensions/markdown-language-features/src/features/documentLinkProvider.ts @@ -104,7 +104,7 @@ export function stripAngleBrackets(link: string) { } const linkPattern = /(\[((!\[[^\]]*?\]\(\s*)([^\s\(\)]+?)\s*\)\]|(?:\\\]|[^\]])*\])\(\s*)(([^\s\(\)]|\([^\s\(\)]*?\))+)\s*(".*?")?\)/g; -const referenceLinkPattern = /(\[((?:\\\]|[^\]])+)\]\[\s*?)([^\s\]]*?)\]/g; +const referenceLinkPattern = /((?<=^|[^\]])\[((?:\\\]|[^\]])+)\])(?!:)(?:[^\[]|$|\[\s*?([^\s\]]*?)\])/g; const definitionPattern = /^([\t ]*\[(?!\^)((?:\\\]|[^\]])+)\]:\s*)([^<]\S*|<[^>]+>)/gm; const inlineCodePattern = /(?:^|[^`])(`+)(?:.+?|.*?(?:(?:\r?\n).+?)*?)(?:\r?\n)?\1(?:$|[^`])/gm; @@ -186,10 +186,10 @@ export default class LinkProvider implements vscode.DocumentLinkProvider { let reference = match[3]; if (reference) { // [text][ref] const pre = match[1]; - const offset = (match.index || 0) + pre.length; + const offset = (match.index || 0) + pre.length + 1; linkStart = document.positionAt(offset); linkEnd = document.positionAt(offset + reference.length); - } else if (match[2]) { // [ref][] + } else if (match[2]) { // [ref][], [ref] reference = match[2]; const offset = (match.index || 0) + 1; linkStart = document.positionAt(offset); diff --git a/extensions/markdown-language-features/src/test/documentLinkProvider.test.ts b/extensions/markdown-language-features/src/test/documentLinkProvider.test.ts index 2394240a1cd..2b358067946 100644 --- a/extensions/markdown-language-features/src/test/documentLinkProvider.test.ts +++ b/extensions/markdown-language-features/src/test/documentLinkProvider.test.ts @@ -151,6 +151,33 @@ suite('markdown.DocumentLinkProvider', () => { assertRangeEqual(link2.range, new vscode.Range(1, 6, 1, 8)); }); + test('Should only find one link for reference sources [a]: source (#141285)', async () => { + const links = await getLinksForFile([ + '[Works]: https://microsoft.com', + ].join('\n')); + + assert.strictEqual(links.length, 1); + }); + + test('Should find links for referenes with only one [] (#141285)', async () => { + let links = await getLinksForFile([ + '[Works]', + '[Works]: https://microsoft.com', + ].join('\n')); + assert.strictEqual(links.length, 2); + + links = await getLinksForFile([ + '[Does Not Work]', + '[Works]: https://microsoft.com', + ].join('\n')); + assert.strictEqual(links.length, 1); + }); + + test('Should not find link for reference using one [] when source does not exist (#141285)', async () => { + const links = await getLinksForFile('[Works]'); + assert.strictEqual(links.length, 0); + }); + test('Should not consider links in code fenced with backticks', async () => { const text = joinLines( '```',