Add support for references when no [] exists (#144781)

* add single reference linking regex

* add less invasive regex

* add lookahead to exclude [tag]: <url> 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 <grate.resales.0r@icloud.com>
Co-authored-by: Jefferson Chen <jc@Jeffersons-MBP.attlocal.net>
This commit is contained in:
BamBamboozled
2022-03-17 12:55:31 -07:00
committed by GitHub
parent 02f15063a0
commit b6f6a37cbd
2 changed files with 30 additions and 3 deletions

View File

@@ -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);

View File

@@ -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(
'```',