mermaid-js / mermaid-cli

Command line tool for the Mermaid library
MIT License
2.48k stars 234 forks source link

mmdc removing leading and trailing newlines of mermaid code blocks #413

Closed craigmac closed 1 year ago

craigmac commented 2 years ago

Describe the bug mmdc removes mermaid code blocks as intended, but messes with surround empty lines (one above and one below the code block).

To Reproduce Steps to reproduce the behavior:

Sample markdown (not sure if this will render correctly here, but added 4 backticks for the embedded mermaid sequence):

### Player end of stream

````mermaid
sequenceDiagram
  accTitle: Player end of stream
  accDescr: Sequence diagram
participant Player Engine
participant ES Player
Note over Player Engine, ES Player: At end of stream
Player Engine->>+ES Player: SetLastFrameAppended()
loop
ES Player->>ES Player: Play remaining buffer
ES Player->>-Player Engine: CurrentTimeUpdated.Emit()
end
ES Player->>Player Engine: EndOfStream.Emit()
Player Engine->>ES Player: Pause()

---

Expected behavior

Replaced like this:

### Player end of stream

![Player end of stream](./temp-5.svg)

---

Actual behavior

### Player end of stream
![diagram](./temp-5.svg)
---

Notice the newlines have been removed - one just below the markdown heading, and one after the ![diagram] line: this causes markdownlint errors and also a YAML parsing error because the --- line doesn't have an empty line above and below it, and so the parser thinks we are trying to start a YAML block.

Desktop (please complete the following information):

Smartphone (please complete the following information):

MindaugasLaganeckas commented 2 years ago

@craigmac : please, provide the version of mermaid-cli. Another thing, be aware, that MacOS users need to use our provided docker image, because of https://github.com/mermaid-js/mermaid-cli/issues/288

craigmac commented 2 years ago

My mistake, the OS is actually docker: pandoc/latex:2.18-ubuntu container with mermaid-cli version 9.1.7

justincjahn commented 1 year ago

I'm experiencing the same issue on Windows using version 9.2.2:

Source File:

# Overview

Hello, World!

```mermaid
graph TD
    PSTN -->|Inbound Call| P1[PRI 1] --> CUBE
    PSTN -->|Inbound Call| P2[PRI 2] --> CUBE
    PSTN -->|Inbound Call| P3[PRI 3] --> CUBE

    CUBE(CUBE) -->CUBE_ROUTE_CALL{Route Call}

    CUBE_ROUTE_CALL -->|eFax| XMEDIUS(XMedius XMFax)
    CUBE_ROUTE_CALL -->|Voice| CUCM(CUCM)

    CUCM --> CUCM_ROUTE_CALL{Route Call}

    CUCM_ROUTE_CALL -->|IVR| UCCX(UCCX)
    CUCM_ROUTE_CALL -->|Voicemail| CUC(CUC)
    CUCM_ROUTE_CALL -->|Endpoint| DEVICE[End Device]

Testing

Test


```bash
npm i @mermaid-js/mermaid-cli
.\node_modules\.bin\mmdc -i .\tmp\MASTER.md -o .\tmp\MASTER_formatted.md

Produces:

# Overview

Hello, World!
![diagram](./MASTER_formatted-1.svg)
## Testing

Test

I was able to work around the issue by surrounding the block with comments:

# Overview

Hello, World!

<!---->
```mermaid
graph TD
    PSTN -->|Inbound Call| P1[PRI 1] --> CUBE
    PSTN -->|Inbound Call| P2[PRI 2] --> CUBE
    PSTN -->|Inbound Call| P3[PRI 3] --> CUBE

    CUBE(CUBE) -->CUBE_ROUTE_CALL{Route Call}

    CUBE_ROUTE_CALL -->|eFax| XMEDIUS(XMedius XMFax)
    CUBE_ROUTE_CALL -->|Voice| CUCM(CUCM)

    CUCM --> CUCM_ROUTE_CALL{Route Call}

    CUCM_ROUTE_CALL -->|IVR| UCCX(UCCX)
    CUCM_ROUTE_CALL -->|Voicemail| CUC(CUC)
    CUCM_ROUTE_CALL -->|Endpoint| DEVICE[End Device]

Testing

Test


Which produced the following:

````markdown
# Overview

Hello, World!

<!---->
![diagram](./MASTER_formatted-1.svg)
<!---->

## Testing

Test

Hope that helps!