mdbook-d2 icon indicating copy to clipboard operation
mdbook-d2 copied to clipboard

Multiple Diagrams Back to Back Breaks Rendering

Open dylanowen opened this issue 9 months ago • 0 comments

I haven't had a chance to dive into the specifics but if you have something in your markdown like

```d2
a -> b
```

```d2
b -> c
```

and are using the inline mode it will break the rendering. It's possible to work around this by inserting some text or probably anything to break up the markdown events.

I quickly dropped in this code from my similar project: https://github.com/dylanowen/mdbook-graphviz/blob/5636dc2c7344495652ba230a918a18073ad8d757/src/renderer.rs#L118-L136

fn format_output(output: String) -> String {
    lazy_static! {
        static ref DOCTYPE_RE: Regex = Regex::new(r"<!DOCTYPE [^>]+>").unwrap();
        static ref XML_TAG_RE: Regex = Regex::new(r"<\?xml [^>]+\?>").unwrap();
        static ref NEW_LINE_TAGS_RE: Regex = Regex::new(r">\s+<").unwrap();
        static ref NEWLINES_RE: Regex = Regex::new(r"\n").unwrap();
    }

    // yes yes: https://stackoverflow.com/a/1732454 ZALGΌ and such
    let output = DOCTYPE_RE.replace(&output, "");
    let output = XML_TAG_RE.replace(&output, "");
    // remove newlines between our tags to help commonmark determine the full set of HTML
    let output = NEW_LINE_TAGS_RE.replace_all(&output, "><");
    // remove explicit newlines as they won't be preserved and break commonmark parsing
    let output = NEWLINES_RE.replace_all(&output, "");
    let output = output.trim();

    format!("<div>{output}</div>")
}

which seemed to fix it, I bet a lot of those replacements aren't necessary but based on my comments around commonmark and newlines I wonder if it's related to that.

dylanowen avatar May 03 '24 04:05 dylanowen