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

Feature/allow markdown #136

Merged
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
8 changes: 4 additions & 4 deletions src-test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const out = "test-positive";
async function compileDiagramFromStdin(file, format) {
return new Promise(function (resolve, reject) {
try {
const result = file.replace(".mmd", "-stdin." + format);
const result = file.replace(/\.(?:mmd|md)$/, "-stdin." + format);
// eslint-disable-next-line no-console
console.warn(`Compiling ${file} into ${result}`);
execSync(`cat ${workflows}/${file} | \
Expand All @@ -31,7 +31,7 @@ async function compileDiagramFromStdin(file, format) {
*/
async function compileDiagram(file, format) {
return new Promise(async function(resolve, reject) {
const result = file.replace(".mmd", "." + format);
const result = file.replace(/\.(?:mmd|md)$/, "." + format);
// eslint-disable-next-line no-console
console.warn(`Compiling ${file} into ${result}`);
const child = spawnSync("node", [
Expand Down Expand Up @@ -75,7 +75,7 @@ async function compileAll() {
fs.readdir(workflows, async (err, files) => {
resolve(Promise.all(
files.map(async file => {
if (!file.endsWith(".mmd")) {
if (!(file.endsWith(".mmd") | /\.md$/.test(file))) {
return Promise.resolve();
}
const expectError = /expect-error/.test(file);
Expand Down Expand Up @@ -106,7 +106,7 @@ async function compileAllStdin() {
fs.readdir(workflows, async (err, files) => {
resolve(Promise.all(
files.map(async file => {
if (!file.endsWith(".mmd")) {
if (!(file.endsWith(".mmd") | /\.md$/.test(file))) {
return Promise.resolve();
}
const expectError = /expect-error/.test(file);
Expand Down
23 changes: 18 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,14 @@ height = parseInt(height)
backgroundColor = backgroundColor || 'white';
const deviceScaleFactor = parseInt(scale || 1, 10);

(async () => {
const browser = await puppeteer.launch(puppeteerConfig)
const parseMMD = async (browser, definition, output) => {
const page = await browser.newPage()
page.setViewport({ width, height, deviceScaleFactor })
await page.goto(`file://${path.join(__dirname, 'index.html')}`)
await page.evaluate(`document.body.style.background = '${backgroundColor}'`)
const definition = await getInputData(input)

const result = await page.$eval('#container', (container, definition, mermaidConfig, myCSS) => {
container.textContent = definition
window.mermaid.initialize(mermaidConfig)

if (myCSS) {
const head = window.document.head || window.document.getElementsByTagName('head')[0]
const style = document.createElement('style')
Expand Down Expand Up @@ -198,5 +194,22 @@ const deviceScaleFactor = parseInt(scale || 1, 10);
})
}
}
}

(async () => {
const browser = await puppeteer.launch(puppeteerConfig)
const definition = await getInputData(input)
if (/\.md$/.test(input)) {
const matches = definition.match(/^```(?:mermaid)(\n([\s\S]*?))```$/gm);
if (matches.length > 0) {
const mmdStrings = matches.map((str) => str.replace(/^```(?:mermaid)(\n([\s\S]*?))```$/,'$1').trim());
await Promise.all(mmdStrings.map((mmdString, index) =>
parseMMD(browser, mmdString, output.replace(/(\..*)$/,`-${index + 1}$1`))
));
}
} else {
await parseMMD(browser, definition, output)
}

await browser.close()
})()
139 changes: 139 additions & 0 deletions test-positive/mermaid.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
1. emojis.mmd
```mermaid
graph TD
A-->B("hello 🐛")
```
2. flowchart1.mmd
```mermaid
sequenceDiagram
%% See https://mermaidjs.github.io/sequenceDiagram.html

loop everyday D-2 working days 16:00
ABCD->>DEE: [14:25] BEE (Calculation per TC until ACK becomes OK)

ABCD->>FGG: BEE (Calculation per TC until ACK becomes OK)
Note over ABCD,FGG: D-2 weekdays at 14:25
ABCD->>HII: Projected Authorisations to HII
Note over ABCD,HII: D-2 weekdays at 16:00
end

loop D-1 before deadline at 7:45
HII->>DEE: Submission
Note over HII,DEE: D-1 before deadline
HII->>FGG: Submission
Note over HII,FGG: D-1 before deadline
end
```
3. flowchart2.mmd
```mermaid
graph BT
subgraph initramfs
/ === ibin[bin]
/ --- DEV((dev))
/ === ilib[lib]
/ --- proc((proc))
/ === tmp

/ === usr
usr === bin
bin === ENV(env)

tmp --- root
tmp --- users((users))

root --- RDEV((dev))
root --- rproc((proc))
root --- rtmp((tmp))

root --- home((home))
end

subgraph usersfs
.workdirs
nodeos
uroot[root]

nodeos --- NDEV((dev))
nodeos --- nproc((proc))
nodeos --- ntmp((tmp))
end

home === .workdirs
home === nodeos
home === uroot

users -.-> home

DEV -.- NDEV
proc -.- nproc

DEV -.- RDEV
proc -.- rproc
```
4. flowchart3.mmd
```mermaid
graph TD
B["fa:fa-car for peace"]
B-->C[fa:fa-ban forbidden]
B-->D(fa:fa-spinner);
B-->E(A fa:fa-camera-retro perhaps?);
```
5. graph-with-br.mmd
```mermaid
graph TD
subgraph sub
node(Line 1<br>Line 2<br/>Line 3)
end
```
6. japanese-chars.mmd
```mermaid
graph LR
A -->|こんにちは| B
```
7. sequence.mmd
```mermaid
sequenceDiagram
%% See https://mermaidjs.github.io/sequenceDiagram.html

loop everyday D-2 working days 16:00
ABCD->>DEE: [14:25] BEE (Calculation per TC until ACK becomes OK)

ABCD->>FGG: BEE (Calculation per TC until ACK becomes OK)
Note over ABCD,FGG: D-2 weekdays at 14:25
ABCD->>HII: Projected Authorisations to HII
Note over ABCD,HII: D-2 weekdays at 16:00
end

loop D-1 before deadline at 7:45
HII->>DEE: Submission
Note over HII,DEE: D-1 before deadline
HII->>FGG: Submission
Note over HII,FGG: D-1 before deadline
end
```
8. state1.mmd
```mermaid
stateDiagram
state Choose <<fork>>
[*] --> Still
Still --> [*]

Still --> Moving
Moving --> Choose
Choose --> Still
Choose --> Crash
Crash --> [*]
```
9. state2.mmd
```mermaid
stateDiagram
state Choose <<fork>>
[*] --> Still
Still --> [*]

Still --> Moving
Moving --> Choose
Choose --> Still
Choose --> Crash
Crash --> [*]
```