mirror of
https://github.com/microsoft/vscode.git
synced 2025-12-21 18:59:15 +00:00
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:
@@ -104,7 +104,7 @@ export function stripAngleBrackets(link: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const linkPattern = /(\[((!\[[^\]]*?\]\(\s*)([^\s\(\)]+?)\s*\)\]|(?:\\\]|[^\]])*\])\(\s*)(([^\s\(\)]|\([^\s\(\)]*?\))+)\s*(".*?")?\)/g;
|
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 definitionPattern = /^([\t ]*\[(?!\^)((?:\\\]|[^\]])+)\]:\s*)([^<]\S*|<[^>]+>)/gm;
|
||||||
const inlineCodePattern = /(?:^|[^`])(`+)(?:.+?|.*?(?:(?:\r?\n).+?)*?)(?:\r?\n)?\1(?:$|[^`])/gm;
|
const inlineCodePattern = /(?:^|[^`])(`+)(?:.+?|.*?(?:(?:\r?\n).+?)*?)(?:\r?\n)?\1(?:$|[^`])/gm;
|
||||||
|
|
||||||
@@ -186,10 +186,10 @@ export default class LinkProvider implements vscode.DocumentLinkProvider {
|
|||||||
let reference = match[3];
|
let reference = match[3];
|
||||||
if (reference) { // [text][ref]
|
if (reference) { // [text][ref]
|
||||||
const pre = match[1];
|
const pre = match[1];
|
||||||
const offset = (match.index || 0) + pre.length;
|
const offset = (match.index || 0) + pre.length + 1;
|
||||||
linkStart = document.positionAt(offset);
|
linkStart = document.positionAt(offset);
|
||||||
linkEnd = document.positionAt(offset + reference.length);
|
linkEnd = document.positionAt(offset + reference.length);
|
||||||
} else if (match[2]) { // [ref][]
|
} else if (match[2]) { // [ref][], [ref]
|
||||||
reference = match[2];
|
reference = match[2];
|
||||||
const offset = (match.index || 0) + 1;
|
const offset = (match.index || 0) + 1;
|
||||||
linkStart = document.positionAt(offset);
|
linkStart = document.positionAt(offset);
|
||||||
|
|||||||
@@ -151,6 +151,33 @@ suite('markdown.DocumentLinkProvider', () => {
|
|||||||
assertRangeEqual(link2.range, new vscode.Range(1, 6, 1, 8));
|
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 () => {
|
test('Should not consider links in code fenced with backticks', async () => {
|
||||||
const text = joinLines(
|
const text = joinLines(
|
||||||
'```',
|
'```',
|
||||||
|
|||||||
Reference in New Issue
Block a user