summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpseiko1989 <pseiko@hotmail.de>2022-11-29 11:40:48 +0100
committerGitHub <noreply@github.com>2022-11-29 11:40:48 +0100
commit727d10e645d55d8a74f4c51a3816dfd7061531d8 (patch)
tree9e9c57d14d354a0744e64815e9944357432b36c5
parentdd64390cf271df683c9ebfdfbaf6afd74fe7fe96 (diff)
Handle CR-LF in mermaid (#27)
* Handle CR-LF in mermaid mermaid blocks containing CR LF are returned line by line from the parser instead of as a complete code chunk for LF only. We need to expand the span for each chunk we get. After the closing ticks we replace the remaining \r\n with \n to have a consistent line ending in the mermaid code at least.
-rw-r--r--src/lib.rs22
1 files changed, 21 insertions, 1 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 2221b06..bd3d9c2 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -57,6 +57,7 @@ fn add_mermaid(content: &str) -> Result<String> {
opts.insert(Options::ENABLE_TASKLISTS);
let mut code_span = 0..0;
+ let mut start_new_code_span = true;
let mut mermaid_blocks = vec![];
@@ -76,8 +77,15 @@ fn add_mermaid(content: &str) -> Result<String> {
}
// We're in the code block. The text is what we want.
+ // Code blocks can come in multiple text events.
if let Event::Text(_) = e {
- code_span = span;
+ if start_new_code_span {
+ code_span = span;
+ start_new_code_span = false;
+ } else {
+ code_span = code_span.start..span.end;
+ }
+
continue;
}
@@ -90,8 +98,10 @@ fn add_mermaid(content: &str) -> Result<String> {
let mermaid_content = &content[code_span.clone()];
let mermaid_content = escape_html(mermaid_content);
+ let mermaid_content = mermaid_content.replace("\r\n", "\n");
let mermaid_code = format!("<pre class=\"mermaid\">{}</pre>\n\n", mermaid_content);
mermaid_blocks.push((span, mermaid_code));
+ start_new_code_span = true;
}
}
@@ -276,4 +286,14 @@ Text
assert_eq!(expected, add_mermaid(content).unwrap());
}
+
+ #[test]
+ fn crlf_line_endings() {
+ let _ = env_logger::try_init();
+ let content = "# Chapter\r\n\r\n````mermaid\r\n\r\ngraph TD\r\nA --> B\r\n````";
+ let expected =
+ "# Chapter\r\n\r\n\n<pre class=\"mermaid\">\ngraph TD\nA --&gt; B\n</pre>\n\n";
+
+ assert_eq!(expected, add_mermaid(content).unwrap());
+ }
}