-
Notifications
You must be signed in to change notification settings - Fork 1
/
transform.mjs
66 lines (54 loc) · 1.7 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
import { getValue, parse } from 'markdown-code-block-meta';
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',
alt: alt || diagramType,
url: toDataURL(data),
},
];
},
'object-base64': ({ node, diagramType, data, alt }) => {
node.type = 'html';
node.value = `<object type="${mime}" class="kroki-object" data-type="${diagramType}" title="${
alt || diagramType
}" data="${toDataURL(data)}">Load SVG fail...</object>`;
},
'img-html-base64': ({ node, diagramType, data, alt }) => {
node.type = 'html';
node.value = `<img class="kroki-image" alt="${
alt || diagramType
}" src="${toDataURL(data)}" />`;
},
'inline-svg': ({ node, diagramType, data, alt }) => {
node.type = 'html';
node.value = `<div class="kroki-inline-svg" data-type="${diagramType}" data-alt="${
alt || diagramType
}">${removeXML(data.toString())}</div>`;
},
};
export const outputType = Object.keys(modes);
export async function transform({ node, server, headers, output }) {
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;
modes[output]({ node, diagramType, data, alt });
}