UNPKG

gatsby-remark-vega

Version:

Adds the ability to include a [Vega](https://vega.github.io/vega/) visualization from a markdown file.

42 lines (31 loc) 1.03 kB
"use strict"; var visit = require('unist-util-visit'); var getPageDir = function getPageDir(_ref) { var parent = _ref.parent; return parent.split(" ").shift().split("/").slice(0, -1).join("/"); }; var escape = function escape(txt) { return txt.replace(/"/g, '&quot;'); }; var MARKDOWN_TAG = "vega"; module.exports = function (_ref2) { var markdownNode = _ref2.markdownNode, markdownAST = _ref2.markdownAST; visit(markdownAST, "inlineCode", function (node) { var value = node.value; if (value.startsWith(MARKDOWN_TAG + ":")) { var dir = getPageDir(markdownNode); var file = value.split(':').pop(); var path = dir + "/" + file; console.log('require fs!'); var fs = require("fs"); if (!fs.existsSync(path)) { throw Error("Invalid file specified; no such file \"" + path + "\""); } var spec = fs.readFileSync(path, "utf8"); node.type = "html"; node.value = "<vega spec=\"" + escape(spec) + "\" />"; } }); return markdownAST; };