-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.mjs
More file actions
89 lines (85 loc) · 2.11 KB
/
render.mjs
File metadata and controls
89 lines (85 loc) · 2.11 KB
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
import { gateSchema } from "./schema.mjs";
import { Plugin } from "prosemirror-state";
import { Element } from "https://www.zeit.de/+/wally/element.js";
function getContent(doc) {
return {
title: doc.firstChild.textContent,
items: [
{
accordion: doc.children.slice(1).map((node) => ({
name: "some",
items: node.children.slice(1, -1).map((n) =>
n.type == gateSchema.nodes.list
? {
list: {
icon: "checkmark",
color: "green",
items: n.children.map((n) => n.textContent),
},
}
: { text: n.textContent },
),
title: node.firstChild.textContent,
button: { label: node.lastChild.textContent },
})),
},
],
};
}
function toYaml(obj, alreadyIndented = true, indent = 0) {
if (typeof obj == "string") return obj;
const indStr = " ".repeat(indent);
if (Array.isArray(obj)) {
return (
"\n" +
obj.map((v) => indStr + "- " + toYaml(v, true, indent + 2)).join("\n")
);
}
return (
(alreadyIndented ? "" : `\n${indStr}`) +
Object.keys(obj)
.map(
(k, idx) =>
`${k}: ${toYaml(obj[k], idx == 0 && !alreadyIndented, indent + 2)}`,
)
.join(`\n${indStr}`)
);
}
export function showYaml(elem) {
return new Plugin({
view(view) {
return {
update({ state }) {
elem.value = toYaml(getContent(state.doc));
},
};
},
});
}
export function showPreview(elem) {
return new Plugin({
view(view) {
return {
update({ state }) {
elem.innerHTML = "";
new Element({
target: elem,
intro: true,
props: {
id: "id",
config: {
id: "paid",
type: "gate",
content: getContent(state.doc),
},
settings: {
ssoUrl: "http://example.com",
},
tracking: null,
},
});
},
};
},
});
}