1
>
/*---------------------------------------------------------------------------------------------
linkedText.ts
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 { memoize } from './decorators.js';
7
>
8
>
export interface ILink {
9
>
readonly label: string;
10
>
readonly href: string;
11
>
readonly title?: string;
12
>
}
13
>
14
>
export type LinkedTextNode = string | ILink;
15
>
16
>
export class LinkedText {
17
>
18
>
constructor(readonly nodes: LinkedTextNode[]) { }
19
>
20
>
@memoize
21
>
toString(): string {
22
return this.nodes.map(node => typeof node === 'string' ? node : node.label).join('');
23
}
25
>
26
>
const LINK_REGEX = /\[([^\]]+)\]\(((?:https?:\/\/|command:|file:)[^\)\s]+)(?: (["'])(.+?)(\3))?\)/gi;
27
>
28
>
export function parseLinkedText(text: string): LinkedText {
29
>
const result: LinkedTextNode[] = [];
30
>
31
>
let index = 0;
32
>
let match: RegExpExecArray | null;
33
>
34
>
while (match = LINK_REGEX.exec(text)) {
35
if (match.index - index > 0) {
36
result.push(text.substring(index, match.index));