mirror of
https://github.com/typst/typst
synced 2025-05-13 12:36:23 +08:00
Custom link callback
This commit is contained in:
parent
7d28e6499e
commit
149e2b055b
@ -48,6 +48,7 @@ impl Html {
|
||||
let mut raw = String::new();
|
||||
md::html::push_html(&mut raw, iter);
|
||||
raw.truncate(raw.trim_end().len());
|
||||
|
||||
Html { md: text.into(), raw, description }
|
||||
}
|
||||
|
||||
@ -171,19 +172,23 @@ impl<'a> Handler<'a> {
|
||||
true
|
||||
}
|
||||
|
||||
fn handle_image(&self, path: &str) -> String {
|
||||
let data = IMAGES
|
||||
.get_file(path)
|
||||
.unwrap_or_else(|| panic!("missing image: {path}"))
|
||||
.contents();
|
||||
self.resolver.image(&path, data).into()
|
||||
fn handle_image(&self, link: &str) -> String {
|
||||
if let Some(file) = IMAGES.get_file(link) {
|
||||
self.resolver.image(&link, file.contents()).into()
|
||||
} else if let Some(url) = self.resolver.link(link) {
|
||||
url
|
||||
} else {
|
||||
panic!("missing image: {link}")
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_link(&self, link: &str) -> Option<String> {
|
||||
if link.starts_with(['#', 'h']) {
|
||||
if link.starts_with('#') || link.starts_with("http") {
|
||||
return Some(link.into());
|
||||
} else if !link.starts_with('$') {
|
||||
return None;
|
||||
}
|
||||
|
||||
if !link.starts_with('$') {
|
||||
return self.resolver.link(link);
|
||||
}
|
||||
|
||||
let root = link.split('/').next()?;
|
||||
@ -196,9 +201,10 @@ impl<'a> Handler<'a> {
|
||||
"$styling" => "/docs/reference/styling/",
|
||||
"$scripting" => "/docs/reference/scripting/",
|
||||
"$types" => "/docs/reference/types/",
|
||||
"$community" => "/docs/community/",
|
||||
"$type" => "/docs/reference/types/",
|
||||
"$func" => "/docs/reference/",
|
||||
"$changelog" => "/docs/changelog/",
|
||||
"$community" => "/docs/community/",
|
||||
_ => panic!("unknown link root: {root}"),
|
||||
};
|
||||
|
||||
@ -221,6 +227,7 @@ impl<'a> Handler<'a> {
|
||||
route.push_str(info.category);
|
||||
route.push('/');
|
||||
route.push_str(name);
|
||||
route.push('/');
|
||||
if let Some(param) = param {
|
||||
route.push_str("#parameters--");
|
||||
route.push_str(param);
|
||||
|
@ -60,6 +60,9 @@ pub fn provide(resolver: &dyn Resolver) -> Vec<PageModel> {
|
||||
|
||||
/// Resolve consumer dependencies.
|
||||
pub trait Resolver {
|
||||
/// Try to resolve a link that the system cannot resolve itself.
|
||||
fn link(&self, link: &str) -> Option<String>;
|
||||
|
||||
/// Produce an URL for an image file.
|
||||
fn image(&self, filename: &str, data: &[u8]) -> String;
|
||||
|
||||
@ -294,6 +297,7 @@ fn category_page(resolver: &dyn Resolver, category: &str) -> PageModel {
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct FuncModel {
|
||||
pub name: &'static str,
|
||||
pub display: &'static str,
|
||||
pub oneliner: &'static str,
|
||||
pub details: Html,
|
||||
pub showable: bool,
|
||||
@ -330,6 +334,7 @@ fn function_page(
|
||||
fn func_model(resolver: &dyn Resolver, func: &Func, info: &FuncInfo) -> FuncModel {
|
||||
FuncModel {
|
||||
name: info.name.into(),
|
||||
display: info.display,
|
||||
oneliner: oneliner(info.docs),
|
||||
details: Html::markdown(resolver, info.docs),
|
||||
showable: func.select(None).is_ok() && info.category != "math",
|
||||
@ -442,7 +447,7 @@ fn types_page(resolver: &dyn Resolver, parent: &str) -> PageModel {
|
||||
let mut items = vec![];
|
||||
|
||||
for model in type_models(resolver) {
|
||||
let route = format!("{route}{}/", model.name);
|
||||
let route = format!("{route}{}/", urlify(&model.name));
|
||||
items.push(CategoryItem {
|
||||
name: model.name.clone(),
|
||||
route: route.clone(),
|
||||
@ -694,7 +699,7 @@ fn details(key: &str) -> &str {
|
||||
}
|
||||
|
||||
/// Turn a title into an URL fragment.
|
||||
fn urlify(title: &str) -> String {
|
||||
pub fn urlify(title: &str) -> String {
|
||||
title
|
||||
.chars()
|
||||
.map(|c| c.to_ascii_lowercase())
|
||||
|
@ -39,9 +39,9 @@ math: |
|
||||
{ x in RR | x "is natural" } $
|
||||
```
|
||||
|
||||
Math mode defines a wide selection of [symbols]($category/math/symbols) like
|
||||
`pi`, `dot`, or `RR`. Many mathematical symbols are available in different
|
||||
variants. You can select between different variants by applying
|
||||
Math mode makes a wide selection of [symbols]($category/math/symbols) like
|
||||
`pi`, `dot`, or `RR` available. Many mathematical symbols are available in
|
||||
different variants. You can select between different variants by applying
|
||||
[modifiers]($type/symbol) to the symbol. Typst further recognizes a number of
|
||||
shorthand sequences like `=>` that approximate a symbol. When such a shorthand
|
||||
exists, the symbol's documentation lists it.
|
||||
|
@ -160,6 +160,7 @@ pub struct FontInfo {
|
||||
bitflags::bitflags! {
|
||||
/// Bitflags describing characteristics of a font.
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(transparent)]
|
||||
pub struct FontFlags: u32 {
|
||||
/// All glyphs have the same width.
|
||||
const MONOSPACE = 1 << 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user