Add math block override

Update `typst` to 0.3
Add preamables
This commit is contained in:
fenjalien
2023-04-27 00:18:18 +01:00
parent 4a59c1a109
commit 18c00f969c
7 changed files with 366 additions and 166 deletions

199
Cargo.lock generated
View File

@ -10,9 +10,9 @@ checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "aho-corasick"
version = "0.7.20"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac"
checksum = "67fc08ce920c31afb70f013dcce1bfc3a3195de6a228474e45e1f145b36f8d04"
dependencies = [
"memchr",
]
@ -92,10 +92,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bumpalo"
version = "3.12.0"
name = "bitflags"
version = "2.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535"
checksum = "24a6904aef64d73cf10ab17ebace7befb918b82164785cb89907993be7f83813"
dependencies = [
"serde",
]
[[package]]
name = "bumpalo"
version = "3.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b1ce199063694f33ffb7dd4e0ee620741495c32833cde5aa08f02a0bf96f0c8"
[[package]]
name = "bytemuck"
@ -123,9 +132,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chinese-number"
version = "0.7.1"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f9a8cffacecd7f477f8395021158af07c8a3f74523e9b90e4e4bb0105deaa74"
checksum = "bb8659add27dbba7575c058a3265d81cc078dc2693848445bf3788a883ec82c8"
dependencies = [
"chinese-variant",
"enum-ordinalize",
@ -228,15 +237,15 @@ dependencies = [
[[package]]
name = "ecow"
version = "0.1.0"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e60e2840fbfc397c7972b11a6e6bd99a0248921cc1e31f293c5f6c5ac24831da"
checksum = "c5c5051925c54d9a42c8652313b5358a7432eed209466b443ed5220431243a14"
[[package]]
name = "elsa"
version = "1.8.0"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f74077c3c3aedb99a2683919698285596662518ea13e5eedcf8bdd43b0d0453b"
checksum = "b5e0aca8dce8856e420195bd13b6a64de3334235ccc9214e824b86b12bf26283"
dependencies = [
"stable_deref_trait",
]
@ -265,6 +274,15 @@ dependencies = [
"regex",
]
[[package]]
name = "fdeflate"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d329bdeac514ee06249dabc27877490f17f5d371ec693360768b838e19f3ae10"
dependencies = [
"simd-adler32",
]
[[package]]
name = "flate2"
version = "1.0.25"
@ -528,6 +546,16 @@ dependencies = [
"adler",
]
[[package]]
name = "miniz_oxide"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7810e0be55b428ada41041c41f32c9f1a42817901b4ccf45fa3d4b6561e74c7"
dependencies = [
"adler",
"simd-adler32",
]
[[package]]
name = "num-bigint"
version = "0.4.3"
@ -611,7 +639,7 @@ version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "249f9b33a3192626f2cd9f4b0cd66c1ec32d65968d58cf4d8239977feddddead"
dependencies = [
"bitflags",
"bitflags 1.3.2",
"itoa",
"ryu",
]
@ -663,14 +691,15 @@ dependencies = [
[[package]]
name = "png"
version = "0.17.7"
version = "0.17.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d708eaf860a19b19ce538740d2b4bdeeb8337fa53f7738455e706623ad5c638"
checksum = "aaeebc51f9e7d2c150d3f3bfeb667f2aa985db5ef1e3d212847bdedb488beeaa"
dependencies = [
"bitflags",
"bitflags 1.3.2",
"crc32fast",
"fdeflate",
"flate2",
"miniz_oxide 0.6.2",
"miniz_oxide 0.7.1",
]
[[package]]
@ -739,13 +768,13 @@ checksum = "9ae028b272a6e99d9f8260ceefa3caa09300a8d6c8d2b2001316474bc52122e9"
[[package]]
name = "regex"
version = "1.7.3"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b1f693b24f6ac912f4893ef08244d70b6067480d2f1a46e950c9691e6749d1d"
checksum = "af83e617f331cc6ae2da5443c602dfa5af81e517212d9d611a5b3ba1777b5370"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
"regex-syntax 0.7.1",
]
[[package]]
@ -754,6 +783,12 @@ version = "0.6.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1"
[[package]]
name = "regex-syntax"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5996294f19bd3aae0453a862ad728f60e6600695733dd5df01da90c54363a3c"
[[package]]
name = "resvg"
version = "0.22.0"
@ -810,7 +845,7 @@ version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a617c811f5c9a7060fe511d35d13bf5b9f0463ce36d63ce666d05779df2b4eba"
dependencies = [
"bitflags",
"bitflags 1.3.2",
"bytemuck",
"smallvec",
"ttf-parser 0.15.2",
@ -852,9 +887,9 @@ checksum = "bebd363326d05ec3e2f532ab7660680f3b02130d780c299bca73469d521bc0ed"
[[package]]
name = "serde"
version = "1.0.159"
version = "1.0.160"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c04e8343c3daeec41f58990b9d77068df31209f2af111e059e9fe9646693065"
checksum = "bb2f3770c8bce3bcda7e149193a069a0f4365bda1fa5cd88e03bca26afc1216c"
dependencies = [
"serde_derive",
]
@ -872,26 +907,35 @@ dependencies = [
[[package]]
name = "serde_derive"
version = "1.0.159"
version = "1.0.160"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c614d17805b093df4b147b51339e7e44bf05ef59fba1e45d83500bcfb4d8585"
checksum = "291a097c63d8497e00160b166a967a4a79c64f3facdd01cbd7502231688d77df"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.13",
"syn 2.0.15",
]
[[package]]
name = "serde_json"
version = "1.0.95"
version = "1.0.96"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d721eca97ac802aa7777b701877c8004d950fc142651367300d21c1cc0194744"
checksum = "057d394a50403bcac12672b2b18fb387ab6d289d957dab67dd201875391e52f1"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "serde_spanned"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0efd8caf556a6cebd3b285caf480045fcc1ac04f6bd786b09a6f11af30c4fcf4"
dependencies = [
"serde",
]
[[package]]
name = "serde_yaml"
version = "0.8.26"
@ -904,6 +948,12 @@ dependencies = [
"yaml-rust",
]
[[package]]
name = "simd-adler32"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "238abfbb77c1915110ad968465608b68e869e0772622c9656714e73e5a1a522f"
[[package]]
name = "simplecss"
version = "0.2.1"
@ -1006,9 +1056,9 @@ dependencies = [
[[package]]
name = "syn"
version = "2.0.13"
version = "2.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c9da457c5285ac1f936ebd076af6dac17a61cfe7826f2076b4d015cf47bc8ec"
checksum = "a34fcf3e8b60f57e6a14301a2e916d323af98b0ea63c599441eec8558660c822"
dependencies = [
"proc-macro2",
"quote",
@ -1022,13 +1072,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c6c454c27d9d7d9a84c7803aaa3c50cd088d2906fe3c6e42da3209aa623576a8"
dependencies = [
"bincode",
"bitflags",
"bitflags 1.3.2",
"fancy-regex",
"flate2",
"fnv",
"lazy_static",
"once_cell",
"regex-syntax",
"regex-syntax 0.6.29",
"serde",
"serde_derive",
"serde_json",
@ -1036,12 +1086,6 @@ dependencies = [
"walkdir",
]
[[package]]
name = "thin-vec"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aac81b6fd6beb5884b0cf3321b8117e6e5d47ecb6fc89f414cfdcca8b2fe2dd8"
[[package]]
name = "thiserror"
version = "1.0.40"
@ -1059,7 +1103,7 @@ checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.13",
"syn 2.0.15",
]
[[package]]
@ -1101,12 +1145,45 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
[[package]]
name = "tracing"
version = "0.1.37"
name = "toml"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8"
checksum = "b403acf6f2bb0859c93c7f0d967cb4a75a7ac552100f9322faf64dc047669b21"
dependencies = [
"serde",
"serde_spanned",
"toml_datetime",
"toml_edit",
]
[[package]]
name = "toml_datetime"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ab8ed2edee10b50132aed5f331333428b011c99402b5a534154ed15746f9622"
dependencies = [
"serde",
]
[[package]]
name = "toml_edit"
version = "0.19.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "239410c8609e8125456927e6707163a3b1fdb40561e4b803bc041f466ccfdc13"
dependencies = [
"indexmap",
"serde",
"serde_spanned",
"toml_datetime",
"winnow",
]
[[package]]
name = "tracing"
version = "0.1.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf9cf6a813d3f40c88b0b6b6f29a5c95c6cdbf97c1f9cc53fb820200f5ad814d"
dependencies = [
"cfg-if",
"pin-project-lite",
"tracing-attributes",
"tracing-core",
@ -1114,13 +1191,13 @@ dependencies = [
[[package]]
name = "tracing-attributes"
version = "0.1.23"
version = "0.1.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4017f8f45139870ca7e672686113917c71c7a6e02d4924eda67186083c03081a"
checksum = "0f57e3ca2a01450b1a921183a9c9cbfda207fd822cef4ccb00a65402cbba7a74"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.109",
"syn 2.0.15",
]
[[package]]
@ -1158,10 +1235,10 @@ checksum = "6af6ae20167a9ece4bcb41af5b80f8a1f1df981f6391189ce00fd257af04126a"
[[package]]
name = "typst"
version = "0.2.0"
source = "git+https://github.com/typst/typst.git#b79121d4cf8cdddb7977cbb22b627ac4706434f0"
version = "0.3.0"
source = "git+https://github.com/typst/typst.git#5100046acf0fd31f805506f156c59c635c46cf93"
dependencies = [
"bitflags",
"bitflags 2.2.1",
"bytemuck",
"comemo",
"ecow",
@ -1171,7 +1248,7 @@ dependencies = [
"image",
"indexmap",
"log",
"miniz_oxide 0.5.4",
"miniz_oxide 0.7.1",
"once_cell",
"pdf-writer",
"pixglyph",
@ -1184,7 +1261,6 @@ dependencies = [
"stacker",
"subsetter",
"svg2pdf",
"thin-vec",
"tiny-skia",
"tracing",
"ttf-parser 0.18.1",
@ -1199,8 +1275,8 @@ dependencies = [
[[package]]
name = "typst-library"
version = "0.2.0"
source = "git+https://github.com/typst/typst.git#b79121d4cf8cdddb7977cbb22b627ac4706434f0"
version = "0.3.0"
source = "git+https://github.com/typst/typst.git#5100046acf0fd31f805506f156c59c635c46cf93"
dependencies = [
"chinese-number",
"comemo",
@ -1218,6 +1294,7 @@ dependencies = [
"serde_yaml",
"smallvec",
"syntect",
"toml",
"tracing",
"ttf-parser 0.18.1",
"typed-arena",
@ -1231,14 +1308,13 @@ dependencies = [
[[package]]
name = "typst-macros"
version = "0.2.0"
source = "git+https://github.com/typst/typst.git#b79121d4cf8cdddb7977cbb22b627ac4706434f0"
version = "0.3.0"
source = "git+https://github.com/typst/typst.git#5100046acf0fd31f805506f156c59c635c46cf93"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn 1.0.109",
"unscanny",
]
[[package]]
@ -1400,7 +1476,7 @@ dependencies = [
"once_cell",
"proc-macro2",
"quote",
"syn 2.0.13",
"syn 2.0.15",
"wasm-bindgen-shared",
]
@ -1431,7 +1507,7 @@ source = "git+https://github.com/fenjalien/wasm-bindgen.git#584383028e0fdb4bad92
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.13",
"syn 2.0.15",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
@ -1487,6 +1563,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "winnow"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae8970b36c66498d8ff1d66685dc86b91b29db0c7739899012f63a63814b4b28"
dependencies = [
"memchr",
]
[[package]]
name = "xi-unicode"
version = "0.3.0"

View File

@ -11,8 +11,8 @@ crate-type = ["cdylib"]
[dependencies]
# Everything to do with Typst
typst = { git = "https://github.com/typst/typst.git", version = "0.2" }
typst-library = { git = "https://github.com/typst/typst.git", version = "0.2" }
typst = { git = "https://github.com/typst/typst.git", version = "0.3" }
typst-library = { git = "https://github.com/typst/typst.git", version = "0.3" }
comemo = "0.2"
once_cell = "1.17.1"

View File

@ -1,28 +1,37 @@
# Obsidian Typst
Renders `typst` code blocks into images using [Typst](https://github.com/typst/typst) through the power of WASM! This is still very much in development, so suggestions/bugs are welcome!
Renders `typst` code blocks, and optionally math blocks, into images using [Typst](https://github.com/typst/typst) through the power of WASM! This is still very much in development, so suggestions and bugs are welcome!
## Things to NOTE
- Typst does not currently support exporting to HTML only PDFs and PNGs. So due to image scaling, the rendered views may look a bit terrible. If you know how to fix this PLEASE HELP.
- File paths should be relative to the vault folder.
- System fonts are not loaded by default as this takes about 20 seconds (on my machine). Their is an option in settings to enable them (requires a reload of the plugin).
- System fonts are not loaded by default as this takes about 20 seconds (on my machine). There is an option in settings to enable them (requires a reload of the plugin).
## Math Block Usage
The plugin can render `typst` inside math blocks! By default this is off, to enable it set the "Override Math Blocks" setting or use the "Toggle Math Block Override" command. Math block types are conserved between Obsidian and Typst, `$...$` -> `$...$` and `$$...$$` -> `$ ... $`.
From what I've experimented with, normal math blocks are okay with `typst` code but Typst is not happy with any Latex code.
For styling and using imports with math blocks see the next section.
## Preamables
Need to style your `typst` code the same way everytime and don't to write it out each time? Or using math blocks and need a way to import things? Use PREAMABLES!
Preamables are prepended to your `typst` code before compiling. There are three different types in settings:
- `shared`: Prepended to all `typst` code.
- `math`: Prepended to `typst` code only in math blocks.
- `code`: Prepended to `typst` code only in code blocks.
## Known Issues
### "File Not Found" Error on First Load of Obsidian
When Obsidian first loads it sometimes tries to render before its files are resolved and cached.
To fix, simply select then deselect everything in the file, or close and re-open the file.
## Example
### `conf.typ`
```typst
#let styling(ct) = {
set page(width: 525pt, height: auto, margin: (x: 0pt, y: 1pt))
set heading(numbering: "1.")
set text(white)
ct
}
```
### `Typst.md`
```
```typst
#import "conf.typ": styling
#show: styling
= Fibonacci sequence
The Fibonacci sequence is defined through the
_recurrence relation_ $F_n = F_(n-1) + F_(n-2)$.
@ -52,14 +61,14 @@ The first #count numbers of the sequence are:
<img src="assets/example.png">
## Installation
Until this plugin is submitted to the community plugins please install it by copying `main.js`, `styles.css`, and `manifest.json` from the releases tab to the folder `.obsidian/plugins/obsidian-typst`.
Until this plugin is submitted to the community plugins please install it by copying `main.js`, `styles.css`, and `manifest.json` from the releases tab to the folder `.obsidian/plugins/obsidian-typst` in your vault.
## TODO / Goals (In no particular order)
- [x] Better font loading
- [x] Fix importing
- [ ] Fix Github Actions
- [x] Fix Github Actions
- [ ] Better error handling
- [ ] Fix output image scaling
- [ ] Use HTML output
- [ ] Override default equation rendering
- [x] Override default equation rendering
- [ ] Custom editor for `.typ` files

202
main.ts
View File

@ -1,4 +1,4 @@
import { App, HexString, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
import { App, HexString, Notice, Plugin, PluginSettingTab, Setting, Workspace, loadMathJax } from 'obsidian';
// @ts-ignore
import typst_wasm_bin from './pkg/obsidian_typst_bg.wasm'
@ -11,93 +11,145 @@ interface TypstPluginSettings {
fill: HexString,
pixel_per_pt: number,
search_system: boolean,
override_math: boolean,
preamable: {
shared: string,
math: string,
code: string,
}
}
const DEFAULT_SETTINGS: TypstPluginSettings = {
noFill: false,
noFill: true,
fill: "#ffffff",
pixel_per_pt: 1,
pixel_per_pt: 3,
search_system: false,
override_math: false,
preamable: {
shared: "#let pxToPt = (p) => if p == auto {p} else {p * DPR * (72/96) * 1pt}\n#set text(fill: white, size: pxToPt(SIZE))",
math: "#set page(width: pxToPt(WIDTH), height: pxToPt(HEIGHT), margin: 0pt)\n#set align(horizon)\n#import \"physics.typ\": *",
code: "#set page(width: auto, height: auto, margin: 1em)"
}
}
export default class TypstPlugin extends Plugin {
settings: TypstPluginSettings;
compiler: typst.SystemWorld;
files: Map<string, string>;
tex2chtml: any;
resizeObserver: ResizeObserver;
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("", (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))
}
}
this.registerEvent(
this.app.metadataCache.on("resolved", () => this.updateFileCache())
)
try {
const image = this.compiler.compile(source, this.settings.pixel_per_pt, `${this.settings.fill}${this.settings.noFill ? "00" : "ff"}`);
// 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: {
width: bitmap.width,
height: bitmap.height,
}
});
let ctx = canvas.getContext("2d");
ctx?.drawImage(bitmap, 0, 0);
} catch (error) {
console.error(error);
}
await loadMathJax()
// @ts-expect-error
this.tex2chtml = MathJax.tex2chtml
this.overrideMathJax(this.settings.override_math)
this.addCommand({
id: "typst-math-override",
name: "Toggle Math Block Override",
callback: () => this.overrideMathJax(!this.settings.override_math)
})
this.addCommand({
id: "typst-update-files",
name: "Update Cached .typ Files",
callback: () => this.updateFileCache()
})
this.addSettingTab(new TypstSettingTab(this.app, this));
this.registerMarkdownCodeBlockProcessor("typst", (source, el, ctx) => {
el.appendChild(this.compileTypst(`${this.settings.preamable.code}\n${source}`, true))
})
/// Renders typst using the cli
// this.registerMarkdownCodeBlockProcessor("typst", (source, el, ctx) => {
// temp.mkdir("obsidian-typst", (err, folder) => {
// if (err) {
// el.innerHTML = err;
// console.log(err);
// } else {
// fs.writeFileSync(path.join(folder, "main.typ"), source)
// exec(
// `typst main.typ --image ${this.settings.noFill ? "--no-fill" : "--fill=" + this.settings.fill} --pixel-per-pt=${this.settings.pixel_per_pt}`,
// { cwd: folder }, (err, stdout, stderr) => {
// if (err || stdout || stderr) {
// // console.log(err, stdout, stderr);
// el.innerHTML = [String(err), stdout, stderr.replace(/\u001b[^m]*?m/g, "")].join("\n")
// } else {
// el.createEl("img", {
// cls: "obsidian-typst",
// attr: {
// src: `data:image/png;base64,${fs.readFileSync(path.join(folder, "main-1.png")).toString("base64")}`
// }
// })
// }
// })
// }
// // temp.cleanup()
// })
// });
console.log("loaded Typst");
}
onunload() {
typst2Image(source: string) {
return this.compiler.compile(source, this.settings.pixel_per_pt, `${this.settings.fill}${this.settings.noFill ? "00" : "ff"}`)
}
typst2Canvas(source: string) {
const image = this.typst2Image(source)
let canvas = createEl("canvas", {
attr: {
width: image.width,
height: image.height
},
cls: "typst"
})
let ctx = canvas.getContext("2d");
ctx!.imageSmoothingEnabled = true
ctx!.imageSmoothingQuality = "high"
ctx?.putImageData(image, 0, 0);
return canvas
}
compileTypst(source: string, display: boolean) {
const fontSize = parseFloat(document.body.getCssPropertyValue("--font-text-size"))
let size = null;
let line_height;
try {
if (display) {
size = parseFloat(document.body.getCssPropertyValue("--file-line-width"))
} else {
line_height = parseFloat(document.body.getCssPropertyValue("--line-height-normal"))
size = line_height * fontSize
}
let canvas = this.typst2Canvas(`#let (WIDTH, HEIGHT, SIZE, DPR) = (${display ? size : "auto"}, ${!display ? size : "auto"}, ${fontSize}, ${window.devicePixelRatio})\n${this.settings.preamable.shared}\n${source}`)
if (display) {
canvas.style.width = `100%`;
} else {
console.log(size, fontSize, line_height);
canvas.style.verticalAlign = "bottom"
canvas.style.height = `${size}px`
}
return canvas
} catch (error) {
console.error(error);
let span = createSpan()
span.innerText = error
return span
}
}
typstMath2Html(source: string, r: { display: boolean }) {
const display = r.display;
source = `${this.settings.preamable.math}\n${display ? `$ ${source} $` : `$${source}$`}`
return this.compileTypst(source, display)
}
onunload() {
//@ts-expect-error
MathJax.tex2chtml = this.tex2chtml
}
async overrideMathJax(value: boolean) {
this.settings.override_math = value
await this.saveSettings();
if (this.settings.override_math) {
// @ts-expect-error
MathJax.tex2chtml = (e, r) => this.typstMath2Html(e, r)
} else {
// @ts-expect-error
MathJax.tex2chtml = this.tex2chtml
}
}
async loadSettings() {
@ -112,9 +164,18 @@ export default class TypstPlugin extends Plugin {
if (this.files.has(path)) {
return this.files.get(path)
}
console.error(`'${path}' is a folder or does not exist`);
console.error(`'${path}' is a folder or does not exist`, this.files.keys());
throw `'${path}' is a folder or does not exist`
}
async updateFileCache() {
this.files = new Map()
for (const file of this.app.vault.getFiles()) {
if (file.extension == "typ") {
this.files.set(file.path, await this.app.vault.cachedRead(file))
}
}
}
}
class TypstSettingTab extends PluginSettingTab {
@ -177,5 +238,22 @@ class TypstSettingTab extends PluginSettingTab {
await this.plugin.saveSettings();
})
})
new Setting(containerEl)
.setName("Override Math Blocks")
.addToggle((toggle) => {
toggle.setValue(this.plugin.settings.override_math)
.onChange((value) => this.plugin.overrideMathJax(value))
});
new Setting(containerEl)
.setName("Shared Preamable")
.addTextArea((c) => c.setValue(this.plugin.settings.preamable.shared).onChange(async (value) => { this.plugin.settings.preamable.shared = value; await this.plugin.saveSettings() }))
new Setting(containerEl)
.setName("Code Block Preamable")
.addTextArea((c) => c.setValue(this.plugin.settings.preamable.code).onChange(async (value) => { this.plugin.settings.preamable.code = value; await this.plugin.saveSettings() }))
new Setting(containerEl)
.setName("Math Block Preamable")
.addTextArea((c) => c.setValue(this.plugin.settings.preamable.math).onChange(async (value) => { this.plugin.settings.preamable.math = value; await this.plugin.saveSettings() }))
}
}

74
package-lock.json generated
View File

@ -1,15 +1,18 @@
{
"name": "obsidian-typst-plugin",
"version": "0.2.0",
"version": "0.3.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "obsidian-typst-plugin",
"version": "0.2.0",
"version": "0.3.0",
"license": "Apache-2.0",
"dependencies": {
"js-base64": "^3.7.5",
"@codemirror/language": "^6.6.0",
"@codemirror/state": "^6.2.0",
"@codemirror/view": "^6.10.0",
"@lezer/common": "^1.0.2",
"obsidian-typst": "file:pkg",
"temp": "^0.9.4"
},
@ -26,19 +29,28 @@
"typescript": "4.7.4"
}
},
"node_modules/@codemirror/language": {
"version": "6.6.0",
"resolved": "https://registry.npmjs.org/@codemirror/language/-/language-6.6.0.tgz",
"integrity": "sha512-cwUd6lzt3MfNYOobdjf14ZkLbJcnv4WtndYaoBkbor/vF+rCNguMPK0IRtvZJG4dsWiaWPcK8x1VijhvSxnstg==",
"dependencies": {
"@codemirror/state": "^6.0.0",
"@codemirror/view": "^6.0.0",
"@lezer/common": "^1.0.0",
"@lezer/highlight": "^1.0.0",
"@lezer/lr": "^1.0.0",
"style-mod": "^4.0.0"
}
},
"node_modules/@codemirror/state": {
"version": "6.2.0",
"resolved": "https://registry.npmjs.org/@codemirror/state/-/state-6.2.0.tgz",
"integrity": "sha512-69QXtcrsc3RYtOtd+GsvczJ319udtBf1PTrr2KbLWM/e2CXUPnh0Nz9AUo8WfhSQ7GeL8dPVNUmhQVgpmuaNGA==",
"dev": true,
"peer": true
"integrity": "sha512-69QXtcrsc3RYtOtd+GsvczJ319udtBf1PTrr2KbLWM/e2CXUPnh0Nz9AUo8WfhSQ7GeL8dPVNUmhQVgpmuaNGA=="
},
"node_modules/@codemirror/view": {
"version": "6.9.3",
"resolved": "https://registry.npmjs.org/@codemirror/view/-/view-6.9.3.tgz",
"integrity": "sha512-BJ5mvEIhFM+SrNwc5X8pLIvMM9ffjkviVbxpg84Xk2OE8ZyKaEbId8kX+nAYEEso7+qnbwsXe1bkAHsasebMow==",
"dev": true,
"peer": true,
"version": "6.10.0",
"resolved": "https://registry.npmjs.org/@codemirror/view/-/view-6.10.0.tgz",
"integrity": "sha512-Oea3rvE4JQLMmLsy2b54yxXQJgJM9xKpUQIpF/LGgKUTH2lA06GAmEtKKWn5OUnbW3jrH1hHeUd8DJEgePMOeQ==",
"dependencies": {
"@codemirror/state": "^6.1.4",
"style-mod": "^4.0.0",
@ -493,6 +505,27 @@
"dev": true,
"peer": true
},
"node_modules/@lezer/common": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/@lezer/common/-/common-1.0.2.tgz",
"integrity": "sha512-SVgiGtMnMnW3ActR8SXgsDhw7a0w0ChHSYAyAUxxrOiJ1OqYWEKk/xJd84tTSPo1mo6DXLObAJALNnd0Hrv7Ng=="
},
"node_modules/@lezer/highlight": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/@lezer/highlight/-/highlight-1.1.4.tgz",
"integrity": "sha512-IECkFmw2l7sFcYXrV8iT9GeY4W0fU4CxX0WMwhmhMIVjoDdD1Hr6q3G2NqVtLg/yVe5n7i4menG3tJ2r4eCrPQ==",
"dependencies": {
"@lezer/common": "^1.0.0"
}
},
"node_modules/@lezer/lr": {
"version": "1.3.4",
"resolved": "https://registry.npmjs.org/@lezer/lr/-/lr-1.3.4.tgz",
"integrity": "sha512-7o+e4og/QoC/6btozDPJqnzBhUaD1fMfmvnEKQO1wRRiTse1WxaJ3OMEXZJnkgT6HCcTVOctSoXK9jGJw2oe9g==",
"dependencies": {
"@lezer/common": "^1.0.0"
}
},
"node_modules/@nodelib/fs.scandir": {
"version": "2.1.5",
"resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz",
@ -1582,11 +1615,6 @@
"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",
@ -2073,9 +2101,7 @@
"node_modules/style-mod": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/style-mod/-/style-mod-4.0.2.tgz",
"integrity": "sha512-C4myMmRTO8iaC5Gg+N1ftK2WT4eXUTMAa+HEFPPrfVeO/NtqLTtAmV1HbqnuGtLwCek44Ra76fdGUkSqjiMPcQ==",
"dev": true,
"peer": true
"integrity": "sha512-C4myMmRTO8iaC5Gg+N1ftK2WT4eXUTMAa+HEFPPrfVeO/NtqLTtAmV1HbqnuGtLwCek44Ra76fdGUkSqjiMPcQ=="
},
"node_modules/supports-color": {
"version": "7.2.0",
@ -2211,9 +2237,7 @@
"node_modules/w3c-keyname": {
"version": "2.2.6",
"resolved": "https://registry.npmjs.org/w3c-keyname/-/w3c-keyname-2.2.6.tgz",
"integrity": "sha512-f+fciywl1SJEniZHD6H+kUO8gOnwIr7f4ijKA6+ZvJFjeGi1r4PDLl53Ayud9O/rk64RqgoQine0feoeOU0kXg==",
"dev": true,
"peer": true
"integrity": "sha512-f+fciywl1SJEniZHD6H+kUO8gOnwIr7f4ijKA6+ZvJFjeGi1r4PDLl53Ayud9O/rk64RqgoQine0feoeOU0kXg=="
},
"node_modules/which": {
"version": "2.0.2",
@ -2267,8 +2291,12 @@
},
"pkg": {
"name": "obsidian-typst",
"version": "0.2.0",
"version": "0.3.0",
"dependencies": {
"@codemirror/language": "^6.6.0",
"@codemirror/state": "^6.2.0",
"@codemirror/view": "^6.10.0",
"@lezer/common": "^1.0.2",
"obsidian-typst": "file:pkg",
"temp": "^0.9.4"
}

View File

@ -26,6 +26,10 @@
"typescript": "4.7.4"
},
"dependencies": {
"@codemirror/language": "^6.6.0",
"@codemirror/state": "^6.2.0",
"@codemirror/view": "^6.10.0",
"@lezer/common": "^1.0.2",
"obsidian-typst": "file:pkg",
"temp": "^0.9.4"
}

View File

@ -1,4 +0,0 @@
.obsidian-typst {
display: block;
margin: auto;
}