Skip to content

Commit

Permalink
feat(rehype-typst): add font caching (#436)
Browse files Browse the repository at this point in the history
  • Loading branch information
Enter-tainer authored Dec 9, 2023
1 parent 4534737 commit 80b231d
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 2 deletions.
53 changes: 53 additions & 0 deletions projects/rehype-typst/lib/cached-font-middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { preloadFontAssets } from '@myriaddreamin/typst.ts/dist/esm/options.init.mjs';
import * as fs from 'fs';
import * as path from 'path';
import { HttpsProxyAgent } from 'https-proxy-agent';

export async function cachedFontInitOptions() {
const { existsSync, mkdirSync, readFileSync, writeFileSync } = fs;
const fetcher = (await import('node-fetch')).default;
const dataDir =
process.env.APPDATA ||
(process.platform == 'darwin'
? process.env.HOME + '/Library/Preferences'
: process.env.HOME + '/.local/share');

const cacheDir = path.join(dataDir, 'typst/fonts');

return {
beforeBuild: [
preloadFontAssets({
assets: ['text', 'cjk', 'emoji'],
fetcher: async (url, init) => {
const cachePath = path.join(cacheDir, url.toString().replace(/[^a-zA-Z0-9]/g, '_'));
if (existsSync(cachePath)) {
const font_res = {
arrayBuffer: async () => {
return readFileSync(cachePath).buffer;
},
};

return font_res;
}

console.log('loading remote font:', url);
const proxyOption = process.env.HTTPS_PROXY
? { agent: new HttpsProxyAgent(process.env.HTTPS_PROXY) }
: {};

const font_res = await fetcher(url, {
...proxyOption,
...((init) || {}),
});
const buffer = await font_res.arrayBuffer();
mkdirSync(path.dirname(cachePath), { recursive: true });
writeFileSync(cachePath, Buffer.from(buffer));
font_res.arrayBuffer = async () => {
return buffer;
};
return font_res;
},
}),
],
};
}
4 changes: 2 additions & 2 deletions projects/rehype-typst/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import { fromHtmlIsomorphic } from 'hast-util-from-html-isomorphic'
import { toText } from 'hast-util-to-text'
import { $typst } from '@myriaddreamin/typst.ts/dist/esm/contrib/snippet.mjs'

import { SKIP, visitParents } from 'unist-util-visit-parents'

import { cachedFontInitOptions } from './cached-font-middleware.js'
/** @type {Readonly<Options>} */
const emptyOptions = {}
/** @type {ReadonlyArray<unknown>} */
const emptyClasses = []
$typst.setCompilerInitOptions(await cachedFontInitOptions());
await $typst.svg({
mainContent: ''
})
Expand Down
1 change: 1 addition & 0 deletions projects/rehype-typst/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@types/katex": "^0.16.0",
"hast-util-from-html-isomorphic": "^2.0.0",
"hast-util-to-text": "^4.0.0",
"https-proxy-agent": "^7.0.2",
"unist-util-visit-parents": "^6.0.0",
"vfile": "^6.0.0"
},
Expand Down
1 change: 1 addition & 0 deletions projects/rehype-typst/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ unified()
.use(rehypeTypst)
.use(rehypeStringify)
.process(test_code, (err, file) => {
if (err) throw err
console.log(String(file))
})
15 changes: 15 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3324,6 +3324,13 @@ agent-base@6, agent-base@^6.0.2:
dependencies:
debug "4"

agent-base@^7.0.2:
version "7.1.0"
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-7.1.0.tgz#536802b76bc0b34aa50195eb2442276d613e3434"
integrity sha512-o/zjMZRhJxny7OyEF+Op8X+efiELC7k7yOjMzgfzVqOzXqkBkWI79YoTdOtsuWd5BWhAGAuOY/Xa6xpiaWXiNg==
dependencies:
debug "^4.3.4"

agentkeepalive@^4.2.1:
version "4.5.0"
resolved "https://registry.yarnpkg.com/agentkeepalive/-/agentkeepalive-4.5.0.tgz#2673ad1389b3c418c5a20c5d7364f93ca04be923"
Expand Down Expand Up @@ -6420,6 +6427,14 @@ [email protected], https-proxy-agent@^5.0.0:
agent-base "6"
debug "4"

https-proxy-agent@^7.0.2:
version "7.0.2"
resolved "https://registry.yarnpkg.com/https-proxy-agent/-/https-proxy-agent-7.0.2.tgz#e2645b846b90e96c6e6f347fb5b2e41f1590b09b"
integrity sha512-NmLNjm6ucYwtcUmL7JQC1ZQ57LmHP4lT15FQ8D61nak1rO6DH+fz5qNK2Ap5UN4ZapYICE3/0KodcLYSPsPbaA==
dependencies:
agent-base "^7.0.2"
debug "4"

human-signals@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/human-signals/-/human-signals-2.1.0.tgz#dc91fcba42e4d06e4abaed33b3e7a3c02f514ea0"
Expand Down

0 comments on commit 80b231d

Please sign in to comment.