Skip to content
Open
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
14 changes: 11 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ function replaceUrlWithGraph(node, vFile) {
*
* @param {object} ast
* @param {vFile} vFile
* @param {boolean} inlineMode
* @return {function}
*/
function visitCodeBlock(ast, vFile) {
function visitCodeBlock(ast, vFile, inlineMode) {
return visit(ast, 'code', (node, index, parent) => {
const { lang, value, position } = node;
const destinationDir = getDestinationDir(vFile);
Expand All @@ -61,6 +62,11 @@ function visitCodeBlock(ast, vFile) {
return node;
}

if (inlineMode) {
parent.children.splice(index, 1, utils.renderToImgTag(value, lang));
return node;
}

// Otherwise, let's try and generate a graph!
let graphSvgFilename;
try {
Expand Down Expand Up @@ -116,15 +122,17 @@ function visitImage(ast, vFile) {
* @link https://github.com/vfile/vfile
* @return {function}
*/
function graphviz() {
function graphviz(options = {}) {
const inlineMode = options.inline || false;

/**
* @param {object} ast MDAST
* @param {vFile} vFile
* @param {function} next
* @return {object}
*/
return function transformer(ast, vFile, next) {
visitCodeBlock(ast, vFile);
visitCodeBlock(ast, vFile, inlineMode);
visitLink(ast, vFile);
visitImage(ast, vFile);

Expand Down
16 changes: 16 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ const viz = require('viz.js');

const PLUGIN_NAME = 'remark-graphviz';

/**
* Accepts the `source` of the graph, and an `engine` to render an SVG using viz.js.
* Returns a MDAST representation of a HTML node.
*
* @param {string} source
* @param {string} engine 'dot' or 'circo'
* @return {object}
*/
function renderToImgTag(source, engine) {
return {
type: 'html',
value: `<img src="data:image/svg+xml;utf8,${encodeURIComponent(viz(source, { engine }))}" />`,
};
}

/**
* Accepts the `source` of the graph, and an `engine` to render an SVG using viz.js.
* Returns the path to the rendered SVG.
Expand Down Expand Up @@ -42,5 +57,6 @@ function getDestinationDir(vFile) {

module.exports = {
render,
renderToImgTag,
getDestinationDir,
};