@yadsh/dsh-qa-integrations 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +33 -16
- package/lib/client/confluence.js +37 -16
- package/lib/client/confluence.js.map +1 -1
- package/lib/client/jira.js +32 -11
- package/lib/client/jira.js.map +1 -1
- package/lib/client.js +128 -73
- package/lib/client.js.map +1 -1
- package/lib/coerce.js +17 -1
- package/lib/coerce.js.map +1 -1
- package/lib/config.js.map +1 -1
- package/lib/errors.js +22 -0
- package/lib/errors.js.map +1 -1
- package/lib/index.js +20 -2
- package/lib/index.js.map +1 -1
- package/lib/providers/bitrix24/config.js +2 -3
- package/lib/providers/bitrix24/config.js.map +1 -1
- package/lib/providers/bitrix24/index.js +1 -14
- package/lib/providers/bitrix24/index.js.map +1 -1
- package/lib/providers/bitrix24/operations.js +5 -9
- package/lib/providers/bitrix24/operations.js.map +1 -1
- package/lib/providers/bitrix24/transport.js +11 -31
- package/lib/providers/bitrix24/transport.js.map +1 -1
- package/lib/providers/confluence/adf.js +28 -23
- package/lib/providers/confluence/adf.js.map +1 -1
- package/lib/providers/confluence/catalog.js +40 -4
- package/lib/providers/confluence/catalog.js.map +1 -1
- package/lib/providers/confluence/config.js +27 -2
- package/lib/providers/confluence/config.js.map +1 -1
- package/lib/providers/confluence/credential-help.js +14 -8
- package/lib/providers/confluence/credential-help.js.map +1 -1
- package/lib/providers/confluence/dialect.js +215 -0
- package/lib/providers/confluence/dialect.js.map +1 -0
- package/lib/providers/confluence/index.js +137 -81
- package/lib/providers/confluence/index.js.map +1 -1
- package/lib/providers/confluence/operations.js +207 -152
- package/lib/providers/confluence/operations.js.map +1 -1
- package/lib/providers/confluence/storage.js +690 -0
- package/lib/providers/confluence/storage.js.map +1 -0
- package/lib/providers/confluence/transport.js +26 -89
- package/lib/providers/confluence/transport.js.map +1 -1
- package/lib/providers/gitlab/config.js +2 -3
- package/lib/providers/gitlab/config.js.map +1 -1
- package/lib/providers/gitlab/index.js +2 -20
- package/lib/providers/gitlab/index.js.map +1 -1
- package/lib/providers/gitlab/operations.js +7 -45
- package/lib/providers/gitlab/operations.js.map +1 -1
- package/lib/providers/gitlab/transport.js +2 -18
- package/lib/providers/gitlab/transport.js.map +1 -1
- package/lib/providers/jira/adf.js +19 -14
- package/lib/providers/jira/adf.js.map +1 -1
- package/lib/providers/jira/catalog.js +52 -1
- package/lib/providers/jira/catalog.js.map +1 -1
- package/lib/providers/jira/config.js +27 -2
- package/lib/providers/jira/config.js.map +1 -1
- package/lib/providers/jira/credential-help.js +16 -7
- package/lib/providers/jira/credential-help.js.map +1 -1
- package/lib/providers/jira/dialect.js +94 -0
- package/lib/providers/jira/dialect.js.map +1 -0
- package/lib/providers/jira/index.js +101 -95
- package/lib/providers/jira/index.js.map +1 -1
- package/lib/providers/jira/jql.js +54 -21
- package/lib/providers/jira/jql.js.map +1 -1
- package/lib/providers/jira/operations.js +77 -55
- package/lib/providers/jira/operations.js.map +1 -1
- package/lib/providers/jira/tools.js +1 -1
- package/lib/providers/jira/tools.js.map +1 -1
- package/lib/providers/jira/transport.js +12 -83
- package/lib/providers/jira/transport.js.map +1 -1
- package/lib/providers/shared/health.js +31 -0
- package/lib/providers/shared/health.js.map +1 -0
- package/lib/providers/shared/http.js +20 -0
- package/lib/providers/shared/http.js.map +1 -1
- package/lib/providers/shared/paths.js +14 -0
- package/lib/providers/shared/paths.js.map +1 -0
- package/lib/providers/shared/payload.js +46 -0
- package/lib/providers/shared/payload.js.map +1 -0
- package/lib/providers/teamcity/artifacts.js +0 -8
- package/lib/providers/teamcity/artifacts.js.map +1 -1
- package/lib/providers/teamcity/config.js +2 -3
- package/lib/providers/teamcity/config.js.map +1 -1
- package/lib/providers/teamcity/index.js +2 -20
- package/lib/providers/teamcity/index.js.map +1 -1
- package/lib/providers/teamcity/operations.js +5 -39
- package/lib/providers/teamcity/operations.js.map +1 -1
- package/lib/providers/teamcity/transport.js +2 -11
- package/lib/providers/teamcity/transport.js.map +1 -1
- package/lib/providers/testit/config.js +2 -3
- package/lib/providers/testit/config.js.map +1 -1
- package/lib/providers/testit/index.js +2 -20
- package/lib/providers/testit/index.js.map +1 -1
- package/lib/providers/testit/operations.js +34 -63
- package/lib/providers/testit/operations.js.map +1 -1
- package/lib/providers/testit/transport.js +2 -18
- package/lib/providers/testit/transport.js.map +1 -1
- package/lib/providers/weblate/config.js +2 -3
- package/lib/providers/weblate/config.js.map +1 -1
- package/lib/providers/weblate/index.js +2 -19
- package/lib/providers/weblate/index.js.map +1 -1
- package/lib/providers/weblate/operations.js +3 -33
- package/lib/providers/weblate/operations.js.map +1 -1
- package/lib/providers/weblate/query.js +3 -6
- package/lib/providers/weblate/query.js.map +1 -1
- package/lib/providers/weblate/transport.js +2 -12
- package/lib/providers/weblate/transport.js.map +1 -1
- package/lib/service-credentials/config.js +2 -3
- package/lib/service-credentials/config.js.map +1 -1
- package/lib/typert.host.js +62 -55
- package/lib/typert.remote-client.d.ts +4 -4
- package/lib/typert.remote-client.js +58 -51
- package/lib/types/client/confluence.d.ts +5 -1
- package/lib/types/client/jira.d.ts +5 -1
- package/lib/types/coerce.d.ts +14 -0
- package/lib/types/config.d.ts +4 -4
- package/lib/types/errors.d.ts +16 -0
- package/lib/types/index.d.ts +10 -2
- package/lib/types/providers/bitrix24/index.d.ts +1 -0
- package/lib/types/providers/confluence/catalog.d.ts +33 -5
- package/lib/types/providers/confluence/config.d.ts +39 -7
- package/lib/types/providers/confluence/credential-help.d.ts +9 -4
- package/lib/types/providers/confluence/dialect.d.ts +113 -0
- package/lib/types/providers/confluence/index.d.ts +24 -4
- package/lib/types/providers/confluence/operations.d.ts +12 -4
- package/lib/types/providers/confluence/storage.d.ts +28 -0
- package/lib/types/providers/confluence/transport.d.ts +10 -21
- package/lib/types/providers/jira/catalog.d.ts +36 -2
- package/lib/types/providers/jira/config.d.ts +37 -6
- package/lib/types/providers/jira/credential-help.d.ts +11 -3
- package/lib/types/providers/jira/dialect.d.ts +79 -0
- package/lib/types/providers/jira/index.d.ts +32 -22
- package/lib/types/providers/jira/jql.d.ts +14 -4
- package/lib/types/providers/jira/operations.d.ts +3 -0
- package/lib/types/providers/jira/transport.d.ts +6 -13
- package/lib/types/providers/shared/health.d.ts +18 -0
- package/lib/types/providers/shared/http.d.ts +9 -0
- package/lib/types/providers/shared/paths.d.ts +10 -0
- package/lib/types/providers/shared/payload.d.ts +26 -0
- package/lib/types/providers/teamcity/artifacts.d.ts +0 -2
- package/lib/types/providers/teamcity/index.d.ts +2 -0
- package/lib/types/providers/testit/index.d.ts +2 -0
- package/lib/types/types.d.ts +8 -0
- package/package.json +5 -5
|
@@ -0,0 +1,690 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Confluence Server and Data Center answer a page body as storage format: the
|
|
3
|
+
* XHTML-ish markup the editor keeps under a page, returned in
|
|
4
|
+
* `body.storage.value`. Its Cloud counterpart is Atlassian Document Format,
|
|
5
|
+
* which `adf.ts` renders; this module is the same trade for the self-hosted
|
|
6
|
+
* product, so a caller reads a page's words instead of the wrappers around
|
|
7
|
+
* them.
|
|
8
|
+
*
|
|
9
|
+
* The flattening is as defensive as the other renderers here, and for the same
|
|
10
|
+
* reason: a body is untrusted text a reader asked for, not instructions to
|
|
11
|
+
* follow. Nothing is fetched, executed or followed — a macro becomes a
|
|
12
|
+
* placeholder line, a reference becomes a marker, and a macro's parameters,
|
|
13
|
+
* which hold arbitrary page-authored text, are dropped except for the one that
|
|
14
|
+
* names a code language. A body that is hostile (unbalanced, nested past all
|
|
15
|
+
* reason, full of control characters) is still only read.
|
|
16
|
+
*
|
|
17
|
+
* Rendering is a scan and a small tree of kept tags, not a DOM: the body is
|
|
18
|
+
* walked once by index, so a megabyte of markup costs one pass, and the depth
|
|
19
|
+
* ceiling bounds both what the tree can hold and what the renderer will walk.
|
|
20
|
+
*/
|
|
21
|
+
/** A node deeper than this is not walked, and a scan does not open past it. */
|
|
22
|
+
const MAX_DEPTH = 64;
|
|
23
|
+
/** Tab and the two line endings survive; every other control character does not. */
|
|
24
|
+
const KEPT_CONTROL_CHARS = new Set([0x09, 0x0a, 0x0d]);
|
|
25
|
+
/** Elements that never hold children, so their `/>` is the whole element. */
|
|
26
|
+
const VOID_TAGS = new Set([
|
|
27
|
+
"area",
|
|
28
|
+
"base",
|
|
29
|
+
"br",
|
|
30
|
+
"col",
|
|
31
|
+
"embed",
|
|
32
|
+
"hr",
|
|
33
|
+
"img",
|
|
34
|
+
"input",
|
|
35
|
+
"link",
|
|
36
|
+
"meta",
|
|
37
|
+
"source",
|
|
38
|
+
"track",
|
|
39
|
+
"wbr",
|
|
40
|
+
]);
|
|
41
|
+
/**
|
|
42
|
+
* Tags the inline renderer knows. One of these at block level is a generator's
|
|
43
|
+
* mistake; rendering it as text keeps the words instead of dropping them.
|
|
44
|
+
*/
|
|
45
|
+
const INLINE_TAGS = new Set([
|
|
46
|
+
"a",
|
|
47
|
+
"b",
|
|
48
|
+
"br",
|
|
49
|
+
"code",
|
|
50
|
+
"del",
|
|
51
|
+
"em",
|
|
52
|
+
"i",
|
|
53
|
+
"ins",
|
|
54
|
+
"mark",
|
|
55
|
+
"s",
|
|
56
|
+
"small",
|
|
57
|
+
"span",
|
|
58
|
+
"strike",
|
|
59
|
+
"strong",
|
|
60
|
+
"sub",
|
|
61
|
+
"sup",
|
|
62
|
+
"tt",
|
|
63
|
+
"u",
|
|
64
|
+
"ac:image",
|
|
65
|
+
"ac:link",
|
|
66
|
+
"ac:parameter",
|
|
67
|
+
"ri:attachment",
|
|
68
|
+
"ri:page",
|
|
69
|
+
"ri:space",
|
|
70
|
+
"ri:url",
|
|
71
|
+
"ri:user",
|
|
72
|
+
"ri:username",
|
|
73
|
+
]);
|
|
74
|
+
/** No entity a page holds is longer than this; a `;` past it ends none. */
|
|
75
|
+
const ENTITY_MAX_LENGTH = 32;
|
|
76
|
+
/**
|
|
77
|
+
* The entities a storage body actually emits. `nbsp` becomes an ordinary
|
|
78
|
+
* space: it is a layout device, and U+00A0 would reach the reader as an
|
|
79
|
+
* invisible byte that looks like no space at all.
|
|
80
|
+
*/
|
|
81
|
+
const NAMED_ENTITIES = Object.freeze({
|
|
82
|
+
amp: "&",
|
|
83
|
+
lt: "<",
|
|
84
|
+
gt: ">",
|
|
85
|
+
quot: '"',
|
|
86
|
+
apos: "'",
|
|
87
|
+
nbsp: " ",
|
|
88
|
+
mdash: "\u2014",
|
|
89
|
+
ndash: "\u2013",
|
|
90
|
+
hellip: "\u2026",
|
|
91
|
+
});
|
|
92
|
+
/** A tag field as text; an attribute a tag did not carry is the empty string. */
|
|
93
|
+
function fieldText(value) {
|
|
94
|
+
return typeof value === "string" ? value : "";
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* One attribute of a kept tag. Values were entity-decoded when the tag was
|
|
98
|
+
* read, so only the control characters a body could smuggle through an
|
|
99
|
+
* attribute are removed here.
|
|
100
|
+
*/
|
|
101
|
+
function attr(element, name) {
|
|
102
|
+
return flatten(fieldText(element.attrs[name]));
|
|
103
|
+
}
|
|
104
|
+
/** Only the tags among a node's children; text is read where text means text. */
|
|
105
|
+
function childElements(element) {
|
|
106
|
+
return element.children.filter((child) => typeof child !== "string");
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Control characters carry no meaning in a page body and would only be noise in
|
|
110
|
+
* an answer, so they are dropped; asking a regular expression to do it reads as
|
|
111
|
+
* a control-character range and lint refuses it, which is why this is a plain
|
|
112
|
+
* walk — the same policy and the same implementation as the ADF renderer.
|
|
113
|
+
*/
|
|
114
|
+
function flatten(value) {
|
|
115
|
+
let result = "";
|
|
116
|
+
for (const char of value) {
|
|
117
|
+
const code = char.codePointAt(0) ?? 0;
|
|
118
|
+
const control = code < 0x20 || code === 0x7f;
|
|
119
|
+
if (control && !KEPT_CONTROL_CHARS.has(code))
|
|
120
|
+
continue;
|
|
121
|
+
result += char;
|
|
122
|
+
}
|
|
123
|
+
return result;
|
|
124
|
+
}
|
|
125
|
+
/** The indentation a pretty-printed body carries is not the reader's text. */
|
|
126
|
+
function collapseWhitespace(value) {
|
|
127
|
+
return value.replace(/\s+/gu, " ");
|
|
128
|
+
}
|
|
129
|
+
/** A tag that rendered nothing can leave two spaces; a page reads with one. */
|
|
130
|
+
function collapseSpaces(value) {
|
|
131
|
+
return value.replace(/ {2,}/gu, " ");
|
|
132
|
+
}
|
|
133
|
+
/** Line endings around a code body are the body's edges, not its text. */
|
|
134
|
+
function trimNewlines(value) {
|
|
135
|
+
return value.replace(/^[\r\n]+|[\r\n]+$/gu, "");
|
|
136
|
+
}
|
|
137
|
+
/** The character one entity names, or `undefined` when it names none we know. */
|
|
138
|
+
function decodedEntity(name) {
|
|
139
|
+
const named = NAMED_ENTITIES[name];
|
|
140
|
+
if (named !== undefined)
|
|
141
|
+
return named;
|
|
142
|
+
if (!name.startsWith("#"))
|
|
143
|
+
return undefined;
|
|
144
|
+
const hexadecimal = name.startsWith("#x") || name.startsWith("#X");
|
|
145
|
+
const digits = name.slice(hexadecimal ? 2 : 1);
|
|
146
|
+
const pattern = hexadecimal ? /^[0-9a-f]+$/iu : /^[0-9]+$/u;
|
|
147
|
+
if (!pattern.test(digits))
|
|
148
|
+
return undefined;
|
|
149
|
+
const code = Number.parseInt(digits, hexadecimal ? 16 : 10);
|
|
150
|
+
// A surrogate half or a code point outside Unicode is not a character.
|
|
151
|
+
if (code < 1 || code > 0x10ffff)
|
|
152
|
+
return undefined;
|
|
153
|
+
if (code >= 0xd800 && code <= 0xdfff)
|
|
154
|
+
return undefined;
|
|
155
|
+
return String.fromCodePoint(code);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Undo the entities of one text run. The walk resumes after the entity it just
|
|
159
|
+
* replaced, so `&lt;` reads as the page wrote it — `<` — and is never
|
|
160
|
+
* decoded twice.
|
|
161
|
+
*/
|
|
162
|
+
function decodeEntities(value) {
|
|
163
|
+
if (!value.includes("&"))
|
|
164
|
+
return value;
|
|
165
|
+
let result = "";
|
|
166
|
+
let index = 0;
|
|
167
|
+
while (index < value.length) {
|
|
168
|
+
const start = value.indexOf("&", index);
|
|
169
|
+
if (start === -1) {
|
|
170
|
+
result += value.slice(index);
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
const end = value.indexOf(";", start + 1);
|
|
174
|
+
if (end === -1 || end - start > ENTITY_MAX_LENGTH) {
|
|
175
|
+
result += value.slice(index, start + 1);
|
|
176
|
+
index = start + 1;
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
const decoded = decodedEntity(value.slice(start + 1, end));
|
|
180
|
+
if (decoded === undefined) {
|
|
181
|
+
result += value.slice(index, start + 1);
|
|
182
|
+
index = start + 1;
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
result += value.slice(index, start) + decoded;
|
|
186
|
+
index = end + 1;
|
|
187
|
+
}
|
|
188
|
+
return result;
|
|
189
|
+
}
|
|
190
|
+
/** Letters, digits and the `:` `-` `_` `.` a tag or attribute is named with. */
|
|
191
|
+
function isNameChar(code) {
|
|
192
|
+
const digit = code >= 0x30 && code <= 0x39;
|
|
193
|
+
const letter = (code >= 0x41 && code <= 0x5a) || (code >= 0x61 && code <= 0x7a);
|
|
194
|
+
return (digit ||
|
|
195
|
+
letter ||
|
|
196
|
+
code === 0x3a || // :
|
|
197
|
+
code === 0x2d || // -
|
|
198
|
+
code === 0x5f || // _
|
|
199
|
+
code === 0x2e // .
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
/** The index just past the tag name starting at `from` (which it is if none). */
|
|
203
|
+
function nameEnd(source, from) {
|
|
204
|
+
let index = from;
|
|
205
|
+
while (index < source.length && isNameChar(source.charCodeAt(index))) {
|
|
206
|
+
index += 1;
|
|
207
|
+
}
|
|
208
|
+
return index;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* The index of the `>` that ends a tag, skipping the ones an attribute value may
|
|
212
|
+
* carry, or `-1` when the tag never ends.
|
|
213
|
+
*/
|
|
214
|
+
function endOfTag(source, from) {
|
|
215
|
+
let quote = "";
|
|
216
|
+
for (let index = from; index < source.length; index += 1) {
|
|
217
|
+
const char = source[index];
|
|
218
|
+
if (char === undefined)
|
|
219
|
+
break;
|
|
220
|
+
if (quote !== "") {
|
|
221
|
+
if (char === quote)
|
|
222
|
+
quote = "";
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
if (char === '"' || char === "'") {
|
|
226
|
+
quote = char;
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
if (char === ">")
|
|
230
|
+
return index;
|
|
231
|
+
}
|
|
232
|
+
return -1;
|
|
233
|
+
}
|
|
234
|
+
/** `name="value"` pairs of one tag; anything else in there names no attribute. */
|
|
235
|
+
const ATTRIBUTE = /([A-Za-z][\w:.-]*)\s*=\s*"([^"]*)"|([A-Za-z][\w:.-]*)\s*=\s*'([^']*)'/gu;
|
|
236
|
+
/** The attributes of one tag, entity-decoded, unquoted ones left out. */
|
|
237
|
+
function parseAttrs(source) {
|
|
238
|
+
const attrs = {};
|
|
239
|
+
for (const match of source.matchAll(ATTRIBUTE)) {
|
|
240
|
+
const name = match[1] ?? match[3] ?? "";
|
|
241
|
+
if (name === "")
|
|
242
|
+
continue;
|
|
243
|
+
attrs[name] = decodeEntities(match[2] ?? match[4] ?? "");
|
|
244
|
+
}
|
|
245
|
+
return attrs;
|
|
246
|
+
}
|
|
247
|
+
/** Add a text run to an element, joining it to the run already there. */
|
|
248
|
+
function appendText(element, value) {
|
|
249
|
+
if (value === "")
|
|
250
|
+
return;
|
|
251
|
+
const last = element.children[element.children.length - 1];
|
|
252
|
+
if (typeof last === "string") {
|
|
253
|
+
element.children[element.children.length - 1] = last + value;
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
element.children.push(value);
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* A list item or a paragraph ends the one it opens inside of: storage format is
|
|
260
|
+
* XHTML and normally balanced, but a hand-built or generated body is not.
|
|
261
|
+
*/
|
|
262
|
+
function implicitClose(stack, name) {
|
|
263
|
+
if (name !== "li" && name !== "p")
|
|
264
|
+
return;
|
|
265
|
+
let open = stack[stack.length - 1];
|
|
266
|
+
while (open !== undefined &&
|
|
267
|
+
(open.name === name || (name === "li" && open.name === "p"))) {
|
|
268
|
+
stack.pop();
|
|
269
|
+
open = stack[stack.length - 1];
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
/** Close the open tag this name belongs to, and everything it contained. */
|
|
273
|
+
function closeTag(stack, name) {
|
|
274
|
+
if (name === "")
|
|
275
|
+
return;
|
|
276
|
+
for (let index = stack.length - 1; index > 0; index -= 1) {
|
|
277
|
+
if (stack[index]?.name === name) {
|
|
278
|
+
stack.length = index;
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
/** Open one tag: attach it, and keep it open unless it can hold nothing. */
|
|
284
|
+
function openTag(source, from, end, stack, parent) {
|
|
285
|
+
const name = source.slice(from, nameEnd(source, from)).toLowerCase();
|
|
286
|
+
if (name === "")
|
|
287
|
+
return;
|
|
288
|
+
const selfClosing = source[end - 1] === "/";
|
|
289
|
+
const element = {
|
|
290
|
+
name,
|
|
291
|
+
attrs: parseAttrs(source.slice(from, selfClosing ? end - 1 : end)),
|
|
292
|
+
children: [],
|
|
293
|
+
};
|
|
294
|
+
implicitClose(stack, name);
|
|
295
|
+
const container = stack[stack.length - 1] ?? parent;
|
|
296
|
+
container.children.push(element);
|
|
297
|
+
if (selfClosing || VOID_TAGS.has(name))
|
|
298
|
+
return;
|
|
299
|
+
// Past the rendering ceiling a tag is not opened any more, which bounds the
|
|
300
|
+
// tree a hostile body can build. One level past the ceiling is still kept, so
|
|
301
|
+
// that the walk always stops on its own guard rather than on a cut tree.
|
|
302
|
+
if (stack.length - 1 > MAX_DEPTH)
|
|
303
|
+
return;
|
|
304
|
+
stack.push(element);
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Read the markup starting at one `<`, which is either a comment, a CDATA
|
|
308
|
+
* section, a declaration, a closing tag or an opening tag. Returns the index to
|
|
309
|
+
* continue from; the scan always moves forward, so no input can trap it.
|
|
310
|
+
*/
|
|
311
|
+
function readMarkup(source, start, stack, current) {
|
|
312
|
+
if (source.startsWith("<!--", start)) {
|
|
313
|
+
const end = source.indexOf("-->", start + "<!--".length);
|
|
314
|
+
return end === -1 ? source.length : end + "-->".length;
|
|
315
|
+
}
|
|
316
|
+
if (source.startsWith("<![CDATA[", start)) {
|
|
317
|
+
const body = start + "<![CDATA[".length;
|
|
318
|
+
const end = source.indexOf("]]>", body);
|
|
319
|
+
// CDATA is not escaped, so its text is kept exactly as the page sent it.
|
|
320
|
+
appendText(current, end === -1 ? source.slice(body) : source.slice(body, end));
|
|
321
|
+
return end === -1 ? source.length : end + "]]>".length;
|
|
322
|
+
}
|
|
323
|
+
if (source.startsWith("</", start)) {
|
|
324
|
+
const end = endOfTag(source, start + 2);
|
|
325
|
+
if (end === -1)
|
|
326
|
+
return source.length;
|
|
327
|
+
const name = source.slice(start + 2, nameEnd(source, start + 2));
|
|
328
|
+
closeTag(stack, name.trim().toLowerCase());
|
|
329
|
+
return end + 1;
|
|
330
|
+
}
|
|
331
|
+
if (source.startsWith("<!", start) || source.startsWith("<?", start)) {
|
|
332
|
+
const end = source.indexOf(">", start + 2);
|
|
333
|
+
return end === -1 ? source.length : end + 1;
|
|
334
|
+
}
|
|
335
|
+
const end = endOfTag(source, start + 1);
|
|
336
|
+
if (end === -1) {
|
|
337
|
+
// A `<` that never closes is a character a page wrote, not markup.
|
|
338
|
+
appendText(current, decodeEntities(source.slice(start)));
|
|
339
|
+
return source.length;
|
|
340
|
+
}
|
|
341
|
+
openTag(source, start + 1, end, stack, current);
|
|
342
|
+
return end + 1;
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Storage markup as a tree of kept elements and text, in one forward pass.
|
|
346
|
+
* Open tags live on a stack rather than on the call stack, so a body cannot
|
|
347
|
+
* exhaust the stack before rendering even starts; past the depth ceiling a tag
|
|
348
|
+
* is no longer opened, which bounds the tree a hostile body can build.
|
|
349
|
+
*/
|
|
350
|
+
function parseMarkup(source) {
|
|
351
|
+
const root = { name: "", attrs: {}, children: [] };
|
|
352
|
+
const stack = [root];
|
|
353
|
+
let index = 0;
|
|
354
|
+
while (index < source.length) {
|
|
355
|
+
const current = stack[stack.length - 1] ?? root;
|
|
356
|
+
const start = source.indexOf("<", index);
|
|
357
|
+
if (start === -1) {
|
|
358
|
+
appendText(current, decodeEntities(source.slice(index)));
|
|
359
|
+
break;
|
|
360
|
+
}
|
|
361
|
+
if (start > index) {
|
|
362
|
+
appendText(current, decodeEntities(source.slice(index, start)));
|
|
363
|
+
}
|
|
364
|
+
index = readMarkup(source, start, stack, current);
|
|
365
|
+
}
|
|
366
|
+
return root;
|
|
367
|
+
}
|
|
368
|
+
/** One text run as the reader sees it: entities undone, indentation collapsed. */
|
|
369
|
+
function text(value) {
|
|
370
|
+
return flatten(collapseWhitespace(value));
|
|
371
|
+
}
|
|
372
|
+
/** Everything a tag holds, its own tags dropped and its text left as it read. */
|
|
373
|
+
function rawText(element) {
|
|
374
|
+
let result = "";
|
|
375
|
+
for (const child of element.children) {
|
|
376
|
+
result += typeof child === "string" ? child : rawText(child);
|
|
377
|
+
}
|
|
378
|
+
return result;
|
|
379
|
+
}
|
|
380
|
+
/** A fenced code block, language-tagged the way the ADF renderer fences one. */
|
|
381
|
+
function fence(language, code) {
|
|
382
|
+
return `\`\`\`${language}\n${code}\n\`\`\``;
|
|
383
|
+
}
|
|
384
|
+
/** The code or preformatted text a body element holds, its edges tidied. */
|
|
385
|
+
function codeOf(element) {
|
|
386
|
+
return flatten(trimNewlines(rawText(element)));
|
|
387
|
+
}
|
|
388
|
+
/** The provider's stand-in for something a page points at and never shows. */
|
|
389
|
+
function marker(kind, label) {
|
|
390
|
+
return label === "" ? `[${kind}]` : `[${kind}: ${label}]`;
|
|
391
|
+
}
|
|
392
|
+
/** One named parameter of a macro. Only a code language is ever read out. */
|
|
393
|
+
function parameter(element, name) {
|
|
394
|
+
const found = childElements(element).find((child) => child.name === "ac:parameter" && attr(child, "ac:name") === name);
|
|
395
|
+
return found === undefined ? "" : text(rawText(found)).trim();
|
|
396
|
+
}
|
|
397
|
+
/** The code body of a code macro: its plain-text body, or the `pre` it wraps. */
|
|
398
|
+
function macroCode(element) {
|
|
399
|
+
const children = childElements(element);
|
|
400
|
+
const plain = children.find((child) => child.name === "ac:plain-text-body");
|
|
401
|
+
const source = plain ??
|
|
402
|
+
children.find((child) => child.name === "pre" || child.name === "code");
|
|
403
|
+
return source === undefined ? "" : codeOf(source);
|
|
404
|
+
}
|
|
405
|
+
/** A macro's only reader-visible content: its body, never its parameters. */
|
|
406
|
+
function macroBody(element, depth) {
|
|
407
|
+
if (attr(element, "ac:name") === "code") {
|
|
408
|
+
const code = macroCode(element);
|
|
409
|
+
return code === "" ? "" : fence(parameter(element, "language"), code);
|
|
410
|
+
}
|
|
411
|
+
const rich = childElements(element).find((child) => child.name === "ac:rich-text-body");
|
|
412
|
+
if (rich !== undefined)
|
|
413
|
+
return blocks(rich.children, depth + 1).join("\n\n");
|
|
414
|
+
const plain = childElements(element).find((child) => child.name === "ac:plain-text-body");
|
|
415
|
+
return plain === undefined ? "" : codeOf(plain);
|
|
416
|
+
}
|
|
417
|
+
/** A macro is never executed: it is a placeholder and, maybe, its body. */
|
|
418
|
+
function macro(element, depth) {
|
|
419
|
+
const name = attr(element, "ac:name");
|
|
420
|
+
const head = name === "" ? "[macro]" : `[macro: ${name}]`;
|
|
421
|
+
const body = macroBody(element, depth);
|
|
422
|
+
return body === "" ? head : `${head}\n${body}`;
|
|
423
|
+
}
|
|
424
|
+
/** An image is a marker naming what a page pointed at, never the bytes. */
|
|
425
|
+
function image(element) {
|
|
426
|
+
const alt = attr(element, "ac:alt");
|
|
427
|
+
if (alt !== "")
|
|
428
|
+
return marker("image", alt);
|
|
429
|
+
const attachment = childElements(element).find((child) => child.name === "ri:attachment");
|
|
430
|
+
const filename = attachment === undefined ? "" : attr(attachment, "ri:filename");
|
|
431
|
+
return marker("image", filename);
|
|
432
|
+
}
|
|
433
|
+
/** The words a link shows: its body elements, or the reference it points at. */
|
|
434
|
+
function linkText(element, depth) {
|
|
435
|
+
const shown = element.children
|
|
436
|
+
.filter((child) => typeof child !== "string" &&
|
|
437
|
+
(child.name === "ac:link-body" ||
|
|
438
|
+
child.name === "ac:plain-text-link-body"))
|
|
439
|
+
.map((child) => child.name === "ac:plain-text-link-body"
|
|
440
|
+
? text(rawText(child))
|
|
441
|
+
: inlineChildren(child, depth + 1))
|
|
442
|
+
.join("");
|
|
443
|
+
return shown !== "" ? shown : inlineChildren(element, depth);
|
|
444
|
+
}
|
|
445
|
+
/** A link's own words, with an address kept beside them but never travelled. */
|
|
446
|
+
function link(element, depth) {
|
|
447
|
+
const url = childElements(element).find((child) => child.name === "ri:url");
|
|
448
|
+
const href = url === undefined ? "" : attr(url, "ri:value");
|
|
449
|
+
const body = collapseSpaces(linkText(element, depth)).trim();
|
|
450
|
+
if (body === "")
|
|
451
|
+
return href === "" ? "[link]" : href;
|
|
452
|
+
return href === "" ? body : `[${body}](${href})`;
|
|
453
|
+
}
|
|
454
|
+
/** One inline child: a text run, or a tag the inline renderer knows. */
|
|
455
|
+
function inlineChild(child, depth) {
|
|
456
|
+
if (depth > MAX_DEPTH)
|
|
457
|
+
return "";
|
|
458
|
+
return typeof child === "string" ? text(child) : inline(child, depth);
|
|
459
|
+
}
|
|
460
|
+
/** Inline children joined without separators, each carrying its own marks. */
|
|
461
|
+
function inlineChildren(element, depth) {
|
|
462
|
+
return element.children
|
|
463
|
+
.map((child) => inlineChild(child, depth + 1))
|
|
464
|
+
.join("");
|
|
465
|
+
}
|
|
466
|
+
/** An empty wrapper would only leave stray marks in the answer. */
|
|
467
|
+
function wrap(inner, open, close) {
|
|
468
|
+
return inner === "" ? "" : `${open}${inner}${close}`;
|
|
469
|
+
}
|
|
470
|
+
/** One inline tag as text; an unknown one keeps its words and loses its tag. */
|
|
471
|
+
function inline(element, depth) {
|
|
472
|
+
if (depth > MAX_DEPTH)
|
|
473
|
+
return "";
|
|
474
|
+
switch (element.name) {
|
|
475
|
+
case "strong":
|
|
476
|
+
case "b":
|
|
477
|
+
return wrap(inlineChildren(element, depth), "**", "**");
|
|
478
|
+
case "em":
|
|
479
|
+
case "i":
|
|
480
|
+
return wrap(inlineChildren(element, depth), "_", "_");
|
|
481
|
+
case "s":
|
|
482
|
+
case "del":
|
|
483
|
+
case "strike":
|
|
484
|
+
return wrap(inlineChildren(element, depth), "~~", "~~");
|
|
485
|
+
case "code":
|
|
486
|
+
return wrap(text(rawText(element)).trim(), "`", "`");
|
|
487
|
+
case "br":
|
|
488
|
+
return "\n";
|
|
489
|
+
case "a": {
|
|
490
|
+
const href = attr(element, "href");
|
|
491
|
+
const inner = inlineChildren(element, depth);
|
|
492
|
+
if (inner === "")
|
|
493
|
+
return href;
|
|
494
|
+
return href === "" ? inner : `[${inner}](${href})`;
|
|
495
|
+
}
|
|
496
|
+
// A macro's parameters are its configuration, not the page's words.
|
|
497
|
+
case "ac:parameter":
|
|
498
|
+
return "";
|
|
499
|
+
case "ac:image":
|
|
500
|
+
return image(element);
|
|
501
|
+
case "ac:link":
|
|
502
|
+
return link(element, depth);
|
|
503
|
+
case "ac:structured-macro":
|
|
504
|
+
return macro(element, depth);
|
|
505
|
+
case "ri:attachment":
|
|
506
|
+
return marker("attachment", attr(element, "ri:filename"));
|
|
507
|
+
case "ri:page": {
|
|
508
|
+
const title = attr(element, "ri:content-title") || attr(element, "ri:page-title");
|
|
509
|
+
return marker("page", title);
|
|
510
|
+
}
|
|
511
|
+
case "ri:space":
|
|
512
|
+
return marker("space", attr(element, "ri:space-key"));
|
|
513
|
+
case "ri:user": {
|
|
514
|
+
// A mention is a name: the provider does not resolve an account for it.
|
|
515
|
+
const name = attr(element, "ri:username") || text(rawText(element)).trim();
|
|
516
|
+
return name === "" ? "@" : `@${name}`;
|
|
517
|
+
}
|
|
518
|
+
default:
|
|
519
|
+
return inlineChildren(element, depth);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
/** Inline content of a block, as one run of text with its edges tightened. */
|
|
523
|
+
function inlineText(element, depth) {
|
|
524
|
+
return collapseSpaces(inlineChildren(element, depth)).trim();
|
|
525
|
+
}
|
|
526
|
+
/** An item's first line, followed by the blocks that come after it. */
|
|
527
|
+
function itemBlocks(item, depth) {
|
|
528
|
+
const head = [];
|
|
529
|
+
let index = 0;
|
|
530
|
+
while (index < item.children.length) {
|
|
531
|
+
const child = item.children[index];
|
|
532
|
+
if (child === undefined)
|
|
533
|
+
break;
|
|
534
|
+
const part = typeof child === "string"
|
|
535
|
+
? text(child)
|
|
536
|
+
: INLINE_TAGS.has(child.name)
|
|
537
|
+
? inline(child, depth + 1)
|
|
538
|
+
: undefined;
|
|
539
|
+
if (part === undefined)
|
|
540
|
+
break;
|
|
541
|
+
head.push(part);
|
|
542
|
+
index += 1;
|
|
543
|
+
}
|
|
544
|
+
const rest = item.children
|
|
545
|
+
.slice(index)
|
|
546
|
+
.map((child) => typeof child === "string" ? text(child).trim() : block(child, depth));
|
|
547
|
+
return [collapseSpaces(head.join("")).trim(), ...rest];
|
|
548
|
+
}
|
|
549
|
+
/** List items, with the text aligned under the marker of each item. */
|
|
550
|
+
function listItems(element, depth, ordered) {
|
|
551
|
+
const indent = " ".repeat(depth);
|
|
552
|
+
return childElements(element)
|
|
553
|
+
.filter((item) => item.name === "li")
|
|
554
|
+
.map((item, index) => {
|
|
555
|
+
const marker = ordered ? `${index + 1}. ` : "- ";
|
|
556
|
+
const pad = " ".repeat(marker.length);
|
|
557
|
+
const lines = itemBlocks(item, depth).filter((line) => line !== "");
|
|
558
|
+
if (lines.length === 0)
|
|
559
|
+
return "";
|
|
560
|
+
return lines
|
|
561
|
+
.join("\n")
|
|
562
|
+
.split("\n")
|
|
563
|
+
.map((line, position) => position === 0
|
|
564
|
+
? `${indent}${marker}${line}`
|
|
565
|
+
: `${indent}${pad}${line}`)
|
|
566
|
+
.join("\n");
|
|
567
|
+
})
|
|
568
|
+
.filter((item) => item !== "")
|
|
569
|
+
.join("\n");
|
|
570
|
+
}
|
|
571
|
+
/** One cell of a table row, with the text a cell may not carry removed. */
|
|
572
|
+
function cellText(cell, depth) {
|
|
573
|
+
const inner = cell.children
|
|
574
|
+
.map((child) => typeof child === "string" ? text(child).trim() : block(child, depth + 1))
|
|
575
|
+
.filter((part) => part !== "")
|
|
576
|
+
.join(" ");
|
|
577
|
+
// A pipe would end the cell early, and a line break has no meaning inside one.
|
|
578
|
+
return collapseSpaces(inner).replaceAll("|", "\\|").replaceAll("\n", " ");
|
|
579
|
+
}
|
|
580
|
+
/** The rows of a table, whether or not the body wrapped them in a section. */
|
|
581
|
+
function rowsOf(element) {
|
|
582
|
+
const rows = [];
|
|
583
|
+
for (const child of childElements(element)) {
|
|
584
|
+
if (child.name === "tr")
|
|
585
|
+
rows.push(child);
|
|
586
|
+
else if (child.name === "thead" ||
|
|
587
|
+
child.name === "tbody" ||
|
|
588
|
+
child.name === "tfoot") {
|
|
589
|
+
rows.push(...childElements(child).filter((row) => row.name === "tr"));
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
return rows;
|
|
593
|
+
}
|
|
594
|
+
/** Rows as `| a | b |`, with a separator under a row that is all headers. */
|
|
595
|
+
function table(element, depth) {
|
|
596
|
+
const lines = [];
|
|
597
|
+
rowsOf(element).forEach((row, index) => {
|
|
598
|
+
const cells = childElements(row).filter((cell) => cell.name === "td" || cell.name === "th");
|
|
599
|
+
if (cells.length === 0)
|
|
600
|
+
return;
|
|
601
|
+
const rendered = cells.map((cell) => cellText(cell, depth + 1));
|
|
602
|
+
lines.push(`| ${rendered.join(" | ")} |`);
|
|
603
|
+
const header = index === 0 && cells.every((cell) => cell.name === "th");
|
|
604
|
+
if (header)
|
|
605
|
+
lines.push(`| ${rendered.map(() => "---").join(" | ")} |`);
|
|
606
|
+
});
|
|
607
|
+
return lines.join("\n");
|
|
608
|
+
}
|
|
609
|
+
/** One block-level tag as text; an empty string when it carries nothing. */
|
|
610
|
+
function block(element, depth) {
|
|
611
|
+
if (depth > MAX_DEPTH)
|
|
612
|
+
return "";
|
|
613
|
+
switch (element.name) {
|
|
614
|
+
case "p":
|
|
615
|
+
return inlineText(element, depth);
|
|
616
|
+
case "h1":
|
|
617
|
+
case "h2":
|
|
618
|
+
case "h3":
|
|
619
|
+
case "h4":
|
|
620
|
+
case "h5":
|
|
621
|
+
case "h6": {
|
|
622
|
+
const level = Number(element.name.slice(1));
|
|
623
|
+
return `${"#".repeat(level)} ${inlineText(element, depth)}`;
|
|
624
|
+
}
|
|
625
|
+
case "ul":
|
|
626
|
+
return listItems(element, depth, false);
|
|
627
|
+
case "ol":
|
|
628
|
+
return listItems(element, depth, true);
|
|
629
|
+
case "li":
|
|
630
|
+
// A stray item outside a list still reads as an item.
|
|
631
|
+
return `${" ".repeat(depth)}- ${inlineText(element, depth)}`;
|
|
632
|
+
case "table":
|
|
633
|
+
return table(element, depth);
|
|
634
|
+
case "blockquote":
|
|
635
|
+
return quote(element, depth);
|
|
636
|
+
case "pre":
|
|
637
|
+
return fence("", codeOf(element));
|
|
638
|
+
case "hr":
|
|
639
|
+
return "---";
|
|
640
|
+
case "br":
|
|
641
|
+
return "";
|
|
642
|
+
case "ac:structured-macro":
|
|
643
|
+
return macro(element, depth);
|
|
644
|
+
case "ac:parameter":
|
|
645
|
+
return "";
|
|
646
|
+
case "ac:plain-text-body":
|
|
647
|
+
return codeOf(element);
|
|
648
|
+
default:
|
|
649
|
+
if (INLINE_TAGS.has(element.name))
|
|
650
|
+
return inline(element, depth);
|
|
651
|
+
// An unknown block: keep the words, drop the wrapper we cannot vouch for.
|
|
652
|
+
return blocks(element.children, depth + 1).join("\n\n");
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
/** A quote as a prefixed block, its blank lines prefixed too. */
|
|
656
|
+
function quote(element, depth) {
|
|
657
|
+
return blocks(element.children, depth + 1)
|
|
658
|
+
.join("\n\n")
|
|
659
|
+
.split("\n")
|
|
660
|
+
.map((line) => `> ${line}`)
|
|
661
|
+
.join("\n");
|
|
662
|
+
}
|
|
663
|
+
function blocks(children, depth) {
|
|
664
|
+
if (depth > MAX_DEPTH)
|
|
665
|
+
return [];
|
|
666
|
+
return children
|
|
667
|
+
.map((child) => typeof child === "string" ? text(child).trim() : block(child, depth))
|
|
668
|
+
.filter((part) => part !== "");
|
|
669
|
+
}
|
|
670
|
+
/** Whether a value looks like a storage body rather than a plain string. */
|
|
671
|
+
export function isStorage(value) {
|
|
672
|
+
if (typeof value !== "string")
|
|
673
|
+
return false;
|
|
674
|
+
// A Cloud body arrives as JSON, handled by `adf.ts`, and never starts with a
|
|
675
|
+
// tag; the first character that is not a space decides.
|
|
676
|
+
return /^\s*<[A-Za-z]/u.test(value);
|
|
677
|
+
}
|
|
678
|
+
/**
|
|
679
|
+
* Render one storage-format body to markdown-like text, or "" when there is
|
|
680
|
+
* nothing to render.
|
|
681
|
+
*/
|
|
682
|
+
export function storageToText(value) {
|
|
683
|
+
if (typeof value !== "string" || value.trim() === "")
|
|
684
|
+
return "";
|
|
685
|
+
return blocks(parseMarkup(value).children, 0)
|
|
686
|
+
.join("\n\n")
|
|
687
|
+
.replace(/\n{3,}/gu, "\n\n")
|
|
688
|
+
.trim();
|
|
689
|
+
}
|
|
690
|
+
//# sourceMappingURL=storage.js.map
|