substrate-rendermarkdown.web.val.run
Readme

Render streaming markdown

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
export default function renderMarkdown(stream: any) {
let { readable, writable } = new TransformStream();
let writer = writable.getWriter();
const textEncoder = new TextEncoder();
writer.write(textEncoder.encode(`<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<style>
.link-pair {
display: inline-block;
margin-right: 10px;
}
.link-pair a {
display: inline-block;
margin-right: 5px;
}
</style>
</head>
<body>
<div id="content"></div>
<script>
let content = document.getElementById('content');
let markdown = '';
</script>`));
new Promise(async () => {
for await (const event of stream) {
if (event.object === "node.delta" && event.node_id === "markdown") {
const chunk = event.data.text || "";
writer.write(textEncoder.encode(`<script>
markdown += ${JSON.stringify(chunk)};
content.innerHTML = marked.parse(markdown);
</script>`));
}
}
writer.write(textEncoder.encode(`</body></html>`));
writer.close();
});
return new Response(readable, {
headers: {
"Content-Type": "text/html",
},
});
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
August 1, 2024