Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generate description metadata for SEO #74

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ typst = "0.11.1"
typst-assets = "0.11.1"
typst-ts-core = { version = "0.5.0-rc4" }
typst-ts-compiler = { version = "0.5.0-rc4" }
typst-ts-text-exporter = { version = "0.5.0-rc4" }
typst-ts-svg-exporter = { version = "0.5.0-rc4", features = [
"experimental-ligature",
] }
Expand Down Expand Up @@ -70,6 +71,7 @@ pathdiff = "0.2.1"
typst = { git = "https://github.com/Myriad-Dreamin/typst.git", branch = "typst.ts-v0.11.1-content-hint" }
typst-syntax = { git = "https://github.com/Myriad-Dreamin/typst.git", branch = "typst.ts-v0.11.1-content-hint" }
typst-ts-svg-exporter = { git = "https://github.com/Myriad-Dreamin/typst.ts", rev = "6cc978011c16328b7aaa0dc660bb73818977475b", package = "typst-ts-svg-exporter" }
typst-ts-text-exporter = { git = "https://github.com/Myriad-Dreamin/typst.ts", rev = "6cc978011c16328b7aaa0dc660bb73818977475b", package = "typst-ts-text-exporter" }
typst-ts-core = { git = "https://github.com/Myriad-Dreamin/typst.ts", rev = "6cc978011c16328b7aaa0dc660bb73818977475b", package = "typst-ts-core" }
typst-ts-compiler = { git = "https://github.com/Myriad-Dreamin/typst.ts", rev = "6cc978011c16328b7aaa0dc660bb73818977475b", package = "typst-ts-compiler" }

Expand Down
1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ typst-ts-compiler.workspace = true
handlebars.workspace = true

typst-ts-svg-exporter.workspace = true
typst-ts-text-exporter.workspace = true
pathdiff.workspace = true

[build-dependencies]
Expand Down
29 changes: 24 additions & 5 deletions cli/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use log::warn;
use serde::{Deserialize, Serialize};
use serde_json::json;
use typst_ts_compiler::service::Compiler;
use typst_ts_core::escape::{escape_str, AttributeEscapes};

use crate::{
error::prelude::*,
Expand Down Expand Up @@ -341,7 +342,7 @@ impl Project {
chapters
}

pub fn compile_chapter(&mut self, _ch: DataDict, path: &str) -> ZResult<String> {
pub fn compile_chapter(&mut self, _ch: DataDict, path: &str) -> ZResult<ChapterArtifact> {
let rel_data_path = std::path::Path::new(&self.path_to_root)
.join(path)
.with_extension("")
Expand All @@ -350,7 +351,7 @@ impl Project {
// windows
.replace('\\', "/");

self.tr.compile_page(Path::new(path))?;
let doc = self.tr.compile_page(Path::new(path))?;

let dynamic_load_trampoline = self
.hr
Expand All @@ -365,7 +366,15 @@ impl Project {
"render typst_load_trampoline for compile_chapter",
))?;

Ok(dynamic_load_trampoline.to_owned())
let description = self.tr.generate_desc(&doc)?;

Ok(ChapterArtifact {
content: dynamic_load_trampoline.to_owned(),
description: escape_str::<AttributeEscapes>(
&description.chars().take(512).collect::<String>(),
)
.into_owned(),
})
}

pub fn render_chapter(&mut self, chapter_data: DataDict, path: &str) -> ZResult<String> {
Expand All @@ -383,11 +392,16 @@ impl Project {
let renderer_module = format!("{}internal/typst_ts_renderer_bg.wasm", self.path_to_root);
data.insert("renderer_module".to_owned(), json!(renderer_module));

// inject content

let art = self.compile_chapter(chapter_data, path)?;

// inject content
data.insert(
"content".to_owned(),
serde_json::Value::String(self.compile_chapter(chapter_data, path)?),
"description".to_owned(),
serde_json::Value::String(art.description),
);
data.insert("content".to_owned(), serde_json::Value::String(art.content));

// inject path_to_root
data.insert("path_to_root".to_owned(), json!(self.path_to_root));
Expand Down Expand Up @@ -423,3 +437,8 @@ impl Project {
// }
// }
}

pub struct ChapterArtifact {
pub description: String,
pub content: String,
}
23 changes: 19 additions & 4 deletions cli/src/render/typst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use typst_ts_core::{
config::{compiler::EntryOpts, CompileOpts},
path::PathClean,
vector::ir::{LayoutRegionNode, PageMetadata},
TakeAs, TypstAbs, TypstDocument,
TakeAs, Transformer, TypstAbs, TypstDocument,
};

const THEME_LIST: [&str; 5] = ["light", "rust", "coal", "navy", "ayu"];
Expand Down Expand Up @@ -136,9 +136,11 @@ impl TypstRenderer {
.ok_or_else(|| error_once!("compile book.typ"))
}

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

let mut any_doc = None;

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

Expand Down Expand Up @@ -189,10 +191,23 @@ impl TypstRenderer {
});

let res = self.compiler.compile(&mut self.fork_env::<true>());
self.report(res)
let doc = self
.report(res)
.ok_or_else(|| error_once!("compile page theme", theme: theme))?;
any_doc = Some(doc.clone());
}

Ok(())
any_doc.ok_or_else(|| error_once!("compile page.typ"))
}

pub fn generate_desc(&mut self, doc: &TypstDocument) -> ZResult<String> {
let e = typst_ts_text_exporter::TextExporter::default();
let mut w = std::io::Cursor::new(Vec::new());
e.export(self.compiler.world(), (Arc::new(doc.clone()), &mut w))
.map_err(|e| error_once!("export text", error: format!("{e:?}")))?;

let w = w.into_inner();

String::from_utf8(w).map_err(|e| error_once!("export text", error: format!("{e:?}")))
}
}
Loading