-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rehype-typst): add font caching (#436)
- Loading branch information
1 parent
4534737
commit 80b231d
Showing
5 changed files
with
72 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}, | ||
}), | ||
], | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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" | ||
|