summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan-Erik Rediger <janerik@fnordig.de>2022-10-18 12:47:27 +0200
committerJan-Erik Rediger <janerik@fnordig.de>2022-10-18 12:49:44 +0200
commit1dd33b40219d57d158b988b23845f98bb08cfe6d (patch)
tree37cdb5051bc8c15d5d1b1558c1285cb996a9f4fb
parentbbba9570534bdf31a91da17c80a5daa8a10e41c4 (diff)
Handle arbitrary code span starts
Previously we hardcoded the start of a code block as |```mermaid|. But it can have more backticks. Or other symbols really. Same for the end. The parser already gives us all the information we need: The span tells us about the start and end and between a code block start and end can only be text, which also has a span. Fixes #24
-rw-r--r--CHANGELOG.md6
-rw-r--r--src/lib.rs47
2 files changed, 45 insertions, 8 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6e72812..977fc6a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,9 @@
+# Unreleased
+
+[Full changelog](https://github.com/badboy/mdbook-mermaid/compare/v0.12.0...main)
+
+* BUGFIX: Handle arbitrary code span starts
+
# v0.12.0 (2022-10-11)
[Full changelog](https://github.com/badboy/mdbook-mermaid/compare/v0.11.2...v0.12.0)
diff --git a/src/lib.rs b/src/lib.rs
index dff9c4e..2221b06 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -56,16 +56,15 @@ fn add_mermaid(content: &str) -> Result<String> {
opts.insert(Options::ENABLE_STRIKETHROUGH);
opts.insert(Options::ENABLE_TASKLISTS);
- let mut mermaid_start = 0..0;
+ let mut code_span = 0..0;
let mut mermaid_blocks = vec![];
let events = Parser::new_ext(content, opts);
for (e, span) in events.into_offset_iter() {
+ log::debug!("e={:?}, span={:?}", e, span);
if let Event::Start(Tag::CodeBlock(Fenced(code))) = e.clone() {
- log::debug!("e={:?}, span={:?}", e, span);
if &*code == "mermaid" {
- mermaid_start = span;
in_mermaid_block = true;
mermaid_content.clear();
}
@@ -76,19 +75,23 @@ fn add_mermaid(content: &str) -> Result<String> {
continue;
}
+ // We're in the code block. The text is what we want.
+ if let Event::Text(_) = e {
+ code_span = span;
+ continue;
+ }
+
if let Event::End(Tag::CodeBlock(Fenced(code))) = e {
assert_eq!(
"mermaid", &*code,
"After an opening mermaid code block we expect it to close again"
);
in_mermaid_block = false;
- let pre = "```mermaid\n";
- let post = "```";
- let mermaid_content = &content[mermaid_start.start + pre.len()..span.end - post.len()];
+ let mermaid_content = &content[code_span.clone()];
let mermaid_content = escape_html(mermaid_content);
let mermaid_code = format!("<pre class=\"mermaid\">{}</pre>\n\n", mermaid_content);
- mermaid_blocks.push((mermaid_start.start..span.end, mermaid_code.clone()));
+ mermaid_blocks.push((span, mermaid_code));
}
}
@@ -216,7 +219,7 @@ Text
#[test]
fn escape_in_mermaid_block() {
- env_logger::init();
+ let _ = env_logger::try_init();
let content = r#"
```mermaid
classDiagram
@@ -245,4 +248,32 @@ hello
assert_eq!(expected, add_mermaid(content).unwrap());
}
+
+ #[test]
+ fn more_backticks() {
+ let _ = env_logger::try_init();
+ let content = r#"# Chapter
+
+````mermaid
+graph TD
+A --> B
+````
+
+Text
+"#;
+
+ let expected = r#"# Chapter
+
+
+<pre class="mermaid">graph TD
+A --&gt; B
+</pre>
+
+
+
+Text
+"#;
+
+ assert_eq!(expected, add_mermaid(content).unwrap());
+ }
}