-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.js
32 lines (27 loc) · 909 Bytes
/
utils.js
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
import { PREVIEW_HOST } from "./constants";
import highlight from "highlight.js";
export const getPreviewUrl = (id, ext) => {
const splitId = id.split(".")[0];
if (ext == undefined) {
return `${PREVIEW_HOST}/p/${splitId}`;
} else {
return `${PREVIEW_HOST}/pk/${splitId}/${ext}`;
}
};
export const getHtmlFromCode = (code, language) => {
const isLengthFine = code.length < 15000;
const hl = isLengthFine ? highlight.highlightAuto(code, language) : code;
const retval = (hl.value ?? hl)
.split("\n")
.map((l, n) => {
return `<tr><td class="line-number">${n + 1}</td><td>${l}</td></tr>`;
})
.join("");
return {
content: `<table>${retval}</table>`,
language: isLengthFine ? hl.language : "large-text",
};
};
export const makeProp = (content, id, ext) => {
return { props: { ...(content && { content }), ...(id && { id }), ...(ext && { ext }) } };
};