Skip to content

Commit

Permalink
Fixing false positive ref link being detected
Browse files Browse the repository at this point in the history
  • Loading branch information
mjbvz committed Apr 1, 2022
1 parent c39d09a commit 8b7086a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
Expand Up @@ -157,7 +157,7 @@ const linkPattern = /(\[((!\[[^\]]*?\]\(\s*)([^\s\(\)]+?)\s*\)\]|(?:\\\]|[^\]])*
/**
* Matches `[text][ref]`
*/
const referenceLinkPattern = /(?:(\[((?:\\\]|[^\]])+)\]\[\s*?)([^\s\]]*?)\]|\[\s*?([^\s\]]*?)\])(?!\:)/g;
const referenceLinkPattern = /(?:(\[((?:\\\]|[^\]])+)\]\[\s*?)([^\s\]]*?)\]|\[\s*?([^\s\]]*?)\])(?![\:\(])/g;

/**
* Matches `[text]: link`
Expand Down
Expand Up @@ -106,12 +106,11 @@ export class MdReferencesProvider extends Disposable implements vscode.Reference

const references: MdReference[] = [];

const line = document.lineAt(header.line);
references.push({
kind: 'header',
isTriggerLocation: true,
isDefinition: true,
location: new vscode.Location(document.uri, new vscode.Range(header.line, 0, header.line, line.text.length)),
location: header.headerLocation,
headerTextLocation: header.headerTextLocation
});

Expand Down
10 changes: 10 additions & 0 deletions extensions/markdown-language-features/src/test/references.test.ts
Expand Up @@ -71,6 +71,16 @@ suite('markdown: find all references', () => {
);
});

test('Should not return references when on link text', async () => {
const doc = new InMemoryDocument(workspacePath('doc.md'), joinLines(
`[ref](#abc)`,
`[ref]: http://example.com`,
));

const refs = await getReferences(doc, new vscode.Position(0, 1), new InMemoryWorkspaceMarkdownDocuments([doc]));
assert.deepStrictEqual(refs, []);
});

test('Should find references using normalized slug', async () => {
const doc = new InMemoryDocument(workspacePath('doc.md'), joinLines(
`# a B c`,
Expand Down
11 changes: 11 additions & 0 deletions extensions/markdown-language-features/src/test/rename.test.ts
Expand Up @@ -245,4 +245,15 @@ suite('markdown: rename', () => {
]
});
});


test('Rename should not be supported on link text', async () => {
const uri = workspacePath('doc.md');
const doc = new InMemoryDocument(uri, joinLines(
`# Header`,
`[text](#header)`,
));

await assert.rejects(getRenameRange(doc, new vscode.Position(1, 2), new InMemoryWorkspaceMarkdownDocuments([doc])));
});
});

0 comments on commit 8b7086a

Please sign in to comment.