Skip to content

Commit 0eb52a3

Browse files
committed
trying to simplify the wasm library to help with testing
1 parent 9f6900d commit 0eb52a3

File tree

4 files changed

+58
-16
lines changed

4 files changed

+58
-16
lines changed

crates/quarto-markdown-pandoc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ pub mod pandoc;
1212
pub mod readers;
1313
pub mod traversals;
1414
pub mod utils;
15+
pub mod wasm_entry_points;
1516
pub mod writers;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* mod.rs
3+
* Copyright (c) 2025 Posit, PBC
4+
*/
5+
6+
use std::{io, panic};
7+
8+
use crate::readers;
9+
use crate::utils::output::VerboseOutput;
10+
use crate::utils::tree_sitter_log_observer::TreeSitterLogObserver;
11+
12+
fn pandoc_to_json(doc: &crate::pandoc::Pandoc) -> Result<String, String> {
13+
let mut buf = Vec::new();
14+
match crate::writers::json::write(doc, &mut buf) {
15+
Ok(_) => {
16+
// Nothing to do
17+
}
18+
Err(err) => {
19+
return Err(format!("Unable to write as json: {:?}", err));
20+
}
21+
}
22+
23+
match String::from_utf8(buf) {
24+
Ok(json) => Ok(json),
25+
Err(err) => Err(format!("Unable to convert json to string: {:?}", err)),
26+
}
27+
}
28+
29+
pub fn parse_qmd(input: &[u8]) -> String {
30+
let mut output = VerboseOutput::Sink(io::sink());
31+
let result = match readers::qmd::read(
32+
input,
33+
false,
34+
"<input>",
35+
&mut output,
36+
None::<fn(&[u8], &TreeSitterLogObserver, &str) -> Vec<String>>,
37+
) {
38+
Ok(result) => result,
39+
Err(err) => panic!("Unable to read as a qmd:\n{}", err.join("\n")),
40+
};
41+
42+
pandoc_to_json(&result).unwrap()
43+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* test_wasm_entrypoints.rs
3+
* Copyright (c) 2025 Posit, PBC
4+
*/
5+
6+
#[test]
7+
fn test_wasm_read_entrypoint() {
8+
let input = "# hello _world_.\n";
9+
let result = quarto_markdown_pandoc::wasm_entry_points::parse_qmd(input.as_bytes());
10+
eprintln!("result: {}", result);
11+
}

crates/wasm-qmd-parser/src/lib.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ pub mod c_shim;
1717

1818
mod utils;
1919

20-
use std::{io, panic};
20+
use std::panic;
2121

2222
use quarto_markdown_pandoc::readers;
23-
use quarto_markdown_pandoc::utils::output::VerboseOutput;
24-
use quarto_markdown_pandoc::utils::tree_sitter_log_observer::TreeSitterLogObserver;
23+
use quarto_markdown_pandoc::wasm_entry_points;
2524
use quarto_markdown_pandoc::writers;
2625
use wasm_bindgen::prelude::*;
2726

@@ -60,23 +59,11 @@ fn pandoc_to_json(doc: &quarto_markdown_pandoc::pandoc::Pandoc) -> Result<String
6059

6160
#[wasm_bindgen]
6261
pub fn parse_qmd(input: JsValue) -> JsValue {
63-
let mut output = VerboseOutput::Sink(io::sink());
6462
let input = match input.as_string() {
6563
Some(input) => input,
6664
None => panic!("Unable to parse `input` as a `String`."),
6765
};
68-
let result = match readers::qmd::read(
69-
input.as_bytes(),
70-
false,
71-
"<input>",
72-
&mut output,
73-
None::<fn(&[u8], &TreeSitterLogObserver, &str) -> Vec<String>>,
74-
) {
75-
Ok(result) => result,
76-
Err(err) => panic!("Unable to read as a qmd:\n{}", err.join("\n")),
77-
};
78-
79-
let json = pandoc_to_json(&result).unwrap();
66+
let json = wasm_entry_points::parse_qmd(input.as_bytes());
8067
JsValue::from_str(&json)
8168
}
8269

0 commit comments

Comments
 (0)