眾所周知,Typst Universe 是 typst 生態中的三方包 registry。今天,向大家介紹新一代的 typst 包管理和三方包倉庫:NPM Universe。顧名思義,就是使用一個名叫「npm」的命令列工具的 Typst Universe 替代品。
NPM Universe 如此重要,甚至被許多執行時捆綁。例如,在你的包管理器上安裝 nodejs
,即可獲取一份 npm
的副本。
pacman -S nodejs # Arch Linux
brew install nodejs # macOS
scoop install nodejs # Windows
npm -v
10.9.2
當然,只有一種實現的標準並不健康。你也可以使用速度更快的 pnpm
作為替代品:
pacman -S pnpm # Arch Linux
brew install pnpm # macOS
scoop install pnpm # Windows
npm install -g pnpm # Already installed npm
pnpm -v
10.11.0
與以往的簡約風格不同,我們開創性地引入了專案的概念,這有助於我們確定我們的 typst 文稿的依賴版本,以及定義常用的配置。執行下面的命令後,會在當前目錄生成一個 package.json
檔案。
npm init
NPM Universe 和 Typst Universe 一樣,提供了一個 社羣倉庫的入口,使用者可以在這裡檢索和查閱。

為了維護各個依賴的版本資訊,現在我們不再需要記住特定的版本號,而是直接使用 npm install
就可以安裝最新版本了。比如,你可以安裝我寫的 sigfig
包來處理不確定度。
npm install typst-sigfig
與匯入 Typst Universe 的 @preview
類似,我們還需要一個特定的字首:node_modules
。這是 NPM Universe 開創性的「節點模組」架構,每個 typst 包是一個單獨的資料夾。
// Pre
// #import "@preview/sigfig:0.1.0": *
// Post
#import "node_modules/typst-sigfig/sigfig.typ": *
之所以叫作 Universe,實際上是因為這個包管理器將 typst 包儲存在 node_modules 中。網際網路上流傳了這樣一張圖片,說明了 typst 生態的繁榮:

此外,NPM Universe 無需經過繁瑣的稽覈流程,init
好之後,幾分鐘內即可上傳你的程式碼:
npm publish # 初次使用需要註冊賬號
NPM Universe 還支援名稱空間,這意味著當 typst 升級時,你可以自己將 fork 發版,不必苦苦等待作者適配發版,或者讓刪掉 styles
的庫原始碼汙染你的 git 歷史。

更棒的是,NPM Universe 甚至還支援檢視 typst 包的原始碼,還提供完善的程式碼高亮。這是上一代 Typst Universe 都沒做到的功能!

如果你稍加設定,甚至可以在 README 中展示 typst 片段的渲染結果!
{
...,
"scripts": {
...,
"pub": "typst compile --features html --format html README.typ README.md && npm publish"
}
}
當然,NPM Universe 不僅改善了 typst 生態的體驗,還意外惠及了前端生態。NPM Universe 慷慨地允許前端開發者分享 Typst 的技術紅利——如果你也在使用 astro-typst,可以直接在 .astro 或 .mdx 檔案中匯入來自 NPM Universe 的包。就以 astro-typst 中的示例來說:
import Simple from "node_modules/astro-typst/src/pages/simp.typ?svg"
<Simple />
NPM Universe 不僅向我們展現了開源軟體千帆競發、共建共享的繁榮與崛起,更體現了其海納百川、相容幷蓄的自由與開放。跨語言的互操作不再是夢想——最後,謹此獻上 NPM Universe 的讚歌:
#import "@preview/jogs:0.2.4": *
#set text(font: "Sart Sans", weight: 300)
#let lib = "let global = {}, setTimeout = (f, _) => {}; " + read("node_modules/echarts/dist/echarts.min.js")
#let code = ```js
const option = {
textStyle: { fontFamily: "Sart Sans" },
title: {
left: "center",
text: "Typst & 前端已經崛起了,這人還沒收到通知嗎?",
},
tooltip: { trigger: "axis" },
legend: {
data: ["typst/typst", "withastro/astro"],
top: "bottom",
},
xAxis: {
type: "time",
axisLabel: {
formatter: (value) => echarts.format.formatTime("yyyy-MM", value),
},
},
yAxis: {
type: "value",
name: "Star Count",
},
series: [
{
name: "typst/typst",
type: "line",
smooth: true,
itemStyle: {
color: "rgb(0, 119, 255)",
},
areaStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{
offset: 0,
color: "rgb(0, 191, 255)",
},
{
offset: 1,
color: "rgb(0, 119, 255)",
},
]),
},
data: [
[1679421679000, 0],
[1679525567000, 5280],
[1679672355000, 7950],
[1679931973000, 10620],
[1680520762000, 13290],
[1682716282000, 15960],
[1689665289000, 18630],
[1698574915000, 21270],
[1704566769000, 23940],
[1711434638000, 26610],
[1720157331000, 29280],
[1725329624000, 31950],
[1731535220000, 34620],
[1739888574000, 37290],
[1746624740000, 39960],
[1751012293000, 43295],
],
},
{
name: "withastro/astro",
type: "line",
smooth: true,
itemStyle: {
color: "rgb(255, 70, 131)",
},
areaStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{
offset: 0,
color: "rgb(255, 158, 68)",
},
{
offset: 1,
color: "rgb(255, 70, 131)",
},
]),
},
data: [
[1627474805000, 5280],
[1639346595000, 7950],
[1650301136000, 10620],
[1660134580000, 13290],
[1660937362000, 15960],
[1663527602000, 18630],
[1668506551000, 21270],
[1674623974000, 23940],
[1678214898000, 26610],
[1685431037000, 29280],
[1693493029000, 31950],
[1697513567000, 34620],
[1704893954000, 37290],
[1710336916000, 39960],
[1751012349000, 51919],
],
},
],
};
function generateSVG() {
const chart = echarts.init(null, null, {
renderer: "svg",
ssr: true,
width: 400,
height: 300,
});
chart.setOption(option);
const svg = chart.renderToSVGString();
chart.dispose();
return svg;
}
```
#let result = eval-js(lib + code.text + "\ngenerateSVG()")
#image(bytes(result))
版權許可
- 本作品 採用 知識共享 署名—非商業性使用 4.0 國際許可協議(CC BY-NC 4.0 International)許可,閣下可自由地共享(複製、發行) 和演繹(修改、轉換或二次創作) 這一作品,唯須遵守許可協議條款。
評論