1 | import { IHtmlEngineHelper } from './html-engine-helper.interface';
|
2 | import { extractLeadingText, splitLinkText } from '../../../utils/link-parser';
|
3 | import DependenciesEngine from '../dependencies.engine';
|
4 |
|
5 | export class ParseDescriptionHelper implements IHtmlEngineHelper {
|
6 | constructor() {}
|
7 |
|
8 | public helperFunc(context: any, description: string, depth: number) {
|
9 | let tagRegExpLight = new RegExp('\\{@link\\s+((?:.|\n)+?)\\}', 'i');
|
10 | let tagRegExpFull = new RegExp('\\{@link\\s+((?:.|\n)+?)\\}', 'i');
|
11 | let tagRegExp;
|
12 | let matches;
|
13 | let previousString;
|
14 | let tagInfo = [];
|
15 |
|
16 | tagRegExp = description.indexOf(']{') !== -1 ? tagRegExpFull : tagRegExpLight;
|
17 |
|
18 | const processTheLink = (originalDescription, matchedTag, leadingText) => {
|
19 | let leading = extractLeadingText(originalDescription, matchedTag.completeTag);
|
20 | let split;
|
21 | let resultInCompodoc;
|
22 | let newLink;
|
23 | let rootPath;
|
24 | let stringtoReplace;
|
25 | let anchor = '';
|
26 | let label;
|
27 | let pageName;
|
28 |
|
29 | split = splitLinkText(matchedTag.text);
|
30 |
|
31 | if (typeof split.linkText !== 'undefined') {
|
32 | resultInCompodoc = DependenciesEngine.findInCompodoc(split.target);
|
33 | } else {
|
34 | let info = matchedTag.text;
|
35 | if (matchedTag.text.indexOf('#') !== -1) {
|
36 | anchor = matchedTag.text.substr(
|
37 | matchedTag.text.indexOf('#'),
|
38 | matchedTag.text.length
|
39 | );
|
40 | info = matchedTag.text.substr(0, matchedTag.text.indexOf('#'));
|
41 | }
|
42 | resultInCompodoc = DependenciesEngine.findInCompodoc(info);
|
43 | }
|
44 |
|
45 | if (resultInCompodoc) {
|
46 | label = resultInCompodoc.name;
|
47 | pageName = resultInCompodoc.name;
|
48 |
|
49 | if (leadingText) {
|
50 | stringtoReplace = '[' + leadingText + ']' + matchedTag.completeTag;
|
51 | } else if (leading.leadingText !== undefined) {
|
52 | stringtoReplace = '[' + leading.leadingText + ']' + matchedTag.completeTag;
|
53 | } else if (typeof split.linkText !== 'undefined') {
|
54 | stringtoReplace = matchedTag.completeTag;
|
55 | } else {
|
56 | stringtoReplace = matchedTag.completeTag;
|
57 | }
|
58 |
|
59 | if (resultInCompodoc.type === 'class') {
|
60 | resultInCompodoc.type = 'classes';
|
61 | } else if (
|
62 | resultInCompodoc.type === 'miscellaneous' ||
|
63 | (resultInCompodoc.ctype && resultInCompodoc.ctype === 'miscellaneous')
|
64 | ) {
|
65 | resultInCompodoc.type = 'miscellaneous';
|
66 | label = resultInCompodoc.name;
|
67 | anchor = '#' + resultInCompodoc.name;
|
68 | if (resultInCompodoc.subtype === 'enum') {
|
69 | pageName = 'enumerations';
|
70 | } else if (resultInCompodoc.subtype === 'function') {
|
71 | pageName = 'functions';
|
72 | } else if (resultInCompodoc.subtype === 'typealias') {
|
73 | pageName = 'typealiases';
|
74 | } else if (resultInCompodoc.subtype === 'variable') {
|
75 | pageName = 'variables';
|
76 | }
|
77 | }
|
78 |
|
79 | rootPath = '';
|
80 |
|
81 | switch (depth) {
|
82 | case 0:
|
83 | rootPath = './';
|
84 | break;
|
85 | case 1:
|
86 | case 2:
|
87 | case 3:
|
88 | case 4:
|
89 | case 5:
|
90 | rootPath = '../'.repeat(depth);
|
91 | break;
|
92 | }
|
93 |
|
94 | if (leading.leadingText !== undefined) {
|
95 | label = leading.leadingText;
|
96 | }
|
97 | if (typeof split.linkText !== 'undefined') {
|
98 | label = split.linkText;
|
99 | }
|
100 |
|
101 | if (
|
102 | resultInCompodoc.type === 'miscellaneous' ||
|
103 | resultInCompodoc.type === 'classes'
|
104 | ) {
|
105 | newLink = `<a href="${rootPath}${resultInCompodoc.type}/${pageName}.html${anchor}">${label}</a>`;
|
106 | } else {
|
107 | newLink = `<a href="${rootPath}${resultInCompodoc.type}s/${pageName}.html${anchor}">${label}</a>`;
|
108 | }
|
109 |
|
110 | return originalDescription.replace(stringtoReplace, newLink);
|
111 | } else if (!resultInCompodoc && typeof split.linkText !== 'undefined') {
|
112 | newLink = `<a href="${split.target}">${split.linkText}</a>`;
|
113 | if (leadingText) {
|
114 | stringtoReplace = '[' + leadingText + ']' + matchedTag.completeTag;
|
115 | } else if (leading.leadingText !== undefined) {
|
116 | stringtoReplace = '[' + leading.leadingText + ']' + matchedTag.completeTag;
|
117 | } else if (typeof split.linkText !== 'undefined') {
|
118 | stringtoReplace = matchedTag.completeTag;
|
119 | } else {
|
120 | stringtoReplace = matchedTag.completeTag;
|
121 | }
|
122 | return originalDescription.replace(stringtoReplace, newLink);
|
123 | } else if (!resultInCompodoc && leading && typeof leading.leadingText !== 'undefined') {
|
124 | newLink = `<a href="${split.target}">${leading.leadingText}</a>`;
|
125 | if (leadingText) {
|
126 | stringtoReplace = '[' + leadingText + ']' + matchedTag.completeTag;
|
127 | } else if (leading.leadingText !== undefined) {
|
128 | stringtoReplace = '[' + leading.leadingText + ']' + matchedTag.completeTag;
|
129 | } else if (typeof split.linkText !== 'undefined') {
|
130 | stringtoReplace = matchedTag.completeTag;
|
131 | } else {
|
132 | stringtoReplace = matchedTag.completeTag;
|
133 | }
|
134 | return originalDescription.replace(stringtoReplace, newLink);
|
135 | } else if (!resultInCompodoc && typeof split.linkText === 'undefined') {
|
136 | newLink = `<a href="${split.target}">${split.target}</a>`;
|
137 | if (leadingText) {
|
138 | stringtoReplace = '[' + leadingText + ']' + matchedTag.completeTag;
|
139 | } else if (leading.leadingText !== undefined) {
|
140 | stringtoReplace = '[' + leading.leadingText + ']' + matchedTag.completeTag;
|
141 | } else {
|
142 | stringtoReplace = matchedTag.completeTag;
|
143 | }
|
144 | return originalDescription.replace(stringtoReplace, newLink);
|
145 | } else {
|
146 | return originalDescription;
|
147 | }
|
148 | };
|
149 |
|
150 | function replaceMatch(replacer, tag, match, text, linkText?) {
|
151 | let matchedTag = {
|
152 | completeTag: match,
|
153 | tag: tag,
|
154 | text: text
|
155 | };
|
156 | tagInfo.push(matchedTag);
|
157 |
|
158 | if (linkText) {
|
159 | return replacer(description, matchedTag, linkText);
|
160 | } else {
|
161 | return replacer(description, matchedTag);
|
162 | }
|
163 | }
|
164 |
|
165 |
|
166 |
|
167 | if (description.indexOf('href=') !== -1) {
|
168 | let insideMarkedATagResults = description.match(/<a [^>]+>([^<]+)<\/a>/g);
|
169 |
|
170 | if (insideMarkedATagResults && insideMarkedATagResults.length > 0) {
|
171 | for (let i = 0; i < insideMarkedATagResults.length; i++) {
|
172 | let markedATagRegExp = new RegExp('<a [^>]+>([^<]+)</a>', 'gm');
|
173 | let parsedATag = markedATagRegExp.exec(description);
|
174 | if (parsedATag && parsedATag.length === 2) {
|
175 | let insideMarkedATag = parsedATag[1];
|
176 | description = description.replace(
|
177 | `{@link <a href="${encodeURI(
|
178 | insideMarkedATag
|
179 | )}">${insideMarkedATag}</a>`,
|
180 | `{@link ${insideMarkedATag}`
|
181 | );
|
182 | }
|
183 | }
|
184 | }
|
185 | }
|
186 |
|
187 | do {
|
188 | matches = tagRegExp.exec(description);
|
189 |
|
190 |
|
191 | if (matches) {
|
192 | previousString = description;
|
193 | if (matches.length === 2) {
|
194 | description = replaceMatch(processTheLink, 'link', matches[0], matches[1]);
|
195 | }
|
196 | if (matches.length === 3) {
|
197 | description = replaceMatch(
|
198 | processTheLink,
|
199 | 'link',
|
200 | matches[0],
|
201 | matches[2],
|
202 | matches[1]
|
203 | );
|
204 | }
|
205 | }
|
206 | } while (matches && previousString !== description);
|
207 |
|
208 | return description;
|
209 | }
|
210 | }
|