mirror of
https://github.com/mii443/obsidian-typst.git
synced 2025-08-22 16:15:34 +00:00
Get files using vault instead of fs
This commit is contained in:
202
Cargo.lock
generated
202
Cargo.lock
generated
@ -49,9 +49,9 @@ checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
|
||||
|
||||
[[package]]
|
||||
name = "biblatex"
|
||||
version = "0.7.0"
|
||||
version = "0.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1bc17a7f4d461f93f5dbbae4c961746cb4aafb5c6c1a61089a86836614932a3c"
|
||||
checksum = "cc9fd60378277e44cd400ec5f35e768ce0d5a63d8d18ac7b1a9231196251dae5"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"numerals",
|
||||
@ -109,12 +109,36 @@ version = "1.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.0.79"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "chinese-number"
|
||||
version = "0.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9f9a8cffacecd7f477f8395021158af07c8a3f74523e9b90e4e4bb0105deaa74"
|
||||
dependencies = [
|
||||
"chinese-variant",
|
||||
"enum-ordinalize",
|
||||
"num-bigint",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "chinese-variant"
|
||||
version = "1.0.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "aeea139b89efab957972956e5d3e4efb66a6c261f726abf6911040cc8ef700f7"
|
||||
|
||||
[[package]]
|
||||
name = "chrono"
|
||||
version = "0.4.24"
|
||||
@ -133,9 +157,9 @@ checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
|
||||
|
||||
[[package]]
|
||||
name = "comemo"
|
||||
version = "0.2.0"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "22bf2c21093020535dd771993fedae8dd55393a4258cca501a9b55a962d350a5"
|
||||
checksum = "1ba423e212681b51c5452a458bb24e88165f4c09857a783c802719cc46313f3f"
|
||||
dependencies = [
|
||||
"comemo-macros",
|
||||
"siphasher",
|
||||
@ -143,9 +167,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "comemo-macros"
|
||||
version = "0.2.0"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9faa23f4534253fa656b176ff524d5cd7306a6fed3048929f9cc01ab38ab5a5a"
|
||||
checksum = "fca5ceeb99665bad04a32fe297d1581a68685e36fb6da92a1c9b7d9693638c01"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
@ -217,6 +241,20 @@ dependencies = [
|
||||
"stable_deref_trait",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "enum-ordinalize"
|
||||
version = "3.1.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a62bb1df8b45ecb7ffa78dca1c17a438fb193eb083db0b1b494d2a61bcb5096a"
|
||||
dependencies = [
|
||||
"num-bigint",
|
||||
"num-traits",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"rustc_version",
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fancy-regex"
|
||||
version = "0.7.1"
|
||||
@ -249,6 +287,17 @@ version = "1.0.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
|
||||
|
||||
[[package]]
|
||||
name = "fontdb"
|
||||
version = "0.9.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d52186a39c335aa6f79fc0bf1c3cf854870b6ad4e50a7bb8a59b4ba1331f478a"
|
||||
dependencies = [
|
||||
"log",
|
||||
"memmap2",
|
||||
"ttf-parser 0.17.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "form_urlencoded"
|
||||
version = "1.1.0"
|
||||
@ -286,9 +335,9 @@ checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
|
||||
|
||||
[[package]]
|
||||
name = "hayagriva"
|
||||
version = "0.2.1"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "55fc197906e4c8f799311502776bd69f8b8a50cb26173ef915d87384786d181e"
|
||||
checksum = "d8a21ff266f0b113789bbf4a27da16330315eebbd7df8e844f95d29f92ad556d"
|
||||
dependencies = [
|
||||
"biblatex",
|
||||
"chrono",
|
||||
@ -409,6 +458,12 @@ version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.142"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6a987beff54b60ffa6d51982e1aa1146bc42f19bd26be28b0586f252fccf5317"
|
||||
|
||||
[[package]]
|
||||
name = "linked-hash-map"
|
||||
version = "0.5.6"
|
||||
@ -446,6 +501,15 @@ version = "2.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
|
||||
|
||||
[[package]]
|
||||
name = "memmap2"
|
||||
version = "0.5.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "83faa42c0a078c393f6b29d5db232d8be22776a891f8f56e5284faee4a20b327"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "miniz_oxide"
|
||||
version = "0.5.4"
|
||||
@ -464,6 +528,17 @@ dependencies = [
|
||||
"adler",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-bigint"
|
||||
version = "0.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.45"
|
||||
@ -571,6 +646,12 @@ version = "0.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "db8bcd96cb740d03149cbad5518db9fd87126a10ab519c011893b1754134c468"
|
||||
|
||||
[[package]]
|
||||
name = "pin-project-lite"
|
||||
version = "0.2.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116"
|
||||
|
||||
[[package]]
|
||||
name = "pixglyph"
|
||||
version = "0.1.0"
|
||||
@ -607,6 +688,15 @@ dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "psm"
|
||||
version = "0.1.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5787f7cda34e3033a72192c018bc5883100330f362ef279a8cbccfce8bb4e874"
|
||||
dependencies = [
|
||||
"cc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.26"
|
||||
@ -699,6 +789,15 @@ dependencies = [
|
||||
"xmlparser",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc_version"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366"
|
||||
dependencies = [
|
||||
"semver",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustversion"
|
||||
version = "1.0.12"
|
||||
@ -745,6 +844,12 @@ dependencies = [
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "semver"
|
||||
version = "1.0.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bebd363326d05ec3e2f532ab7660680f3b02130d780c299bca73469d521bc0ed"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.159"
|
||||
@ -826,6 +931,19 @@ version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3"
|
||||
|
||||
[[package]]
|
||||
name = "stacker"
|
||||
version = "0.1.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c886bd4480155fd3ef527d45e9ac8dd7118a898a46530b7b94c3e21866259fce"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"psm",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strum"
|
||||
version = "0.24.1"
|
||||
@ -982,12 +1100,50 @@ version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
|
||||
|
||||
[[package]]
|
||||
name = "tracing"
|
||||
version = "0.1.37"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"pin-project-lite",
|
||||
"tracing-attributes",
|
||||
"tracing-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-attributes"
|
||||
version = "0.1.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4017f8f45139870ca7e672686113917c71c7a6e02d4924eda67186083c03081a"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-core"
|
||||
version = "0.1.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "24eb03ba0eab1fd845050058ce5e616558e8f8d8fca633e6b163fe25c797213a"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ttf-parser"
|
||||
version = "0.15.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7b3e06c9b9d80ed6b745c7159c40b311ad2916abb34a49e9be2653b90db0d8dd"
|
||||
|
||||
[[package]]
|
||||
name = "ttf-parser"
|
||||
version = "0.17.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "375812fa44dab6df41c195cd2f7fecb488f6c09fbaafb62807488cefab642bff"
|
||||
|
||||
[[package]]
|
||||
name = "ttf-parser"
|
||||
version = "0.18.1"
|
||||
@ -1002,16 +1158,18 @@ checksum = "6af6ae20167a9ece4bcb41af5b80f8a1f1df981f6391189ce00fd257af04126a"
|
||||
|
||||
[[package]]
|
||||
name = "typst"
|
||||
version = "0.0.0"
|
||||
source = "git+https://github.com/typst/typst.git#2c735294cd5e47f1f1eb6402a3b8c500dccd047b"
|
||||
version = "0.2.0"
|
||||
source = "git+https://github.com/typst/typst.git#b79121d4cf8cdddb7977cbb22b627ac4706434f0"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"bytemuck",
|
||||
"comemo",
|
||||
"ecow",
|
||||
"flate2",
|
||||
"fontdb",
|
||||
"if_chain",
|
||||
"image",
|
||||
"indexmap",
|
||||
"log",
|
||||
"miniz_oxide 0.5.4",
|
||||
"once_cell",
|
||||
@ -1023,10 +1181,12 @@ dependencies = [
|
||||
"rustybuzz",
|
||||
"serde",
|
||||
"siphasher",
|
||||
"stacker",
|
||||
"subsetter",
|
||||
"svg2pdf",
|
||||
"thin-vec",
|
||||
"tiny-skia",
|
||||
"tracing",
|
||||
"ttf-parser 0.18.1",
|
||||
"typst-macros",
|
||||
"unicode-math-class",
|
||||
@ -1039,9 +1199,10 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "typst-library"
|
||||
version = "0.0.0"
|
||||
source = "git+https://github.com/typst/typst.git#2c735294cd5e47f1f1eb6402a3b8c500dccd047b"
|
||||
version = "0.2.0"
|
||||
source = "git+https://github.com/typst/typst.git#b79121d4cf8cdddb7977cbb22b627ac4706434f0"
|
||||
dependencies = [
|
||||
"chinese-number",
|
||||
"comemo",
|
||||
"csv",
|
||||
"ecow",
|
||||
@ -1057,6 +1218,7 @@ dependencies = [
|
||||
"serde_yaml",
|
||||
"smallvec",
|
||||
"syntect",
|
||||
"tracing",
|
||||
"ttf-parser 0.18.1",
|
||||
"typed-arena",
|
||||
"typst",
|
||||
@ -1069,8 +1231,8 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "typst-macros"
|
||||
version = "0.0.0"
|
||||
source = "git+https://github.com/typst/typst.git#2c735294cd5e47f1f1eb6402a3b8c500dccd047b"
|
||||
version = "0.2.0"
|
||||
source = "git+https://github.com/typst/typst.git#b79121d4cf8cdddb7977cbb22b627ac4706434f0"
|
||||
dependencies = [
|
||||
"heck",
|
||||
"proc-macro2",
|
||||
@ -1154,6 +1316,12 @@ version = "1.10.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-vo"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b1d386ff53b415b7fe27b50bb44679e2cc4660272694b7b6f3326d8480823a94"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.4"
|
||||
@ -1187,14 +1355,20 @@ dependencies = [
|
||||
"data-url",
|
||||
"flate2",
|
||||
"float-cmp",
|
||||
"fontdb",
|
||||
"kurbo",
|
||||
"log",
|
||||
"pico-args",
|
||||
"rctree",
|
||||
"roxmltree",
|
||||
"rustybuzz",
|
||||
"simplecss",
|
||||
"siphasher",
|
||||
"svgtypes",
|
||||
"ttf-parser 0.15.2",
|
||||
"unicode-bidi",
|
||||
"unicode-script",
|
||||
"unicode-vo",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -11,8 +11,8 @@ crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
# Everything to do with Typst
|
||||
typst = { git = "https://github.com/typst/typst.git" }
|
||||
typst-library = { git = "https://github.com/typst/typst.git" }
|
||||
typst = { git = "https://github.com/typst/typst.git", version = "0.2" }
|
||||
typst-library = { git = "https://github.com/typst/typst.git", version = "0.2" }
|
||||
comemo = "0.2"
|
||||
|
||||
once_cell = "1.17.1"
|
||||
@ -35,5 +35,5 @@ js-sys = { git = "https://github.com/fenjalien/wasm-bindgen.git" }
|
||||
wasm-bindgen-futures = { git = "https://github.com/fenjalien/wasm-bindgen.git" }
|
||||
wasm-bindgen = { git = "https://github.com/fenjalien/wasm-bindgen.git" }
|
||||
|
||||
[profile.release]
|
||||
debug = true
|
||||
# [profile.release]
|
||||
# debug = true
|
||||
|
30
main.ts
30
main.ts
@ -1,5 +1,6 @@
|
||||
import { App, FileSystemAdapter, HexString, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
|
||||
import { App, FileSystemAdapter, HexString, Notice, Plugin, PluginSettingTab, Setting, TFile, TFolder, arrayBufferToBase64 } from 'obsidian';
|
||||
|
||||
import { Base64 } from "js-base64"
|
||||
|
||||
// @ts-ignore
|
||||
import typst_wasm_bin from './pkg/obsidian_typst_bg.wasm'
|
||||
@ -24,20 +25,35 @@ const DEFAULT_SETTINGS: TypstPluginSettings = {
|
||||
export default class TypstPlugin extends Plugin {
|
||||
settings: TypstPluginSettings;
|
||||
compiler: typst.SystemWorld;
|
||||
files: Map<string, string>;
|
||||
|
||||
async onload() {
|
||||
await typstInit(typst_wasm_bin)
|
||||
await this.loadSettings()
|
||||
this.files = new Map()
|
||||
let notice = new Notice("Loading fonts for Typst...");
|
||||
this.compiler = await new typst.SystemWorld((this.app.vault.adapter as FileSystemAdapter).getBasePath(), this.settings.search_system);
|
||||
this.compiler = await new typst.SystemWorld("", (path: string) => this.get_file(path), this.settings.search_system);
|
||||
notice.hide();
|
||||
notice = new Notice("Finished loading fonts for Typst", 5000);
|
||||
|
||||
this.addSettingTab(new TypstSettingTab(this.app, this));
|
||||
this.registerMarkdownCodeBlockProcessor("typst", async (source, el, ctx) => {
|
||||
this.files.clear()
|
||||
for (const file of this.app.vault.getFiles()) {
|
||||
if (file.extension == "typ") {
|
||||
this.files.set(file.path, await this.app.vault.cachedRead(file))
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const image = this.compiler.compile(source, this.settings.pixel_per_pt, `${this.settings.fill}${this.settings.noFill ? "00" : "ff"}`);
|
||||
const bitmap = await createImageBitmap(image, { resizeWidth: 700, resizeHeight: image.height * (700 / image.width), resizeQuality: "high" })
|
||||
// el.createEl("img", {
|
||||
// attr: {
|
||||
// src: "data:image/png;base64," + Base64.fromUint8Array(image.data.)
|
||||
// }
|
||||
// })
|
||||
const width = el.clientWidth
|
||||
const bitmap = await createImageBitmap(image, { resizeWidth: width, resizeHeight: image.height * (width / image.width), resizeQuality: "high" })
|
||||
let canvas = el.createEl("canvas", {
|
||||
cls: "obsidian-typst",
|
||||
attr: {
|
||||
@ -93,6 +109,14 @@ export default class TypstPlugin extends Plugin {
|
||||
async saveSettings() {
|
||||
await this.saveData(this.settings);
|
||||
}
|
||||
|
||||
get_file(path: string) {
|
||||
if (this.files.has(path)) {
|
||||
return this.files.get(path)
|
||||
}
|
||||
console.error(`'${path}' is a folder or does not exist`);
|
||||
throw `'${path}' is a folder or does not exist`
|
||||
}
|
||||
}
|
||||
|
||||
class TypstSettingTab extends PluginSettingTab {
|
||||
|
44
package-lock.json
generated
44
package-lock.json
generated
@ -1,15 +1,15 @@
|
||||
{
|
||||
"name": "obsidian-sample-plugin",
|
||||
"version": "1.0.0",
|
||||
"name": "obsidian-typst-plugin",
|
||||
"version": "0.2.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "obsidian-sample-plugin",
|
||||
"version": "1.0.0",
|
||||
"license": "MIT",
|
||||
"name": "obsidian-typst-plugin",
|
||||
"version": "0.2.0",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"get-system-fonts": "^2.0.2",
|
||||
"js-base64": "^3.7.5",
|
||||
"obsidian-typst": "file:pkg",
|
||||
"temp": "^0.9.4"
|
||||
},
|
||||
@ -1400,14 +1400,6 @@
|
||||
"integrity": "sha512-dsKNQNdj6xA3T+QlADDA7mOSlX0qiMINjn0cgr+eGHGsbSHzTabcIogz2+p/iqP1Xs6EP/sS2SbqH+brGTbq0g==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/get-system-fonts": {
|
||||
"version": "2.0.2",
|
||||
"resolved": "https://registry.npmjs.org/get-system-fonts/-/get-system-fonts-2.0.2.tgz",
|
||||
"integrity": "sha512-zzlgaYnHMIEgHRrfC7x0Qp0Ylhw/sHpM6MHXeVBTYIsvGf5GpbnClB+Q6rAPdn+0gd2oZZIo6Tj3EaWrt4VhDQ==",
|
||||
"engines": {
|
||||
"node": ">8.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/glob": {
|
||||
"version": "7.2.3",
|
||||
"resolved": "https://registry.npmjs.org/glob/-/glob-7.2.3.tgz",
|
||||
@ -1590,6 +1582,11 @@
|
||||
"dev": true,
|
||||
"peer": true
|
||||
},
|
||||
"node_modules/js-base64": {
|
||||
"version": "3.7.5",
|
||||
"resolved": "https://registry.npmjs.org/js-base64/-/js-base64-3.7.5.tgz",
|
||||
"integrity": "sha512-3MEt5DTINKqfScXKfJFrRbxkrnk2AxPWGBL/ycjz4dK8iqiSJ06UxD8jh8xuh6p10TX4t2+7FsBYVxxQbMg+qA=="
|
||||
},
|
||||
"node_modules/js-sdsl": {
|
||||
"version": "4.4.0",
|
||||
"resolved": "https://registry.npmjs.org/js-sdsl/-/js-sdsl-4.4.0.tgz",
|
||||
@ -1766,8 +1763,8 @@
|
||||
}
|
||||
},
|
||||
"node_modules/obsidian-typst": {
|
||||
"version": "0.1.0",
|
||||
"resolved": "file:pkg"
|
||||
"resolved": "pkg",
|
||||
"link": true
|
||||
},
|
||||
"node_modules/once": {
|
||||
"version": "1.4.0",
|
||||
@ -2267,6 +2264,19 @@
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/sindresorhus"
|
||||
}
|
||||
}
|
||||
},
|
||||
"pkg": {
|
||||
"name": "obsidian-typst",
|
||||
"version": "0.2.0",
|
||||
"dependencies": {
|
||||
"obsidian-typst": "file:pkg",
|
||||
"temp": "^0.9.4"
|
||||
}
|
||||
},
|
||||
"pkg/node_modules/obsidian-typst": {
|
||||
"resolved": "pkg/pkg",
|
||||
"link": true
|
||||
},
|
||||
"pkg/pkg": {}
|
||||
}
|
||||
}
|
||||
|
@ -29,4 +29,4 @@
|
||||
"obsidian-typst": "file:pkg",
|
||||
"temp": "^0.9.4"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
53
src/lib.rs
53
src/lib.rs
@ -1,6 +1,5 @@
|
||||
use comemo::Prehashed;
|
||||
use elsa::FrozenVec;
|
||||
use js_sys::Uint8Array;
|
||||
use once_cell::unsync::OnceCell;
|
||||
use siphasher::sip128::{Hasher128, SipHasher};
|
||||
use std::{
|
||||
@ -33,11 +32,6 @@ extern "C" {
|
||||
#[wasm_bindgen(catch)]
|
||||
fn readFileSync(path: &str) -> Result<JsValue, JsValue>;
|
||||
}
|
||||
#[wasm_bindgen(module = "utils")]
|
||||
extern "C" {
|
||||
async fn get_fonts() -> JsValue;
|
||||
async fn get_local_fonts() -> JsValue;
|
||||
}
|
||||
|
||||
/// A world that provides access to the operating system.
|
||||
#[wasm_bindgen]
|
||||
@ -46,16 +40,21 @@ pub struct SystemWorld {
|
||||
library: Prehashed<Library>,
|
||||
book: Prehashed<FontBook>,
|
||||
fonts: Vec<FontSlot>,
|
||||
hashes: RefCell<HashMap<PathBuf, FileResult<PathHash>>>,
|
||||
hashes: RefCell<HashMap<PathBuf, PathHash>>,
|
||||
paths: RefCell<HashMap<PathHash, PathSlot>>,
|
||||
sources: FrozenVec<Box<Source>>,
|
||||
main: SourceId,
|
||||
js_read_file: js_sys::Function,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl SystemWorld {
|
||||
#[wasm_bindgen(constructor)]
|
||||
pub async fn new(root: String, search_system: bool) -> Result<SystemWorld, JsValue> {
|
||||
pub async fn new(
|
||||
root: String,
|
||||
js_read_file: &js_sys::Function,
|
||||
search_system: bool,
|
||||
) -> Result<SystemWorld, JsValue> {
|
||||
let mut searcher = FontSearcher::new();
|
||||
if search_system {
|
||||
searcher.search_system().await?;
|
||||
@ -72,6 +71,7 @@ impl SystemWorld {
|
||||
paths: RefCell::default(),
|
||||
sources: FrozenVec::new(),
|
||||
main: SourceId::detached(),
|
||||
js_read_file: js_read_file.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
@ -81,7 +81,6 @@ impl SystemWorld {
|
||||
pixel_per_pt: f32,
|
||||
fill: String,
|
||||
) -> Result<ImageData, JsValue> {
|
||||
|
||||
self.sources.as_mut().clear();
|
||||
self.hashes.borrow_mut().clear();
|
||||
self.paths.borrow_mut().clear();
|
||||
@ -124,7 +123,7 @@ impl World for SystemWorld {
|
||||
self.slot(path)?
|
||||
.source
|
||||
.get_or_init(|| {
|
||||
let buf = read(path)?;
|
||||
let buf = self.read_file(path)?;
|
||||
let text = String::from_utf8(buf)?;
|
||||
Ok(self.insert(path, text))
|
||||
})
|
||||
@ -151,7 +150,7 @@ impl World for SystemWorld {
|
||||
let path = path.as_path();
|
||||
self.slot(path)?
|
||||
.buffer
|
||||
.get_or_init(|| read(path).map(Buffer::from))
|
||||
.get_or_init(|| self.read_file(path).map(Buffer::from))
|
||||
.clone()
|
||||
}
|
||||
}
|
||||
@ -162,14 +161,14 @@ impl SystemWorld {
|
||||
let hash = match hashes.get(path).cloned() {
|
||||
Some(hash) => hash,
|
||||
None => {
|
||||
let hash = PathHash::new(path);
|
||||
let hash = PathHash::new(Buffer::from(self.read_file(&path)?));
|
||||
if let Ok(canon) = path.canonicalize() {
|
||||
hashes.insert(canon.normalize(), hash.clone());
|
||||
}
|
||||
hashes.insert(path.into(), hash.clone());
|
||||
hash
|
||||
}
|
||||
}?;
|
||||
};
|
||||
|
||||
Ok(std::cell::RefMut::map(self.paths.borrow_mut(), |paths| {
|
||||
paths.entry(hash).or_default()
|
||||
@ -182,15 +181,20 @@ impl SystemWorld {
|
||||
self.sources.push(Box::new(source));
|
||||
id
|
||||
}
|
||||
}
|
||||
|
||||
fn read(path: &Path) -> FileResult<Vec<u8>> {
|
||||
let f = |e: JsValue| {
|
||||
console::log_1(&e);
|
||||
FileError::Other
|
||||
};
|
||||
|
||||
Ok(Uint8Array::new(&readFileSync(path.to_str().unwrap()).map_err(f)?).to_vec())
|
||||
fn read_file(&self, path: &Path) -> FileResult<Vec<u8>> {
|
||||
let f1 = |e: JsValue| {
|
||||
console::error_1(&e);
|
||||
FileError::Other
|
||||
};
|
||||
Ok(self
|
||||
.js_read_file
|
||||
.call1(&JsValue::NULL, &path.to_str().unwrap().into())
|
||||
.map_err(f1)?
|
||||
.as_string()
|
||||
.unwrap()
|
||||
.into_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
/// Holds details about the location of a font and lazily the font itself.
|
||||
@ -205,11 +209,11 @@ struct FontSlot {
|
||||
struct PathHash(u128);
|
||||
|
||||
impl PathHash {
|
||||
fn new(path: &Path) -> FileResult<Self> {
|
||||
let handle = Buffer::from(read(path)?);
|
||||
fn new(handle: Buffer) -> Self {
|
||||
// let handle = Buffer::from(read(path)?);
|
||||
let mut state = SipHasher::new();
|
||||
handle.hash(&mut state);
|
||||
Ok(Self(state.finish128().as_u128()))
|
||||
Self(state.finish128().as_u128())
|
||||
}
|
||||
}
|
||||
|
||||
@ -262,7 +266,6 @@ impl FontSearcher {
|
||||
}
|
||||
|
||||
async fn search_system(&mut self) -> Result<(), JsValue> {
|
||||
self.add_embedded();
|
||||
if let Some(window) = web_sys::window() {
|
||||
for fontdata in JsFuture::from(window.query_local_fonts()?)
|
||||
.await?
|
||||
|
Reference in New Issue
Block a user