Skip to content

Commit

Permalink
Revert "build: upgrade typst.ts to v0.4.1-rc2 (#34)"
Browse files Browse the repository at this point in the history
This reverts commit 5e82eb3.
  • Loading branch information
Myriad-Dreamin authored Nov 30, 2023
1 parent 5e82eb3 commit 9e415c4
Show file tree
Hide file tree
Showing 8 changed files with 484 additions and 345 deletions.
346 changes: 152 additions & 194 deletions Cargo.lock

Large diffs are not rendered by default.

23 changes: 11 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ strip = true

[workspace.dependencies]

typst = "0.9.0"
typst-library = "0.9.0"
typst-ts-svg-exporter = "0.4.1-rc2"
typst-ts-core = "0.4.1-rc2"
typst-ts-compiler = "0.4.1-rc2"
typst = "0.8.0"
typst-library = "0.8.0"
typst-ts-svg-exporter = "0.4.0-rc5"
typst-ts-core = "0.4.0-rc5"
typst-ts-compiler = "0.4.0-rc5"

handlebars = "4.3"

Expand Down Expand Up @@ -59,13 +59,12 @@ include_dir = "0.7.3"
pathdiff = "0.1.0"

[patch.crates-io]
typst = { git = "https://github.com/Myriad-Dreamin/typst.git", branch = "typst.ts-v0.9.0-content-hint" }
typst-library = { git = "https://github.com/Myriad-Dreamin/typst.git", branch = "typst.ts-v0.9.0-content-hint" }
typst-syntax = { git = "https://github.com/Myriad-Dreamin/typst.git", branch = "typst.ts-v0.9.0-content-hint" }
typst-ts-svg-exporter = { git = "https://github.com/Myriad-Dreamin/typst.ts", rev = "005161c13cd2493fc1b7da8337dec2a8fbedd3fb", package = "typst-ts-svg-exporter" }
typst-ts-core = { git = "https://github.com/Myriad-Dreamin/typst.ts", rev = "005161c13cd2493fc1b7da8337dec2a8fbedd3fb", package = "typst-ts-core" }
typst-ts-compiler = { git = "https://github.com/Myriad-Dreamin/typst.ts", rev = "005161c13cd2493fc1b7da8337dec2a8fbedd3fb", package = "typst-ts-compiler" }
hayagriva = { git = "https://github.com/Myriad-Dreamin/hayagriva.git", branch = "typst.ts-v0.9.0" }
typst = { git = "https://github.com/Myriad-Dreamin/typst.git", branch = "typst.ts-v0.8.0" }
typst-library = { git = "https://github.com/Myriad-Dreamin/typst.git", branch = "typst.ts-v0.8.0" }
typst-syntax = { git = "https://github.com/Myriad-Dreamin/typst.git", branch = "typst.ts-v0.8.0" }
typst-ts-svg-exporter = { git = "https://github.com/Myriad-Dreamin/typst.ts", rev = "ebb4b81c6caf359648ae3343423b5b11ce77a9d2", package = "typst-ts-svg-exporter" }
typst-ts-core = { git = "https://github.com/Myriad-Dreamin/typst.ts", rev = "ebb4b81c6caf359648ae3343423b5b11ce77a9d2", package = "typst-ts-core" }
typst-ts-compiler = { git = "https://github.com/Myriad-Dreamin/typst.ts", rev = "ebb4b81c6caf359648ae3343423b5b11ce77a9d2", package = "typst-ts-compiler" }

# typst = { path = "../typst/crates/typst" }
# typst-library = { path = "../typst/crates/typst-library" }
Expand Down
2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ bench = false
doc = false

[dependencies]
typst.workspace = true
# typst.workspace = true
# typst-library.workspace = true

warp.workspace = true
Expand Down
25 changes: 11 additions & 14 deletions cli/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::path::{Path, PathBuf};
use log::warn;
use serde::{Deserialize, Serialize};
use serde_json::json;
use typst_ts_compiler::service::Compiler;
use typst_ts_compiler::service::{Compiler, DiagObserver};

use crate::{
error::prelude::*,
Expand Down Expand Up @@ -131,10 +131,9 @@ impl Project {
let res = self
.tr
.compiler
.query("<typst-book-internal-package-meta>".to_string(), &doc);
let res = self
.tr
.report(res)
.with_compile_diag::<false, _>(|c| {
c.query("<typst-book-internal-package-meta>".to_string(), &doc)
})
.ok_or_else(|| error_once!("retrieve book meta from book.toml"))?;
let res = serde_json::to_value(&res)
.map_err(map_string_err("convert_to<InternalPackageMeta>"))?;
Expand All @@ -161,10 +160,9 @@ impl Project {
let res = self
.tr
.compiler
.query("<typst-book-book-meta>".to_string(), &doc);
let res = self
.tr
.report(res)
.with_compile_diag::<false, _>(|c| {
c.query("<typst-book-book-meta>".to_string(), &doc)
})
.ok_or_else(|| error_once!("retrieve book meta from book.toml"))?;
let res = serde_json::to_value(&res).map_err(map_string_err("convert_to<BookMeta>"))?;
let res: Json<BookMeta> =
Expand All @@ -186,10 +184,9 @@ impl Project {
let res = self
.tr
.compiler
.query("<typst-book-build-meta>".to_string(), &doc);
let res = self
.tr
.report(res)
.with_compile_diag::<false, _>(|c| {
c.query("<typst-book-build-meta>".to_string(), &doc)
})
.ok_or_else(|| error_once!("retrieve build meta from book.toml"))?;
let res =
serde_json::to_value(&res).map_err(map_string_err("convert_to<BuildMeta>"))?;
Expand Down Expand Up @@ -235,7 +232,7 @@ impl Project {
// .unwrap();

// copy files
create_dirs(self.dest_dir.join("renderer"))?;
create_dirs(&self.dest_dir.join("renderer"))?;
write_file(
self.dest_dir.join("renderer/typst_ts_renderer_bg.wasm"),
include_bytes!(
Expand Down
74 changes: 16 additions & 58 deletions cli/src/render/typst.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
use std::{
path::{Path, PathBuf},
sync::Arc,
};
use std::path::{Path, PathBuf};

use crate::{
error::prelude::*,
font::EMBEDDED_FONT,
utils::{make_absolute, make_absolute_from, UnwrapOrExit},
CompileArgs,
};
use typst::diag::SourceResult;
use typst_ts_compiler::{
service::{
features::WITH_COMPILING_STATUS_FEATURE, CompileDriver, CompileEnv, CompileReport,
CompileReporter, Compiler, ConsoleDiagReporter, DynamicLayoutCompiler, FeatureSet,
},
service::{CompileDriver, Compiler, DiagObserver, DynamicLayoutCompiler},
TypstSystemWorld,
};
use typst_ts_core::{config::CompileOpts, path::PathClean, TypstAbs, TypstDocument};

const THEME_LIST: [&str; 5] = ["light", "rust", "coal", "navy", "ayu"];

pub struct TypstRenderer {
pub status_env: Arc<FeatureSet>,
pub compiler: CompileReporter<DynamicLayoutCompiler<CompileDriver>>,
pub compiler: DynamicLayoutCompiler<CompileDriver>,
pub root_dir: PathBuf,
pub dest_dir: PathBuf,
}
Expand All @@ -50,94 +42,60 @@ impl TypstRenderer {
let mut driver = DynamicLayoutCompiler::new(driver, Default::default()).with_enable(true);
driver.set_extension("multi.sir.in".to_owned());
driver.set_layout_widths([750., 650., 550., 450., 350.].map(TypstAbs::raw).to_vec());
let driver =
CompileReporter::new(driver).with_generic_reporter(ConsoleDiagReporter::default());

Self {
status_env: Arc::new(
FeatureSet::default().configure(&WITH_COMPILING_STATUS_FEATURE, true),
),
compiler: driver,
root_dir,
dest_dir,
}
}

fn compiler_layer_mut(&mut self) -> &mut DynamicLayoutCompiler<CompileDriver> {
&mut self.compiler.compiler
}

pub fn fix_dest_dir(&mut self, path: &Path) {
let dest_dir = make_absolute_from(path, || self.root_dir.clone()).clean();
self.dest_dir = dest_dir;
}

fn set_theme_target(&mut self, theme: &str) {
self.compiler_layer_mut().set_target(if theme.is_empty() {
self.compiler.set_target(if theme.is_empty() {
"web".to_owned()
} else {
format!("web-{theme}")
});

self.compiler_layer_mut()
.set_extension(if theme.is_empty() {
"multi.sir.in".to_owned()
} else {
format!("{theme}.multi.sir.in")
});
self.compiler.set_extension(if theme.is_empty() {
"multi.sir.in".to_owned()
} else {
format!("{theme}.multi.sir.in")
});
}

fn setup_entry(&mut self, path: &Path) {
if path.is_absolute() {
panic!("entry file must be relative to the workspace");
}
self.compiler_layer_mut().compiler.entry_file = self.root_dir.join(path).clean();
self.compiler.compiler.entry_file = self.root_dir.join(path).clean();
let output_path = self.dest_dir.join(path).with_extension("").clean();
std::fs::create_dir_all(output_path.parent().unwrap()).unwrap_or_exit();
self.compiler_layer_mut().set_output(output_path);
self.compiler.set_output(output_path);
}

pub fn fork_env<const REPORT_STATUS: bool>(&self) -> CompileEnv {
let res = CompileEnv::default();
if REPORT_STATUS {
res.configure_shared(self.status_env.clone())
} else {
res
}
}

pub fn report<T>(&self, may_value: SourceResult<T>) -> Option<T> {
match may_value {
Ok(v) => Some(v),
Err(err) => {
let rep =
CompileReport::CompileError(self.compiler.main_id(), err, Default::default());
let rep = Arc::new((Default::default(), rep));
// we currently ignore export error here
let _ = self.compiler.reporter.export(self.compiler.world(), rep);
None
}
}
}

pub fn compile_book(&mut self, path: &Path) -> ZResult<Arc<TypstDocument>> {
pub fn compile_book(&mut self, path: &Path) -> ZResult<TypstDocument> {
self.setup_entry(path);
self.set_theme_target("");

self.compiler
.pure_compile(&mut self.fork_env::<true>())
.map_err(|_| error_once!("compile book.typ"))
.with_compile_diag::<true, _>(Compiler::pure_compile)
.ok_or_else(|| error_once!("compile book.typ"))
}

pub fn compile_page(&mut self, path: &Path) -> ZResult<()> {
self.setup_entry(path);

for theme in THEME_LIST {
self.set_theme_target(theme);

self.compiler
.compile(&mut self.fork_env::<true>())
.map_err(|_| error_once!("compile page theme", theme: theme))?;
.with_compile_diag::<true, _>(Compiler::compile)
.ok_or_else(|| error_once!("compile page theme", theme: theme))?;
}

Ok(())
Expand Down
13 changes: 5 additions & 8 deletions contrib/typst/gh-pages.typ
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,13 @@
// render a dash to hint headings instead of bolding it.
show heading : set text(weight: "regular") if is-web-target
show heading : it => locate(loc => {
it
if is-web-target {
style(styles => {
let h = measure(it.body, styles).height;
place(left, dx: -20pt, dy: -h - 12pt, [
#set text(fill: dash-color)
#link(loc)[\#]
])
})
place(left, dx: -20pt, [
#set text(fill: dash-color)
#link(loc)[\#]
])
}
it
})

// link setting
Expand Down
48 changes: 4 additions & 44 deletions frontend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { kObject } from '@myriaddreamin/typst.ts/dist/esm/internal.types.mjs';
import {
RenderSession,
TypstRenderer,
TypstSvgRenderer,
createTypstRenderer,
} from '@myriaddreamin/typst.ts/dist/esm/renderer.mjs';

Expand All @@ -20,45 +20,9 @@ window.TypstRenderModule = {
// }, delay);
// }) as unknown as T;
// };
function postProcessCrossLinks(appElem: HTMLDivElement) {
appElem.querySelectorAll('.pseudo-link').forEach(link => {
// update target
const a = link.parentElement!;
if (origin && a.getAttribute('onclick') === null) {
let target = a.getAttribute('target');
if (target === '_blank') {
// remove the target attribute
a.removeAttribute('target');
}
}

// update cross-link
const href = a.getAttribute('href')! || a.getAttribute('xlink:href')!;
if (href.startsWith('cross-link')) {
const url = new URL(href);
const pathLabelUnicodes = url.searchParams.get('path-label')!;
const plb = pathLabelUnicodes
.split('-')
.map(s => {
const n = Number.parseInt(s);
if (Number.isNaN(n)) {
return s;
} else {
return String.fromCharCode(n);
}
})
.join('')
.replace('.typ', '.html');
const absolutePath = new URL(plb, window.location.href).href;
a.setAttribute('href', absolutePath);
a.setAttribute('xlink:href', absolutePath);
// todo: label handling
}
});
}

window.typstBookRenderPage = function (
plugin: TypstRenderer,
plugin: TypstSvgRenderer,
relPath: string,
appContainer: HTMLDivElement | undefined,
) {
Expand Down Expand Up @@ -108,12 +72,8 @@ window.typstBookRenderPage = function (
// todo: bad performance
appElem.style.margin = `0px`;

await plugin.renderToSvg({
renderSession: svgModule!,
container: appElem,
});

postProcessCrossLinks(appElem);
// todo: merge
await plugin.renderSvg(svgModule!, appElem);

// const t2 = performance.now();
// console.log(
Expand Down
Loading

0 comments on commit 9e415c4

Please sign in to comment.