Skip to content

Commit 6415faa

Browse files
committed
introduce LinkedText
1 parent ff4b55d commit 6415faa

2 files changed

Lines changed: 107 additions & 0 deletions

File tree

‎src/vs/base/browser/linkedText.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
export interface ILink {
7+
readonly label: string;
8+
readonly href: string;
9+
readonly title?: string;
10+
}
11+
12+
export type LinkedTextNode = string | ILink;
13+
export type LinkedText = LinkedTextNode[];
14+
15+
const LINK_REGEX = /\[([^\]]+)\]\(((?:https?:\/\/|command:)[^\)\s]+)(?: "([^"]+)")?\)/gi;
16+
17+
export function parseLinkedText(text: string): LinkedText {
18+
const result: LinkedTextNode[] = [];
19+
20+
let index = 0;
21+
let match: RegExpExecArray | null;
22+
23+
while (match = LINK_REGEX.exec(text)) {
24+
if (match.index - index > 0) {
25+
result.push(text.substring(index, match.index));
26+
}
27+
28+
const [, label, href, title] = match;
29+
30+
if (title) {
31+
result.push({ label, href, title });
32+
} else {
33+
result.push({ label, href });
34+
}
35+
36+
index = match.index + match[0].length;
37+
}
38+
39+
if (index < text.length) {
40+
result.push(text.substring(index));
41+
}
42+
43+
return result;
44+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import * as assert from 'assert';
7+
import { parseLinkedText } from 'vs/base/browser/linkedText';
8+
9+
suite('LinkedText', () => {
10+
test('parses correctly', () => {
11+
assert.deepEqual(parseLinkedText(''), []);
12+
assert.deepEqual(parseLinkedText('hello'), ['hello']);
13+
assert.deepEqual(parseLinkedText('hello there'), ['hello there']);
14+
assert.deepEqual(parseLinkedText('Some message with [link text](http://link.href).'), [
15+
'Some message with ',
16+
{ label: 'link text', href: 'http://link.href' },
17+
'.'
18+
]);
19+
assert.deepEqual(parseLinkedText('Some message with [link text](http://link.href "and a title").'), [
20+
'Some message with ',
21+
{ label: 'link text', href: 'http://link.href', title: 'and a title' },
22+
'.'
23+
]);
24+
assert.deepEqual(parseLinkedText('Some message with [link text](random stuff).'), [
25+
'Some message with [link text](random stuff).'
26+
]);
27+
assert.deepEqual(parseLinkedText('Some message with [https link](https://link.href).'), [
28+
'Some message with ',
29+
{ label: 'https link', href: 'https://link.href' },
30+
'.'
31+
]);
32+
assert.deepEqual(parseLinkedText('Some message with [https link](https:).'), [
33+
'Some message with [https link](https:).'
34+
]);
35+
assert.deepEqual(parseLinkedText('Some message with [a command](command:foobar).'), [
36+
'Some message with ',
37+
{ label: 'a command', href: 'command:foobar' },
38+
'.'
39+
]);
40+
assert.deepEqual(parseLinkedText('Some message with [a command](command:).'), [
41+
'Some message with [a command](command:).'
42+
]);
43+
assert.deepEqual(parseLinkedText('link [one](command:foo "nice") and link [two](http://foo)...'), [
44+
'link ',
45+
{ label: 'one', href: 'command:foo', title: 'nice' },
46+
' and link ',
47+
{ label: 'two', href: 'http://foo' },
48+
'...'
49+
]);
50+
});
51+
});
52+
53+
/*
54+
[I'm an inline-style link](https://www.google.com)
55+
56+
[I'm an inline-style link with title](https://www.google.com "Google's Homepage")
57+
58+
[I'm a reference-style link][Arbitrary case-insensitive reference text]
59+
60+
[I'm a relative reference to a repository file](../blob/master/LICENSE)
61+
62+
[You can use numbers for reference-style link definitions][1]
63+
*/

0 commit comments

Comments
 (0)