Skip to content

Commit

Permalink
Support definition links with spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
mjbvz committed Nov 1, 2021
1 parent f145c79 commit a9bc055
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,21 @@ function extractDocumentLink(
}
}

/* Used to strip brackets from the markdown link
<http://example.com> will be transformed to
http://example.com
const angleBracketLinkRe = /^<(.*)>$/;

/**
* Used to strip brackets from the markdown link
*
* <http://example.com> will be transformed to http://example.com
*/
export function stripAngleBrackets(link: string) {
const bracketMatcher = /^<(.*)>$/;
return link.replace(bracketMatcher, '$1');
return link.replace(angleBracketLinkRe, '$1');
}

export default class LinkProvider implements vscode.DocumentLinkProvider {
private readonly linkPattern = /(\[((!\[[^\]]*?\]\(\s*)([^\s\(\)]+?)\s*\)\]|(?:\\\]|[^\]])*\])\(\s*)(([^\s\(\)]|\([^\s\(\)]*?\))+)\s*(".*?")?\)/g;
private readonly referenceLinkPattern = /(\[((?:\\\]|[^\]])+)\]\[\s*?)([^\s\]]*?)\]/g;
private readonly definitionPattern = /^([\t ]*\[(?!\^)((?:\\\]|[^\]])+)\]:\s*)(\S+)/gm;
private readonly definitionPattern = /^([\t ]*\[(?!\^)((?:\\\]|[^\]])+)\]:\s*)([^<]\S*|<[^>]+>)/gm;

public provideDocumentLinks(
document: vscode.TextDocument,
Expand Down Expand Up @@ -192,15 +194,23 @@ export default class LinkProvider implements vscode.DocumentLinkProvider {
const pre = match[1];
const reference = match[2];
const link = match[3].trim();

const offset = (match.index || 0) + pre.length;
const linkStart = document.positionAt(offset);
const linkEnd = document.positionAt(offset + link.length);

out.set(reference, {
link: link,
linkRange: new vscode.Range(linkStart, linkEnd)
});
if (angleBracketLinkRe.test(link)) {
const linkStart = document.positionAt(offset + 1);
const linkEnd = document.positionAt(offset + link.length - 1);
out.set(reference, {
link: link.substring(1, link.length - 1),
linkRange: new vscode.Range(linkStart, linkEnd)
});
} else {
const linkStart = document.positionAt(offset);
const linkEnd = document.positionAt(offset + link.length);
out.set(reference, {
link: link,
linkRange: new vscode.Range(linkStart, linkEnd)
});
}
}
return out;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,22 @@ suite('markdown.DocumentLinkProvider', () => {
}
});

// #107471
test('Should not consider link references starting with ^ character valid', () => {
test('Should not consider link references starting with ^ character valid (#107471)', () => {
const links = getLinksForFile('[^reference]: https://example.com');
assert.strictEqual(links.length, 0);
});

test('Should find definitions links with spaces in angle brackets (#136073)', () => {
const links = getLinksForFile([
'[a]: <b c>',
'[b]: <cd>',
].join('\n'));
assert.strictEqual(links.length, 2);

const [link1, link2] = links;
assertRangeEqual(link1.range, new vscode.Range(0, 6, 0, 9));
assertRangeEqual(link2.range, new vscode.Range(1, 6, 1, 8));
});
});


Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export class InMemoryDocument implements vscode.TextDocument {
this._lines = this._contents.split(/\r\n|\n/g);
}


isUntitled: boolean = false;
languageId: string = '';
isDirty: boolean = false;
Expand Down Expand Up @@ -49,7 +48,7 @@ export class InMemoryDocument implements vscode.TextDocument {
const before = this._contents.slice(0, offset);
const newLines = before.match(/\r\n|\n/g);
const line = newLines ? newLines.length : 0;
const preCharacters = before.match(/(\r\n|\n|^).*$/g);
const preCharacters = before.match(/(?<=\r\n|\n|^).*$/g);
return new vscode.Position(line, preCharacters ? preCharacters[0].length : 0);
}
getText(_range?: vscode.Range | undefined): string {
Expand Down

0 comments on commit a9bc055

Please sign in to comment.