From 224504905b88267e5a132583716d24b87e5416b7 Mon Sep 17 00:00:00 2001 From: ache Date: Thu, 23 Nov 2017 17:29:08 +0100 Subject: Init commit --- LICENSE | 19 ++++++++++++++++ README.md | 3 +++ app.js | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 21 +++++++++++++++++ 4 files changed, 117 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 app.js create mode 100644 package.json diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9cf1062 --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +MIT License + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..9f10c02 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# remark-mermaid-simple + + diff --git a/app.js b/app.js new file mode 100644 index 0000000..1080a20 --- /dev/null +++ b/app.js @@ -0,0 +1,74 @@ +const visit = require('unist-util-visit'); + + +const PLUGIN_NAME = 'remark-mermaid-simple'; + +/** + * Given the MDAST ast, look for all fenced codeblocks that have a language of + * `mermaid` and pass that to mermaid.cli to render the image. Replaces the + * codeblocks with an image of the rendered graph. + * + * @param {object} ast + * @param {vFile} vFile + * @return {function} + */ +function visitCodeBlock(ast, vFile) { + return visit(ast, 'code', (node, index, parent) => { + const { lang, value, position } = node; + const destinationDir = getDestinationDir(vFile); + + // If this codeblock is not mermaid, bail. + if (lang !== 'mermaid') { + return node; + } + + const image = { + type: 'mermaid', + value: value, + data: { + hName: 'div', + hProperties: { + className: 'mermaid' + }, + hChildren: [ + { + type: 'text', + value: value + } + ] + } + }; + + parent.children.splice(index, 1, image); + + return node; + }); +} + +/** + * Returns the transformer which acst on the MDAST tree and given VFile. + * + * @link https://github.com/unifiedjs/unified#function-transformernode-file-next + * @link https://github.com/syntax-tree/mdast + * @link https://github.com/vfile/vfile + * @return {function} + */ +function mermaid() { + /** + * @param {object} ast MDAST + * @param {vFile} vFile + * @param {function} next + * @return {object} + */ + return function transformer(ast, vFile, next) { + visitCodeBlock(ast, vFile); + + if (typeof next === 'function') { + return next(null, ast, vFile); + } + + return ast; + }; +} + +module.exports = mermaid; diff --git a/package.json b/package.json new file mode 100644 index 0000000..3d3600f --- /dev/null +++ b/package.json @@ -0,0 +1,21 @@ +{ + "name": "remark-mermaid-simple", + "version": "0.1", + "description": "A remark plugin for Markdown that replaces `mermaid` to block.", + "main": "app.js", + "scripts": { + "start": "node app.js" + }, + "dependencies": { + "fs-extra": "^4.0.1", + "npm-which": "^3.0.1", + "unist-util-visit": "^1.1.3" + }, + "author": "ache", + "license": "MIT", + "keywords": [ + "mermaid", + "graphs", + "remark" + ] +} -- cgit v1.2.3