Skip to content

Commit

Permalink
Rewrite puml plugin to use preRenderNode hook
Browse files Browse the repository at this point in the history
  • Loading branch information
ang-zeyu committed May 28, 2020
1 parent 4f2ca25 commit f74f118
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/lib/markbind/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ module.exports = {
getTextContent(element) {
const elements = element.children;
if (!elements || !elements.length) {
return undefined;
return '';
}

const elementStack = elements.slice();
Expand All @@ -103,6 +103,6 @@ module.exports = {
}
}

return text.join('').trim();
return text.join('');
},
};
37 changes: 13 additions & 24 deletions src/plugins/default/markbind-plugin-plantuml.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* Replaces <puml> tags with <pic> tags with the appropriate src attribute and generates the diagrams
* by running the JAR executable
*/
const cheerio = module.parent.require('cheerio');
const fs = require('fs');
const path = require('path');

Expand All @@ -17,9 +16,6 @@ const {
ERR_PROCESSING,
} = require('../../constants');

// Tracks diagrams that have already been processed
const processedDiagrams = new Set();

/**
* Generates diagram and returns the file name of the diagram
* @param fileName name of the file to be generated
Expand All @@ -33,9 +29,6 @@ function generateDiagram(fileName, content, config) {
const outputDir = path.join(path.dirname(resultPath), path.dirname(fileName));
// Path of the .puml file
const outputFilePath = path.join(outputDir, path.basename(fileName));
// Tracks built files to avoid accessing twice
if (processedDiagrams.has(outputFilePath)) { return fileName; }
processedDiagrams.add(outputFilePath);

// Creates output dir if it doesn't exist
if (!fs.existsSync(outputDir)) {
Expand Down Expand Up @@ -76,24 +69,20 @@ function generateDiagram(fileName, content, config) {
}

module.exports = {
preRender: (content, pluginContext, frontmatter, config) => {
// Clear <puml> tags processed before for live reload
processedDiagrams.clear();
// Processes all <puml> tags
const $ = cheerio.load(content, { xmlMode: true });
$('puml').each((i, tag) => {
tag.name = 'pic';
const { cwf } = tag.attribs;
const pumlContent = pluginUtil.getPluginContent($, tag, cwf);

const filePath = pluginUtil.getFilePathForPlugin(tag.attribs, pumlContent);

tag.attribs.src = generateDiagram(filePath, pumlContent, config);
tag.children = [];
});

return $.html();
preRenderNode: (node, pluginContext, frontmatter, config) => {
if (node.name === 'puml') {
node.name = 'pic';
const pumlContent = pluginUtil.getNodeSrcOrTextContent(node);

const filePath = pluginUtil.getFilePathForPlugin(node.attribs, pumlContent);

node.attribs.src = generateDiagram(filePath, pumlContent, config);
node.children = [];
}

return node;
},

getSources: () => ({
tagMap: [['puml', 'src']],
}),
Expand Down
14 changes: 9 additions & 5 deletions src/util/pluginUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const cryptoJS = require('crypto-js');
const fs = require('fs-extra-promise');
const path = require('path');
const fsUtil = require('./fsUtil');
const utils = require('../lib/markbind/src/utils');
const logger = require('./logger');

const {
Expand Down Expand Up @@ -31,12 +32,15 @@ module.exports = {
},

/**
* Returns the string content of the plugin.
* Returns the content of a node from a file pointed to by its 'src' attribute
* or the text it contains otherwise.
* This is only to be used during the preRender stage, where nodes have their 'cwf'
* attribute tagged to them.
*/
getPluginContent: ($, element, cwf) => {
if (element.attribs.src !== undefined) {
getNodeSrcOrTextContent: (node) => {
if (node.attribs.src !== undefined) {
// Path of the plugin content
const rawPath = path.resolve(path.dirname(cwf), element.attribs.src);
const rawPath = path.resolve(path.dirname(node.attribs.cwf), node.attribs.src);
try {
return fs.readFileSync(rawPath, 'utf8');
} catch (err) {
Expand All @@ -46,6 +50,6 @@ module.exports = {
}
}

return $(element).text();
return utils.getTextContent(node);
},
};

0 comments on commit f74f118

Please sign in to comment.