-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathdocumentation.ts
213 lines (193 loc) · 5.73 KB
/
documentation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { ReplaceFunction, forEach, unescape, concat, includes } from "lodash";
import {
UI5DeprecatedInfo,
UI5SemanticModel,
} from "@ui5-language-assistant/semantic-model-types";
import { OPEN_FRAMEWORK } from "@ui5-language-assistant/constant";
export function getDeprecationPlainTextSnippet({
title,
deprecatedInfo,
model,
}: {
title?: string;
deprecatedInfo: UI5DeprecatedInfo;
model: UI5SemanticModel;
}): string {
let text = deprecatedInfo.text;
if (text !== undefined) {
text = convertJSDocToPlainText(text, model);
// Only take the first line.
// Multi-line problems are displayed open in the problem view which makes less problems visible unless the user
// explicitly closes them.
// For the full deprecation message the user can hover over the tag/attribute.
// Note: long lines are displayed with "..." in the problems view and the user can hover over the problem to see
// the full text.
if (includes(text, "\n")) {
text = text.substring(0, text.indexOf("\n")) + " ...";
}
}
const doc = getDeprecationMessage({
title,
since: deprecatedInfo.since,
text,
});
return doc;
}
export function getDeprecationMessage({
title,
since,
text,
}: {
title?: string;
since: string | undefined;
text: string | undefined;
}): string {
let contents = title ?? "Deprecated";
contents += since ? ` since version ${since}` : "";
contents += ".";
contents += text ? ` ${text}` : "";
return contents;
}
// Exported for testing purpose
export function convertJSDocToPlainText(
jsdocDescription: string,
model: UI5SemanticModel
): string {
return convertJSDoc(jsdocDescription, "plaintext", model);
}
export function convertJSDocToMarkdown(
jsdocDescription: string,
model: UI5SemanticModel
): string {
return convertJSDoc(jsdocDescription, "markdown", model);
}
type ConvertTarget = "markdown" | "plaintext";
// The replacements are applied in order
const tagMatcherToReplacement: {
matcher: RegExp;
replacement: Record<ConvertTarget, string | ReplaceFunction>;
}[] = [
// Italics
{
matcher: /<i>(.+?)<\/i>/g,
replacement: { markdown: "*$1*", plaintext: "$1" },
},
// Bold
{
matcher: /<b>(.+?)<\/b>/g,
replacement: { markdown: "**$1**", plaintext: "$1" },
},
{
matcher: /<strong>(.+?)<\/strong>/g,
replacement: { markdown: "**$1**", plaintext: "$1" },
},
// Emphasis
{
matcher: /<em>(.+?)<\/em>/g,
replacement: { markdown: "***$1***", plaintext: "$1" },
},
// Headers
{
matcher: /<h1>(.+?)<\/h1>/g,
replacement: { markdown: "\n# $1\n\n", plaintext: "\n$1\n" },
},
{
matcher: /<h2>(.+?)<\/h2>/g,
replacement: { markdown: "\n## $1\n\n", plaintext: "\n$1\n" },
},
{
matcher: /<h3>(.+?)<\/h3>/g,
replacement: { markdown: "\n### $1\n\n", plaintext: "\n$1\n" },
},
{
matcher: /<h4>(.+?)<\/h4>/g,
replacement: { markdown: "\n#### $1\n\n", plaintext: "\n$1\n" },
},
// Lists
{
matcher: /<li>(.+?)<\/li>/g,
replacement: { markdown: "\n* $1", plaintext: "\n* $1" },
},
{ matcher: /<ul>/g, replacement: { markdown: "", plaintext: "" } },
{ matcher: /<\/ul>/g, replacement: { markdown: "\n\n", plaintext: "\n" } },
// TODO should we handle ordered lists (ol)?
// Code value
{
matcher: /<code>(.+?)<\/code>/g,
replacement: { markdown: "`$1`", plaintext: "$1" },
},
// Code block
{
matcher: /<pre>([^]+?)<\/pre>/g,
replacement: {
markdown: "\n```javascript\n$1```\n",
plaintext: "\n$1\n",
},
},
// Line break
{ matcher: /<br\/>/g, replacement: { markdown: "\n", plaintext: "\n" } },
{ matcher: /<br>/g, replacement: { markdown: "\n", plaintext: "\n" } },
{ matcher: /<\/br>/g, replacement: { markdown: "\n", plaintext: "\n" } },
// HTML Escaping, e.g. "<View" --> "<View"
// Note: this doesn't replace all html-encoded characters, only the most common ones
{
matcher: /.*/gm,
replacement: { markdown: unescape, plaintext: unescape },
},
];
function convertJSDoc(
jsdoc: string,
target: ConvertTarget,
model: UI5SemanticModel
): string {
// We add replacements that require the model here (because they cannot be in tagMatcherToReplacement which is defined
// outside of the function).
// Note that they are applied after the replacements defined in tagMatcherToReplacement.
const allTagMatcherToReplacement = concat(tagMatcherToReplacement, [
// Links
// Assuming links are of the form: {@link <type>[ <text>]}
// Where the type doesn't contain whitespace, and neither the type nor text contain the "}" character
{
matcher: /{@link (([^\s}]+)\s)?([^}]+)}/g,
replacement: {
markdown: (all, _, type, text): string => {
return `[${text}](${getLink(model, type ?? text)})`;
},
plaintext: "$3",
},
},
]);
let contents = jsdoc;
forEach(allTagMatcherToReplacement, (_) => {
contents = replace(contents, _.matcher, _.replacement[target]);
});
return contents;
}
function replace(
string: string,
matcher: RegExp,
replacement: string | ReplaceFunction
): string {
// @ts-expect-error - 'replace' is defined with 2 overloads instead of a union type in the definitions file
return string.replace(matcher, replacement);
}
export function getLink(model: UI5SemanticModel, link: string): string {
if (link.startsWith("http:") || link.startsWith("https:")) {
return link;
}
if (link.startsWith("topic:")) {
link = `topic/${link.substring(6)}`;
} else {
link = `api/${link}`;
}
let baseUrl;
if (model.framework === OPEN_FRAMEWORK) {
baseUrl = "https://sdk.openui5.org/";
} else {
baseUrl = "https://ui5.sap.com/";
}
if (model.version) {
return `${baseUrl}${model.version}/#/${link}`;
}
return `${baseUrl}#/${link}`;
}