-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.mjs
49 lines (39 loc) · 1.1 KB
/
utils.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
import { readFileSync } from 'node:fs';
import { parse } from 'markdown-code-block-meta';
import pMemoize from 'p-memoize';
import { httpPost } from './fetch.mjs';
export function isKroki(alias = []) {
return ({ type, lang, meta, value }) => {
return (
type === 'code' &&
value &&
value.trim() &&
(alias.includes(lang) ||
(lang === 'kroki' && meta && parse(meta).has('type')))
);
};
}
const failImage = new URL('fail.svg', import.meta.url);
function createFailImage(message) {
return readFileSync(failImage, 'utf8').replace(
'======',
message.slice(0, 500),
);
}
function Fetch({ server, headers, type, value }) {
return httpPost({
url: `${server}/${type}/svg`,
body: value,
headers,
})
.catch((error) => createFailImage(error.message))
.then((data) => Buffer.from(data));
}
export const mime = 'image/svg+xml';
export function toDataURL(buffer) {
const base64 = buffer.toString('base64');
return `data:${mime};base64,${base64}`;
}
export const fetchData = pMemoize(Fetch, {
cacheKey: (arguments_) => JSON.stringify(arguments_),
});