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

Add svg-as-string mode #5

Closed
wants to merge 5 commits into from
Closed
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
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
language: node_js

node_js: ['6']
node_js:
- '6'
- '8'
- '10'

install:
- yarn install
Expand Down
1 change: 0 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module.exports = {
coveragePathIgnorePatterns: [
'/node_modules/',
],
mapCoverage: true,
Copy link
Author

Choose a reason for hiding this comment

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

moduleDirectories: [
'node_modules',
],
Expand Down
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@
"url": "git@github.com:temando/remark-mermaid.git"
},
"dependencies": {
"fs-extra": "^4.0.1",
"unist-util-visit": "^1.1.3",
"which": "^1.3.0"
"fs-extra": "^7.0.0",
"unist-util-visit": "^1.3.1",
"which": "^1.3.1"
},
"devDependencies": {
"changelog-verify": "^1.0.4",
"eslint": "^4.2.0",
"eslint-config-airbnb-base": "^11.1.0",
"eslint-plugin-import": "^2.0.0",
"jest": "^21.2.1",
"mermaid.cli": "^0.3.1",
"remark-parse": "^4.0.0",
"remark-stringify": "^4.0.0",
"to-vfile": "^2.1.2",
"unified": "^6.1.5",
"version-changelog": "^2.1.0",
"vfile": "^2.2.0"
"jest": "^23.4.1",
"mermaid.cli": "^0.5.1",
"remark-parse": "^5.0.0",
"remark-stringify": "^5.0.0",
"to-vfile": "^5.0.0",
"unified": "^7.0.0",
"version-changelog": "^3.1.0",
"vfile": "^3.0.0"
},
"peerDependencies": {
"mermaid.cli": "^0.3.1"
"mermaid.cli": "^0.5.1"
},
"scripts": {
"lint": "$(npm bin)/eslint src",
Expand Down
36 changes: 22 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function replaceLinkWithEmbedded(node, index, parent, vFile) {
* @param {boolean} isSimple
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor, JSDoc update now that this an object that has two possible properties (there's a few other updates needed around this as well).

* @return {function}
*/
function visitCodeBlock(ast, vFile, isSimple) {
function visitCodeBlock(ast, vFile, { isSimple, asString }) {
return visit(ast, 'code', (node, index, parent) => {
const { lang, value, position } = node;
const destinationDir = getDestinationDir(vFile);
Expand All @@ -109,21 +109,28 @@ function visitCodeBlock(ast, vFile, isSimple) {

// Otherwise, let's try and generate a graph!
} else {
let graphSvgFilename;
let svg;
try {
graphSvgFilename = render(value, destinationDir);
svg = render(value, destinationDir, asString);

vFile.info(`${lang} code block replaced with graph`, position, PLUGIN_NAME);
} catch (error) {
vFile.message(error, position, PLUGIN_NAME);
return node;
}

newNode = {
type: 'image',
title: '`mermaid` image',
url: graphSvgFilename,
};
if (asString) {
newNode = {
type: 'html',
value: svg,
};
} else {
newNode = {
type: 'image',
title: '`mermaid` image',
url: svg,
};
}
}

parent.children.splice(index, 1, newNode);
Expand All @@ -142,7 +149,7 @@ function visitCodeBlock(ast, vFile, isSimple) {
* @param {boolean} isSimple
* @return {function}
*/
function visitLink(ast, vFile, isSimple) {
function visitLink(ast, vFile, { isSimple }) {
if (isSimple) {
return visit(ast, 'link', (node, index, parent) => replaceLinkWithEmbedded(node, index, parent, vFile));
}
Expand All @@ -160,7 +167,7 @@ function visitLink(ast, vFile, isSimple) {
* @param {boolean} isSimple
* @return {function}
*/
function visitImage(ast, vFile, isSimple) {
function visitImage(ast, vFile, { isSimple }) {
if (isSimple) {
return visit(ast, 'image', (node, index, parent) => replaceLinkWithEmbedded(node, index, parent, vFile));
}
Expand All @@ -182,7 +189,8 @@ function visitImage(ast, vFile, isSimple) {
* @return {function}
*/
function mermaid(options = {}) {
const simpleMode = options.simple || false;
const isSimple = options.simple || false;
const asString = options.asString || false;

/**
* @param {object} ast MDAST
Expand All @@ -191,9 +199,9 @@ function mermaid(options = {}) {
* @return {object}
*/
return function transformer(ast, vFile, next) {
visitCodeBlock(ast, vFile, simpleMode);
visitLink(ast, vFile, simpleMode);
visitImage(ast, vFile, simpleMode);
visitCodeBlock(ast, vFile, { isSimple, asString });
visitLink(ast, vFile, { isSimple });
visitImage(ast, vFile, { isSimple });

if (typeof next === 'function') {
return next(null, ast, vFile);
Expand Down
12 changes: 9 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const PLUGIN_NAME = 'remark-mermaid';
* @param {string} destination
* @return {string}
*/
function render(source, destination) {
function render(source, destination, asString) {
const unique = crypto.createHmac('sha1', PLUGIN_NAME).update(source).digest('hex');
const mmdcExecutable = which.sync('mmdc');
const mmdPath = path.join(destination, `${unique}.mmd`);
Expand All @@ -30,15 +30,21 @@ function render(source, destination) {
// Clean up temporary file
fs.removeSync(mmdPath);

if (asString) {
const string = fs.readFileSync(svgPath, { encoding: 'utf-8' });
fs.removeSync(svgPath);
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if this is the cause of the test errors 🤔

return string;
}

return `./${svgFilename}`;
}

/**
* Accepts the `source` of the graph as a string, and render an SVG using
* Accepts the path `inputFile` to the graph, and render an SVG using
* mermaid.cli. Returns the path to the rendered SVG.
*
* @param {string} inputFile
* @param {string} destination
* @param {string} source
* @return {string}
*/
function renderFromFile(inputFile, destination) {
Expand Down
14 changes: 13 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ const toVFile = require('to-vfile');
const unified = require('unified');
const mermaid = require('../src/');

const isTravis = 'TRAVIS' in process.env && 'CI' in process.env;
const fixturesDir = path.join(__dirname, '/fixtures');
const runtimeDir = path.join(__dirname, '/runtime');
const runtimeDir = isTravis ? '/tmp' : path.join(__dirname, '/runtime');
const remark = unified().use(parse).use(stringify).freeze();

// Utility function to add metdata to a vFile.
Expand Down Expand Up @@ -40,6 +41,17 @@ describe('remark-mermaid', () => {
expect(vfile.messages[0].message).toBe('mermaid code block replaced with graph');
});

it('can handle code blocks to svg string', () => {
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 curious if the failing test is timing related. If this is pulled up before the previous "can handle code blocks" test, does this one pass and the old test fail?

const srcFile = `${fixturesDir}/code-block.md`;
const destFile = `${runtimeDir}/code-block.md`;
const vfile = toVFile.readSync(srcFile);
addMetadata(vfile, destFile);

const result = remark().use(mermaid, { asString: true }).processSync(vfile).toString();
expect(vfile.messages[0].message).toBe('mermaid code block replaced with graph');
expect(result).toMatch(/<svg id="[\s\S]*<\/svg>/);
});

it('can handle mermaid images', () => {
const srcFile = `${fixturesDir}/image-mermaid.md`;
const destFile = `${runtimeDir}/image-mermaid.md`;
Expand Down
3 changes: 2 additions & 1 deletion test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ const path = require('path');
const toVFile = require('to-vfile');
const { render, renderFromFile, getDestinationDir } = require('../src/utils');

const isTravis = 'TRAVIS' in process.env && 'CI' in process.env;
const fixturesDir = path.join(__dirname, '/fixtures');
const runtimeDir = path.join(__dirname, '/runtime');
const runtimeDir = isTravis ? '/tmp' : path.join(__dirname, '/runtime');

jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000;

Expand Down
Loading