mirror of
https://github.com/morten-olsen/morten-olsen.github.io.git
synced 2026-02-08 01:46:28 +01:00
init
This commit is contained in:
29
bin/resources/latex/index.ts
Normal file
29
bin/resources/latex/index.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { Asset, Bundler } from "../../bundler";
|
||||
import { Observable } from "../../observable";
|
||||
import { createEjs } from "../ejs";
|
||||
import { latexToPdf } from "./utils";
|
||||
|
||||
type LatexOptions = {
|
||||
path: string;
|
||||
bundler: Bundler;
|
||||
template: ReturnType<typeof createEjs>;
|
||||
data: Observable<any>;
|
||||
};
|
||||
const createLatex = ({ template, data, path, bundler }: LatexOptions) => {
|
||||
const pdf = Observable.combine({
|
||||
template,
|
||||
data,
|
||||
})
|
||||
.pipe(async ({ template, data }) => template(data))
|
||||
.pipe(async (latex) => {
|
||||
const pdf = await latexToPdf(latex);
|
||||
|
||||
const asset: Asset = {
|
||||
content: pdf,
|
||||
};
|
||||
return asset;
|
||||
});
|
||||
return bundler.register(`${path}.pdf`, pdf);
|
||||
};
|
||||
|
||||
export { createLatex };
|
||||
23
bin/resources/latex/utils.ts
Normal file
23
bin/resources/latex/utils.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import latex from "node-latex";
|
||||
import { Readable } from "stream";
|
||||
|
||||
const latexToPdf = (doc: string) =>
|
||||
new Promise<Buffer>((resolve, reject) => {
|
||||
const chunks: Buffer[] = [];
|
||||
const input = new Readable();
|
||||
input.push(doc);
|
||||
input.push(null);
|
||||
const latexStream = latex(input);
|
||||
latexStream.on("data", (chunk) => {
|
||||
chunks.push(Buffer.from(chunk));
|
||||
});
|
||||
latexStream.on("finish", () => {
|
||||
const result = Buffer.concat(chunks);
|
||||
resolve(result);
|
||||
});
|
||||
latexStream.on("error", (err) => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
|
||||
export { latexToPdf };
|
||||
Reference in New Issue
Block a user