Patch incomplete markdown for links that contain a title/argument

This commit is contained in:
Rob Lourens
2024-01-14 14:55:14 -03:00
parent 12443ca030
commit d8bb2b7956
2 changed files with 31 additions and 1 deletions
+13 -1
View File
@@ -581,7 +581,8 @@ function mergeRawTokenText(tokens: marked.Token[]): string {
}
function completeSingleLinePattern(token: marked.Tokens.ListItem | marked.Tokens.Paragraph): marked.Token | undefined {
for (const subtoken of token.tokens) {
for (let i = 0; i < token.tokens.length; i++) {
const subtoken = token.tokens[i];
if (subtoken.type === 'text') {
const lines = subtoken.raw.split('\n');
const lastLine = lines[lines.length - 1];
@@ -596,6 +597,13 @@ function completeSingleLinePattern(token: marked.Tokens.ListItem | marked.Tokens
} else if (lastLine.match(/(^|\s)_\w/)) {
return completeUnderscore(token);
} else if (lastLine.match(/(^|\s)\[.*\]\(\w*/)) {
const nextTwoSubTokens = token.tokens.slice(i + 1);
if (nextTwoSubTokens[0]?.type === 'link' && nextTwoSubTokens[1]?.type === 'text' && nextTwoSubTokens[1].raw.match(/^ *"[^"]*$/)) {
// A markdown link can look like
// [link text](https://microsoft.com "more text")
// Where "more text" is a title for the link or an argument to a vscode command link
return completeLinkTargetArg(token);
}
return completeLinkTarget(token);
} else if (lastLine.match(/(^|\s)\[\w/)) {
return completeLinkText(token);
@@ -693,6 +701,10 @@ function completeLinkTarget(tokens: marked.Token): marked.Token {
return completeWithString(tokens, ')');
}
function completeLinkTargetArg(tokens: marked.Token): marked.Token {
return completeWithString(tokens, '")');
}
function completeLinkText(tokens: marked.Token): marked.Token {
return completeWithString(tokens, '](about:blank)');
}
@@ -678,6 +678,24 @@ suite('MarkdownRenderer', () => {
assert.deepStrictEqual(newTokens, completeTokens);
});
test('incomplete link target 2', () => {
const incomplete = 'foo [text](http://microsoft.com';
const tokens = marked.lexer(incomplete);
const newTokens = fillInIncompleteTokens(tokens);
const completeTokens = marked.lexer(incomplete + ')');
assert.deepStrictEqual(newTokens, completeTokens);
});
test('incomplete link target with arg', () => {
const incomplete = 'foo [text](http://microsoft.com "more text here ';
const tokens = marked.lexer(incomplete);
const newTokens = fillInIncompleteTokens(tokens);
const completeTokens = marked.lexer(incomplete + '")');
assert.deepStrictEqual(newTokens, completeTokens);
});
test.skip('incomplete link in list', () => {
const incomplete = '- [text';
const tokens = marked.lexer(incomplete);