@xwiki/cristal-uniast-html 0.22.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/CHANGELOG.md +13 -0
- package/LICENSE +505 -0
- package/api-extractor.json +4 -0
- package/dist/component-init.d.ts +14 -0
- package/dist/html/default-uni-ast-to-html-converter.d.ts +21 -0
- package/dist/html/uni-ast-to-html-converter.d.ts +10 -0
- package/dist/index.d.cts +21 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.es.js +140 -0
- package/dist/index.es.js.map +1 -0
- package/dist/index.umd.js +3 -0
- package/dist/index.umd.js.map +1 -0
- package/etc/cristal-uniast-html.api.md +26 -0
- package/package.json +56 -0
- package/src/component-init.ts +43 -0
- package/src/html/__tests__/uniast-to-html.test.ts +394 -0
- package/src/html/default-uni-ast-to-html-converter.ts +251 -0
- package/src/html/uni-ast-to-html-converter.ts +30 -0
- package/src/index.ts +21 -0
- package/tsconfig.json +15 -0
- package/tsdoc.json +4 -0
- package/vite.config.ts +23 -0
- package/vitest.config.ts +25 -0
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* See the LICENSE file distributed with this work for additional
|
|
3
|
+
* information regarding copyright ownership.
|
|
4
|
+
*
|
|
5
|
+
* This is free software; you can redistribute it and/or modify it
|
|
6
|
+
* under the terms of the GNU Lesser General Public License as
|
|
7
|
+
* published by the Free Software Foundation; either version 2.1 of
|
|
8
|
+
* the License, or (at your option) any later version.
|
|
9
|
+
*
|
|
10
|
+
* This software is distributed in the hope that it will be useful,
|
|
11
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
13
|
+
* Lesser General Public License for more details.
|
|
14
|
+
*
|
|
15
|
+
* You should have received a copy of the GNU Lesser General Public
|
|
16
|
+
* License along with this software; if not, write to the Free
|
|
17
|
+
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
|
18
|
+
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { DefaultUniAstToHTMLConverter } from "../default-uni-ast-to-html-converter";
|
|
22
|
+
import {
|
|
23
|
+
AttachmentReference,
|
|
24
|
+
DocumentReference,
|
|
25
|
+
SpaceReference,
|
|
26
|
+
} from "@xwiki/cristal-model-api";
|
|
27
|
+
import { Container } from "inversify";
|
|
28
|
+
import { describe, expect, test } from "vitest";
|
|
29
|
+
import { any, mock } from "vitest-mock-extended";
|
|
30
|
+
import type {
|
|
31
|
+
ModelReferenceHandlerProvider,
|
|
32
|
+
ModelReferenceParser,
|
|
33
|
+
ModelReferenceParserProvider,
|
|
34
|
+
ModelReferenceSerializerProvider,
|
|
35
|
+
} from "@xwiki/cristal-model-reference-api";
|
|
36
|
+
import type {
|
|
37
|
+
RemoteURLParserProvider,
|
|
38
|
+
RemoteURLSerializer,
|
|
39
|
+
RemoteURLSerializerProvider,
|
|
40
|
+
} from "@xwiki/cristal-model-remote-url-api";
|
|
41
|
+
|
|
42
|
+
// eslint-disable-next-line max-statements
|
|
43
|
+
function init() {
|
|
44
|
+
const modelReferenceParserProvider = mock<ModelReferenceParserProvider>();
|
|
45
|
+
const remoteURLSerializerProvider = mock<RemoteURLSerializerProvider>();
|
|
46
|
+
const modelReferenceParser = mock<ModelReferenceParser>();
|
|
47
|
+
const remoteURLSerializer = mock<RemoteURLSerializer>();
|
|
48
|
+
|
|
49
|
+
const containerMock = mock<Container>();
|
|
50
|
+
|
|
51
|
+
containerMock.get
|
|
52
|
+
.calledWith("ModelReferenceParserProvider")
|
|
53
|
+
.mockReturnValue(modelReferenceParserProvider);
|
|
54
|
+
|
|
55
|
+
containerMock.get
|
|
56
|
+
.calledWith("ModelReferenceSerializerProvider")
|
|
57
|
+
.mockReturnValue(mock<ModelReferenceSerializerProvider>());
|
|
58
|
+
|
|
59
|
+
containerMock.get
|
|
60
|
+
.calledWith("RemoteURLParserProvider")
|
|
61
|
+
.mockReturnValue(mock<RemoteURLParserProvider>());
|
|
62
|
+
containerMock.get
|
|
63
|
+
.calledWith("RemoteURLSerializerProvider")
|
|
64
|
+
.mockReturnValue(remoteURLSerializerProvider);
|
|
65
|
+
|
|
66
|
+
containerMock.get
|
|
67
|
+
.calledWith("ModelReferenceHandlerProvider")
|
|
68
|
+
.mockReturnValue(mock<ModelReferenceHandlerProvider>());
|
|
69
|
+
|
|
70
|
+
modelReferenceParserProvider.get.mockReturnValue(modelReferenceParser);
|
|
71
|
+
remoteURLSerializerProvider.get.mockReturnValue(remoteURLSerializer);
|
|
72
|
+
|
|
73
|
+
const attachmentReference = new AttachmentReference(
|
|
74
|
+
"image.png",
|
|
75
|
+
new DocumentReference("B", new SpaceReference(undefined, "A")),
|
|
76
|
+
);
|
|
77
|
+
modelReferenceParser.parse
|
|
78
|
+
.calledWith("A.B@image.png", any())
|
|
79
|
+
.mockReturnValue(attachmentReference);
|
|
80
|
+
|
|
81
|
+
remoteURLSerializer.serialize
|
|
82
|
+
.calledWith(attachmentReference)
|
|
83
|
+
.mockReturnValue("https://my.site/A/B/image.png");
|
|
84
|
+
|
|
85
|
+
return { remoteURLSerializerProvider, modelReferenceParserProvider };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// eslint-disable-next-line max-statements
|
|
89
|
+
describe("UniAstToHTMLConverter", () => {
|
|
90
|
+
const { remoteURLSerializerProvider, modelReferenceParserProvider } = init();
|
|
91
|
+
const uniAstToHTMLConverter = new DefaultUniAstToHTMLConverter(
|
|
92
|
+
remoteURLSerializerProvider,
|
|
93
|
+
modelReferenceParserProvider,
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
test("empty ast", () => {
|
|
97
|
+
const res = uniAstToHTMLConverter.toHtml({ blocks: [] });
|
|
98
|
+
expect(res).toBe("");
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test("simple text", () => {
|
|
102
|
+
const res = uniAstToHTMLConverter.toHtml({
|
|
103
|
+
blocks: [
|
|
104
|
+
{
|
|
105
|
+
type: "paragraph",
|
|
106
|
+
styles: {},
|
|
107
|
+
content: [
|
|
108
|
+
{
|
|
109
|
+
type: "text",
|
|
110
|
+
styles: {},
|
|
111
|
+
content: "test",
|
|
112
|
+
},
|
|
113
|
+
],
|
|
114
|
+
},
|
|
115
|
+
],
|
|
116
|
+
});
|
|
117
|
+
expect(res).toBe("<p>test</p>");
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
test("bold text", () => {
|
|
121
|
+
const res = uniAstToHTMLConverter.toHtml({
|
|
122
|
+
blocks: [
|
|
123
|
+
{
|
|
124
|
+
type: "paragraph",
|
|
125
|
+
styles: {},
|
|
126
|
+
content: [
|
|
127
|
+
{
|
|
128
|
+
type: "text",
|
|
129
|
+
styles: {
|
|
130
|
+
bold: true,
|
|
131
|
+
},
|
|
132
|
+
content: "test",
|
|
133
|
+
},
|
|
134
|
+
],
|
|
135
|
+
},
|
|
136
|
+
],
|
|
137
|
+
});
|
|
138
|
+
expect(res).toBe("<p><strong>test</strong></p>");
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test("strikethrough and italic text", () => {
|
|
142
|
+
const res = uniAstToHTMLConverter.toHtml({
|
|
143
|
+
blocks: [
|
|
144
|
+
{
|
|
145
|
+
type: "paragraph",
|
|
146
|
+
styles: {},
|
|
147
|
+
content: [
|
|
148
|
+
{
|
|
149
|
+
type: "text",
|
|
150
|
+
styles: {
|
|
151
|
+
strikethrough: true,
|
|
152
|
+
italic: true,
|
|
153
|
+
},
|
|
154
|
+
content: "test",
|
|
155
|
+
},
|
|
156
|
+
],
|
|
157
|
+
},
|
|
158
|
+
],
|
|
159
|
+
});
|
|
160
|
+
expect(res).toBe("<p><s><em>test</em></s></p>");
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test("text with special char", () => {
|
|
164
|
+
const res = uniAstToHTMLConverter.toHtml({
|
|
165
|
+
blocks: [
|
|
166
|
+
{
|
|
167
|
+
type: "paragraph",
|
|
168
|
+
styles: {},
|
|
169
|
+
content: [
|
|
170
|
+
{
|
|
171
|
+
type: "text",
|
|
172
|
+
styles: {},
|
|
173
|
+
content: "<bold>escape me </bold>",
|
|
174
|
+
},
|
|
175
|
+
],
|
|
176
|
+
},
|
|
177
|
+
],
|
|
178
|
+
});
|
|
179
|
+
expect(res).toBe("<p><bold>escape me </bold></p>");
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test("text with special char", () => {
|
|
183
|
+
const res = uniAstToHTMLConverter.toHtml({
|
|
184
|
+
blocks: [
|
|
185
|
+
{
|
|
186
|
+
type: "paragraph",
|
|
187
|
+
styles: {},
|
|
188
|
+
content: [
|
|
189
|
+
{
|
|
190
|
+
type: "text",
|
|
191
|
+
styles: {},
|
|
192
|
+
content: "<bold>escape me </bold>",
|
|
193
|
+
},
|
|
194
|
+
],
|
|
195
|
+
},
|
|
196
|
+
],
|
|
197
|
+
});
|
|
198
|
+
expect(res).toBe("<p><bold>escape me </bold></p>");
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
test.each([1, 2, 3, 4, 5, 6])("Heading level $level", (level) => {
|
|
202
|
+
expect(
|
|
203
|
+
uniAstToHTMLConverter.toHtml({
|
|
204
|
+
// @ts-expect-error level is an union but we know the provided values are fine.
|
|
205
|
+
blocks: [{ type: "heading", level, content: [], styles: {} }],
|
|
206
|
+
}),
|
|
207
|
+
).toBe(`<h${level}></h${level}>`);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
test("list", () => {
|
|
211
|
+
const res = uniAstToHTMLConverter.toHtml({
|
|
212
|
+
blocks: [
|
|
213
|
+
{
|
|
214
|
+
type: "list",
|
|
215
|
+
styles: {},
|
|
216
|
+
items: [
|
|
217
|
+
{
|
|
218
|
+
content: [
|
|
219
|
+
{
|
|
220
|
+
type: "paragraph",
|
|
221
|
+
content: [
|
|
222
|
+
{
|
|
223
|
+
type: "text",
|
|
224
|
+
content: "item A",
|
|
225
|
+
styles: {},
|
|
226
|
+
},
|
|
227
|
+
{
|
|
228
|
+
type: "text",
|
|
229
|
+
content: "item A bis",
|
|
230
|
+
styles: {},
|
|
231
|
+
},
|
|
232
|
+
],
|
|
233
|
+
styles: {},
|
|
234
|
+
},
|
|
235
|
+
],
|
|
236
|
+
styles: {},
|
|
237
|
+
},
|
|
238
|
+
{
|
|
239
|
+
content: [
|
|
240
|
+
{
|
|
241
|
+
type: "paragraph",
|
|
242
|
+
content: [
|
|
243
|
+
{
|
|
244
|
+
type: "text",
|
|
245
|
+
content: "item B",
|
|
246
|
+
styles: {},
|
|
247
|
+
},
|
|
248
|
+
],
|
|
249
|
+
styles: {},
|
|
250
|
+
},
|
|
251
|
+
],
|
|
252
|
+
styles: {},
|
|
253
|
+
},
|
|
254
|
+
],
|
|
255
|
+
},
|
|
256
|
+
],
|
|
257
|
+
});
|
|
258
|
+
expect(res).toBe(
|
|
259
|
+
"<ul><li><p>item Aitem A bis</p></li><li><p>item B</p></li></ul>",
|
|
260
|
+
);
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
test("quote", () => {
|
|
264
|
+
const res = uniAstToHTMLConverter.toHtml({
|
|
265
|
+
blocks: [
|
|
266
|
+
{
|
|
267
|
+
type: "quote",
|
|
268
|
+
styles: {},
|
|
269
|
+
content: [
|
|
270
|
+
{
|
|
271
|
+
type: "paragraph",
|
|
272
|
+
content: [
|
|
273
|
+
{
|
|
274
|
+
type: "text",
|
|
275
|
+
content: "item A",
|
|
276
|
+
styles: {},
|
|
277
|
+
},
|
|
278
|
+
],
|
|
279
|
+
styles: {},
|
|
280
|
+
},
|
|
281
|
+
],
|
|
282
|
+
},
|
|
283
|
+
],
|
|
284
|
+
});
|
|
285
|
+
expect(res).toBe("<blockquote><p>item A</p></blockquote>");
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
test("code", () => {
|
|
289
|
+
const res = uniAstToHTMLConverter.toHtml({
|
|
290
|
+
blocks: [
|
|
291
|
+
{
|
|
292
|
+
type: "code",
|
|
293
|
+
language: "typescript",
|
|
294
|
+
content: "console.log('hello world')",
|
|
295
|
+
},
|
|
296
|
+
],
|
|
297
|
+
});
|
|
298
|
+
expect(res).toBe("<pre>console.log('hello world')</pre>");
|
|
299
|
+
});
|
|
300
|
+
test("table", () => {
|
|
301
|
+
const res = uniAstToHTMLConverter.toHtml({
|
|
302
|
+
blocks: [
|
|
303
|
+
{
|
|
304
|
+
type: "table",
|
|
305
|
+
columns: [
|
|
306
|
+
{
|
|
307
|
+
headerCell: {
|
|
308
|
+
content: [
|
|
309
|
+
{
|
|
310
|
+
type: "text",
|
|
311
|
+
content: "header 1",
|
|
312
|
+
styles: {},
|
|
313
|
+
},
|
|
314
|
+
],
|
|
315
|
+
styles: {},
|
|
316
|
+
},
|
|
317
|
+
},
|
|
318
|
+
],
|
|
319
|
+
rows: [
|
|
320
|
+
[
|
|
321
|
+
{
|
|
322
|
+
content: [
|
|
323
|
+
{
|
|
324
|
+
type: "text",
|
|
325
|
+
content: "cell 1",
|
|
326
|
+
styles: {},
|
|
327
|
+
},
|
|
328
|
+
],
|
|
329
|
+
styles: {},
|
|
330
|
+
},
|
|
331
|
+
],
|
|
332
|
+
],
|
|
333
|
+
styles: {},
|
|
334
|
+
},
|
|
335
|
+
],
|
|
336
|
+
});
|
|
337
|
+
expect(res).toBe(
|
|
338
|
+
"<table><thead><th>header 1</th></thead><tbody><tr><td>cell 1</td></tr></tbody></table>",
|
|
339
|
+
);
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
test("external image", () => {
|
|
343
|
+
const res = uniAstToHTMLConverter.toHtml({
|
|
344
|
+
blocks: [
|
|
345
|
+
{
|
|
346
|
+
type: "image",
|
|
347
|
+
target: { type: "external", url: "https://my.site/image.png" },
|
|
348
|
+
styles: {},
|
|
349
|
+
},
|
|
350
|
+
],
|
|
351
|
+
});
|
|
352
|
+
expect(res).toBe('<img src="https://my.site/image.png" alt="">');
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
test("external image with alt", () => {
|
|
356
|
+
const res = uniAstToHTMLConverter.toHtml({
|
|
357
|
+
blocks: [
|
|
358
|
+
{
|
|
359
|
+
type: "image",
|
|
360
|
+
target: { type: "external", url: "https://my.site/image.png" },
|
|
361
|
+
// caption?
|
|
362
|
+
alt: "image alt",
|
|
363
|
+
// widthPx?
|
|
364
|
+
// heightPx?
|
|
365
|
+
styles: {},
|
|
366
|
+
},
|
|
367
|
+
],
|
|
368
|
+
});
|
|
369
|
+
expect(res).toBe('<img src="https://my.site/image.png" alt="image alt">');
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
test("internal image", () => {
|
|
373
|
+
const res = uniAstToHTMLConverter.toHtml({
|
|
374
|
+
blocks: [
|
|
375
|
+
{
|
|
376
|
+
type: "image",
|
|
377
|
+
target: {
|
|
378
|
+
type: "internal",
|
|
379
|
+
rawReference: "A.B@image.png",
|
|
380
|
+
parsedReference: null,
|
|
381
|
+
},
|
|
382
|
+
// caption?
|
|
383
|
+
alt: "image alt",
|
|
384
|
+
// widthPx?
|
|
385
|
+
// heightPx?
|
|
386
|
+
styles: {},
|
|
387
|
+
},
|
|
388
|
+
],
|
|
389
|
+
});
|
|
390
|
+
expect(res).toBe(
|
|
391
|
+
'<img src="https://my.site/A/B/image.png" alt="image alt">',
|
|
392
|
+
);
|
|
393
|
+
});
|
|
394
|
+
});
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* See the LICENSE file distributed with this work for additional
|
|
3
|
+
* information regarding copyright ownership.
|
|
4
|
+
*
|
|
5
|
+
* This is free software; you can redistribute it and/or modify it
|
|
6
|
+
* under the terms of the GNU Lesser General Public License as
|
|
7
|
+
* published by the Free Software Foundation; either version 2.1 of
|
|
8
|
+
* the License, or (at your option) any later version.
|
|
9
|
+
*
|
|
10
|
+
* This software is distributed in the hope that it will be useful,
|
|
11
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
13
|
+
* Lesser General Public License for more details.
|
|
14
|
+
*
|
|
15
|
+
* You should have received a copy of the GNU Lesser General Public
|
|
16
|
+
* License along with this software; if not, write to the Free
|
|
17
|
+
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
|
18
|
+
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
|
|
19
|
+
*/
|
|
20
|
+
import { EntityType } from "@xwiki/cristal-model-api";
|
|
21
|
+
import { inject, injectable } from "inversify";
|
|
22
|
+
import { escape } from "lodash-es";
|
|
23
|
+
import type { UniAstToHTMLConverter } from "./uni-ast-to-html-converter";
|
|
24
|
+
import type { EntityReference } from "@xwiki/cristal-model-api";
|
|
25
|
+
import type { ModelReferenceParserProvider } from "@xwiki/cristal-model-reference-api";
|
|
26
|
+
import type { RemoteURLSerializerProvider } from "@xwiki/cristal-model-remote-url-api";
|
|
27
|
+
import type {
|
|
28
|
+
Block,
|
|
29
|
+
Image,
|
|
30
|
+
InlineContent,
|
|
31
|
+
ListItem,
|
|
32
|
+
TableCell,
|
|
33
|
+
Text,
|
|
34
|
+
UniAst,
|
|
35
|
+
} from "@xwiki/cristal-uniast-api";
|
|
36
|
+
|
|
37
|
+
@injectable()
|
|
38
|
+
export class DefaultUniAstToHTMLConverter implements UniAstToHTMLConverter {
|
|
39
|
+
constructor(
|
|
40
|
+
@inject("RemoteURLSerializerProvider")
|
|
41
|
+
private readonly remoteURLSerializerProvider: RemoteURLSerializerProvider,
|
|
42
|
+
@inject("ModelReferenceParserProvider")
|
|
43
|
+
private readonly modelReferenceParserProvider: ModelReferenceParserProvider,
|
|
44
|
+
) {}
|
|
45
|
+
|
|
46
|
+
toHtml(uniAst: UniAst): string | Error {
|
|
47
|
+
const { blocks } = uniAst;
|
|
48
|
+
|
|
49
|
+
const out: string[] = [];
|
|
50
|
+
|
|
51
|
+
for (const block of blocks) {
|
|
52
|
+
try {
|
|
53
|
+
out.push(this.blockToHTML(block));
|
|
54
|
+
} catch (e) {
|
|
55
|
+
console.error(e);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return out.join("\n");
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
private blockToHTML(block: Block): string {
|
|
63
|
+
switch (block.type) {
|
|
64
|
+
case "paragraph":
|
|
65
|
+
return `<p>${this.convertInlineContents(block.content)}</p>`;
|
|
66
|
+
|
|
67
|
+
case "heading":
|
|
68
|
+
return `<h${block.level}>${this.convertInlineContents(block.content)}</h${block.level}>`;
|
|
69
|
+
|
|
70
|
+
case "list": {
|
|
71
|
+
const tag = block.items[0]?.number ? "ol" : "ul";
|
|
72
|
+
return `<${tag}>${block.items
|
|
73
|
+
.map((item) => this.convertListItem(item))
|
|
74
|
+
.join("")}</${tag}>`;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
case "quote": {
|
|
78
|
+
const blockquoteContent = block.content
|
|
79
|
+
.map((item) => this.blockToHTML(item))
|
|
80
|
+
?.join("");
|
|
81
|
+
return `<blockquote>${blockquoteContent}</blockquote>`;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
case "code":
|
|
85
|
+
// TODO: support for syntax highlighting
|
|
86
|
+
return `<pre>${this.escapeHTML(block.content)}</pre>`;
|
|
87
|
+
|
|
88
|
+
case "table":
|
|
89
|
+
return this.convertTable(block);
|
|
90
|
+
|
|
91
|
+
case "image":
|
|
92
|
+
return this.convertImage(block);
|
|
93
|
+
|
|
94
|
+
case "break":
|
|
95
|
+
return "<hr>";
|
|
96
|
+
|
|
97
|
+
case "macroBlock":
|
|
98
|
+
// TODO: currently unsupported
|
|
99
|
+
return "";
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
private convertListItem(listItem: ListItem): string {
|
|
104
|
+
return `<li>${listItem.content.map((item) => this.blockToHTML(item)).join("")}</li>`;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private convertImage(image: Image): string {
|
|
108
|
+
const target = image.target;
|
|
109
|
+
let srcValue: string;
|
|
110
|
+
if (target.type === "external") {
|
|
111
|
+
srcValue = escape(target.url);
|
|
112
|
+
} else if (target.parsedReference !== null) {
|
|
113
|
+
srcValue = escape(
|
|
114
|
+
this.remoteURLSerializerProvider
|
|
115
|
+
.get()!
|
|
116
|
+
.serialize(target.parsedReference),
|
|
117
|
+
);
|
|
118
|
+
} else {
|
|
119
|
+
srcValue = escape(
|
|
120
|
+
this.convertReference(target.rawReference, EntityType.ATTACHMENT),
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
const altValue: string = escape(image.alt) ?? "";
|
|
124
|
+
return `<img src="${srcValue}" alt="${altValue}">`;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
private convertTable(table: Extract<Block, { type: "table" }>): string {
|
|
128
|
+
const { columns, rows } = table;
|
|
129
|
+
|
|
130
|
+
const ths: string = columns
|
|
131
|
+
.map(
|
|
132
|
+
(column) =>
|
|
133
|
+
`<th>${column.headerCell ? this.convertTableCell(column.headerCell) : ""}</th>`,
|
|
134
|
+
)
|
|
135
|
+
.join("");
|
|
136
|
+
|
|
137
|
+
const trs: string = rows
|
|
138
|
+
.map((row) =>
|
|
139
|
+
row.map((cell) => `<td>${this.convertTableCell(cell)}</td>`).join(""),
|
|
140
|
+
)
|
|
141
|
+
.map((row) => `<tr>${row}</tr>`)
|
|
142
|
+
.join("");
|
|
143
|
+
|
|
144
|
+
return `<table><thead>${ths}</thead><tbody>${trs}</tbody></table>`;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
private convertTableCell(cell: TableCell): string {
|
|
148
|
+
return this.convertInlineContents(cell.content);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
private convertInlineContents(inlineContents: InlineContent[]): string {
|
|
152
|
+
return inlineContents
|
|
153
|
+
.map((item) => this.convertInlineContent(item))
|
|
154
|
+
.join("");
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
private convertInlineContent(inlineContent: InlineContent): string {
|
|
158
|
+
switch (inlineContent.type) {
|
|
159
|
+
case "text":
|
|
160
|
+
return this.convertText(inlineContent);
|
|
161
|
+
|
|
162
|
+
case "image":
|
|
163
|
+
return this.convertImage(inlineContent);
|
|
164
|
+
|
|
165
|
+
case "link": {
|
|
166
|
+
const linkContent = this.convertInlineContents(inlineContent.content);
|
|
167
|
+
switch (inlineContent.target.type) {
|
|
168
|
+
case "external":
|
|
169
|
+
return `<a href="${escape(inlineContent.target.url)}" class="wikiexternallink">${linkContent}</a>`;
|
|
170
|
+
|
|
171
|
+
case "internal": {
|
|
172
|
+
// TODO: convert reference
|
|
173
|
+
|
|
174
|
+
const href = inlineContent.target.parsedReference
|
|
175
|
+
? this.serializeReference(inlineContent.target.parsedReference)
|
|
176
|
+
: this.convertReference(
|
|
177
|
+
inlineContent.target.rawReference,
|
|
178
|
+
EntityType.DOCUMENT,
|
|
179
|
+
);
|
|
180
|
+
return `<a href="${escape(href)}">${linkContent}</a>`;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
case "inlineMacro":
|
|
187
|
+
// TODO: currently unsupported
|
|
188
|
+
return "";
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// eslint-disable-next-line max-statements
|
|
193
|
+
private convertText(text: Text): string {
|
|
194
|
+
const { content, styles } = text;
|
|
195
|
+
|
|
196
|
+
const { bold, italic, strikethrough, code } = styles;
|
|
197
|
+
|
|
198
|
+
const surroundings = [];
|
|
199
|
+
|
|
200
|
+
// Code must be first as it's going to be the most outer surrounding
|
|
201
|
+
// Otherwise other surroundings would be "trapped" inside the inline code content
|
|
202
|
+
if (code) {
|
|
203
|
+
surroundings.push("pre");
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (strikethrough) {
|
|
207
|
+
surroundings.push("s");
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (italic) {
|
|
211
|
+
surroundings.push("em");
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (bold) {
|
|
215
|
+
surroundings.push("strong");
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
let output = "";
|
|
219
|
+
|
|
220
|
+
for (const surrounding of surroundings) {
|
|
221
|
+
output += `<${surrounding}>`;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
output += this.escapeHTML(content);
|
|
225
|
+
|
|
226
|
+
surroundings.reverse();
|
|
227
|
+
for (const surrounding of surroundings) {
|
|
228
|
+
output += `</${surrounding}>`;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return output;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
private escapeHTML(content: string) {
|
|
235
|
+
const text = document.createTextNode(content);
|
|
236
|
+
const p = document.createElement("p");
|
|
237
|
+
p.appendChild(text);
|
|
238
|
+
return p.innerHTML;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
private convertReference(rawReference: string, type: EntityType) {
|
|
242
|
+
const parseReference = this.modelReferenceParserProvider
|
|
243
|
+
.get()!
|
|
244
|
+
.parse(rawReference, { type });
|
|
245
|
+
return this.serializeReference(parseReference);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
private serializeReference(parseReference: EntityReference) {
|
|
249
|
+
return this.remoteURLSerializerProvider.get()!.serialize(parseReference);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* See the LICENSE file distributed with this work for additional
|
|
3
|
+
* information regarding copyright ownership.
|
|
4
|
+
*
|
|
5
|
+
* This is free software; you can redistribute it and/or modify it
|
|
6
|
+
* under the terms of the GNU Lesser General Public License as
|
|
7
|
+
* published by the Free Software Foundation; either version 2.1 of
|
|
8
|
+
* the License, or (at your option) any later version.
|
|
9
|
+
*
|
|
10
|
+
* This software is distributed in the hope that it will be useful,
|
|
11
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
13
|
+
* Lesser General Public License for more details.
|
|
14
|
+
*
|
|
15
|
+
* You should have received a copy of the GNU Lesser General Public
|
|
16
|
+
* License along with this software; if not, write to the Free
|
|
17
|
+
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
|
18
|
+
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
|
|
19
|
+
*/
|
|
20
|
+
import type { UniAst } from "@xwiki/cristal-uniast-api";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Converts Universal AST trees to HTML.
|
|
24
|
+
*
|
|
25
|
+
* @since 0.22
|
|
26
|
+
* @beta
|
|
27
|
+
*/
|
|
28
|
+
export interface UniAstToHTMLConverter {
|
|
29
|
+
toHtml(uniAst: UniAst): string | Error;
|
|
30
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* See the LICENSE file distributed with this work for additional
|
|
3
|
+
* information regarding copyright ownership.
|
|
4
|
+
*
|
|
5
|
+
* This is free software; you can redistribute it and/or modify it
|
|
6
|
+
* under the terms of the GNU Lesser General Public License as
|
|
7
|
+
* published by the Free Software Foundation; either version 2.1 of
|
|
8
|
+
* the License, or (at your option) any later version.
|
|
9
|
+
*
|
|
10
|
+
* This software is distributed in the hope that it will be useful,
|
|
11
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
13
|
+
* Lesser General Public License for more details.
|
|
14
|
+
*
|
|
15
|
+
* You should have received a copy of the GNU Lesser General Public
|
|
16
|
+
* License along with this software; if not, write to the Free
|
|
17
|
+
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
|
18
|
+
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
|
|
19
|
+
*/
|
|
20
|
+
export { ComponentInit, uniAstToHTMLConverterName } from "./component-init";
|
|
21
|
+
export { type UniAstToHTMLConverter } from "./html/uni-ast-to-html-converter";
|
package/tsconfig.json
ADDED