atproto blogging
1//! Types exposed to JavaScript via wasm-bindgen.
2
3use jacquard::IntoStatic;
4use wasm_bindgen::prelude::*;
5use weaver_common::ResolvedContent;
6
7/// Result from rendering LaTeX math.
8#[wasm_bindgen]
9pub struct JsMathResult {
10 pub success: bool,
11 #[wasm_bindgen(getter_with_clone)]
12 pub html: String,
13 #[wasm_bindgen(getter_with_clone)]
14 pub error: Option<String>,
15}
16
17/// Pre-rendered embed content for synchronous rendering.
18///
19/// Build this by calling `create_resolved_content()` and adding embeds
20/// with `resolved_content_add_embed()`.
21#[wasm_bindgen]
22pub struct JsResolvedContent {
23 inner: ResolvedContent,
24}
25
26#[wasm_bindgen]
27impl JsResolvedContent {
28 /// Create an empty resolved content container.
29 #[wasm_bindgen(constructor)]
30 pub fn new() -> Self {
31 Self {
32 inner: ResolvedContent::new(),
33 }
34 }
35
36 /// Add pre-rendered embed HTML for an AT URI.
37 ///
38 /// # Arguments
39 /// * `at_uri` - The AT Protocol URI (e.g., "at://did:plc:.../app.bsky.feed.post/...")
40 /// * `html` - The pre-rendered HTML for this embed
41 #[wasm_bindgen(js_name = addEmbed)]
42 pub fn add_embed(&mut self, at_uri: &str, html: &str) -> Result<(), JsError> {
43 use jacquard::types::string::AtUri;
44 use jacquard::CowStr;
45
46 let uri = AtUri::new(at_uri)
47 .map_err(|e| JsError::new(&format!("Invalid AT URI: {}", e)))?
48 .into_static();
49
50 self.inner.add_embed(uri, CowStr::from(html.to_string()), None);
51 Ok(())
52 }
53}
54
55impl JsResolvedContent {
56 pub fn into_inner(self) -> ResolvedContent {
57 self.inner
58 }
59}
60
61impl Default for JsResolvedContent {
62 fn default() -> Self {
63 Self::new()
64 }
65}
66
67/// Create an empty resolved content container.
68///
69/// Use this to pre-render embeds before calling render functions.
70#[wasm_bindgen]
71pub fn create_resolved_content() -> JsResolvedContent {
72 JsResolvedContent::new()
73}