1 | export function extractLeadingText(string, completeTag) {
|
2 | let tagIndex = string.indexOf(completeTag);
|
3 | let leadingText = undefined;
|
4 | let leadingTextRegExp = /\[(.+?)\]/g;
|
5 | let leadingTextInfo = leadingTextRegExp.exec(string);
|
6 |
|
7 |
|
8 | while (leadingTextInfo && leadingTextInfo.length) {
|
9 | if (leadingTextInfo.index + leadingTextInfo[0].length === tagIndex) {
|
10 | string = string.replace(leadingTextInfo[0], '');
|
11 | leadingText = leadingTextInfo[1];
|
12 | break;
|
13 | }
|
14 |
|
15 | leadingTextInfo = leadingTextRegExp.exec(string);
|
16 | }
|
17 |
|
18 | return {
|
19 | leadingText: leadingText,
|
20 | string: string
|
21 | };
|
22 | }
|
23 |
|
24 | export function splitLinkText(text) {
|
25 | let linkText;
|
26 | let target;
|
27 | let splitIndex;
|
28 |
|
29 |
|
30 | splitIndex = text.indexOf('|');
|
31 | if (splitIndex === -1) {
|
32 | splitIndex = text.search(/\s/);
|
33 | }
|
34 |
|
35 | if (splitIndex !== -1) {
|
36 | linkText = text.substr(splitIndex + 1);
|
37 |
|
38 | linkText = linkText.replace(/\n+/, ' ');
|
39 | target = text.substr(0, splitIndex);
|
40 | }
|
41 |
|
42 | return {
|
43 | linkText: linkText,
|
44 | target: target || text
|
45 | };
|
46 | }
|
47 |
|
48 | export let LinkParser = (function() {
|
49 | let processTheLink = function(string, tagInfo, leadingText) {
|
50 | let leading = extractLeadingText(string, tagInfo.completeTag),
|
51 | linkText,
|
52 | split,
|
53 | target,
|
54 | stringtoReplace;
|
55 |
|
56 | linkText = leadingText ? leadingText : leading.leadingText || '';
|
57 |
|
58 | split = splitLinkText(tagInfo.text);
|
59 | target = split.target;
|
60 |
|
61 | if (leading.leadingText !== undefined) {
|
62 | stringtoReplace = '[' + leading.leadingText + ']' + tagInfo.completeTag;
|
63 | } else if (typeof split.linkText !== 'undefined') {
|
64 | stringtoReplace = tagInfo.completeTag;
|
65 | linkText = split.linkText;
|
66 | }
|
67 | if (linkText === '' || linkText == null || target == null) {
|
68 | return string;
|
69 | }
|
70 |
|
71 | return string.replace(stringtoReplace, '[' + linkText + '](' + target + ')');
|
72 | };
|
73 |
|
74 | |
75 |
|
76 |
|
77 |
|
78 |
|
79 | let replaceLinkTag = function(str: string) {
|
80 | if (typeof str === 'undefined') {
|
81 | return {
|
82 | newString: ''
|
83 | };
|
84 | }
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | let tagRegExpLight = new RegExp('\\{@link\\s+((?:.|\n)+?)\\}', 'i'),
|
90 | tagRegExpFull = new RegExp('\\{@link\\s+((?:.|\n)+?)\\}', 'i'),
|
91 | tagRegExp,
|
92 | matches,
|
93 | previousString,
|
94 | tagInfo = [];
|
95 |
|
96 | tagRegExp = str.indexOf(']{') !== -1 ? tagRegExpFull : tagRegExpLight;
|
97 |
|
98 | function replaceMatch(replacer, tag, match, text, linkText?) {
|
99 | let matchedTag = {
|
100 | completeTag: match,
|
101 | tag: tag,
|
102 | text: text
|
103 | };
|
104 | tagInfo.push(matchedTag);
|
105 | if (linkText) {
|
106 | return replacer(str, matchedTag, linkText);
|
107 | } else {
|
108 | return replacer(str, matchedTag);
|
109 | }
|
110 | }
|
111 |
|
112 | do {
|
113 | matches = tagRegExp.exec(str);
|
114 | if (matches) {
|
115 | previousString = str;
|
116 | if (matches.length === 2) {
|
117 | str = replaceMatch(processTheLink, 'link', matches[0], matches[1]);
|
118 | }
|
119 | if (matches.length === 3) {
|
120 | str = replaceMatch(processTheLink, 'link', matches[0], matches[2], matches[1]);
|
121 | }
|
122 | }
|
123 | } while (matches && previousString !== str);
|
124 |
|
125 | return {
|
126 | newString: str
|
127 | };
|
128 | };
|
129 |
|
130 | let _resolveLinks = function(str: string) {
|
131 | return replaceLinkTag(str).newString;
|
132 | };
|
133 |
|
134 | return {
|
135 | resolveLinks: _resolveLinks
|
136 | };
|
137 | })();
|