diff --git a/crates/typst-docs/src/lib.rs b/crates/typst-docs/src/lib.rs index fae94a6fa..4dda2c0d7 100644 --- a/crates/typst-docs/src/lib.rs +++ b/crates/typst-docs/src/lib.rs @@ -214,7 +214,8 @@ pub struct CategoryModel { pub details: Html, pub kind: &'static str, pub items: Vec, - pub shorthands: Option>, + pub markup_shorthands: Option>, + pub math_shorthands: Option>, } /// Details about a category item. @@ -325,13 +326,20 @@ fn category_page(resolver: &dyn Resolver, category: &str) -> PageModel { items.sort_by_cached_key(|item| item.name.clone()); // Add symbol pages. These are ordered manually. - let mut shorthands = vec![]; + let mut markup_shorthands = vec![]; + let mut math_shorthands = vec![]; if category == "symbols" { for module in ["sym", "emoji"] { let subpage = symbol_page(resolver, &route, module); let BodyModel::Symbols(model) = &subpage.body else { continue }; - shorthands.extend( - model.list.iter().filter(|symbol| symbol.shorthand.is_some()).cloned(), + let list = &model.list; + markup_shorthands.extend( + list.iter() + .filter(|symbol| symbol.markup_shorthand.is_some()) + .cloned(), + ); + math_shorthands.extend( + list.iter().filter(|symbol| symbol.math_shorthand.is_some()).cloned(), ); items.push(CategoryItem { @@ -361,7 +369,8 @@ fn category_page(resolver: &dyn Resolver, category: &str) -> PageModel { details: Html::markdown(resolver, details(category)), kind, items, - shorthands: Some(shorthands), + markup_shorthands: Some(markup_shorthands), + math_shorthands: Some(math_shorthands), }), children, } @@ -659,7 +668,8 @@ fn types_page(resolver: &dyn Resolver, parent: &str) -> PageModel { details: Html::markdown(resolver, details("types")), kind: "Types", items, - shorthands: None, + markup_shorthands: None, + math_shorthands: None, }), children, } @@ -836,7 +846,8 @@ pub struct SymbolsModel { #[serde(rename_all = "camelCase")] pub struct SymbolModel { pub name: String, - pub shorthand: Option<&'static str>, + pub markup_shorthand: Option<&'static str>, + pub math_shorthand: Option<&'static str>, pub codepoint: u32, pub accent: bool, pub unicode_name: Option, @@ -859,13 +870,14 @@ fn symbol_page(resolver: &dyn Resolver, parent: &str, name: &str) -> PageModel { }; for (variant, c) in symbol.variants() { + let shorthand = |list: &[(&'static str, char)]| { + list.iter().copied().find(|&(_, x)| x == c).map(|(s, _)| s) + }; + list.push(SymbolModel { name: complete(variant), - shorthand: typst::syntax::ast::Shorthand::LIST - .iter() - .copied() - .find(|&(_, x)| x == c) - .map(|(s, _)| s), + markup_shorthand: shorthand(typst::syntax::ast::Shorthand::MARKUP_LIST), + math_shorthand: shorthand(typst::syntax::ast::Shorthand::MATH_LIST), codepoint: c as u32, accent: typst::eval::Symbol::combining_accent(c).is_some(), unicode_name: unicode_names2::name(c) diff --git a/crates/typst-syntax/src/ast.rs b/crates/typst-syntax/src/ast.rs index c2755d0c6..2ea318a7f 100644 --- a/crates/typst-syntax/src/ast.rs +++ b/crates/typst-syntax/src/ast.rs @@ -435,16 +435,18 @@ node! { } impl Shorthand { - /// A list of all shorthands. - pub const LIST: &[(&'static str, char)] = &[ - // Both. + /// A list of all shorthands in markup mode. + pub const MARKUP_LIST: &[(&'static str, char)] = &[ ("...", '…'), - // Text only. ("~", '\u{00A0}'), ("--", '\u{2013}'), ("---", '\u{2014}'), ("-?", '\u{00AD}'), - // Math only. + ]; + + /// A list of all shorthands in math mode. + pub const MATH_LIST: &[(&'static str, char)] = &[ + ("...", '…'), ("-", '\u{2212}'), ("'", '′'), ("*", '∗'), @@ -487,8 +489,7 @@ impl Shorthand { /// Get the shorthanded character. pub fn get(&self) -> char { let text = self.0.text(); - Self::LIST - .iter() + (Self::MARKUP_LIST.iter().chain(Self::MATH_LIST)) .find(|&&(s, _)| s == text) .map_or_else(char::default, |&(_, c)| c) }