tikzify 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +73 -0
- package/package.json +23 -0
- package/src/book.ts +313 -0
- package/src/emojis.ts +977 -0
- package/src/server.ts +45 -0
package/src/server.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { $ } from "bun"
|
|
2
|
+
import { mkdir, writeFile } from "node:fs/promises"
|
|
3
|
+
import PQueue from "p-queue"
|
|
4
|
+
import { Book, toTex } from "./book"
|
|
5
|
+
|
|
6
|
+
const q = new PQueue({ concurrency: 1 })
|
|
7
|
+
const server = Bun.serve({
|
|
8
|
+
port: 3000,
|
|
9
|
+
async fetch(req) {
|
|
10
|
+
const body = await req.json()
|
|
11
|
+
const book = Book.parse(body)
|
|
12
|
+
|
|
13
|
+
const id = crypto.randomUUID()
|
|
14
|
+
const tmp = `/tmp/doks/${id}`
|
|
15
|
+
await mkdir(tmp, { recursive: true })
|
|
16
|
+
await $`ln -s ${process.cwd()}/Fredoka-Bold.ttf ${tmp}/Fredoka-Bold.ttf`
|
|
17
|
+
|
|
18
|
+
const tex = toTex(book)
|
|
19
|
+
await writeFile(`${tmp}/book.tex`, tex)
|
|
20
|
+
await Promise.all(book.pages.map(async (page, i) => {
|
|
21
|
+
console.log(`Writing page ${i} jpg`)
|
|
22
|
+
writeFile(`${tmp}/${i}.jpg`, Buffer.from(page.jpgBase64, 'base64'))
|
|
23
|
+
}))
|
|
24
|
+
|
|
25
|
+
// Run twice to resolve "current page" coordinates
|
|
26
|
+
const runTex = () => $`xelatex -interaction=nonstopmode book.tex`.cwd(tmp).quiet()
|
|
27
|
+
await q.add(async () => {
|
|
28
|
+
await runTex()
|
|
29
|
+
await runTex()
|
|
30
|
+
})
|
|
31
|
+
const pdf = await Bun.file(`${tmp}/book.pdf`).arrayBuffer()
|
|
32
|
+
|
|
33
|
+
return new Response(pdf, {
|
|
34
|
+
headers: {
|
|
35
|
+
"Content-Type": "application/pdf"
|
|
36
|
+
}
|
|
37
|
+
})
|
|
38
|
+
},
|
|
39
|
+
error(err) {
|
|
40
|
+
console.error(err)
|
|
41
|
+
return new Response(JSON.stringify({ error: err.message }), { status: 500 })
|
|
42
|
+
}
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
console.log(`Server running at http://localhost:${server.port}/`)
|