Skip to content
This repository has been archived by the owner on Jul 22, 2023. It is now read-only.

Adding option for custom theme #24

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

Command-line interface for [mermaid](https://mermaidjs.github.io/).

This CLI tool takes a mermaid definition file as input and generates svg/png file as output.
This CLI tool takes a mermaid definition file as input and generates svg/png/pdf file as output.


## Installation

```
```sh
yarn global add mermaid.cli
```

Or

```
```sh
npm install -g mermaid.cli
```

Expand All @@ -22,31 +22,31 @@ Please install via `npm` instead of `yarn` if you encounter [this issue](https:/

## Examples

```
```sh
mmdc -i input.mmd -o output.svg
```

```
```sh
mmdc -i input.mmd -o output.png
```

```
```sh
mmdc -i input.mmd -o output.pdf
```

```
```sh
mmdc -i input.mmd -o output.svg -w 1024 -H 768
```

```
```sh
mmdc -i input.mmd -t forest
```

```
```sh
mmdc -i input.mmd -o output.png -b '#FFF000'
```

```
```sh
mmdc -i input.mmd -o output.png -b transparent
```

Expand All @@ -55,16 +55,15 @@ mmdc -i input.mmd -o output.png -b transparent

Please run the following command to see the latest options:

```
```sh
mmdc -h
```

The following is for your quick reference (may not be the latest version):

```
```text
Usage: mmdc [options]


Options:

-V, --version output the version number
Expand All @@ -76,5 +75,6 @@ Usage: mmdc [options]
-b, --backgroundColor [backgroundColor] Background color. Example: transparent, red, '#F0F0F0'. Optional. Default: white
-c, --configFile [config] JSON configuration file for mermaid. Optional
-C, --cssFile [cssFile] CSS alternate file for mermaid. Optional
-T, --customTheme <customThemeCssFile> CSS file replacing CSS injected into SVG container. Optional
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not saying <...> is wrong but maybe [customThemeCssFile] for consistency?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea - I was weighing consistency over correctness here. If you use the <...> version, then commander stops the run and reports the missing option. My inclination was to change all the others to <...> but that seemed presumptuous to do as part of my change.

That said, I'm fine with switching to the [...] version if you like. I'd just want to make sure that the application handles a missing file name gracefully.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI - I just tested using one of the existing options. As it stands, if you just specify one of the options which expects a file but don't provide one, you get this.

$  node index.js -C -i test/flowchart.mmd

CSS file "true" doesn't exist

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm with you regarding changing all, @tylerlong is the developer I just proposed a couple improvements like you did.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK - I've included that in this PR now as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get the point. If you specify -C, you should specify a CSS file such as -C ./test/index.css.

Copy link
Contributor

@tylerlong tylerlong Feb 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have any issue with the current commander code. You can omit any options except those marked as required. So I probably won't change this part.

-h, --help output usage information
```
30 changes: 25 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ commander
.option('-b, --backgroundColor [backgroundColor]', 'Background color. Example: transparent, red, \'#F0F0F0\'. Optional. Default: white')
.option('-c, --configFile [config]', 'JSON configuration file for mermaid. Optional')
.option('-C, --cssFile [cssFile]', 'CSS alternate file for mermaid. Optional')
.option('-T, --customTheme <customThemeCssFile>', 'CSS file replacing CSS injected into SVG container. Optional')
.parse(process.argv)

let { theme, width, height, input, output, backgroundColor, configFile, cssFile } = commander
let { theme, width, height, input, output, backgroundColor, configFile, cssFile, customTheme } = commander

// check input file
if (!input) {
Expand Down Expand Up @@ -62,6 +63,14 @@ if (cssFile) {
}
}

if (customTheme) {
if (!fs.existsSync(customTheme)) {
error(`CSS file "${customTheme}" doesn't exist`)
} else if (!/\.(?:css)$/.test(customTheme)) {
error(`CSS file must end with ".css"`)
}
}

// normalize args
width = parseInt(width)
height = parseInt(height)
Expand All @@ -70,15 +79,16 @@ backgroundColor = backgroundColor || 'white'
;(async () => {
const browser = await puppeteer.launch()
const page = await browser.newPage()
page.on('console', msg => console.log('PAGE LOG:', ...msg.args))
page.setViewport({ width, height })
await page.goto(`file://${path.join(__dirname, 'index.html')}`)

await page.evaluate(`document.body.style.background = '${backgroundColor}'`)

const definition = fs.readFileSync(input, 'utf-8')

var myconfig, myCSS
let myconfig, myCSS, myTheme

if (configFile) {
myconfig = JSON.parse(fs.readFileSync(configFile, 'utf-8'))
}
Expand All @@ -87,7 +97,11 @@ backgroundColor = backgroundColor || 'white'
myCSS = fs.readFileSync(cssFile, 'utf-8')
}

await page.$eval('#container', (container, definition, theme, myconfig, myCSS) => {
if (customTheme) {
myTheme = fs.readFileSync(customTheme, 'utf-8')
}

await page.$eval('#container', (container, definition, theme, myconfig, myCSS, myTheme) => {
container.innerHTML = definition
window.mermaid_config = { theme }

Expand All @@ -111,7 +125,13 @@ backgroundColor = backgroundColor || 'white'
}

window.mermaid.init(undefined, container)
}, definition, theme, myconfig, myCSS)

if (myTheme) {
// console.log('Replacing CSS with Custom Theme')
window.document.getElementById('mermaidChart0').getElementsByTagName('style')[0].innerHTML = myTheme;
}

}, definition, theme, myconfig, myCSS, myTheme)

if (output.endsWith('svg')) {
const svg = await page.$eval('#container', container => container.innerHTML)
Expand Down
Loading