jotdown_wasm: add events with spans format

This commit is contained in:
Noah Hellman 2023-05-02 20:16:20 +02:00
parent 79b5119209
commit 2a3973674f
2 changed files with 15 additions and 5 deletions

View file

@ -21,7 +21,10 @@
output.innerText = jotdown_render(input.innerText);
} else if (fmt.value == "events") {
output.classList.add("verbatim")
output.innerText = jotdown_parse(input.innerText);
output.innerText = jotdown_parse(input.innerText, false);
} else if (fmt.value == "events_spans") {
output.classList.add("verbatim")
output.innerText = jotdown_parse(input.innerText, true);
} else if (fmt.value == "events_indent") {
output.classList.add("verbatim")
output.innerText = jotdown_parse_indent(input.innerText);
@ -50,6 +53,7 @@
<option value="preview">preview</option>
<option value="html">html</option>
<option value="events">events</option>
<option value="events_spans">events (with offsets)</option>
<option value="events_indent">events (indented)</option>
</select>
</div>

View file

@ -22,10 +22,16 @@ pub fn jotdown_render(djot: &str) -> String {
#[must_use]
#[wasm_bindgen]
pub fn jotdown_parse(djot: &str) -> String {
jotdown::Parser::new(djot)
.map(|e| format!("{:?}\n", e))
.collect()
pub fn jotdown_parse(djot: &str, spans: bool) -> String {
let mut out = String::new();
for (e, sp) in jotdown::Parser::new(djot).into_offset_iter() {
write!(out, "{:?}", e).unwrap();
if spans {
write!(out, " {:?} {:?}", &djot[sp.clone()], sp).unwrap();
}
writeln!(out).unwrap();
}
out
}
#[must_use]