Skip to content

Commit

Permalink
Don't interpret escaped links as backlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
svsool committed Jul 30, 2020
1 parent 9860a98 commit b7df0d5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
35 changes: 35 additions & 0 deletions src/extensions/BacklinksTreeDataProvider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,41 @@ describe('BacklinksTreeDataProvider()', () => {
]);
});

it('should not provide backlinks for link within code span', async () => {
const link = rndName();
const name0 = rndName();

await createFile(`${link}.md`);
await createFile(`a-${name0}.md`, `\`[[${link}]]\``);

const doc = await openTextDocument(`${link}.md`);
await window.showTextDocument(doc);

expect(toPlainObject(await getChildren())).toHaveLength(0);
});

it('should not provide backlinks for link within fenced code block', async () => {
const link = rndName();
const name0 = rndName();

await createFile(`${link}.md`);
await createFile(
`a-${name0}.md`,
`
\`\`\`
Preceding text
[[${link}]]
Following text
\`\`\`
`,
);

const doc = await openTextDocument(`${link}.md`);
await window.showTextDocument(doc);

expect(toPlainObject(await getChildren())).toHaveLength(0);
});

it('should collapse parent items according to configuration', async () => {
const link = rndName();
const name0 = rndName();
Expand Down
1 change: 1 addition & 0 deletions src/extensions/DocumentLinkProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default class DocumentLinkProvider implements vscode.DocumentLinkProvider
public provideDocumentLinks(document: vscode.TextDocument): vscode.DocumentLink[] {
const results: vscode.DocumentLink[] = [];

// TODO: Refactor to use ref utils
document
.getText()
.split(/\r?\n/g)
Expand Down
1 change: 0 additions & 1 deletion src/extensions/newVersionNotifier.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ describe('newVersionNotifier extension', () => {

it('should not fail on activate', () => {
expect(() => {
console.log('test', path.resolve(path.join(__dirname, '..', '..')));
const mockContext = ({
subscriptions: [],
extensionPath: path.resolve(path.join(__dirname, '..', '..')),
Expand Down
9 changes: 9 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ export const findReferences = async (

const refStart = currentDocument.positionAt(offset);
const lineStart = currentDocument.lineAt(refStart);

if (
isInFencedCodeBlock(currentDocument, lineStart.lineNumber) ||
isInCodeSpan(currentDocument, lineStart.lineNumber, offset)
) {
return;
}

const matchText = lineStart.text.slice(
Math.max(refStart.character - 2, 0),
lineStart.text.length,
Expand Down Expand Up @@ -246,6 +254,7 @@ export const findUriByRef = (uris: vscode.Uri[], ref: string): vscode.Uri | unde
return containsMarkdownExt(path.basename(uri.fsPath)) && name === ref.toLowerCase();
});

// TODO: Rename to ensureDirectoryExists
export const ensureDirectoryExistence = (filePath: string) => {
const dirname = path.dirname(filePath);
if (!fs.existsSync(dirname)) {
Expand Down

0 comments on commit b7df0d5

Please sign in to comment.