-
Notifications
You must be signed in to change notification settings - Fork 1
/
transform.mjs
95 lines (83 loc) · 2.17 KB
/
transform.mjs
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
import { getValue, parse } from 'markdown-code-block-meta';
import { create } from './ast.mjs';
import { fetchData, mime, toDataURL } from './utils.mjs';
/* eslint-disable no-param-reassign */
function removeXML(string) {
return string.replace(/<\?xml.+\?>/, '');
}
const modes = {
'img-base64': ({ node, diagramType, data, alt }) => {
node.type = 'paragraph';
node.children = [
{
type: 'image',
_meta: { kroki: true, type: diagramType },
alt: alt || diagramType,
url: toDataURL(data),
},
];
},
'object-base64': ({ target, node, diagramType, data, alt }) => {
Object.assign(
node,
create(target, {
tag: 'object',
child: 'Load SVG fail...',
attr: {
type: mime,
class: 'kroki-object',
'data-type': diagramType,
title: alt || diagramType,
data: toDataURL(data),
},
}),
);
},
'img-html-base64': ({ target, node, diagramType, data, alt }) => {
node.type = 'paragraph';
node.children = [
create(target, {
tag: 'img',
attr: {
class: 'kroki-image',
alt: alt || diagramType,
'data-type': diagramType,
src: toDataURL(data),
},
}),
];
},
'inline-svg': ({ target, node, diagramType, data, alt }) => {
Object.assign(
node,
create(target, {
tag: 'p',
attr: {
class: 'kroki-inline-svg',
'data-type': diagramType,
'data-alt': alt || diagramType,
},
child: removeXML(data.toString()),
}),
);
},
};
export const outputType = Object.keys(modes);
export async function transform({ node, server, headers, output, target }) {
const { meta, value, lang } = node;
const object = parse(meta);
const alt = getValue(object.get('alt'));
const type = getValue(object.get('type'));
const diagramType = lang === 'kroki' ? type : lang;
const data = await fetchData({
server,
headers,
type: diagramType,
value,
});
delete node.lang;
delete node.value;
delete node.meta;
delete node.type;
modes[output]({ node, diagramType, data, alt, target });
}