dgeni-packages
Version:
A collection of dgeni packages for generating documentation from source code
53 lines • 2.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PostProcessHtml = exports.postProcessHtml = void 0;
const rehype = require("rehype");
function postProcessHtml(log, createDocMessage) {
return new PostProcessHtml(log, createDocMessage);
}
exports.postProcessHtml = postProcessHtml;
/**
* @dgProcessor postProcessHtml
*
* @description
* Use the rehype processing engine to manipulate the
* `renderedContent` HTML via rehype "plugins" that work with HTML ASTs (HASTs).
* See https://github.com/rehypejs/rehype
*
* Each plugin is a factory function that will be called with the "rehype" engine as `this`.
* The factory should return a "transform" function that takes a HAST and returns a `boolean` or `undefined`.
* The HAST can be mutated by the "transform" function.
* If `false` is returned then the processing stops with that plugin.
*
*/
class PostProcessHtml {
constructor(log, createDocMessage) {
this.log = log;
this.createDocMessage = createDocMessage;
this.$runAfter = ['docs-rendered'];
this.$runBefore = ['writing-files'];
this.docTypes = [];
this.plugins = [];
}
$process(docs) {
const engine = rehype().data('settings', { fragment: true });
this.plugins.forEach(plugin => engine.use(plugin));
let vFile;
docs
.filter((doc) => this.docTypes.indexOf(doc.docType) !== -1)
.forEach((doc) => {
try {
vFile = engine.processSync(doc.renderedContent);
doc.renderedContent = vFile.contents.toString();
vFile.messages.forEach((message) => this.log.warn(this.createDocMessage(message.message, doc)));
doc.vFile = vFile;
}
catch (e) {
const errorMessage = (e instanceof Error) ? e.message : `${e}`;
throw new Error(this.createDocMessage(errorMessage, doc));
}
});
}
}
exports.PostProcessHtml = PostProcessHtml;
//# sourceMappingURL=postProcessHtml.js.map