Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle empty newlines before diagrams in md #455

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src-test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,9 @@ describe("NodeJS API (import ... from '@mermaid-js/mermaid-cli')", () => {
'(./mermaid-run-output-test-png-8.png "State diagram example with \\\\\\"double-quotes\\"")')
expect(markdownFile).toContain(markdownImageWithCustomTitle)

// check whether newlines before/after mermaid diagram are kept
expect(markdownFile).toContain('There should be an empty newline after this line, but before the Mermaid diagram:\n\n')

// files should exist, and they should be PNGs
await Promise.all(expectedOutputPngs.map(async (expectedOutputPng) => {
// markdown file should point to png relative to md file
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,8 @@ async function run (input, output, { puppeteerConfig = {}, quiet = false, output
}
}

const mermaidChartsInMarkdown = /^\s*```(?:mermaid)(\r?\n([\s\S]*?))```\s*$/
// TODO: should we use a Markdown parser like remark instead of rolling our own parser?
const mermaidChartsInMarkdown = /^[^\S\n]*```(?:mermaid)(\r?\n([\s\S]*?))```[^\S\n]*$/
const mermaidChartsInMarkdownRegexGlobal = new RegExp(mermaidChartsInMarkdown, 'gm')
const browser = await puppeteer.launch(puppeteerConfig)
try {
Expand Down
4 changes: 4 additions & 0 deletions test-positive/mermaid.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ graph LR
A -->|こんにちは| B
```
7. sequence.mmd

There should be an empty newline after this line, but before the Mermaid diagram:

```mermaid
sequenceDiagram
%% See https://mermaidjs.github.io/sequenceDiagram.html
Expand All @@ -103,6 +106,7 @@ loop D-1 before deadline at 7:45
Note over HII,FGG: D-1 before deadline
end
```

8. Should still find mermaid code even when code-block is indented.

```mermaid
Expand Down