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

feat(gatsby-remark-graphviz): custom SVG attributes and default styling #11624

Merged
merged 3 commits into from
Jun 3, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 27 additions & 0 deletions packages/gatsby-remark-graphviz/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,33 @@ Which will be rendered using viz.js and the output html will replace the code bl

![rendered-graph](/packages/gatsby-remark-graphviz/rendered-graph.svg)

Custom attributes can be passed to the rendered SVG:

```dot id="my-id" class="my-class"
digraph graphname {
a -> b;
b -> c;
a -> c;
}
```

By default, the following inline style is applied to all rendered SVGs in order to make them responsive:

```css
max-width: 100%;
height: auto;
```

This can be overwritten by using the custom attributes feature:

```dot style=""
digraph graphname {
a -> b;
b -> c;
a -> c;
}
```

## Caveats

In your gatsby-config.js, make sure you place this plugin before other remark plugins that modify code blocks (like prism).
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-remark-graphviz/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
},
"dependencies": {
"@babel/runtime": "^7.0.0",
"cheerio": "^1.0.0-rc.2",
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 not sure we want to include this dependency as it has a lot of dependencies. I'm a bit worried that this will introduce some security issues in the long ron

Copy link
Contributor

Choose a reason for hiding this comment

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

@wardpeet several of the other gatsby-remark plugins also use cheerio https://github.com/gatsbyjs/gatsby/search?q=cheerio&unscoped_q=cheerio so this isn't a big deal

"unist-util-visit": "^1.4.0",
"viz.js": "^2.0.0"
},
Expand Down
23 changes: 19 additions & 4 deletions packages/gatsby-remark-graphviz/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const visit = require(`unist-util-visit`)
const Viz = require(`viz.js`)
const { Module, render } = require(`viz.js/full.render.js`)
const cheerio = require(`cheerio`)

const viz = new Viz({ Module, render })

Expand All @@ -10,25 +11,39 @@ module.exports = async ({ markdownAST }, pluginOptions = {}) => {
let codeNodes = []

visit(markdownAST, `code`, node => {
const chunks = (node.lang || ``).match(
/^(?<lang>\S+)(\s+(?<attrString>.+))?/
)
// Only act on languages supported by graphviz
if (validLanguages.includes(node.lang)) {
codeNodes.push(node)
if (chunks && validLanguages.includes(chunks.groups.lang)) {
node.lang = chunks.groups.lang
codeNodes.push({ node, attrString: chunks.groups.attrString })
}
return node
})

await Promise.all(
codeNodes.map(async node => {
codeNodes.map(async ({ node, attrString }) => {
const { value, lang } = node

try {
// Perform actual render
const svgString = await viz.renderString(value, { engine: lang })

// Add default inline styling
const $ = cheerio.load(svgString)
$(`svg`).attr(`style`, `max-width: 100%; height: auto;`)
wardpeet marked this conversation as resolved.
Show resolved Hide resolved

// Merge custom attributes if provided by user (adds and overwrites)
if (attrString) {
const attrElement = cheerio.load(`<element ${attrString}></element>`)
$(`svg`).attr(attrElement(`element`).attr())
}

// Mutate the current node. Converting from a code block to
// HTML (with svg content)
node.type = `html`
node.value = svgString
node.value = $.html(`svg`)
} catch (error) {
console.log(
`Error during viz.js execution. Leaving code block unchanged`
Expand Down