From 17f6dce3ed2ff04580120d0dae401ecff89a048f Mon Sep 17 00:00:00 2001 From: Lloyd Brookes Date: Sat, 31 Aug 2024 22:49:12 +0100 Subject: [PATCH] minor refactor.. test plugins can override helpers correctly --- index.js | 7 +++++-- lib/handlebars-template.js | 9 --------- test/fixture/dmd-plugin-example/helper/helper.js | 8 ++++++-- .../fixture/dmd-plugin-example/lib/dmd-plugin-example.js | 7 +++++++ test/fixture/dmd-plugin-example/partial/main.hbs | 2 ++ 5 files changed, 20 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index f1f2579..d0be5b4 100644 --- a/index.js +++ b/index.js @@ -13,6 +13,10 @@ const HandlebarsTemplate = require('./lib/handlebars-template.js') const pkg = JSON.parse(fs.readFileSync(path.resolve(__dirname, './package.json'), 'utf8')) const dmdVersion = pkg.version +/** + * @module dmd + */ + /** * Transforms jsdoc-parse data into markdown documentation. * @param {object[]} @@ -64,12 +68,11 @@ async function generate (templateData, options) { const inputData = templateData.map(row => Object.assign({}, row)) const inputOptions = Object.assign({}, options) - options = Object.assign(new DmdOptions(), options) - options.plugin = arrayify(options.plugin) /* used by helpers.headingDepth */ options._depth = 0 /* used by helpers.indexDepth */ options._indexDepth = 0 + /* This gives all helper code access to the dmd options */ templateData.options = options /* state module, for sharing data between the helpers - functions as a global object */ diff --git a/lib/handlebars-template.js b/lib/handlebars-template.js index 8c5fde0..dab2a4b 100644 --- a/lib/handlebars-template.js +++ b/lib/handlebars-template.js @@ -1,17 +1,8 @@ const handlebars = require('handlebars') -const path = require('path') -const fs = require('fs') class HandlebarsTemplate { handlebars = handlebars - async registerPartialFile (file) { - handlebars.registerPartial( - path.basename(file, '.hbs'), - fs.readFileSync(file, 'utf8') || '' - ) - } - generate (template, templateData) { const compiled = handlebars.compile(template, { preventIndent: true, diff --git a/test/fixture/dmd-plugin-example/helper/helper.js b/test/fixture/dmd-plugin-example/helper/helper.js index 38d074e..7d366c0 100644 --- a/test/fixture/dmd-plugin-example/helper/helper.js +++ b/test/fixture/dmd-plugin-example/helper/helper.js @@ -1,3 +1,7 @@ -exports.generatedDate = function(){ - return new Date().toUTCString(); +exports.generatedDate = function () { + return new Date().toUTCString() +} + +exports.orphans = function () { + return 'Orphans Override.\n' } diff --git a/test/fixture/dmd-plugin-example/lib/dmd-plugin-example.js b/test/fixture/dmd-plugin-example/lib/dmd-plugin-example.js index 94fbab8..99260d1 100644 --- a/test/fixture/dmd-plugin-example/lib/dmd-plugin-example.js +++ b/test/fixture/dmd-plugin-example/lib/dmd-plugin-example.js @@ -4,3 +4,10 @@ module.exports = function(options){ helper: __dirname + "/../helper/*.js" }; }; + +/* +Command to test this plugin: + +jsdoc2md --plugin test/fixture/dmd-plugin-example/lib/dmd-plugin-example.js --files index.js + */ + diff --git a/test/fixture/dmd-plugin-example/partial/main.hbs b/test/fixture/dmd-plugin-example/partial/main.hbs index 2ffc719..d3eb84c 100644 --- a/test/fixture/dmd-plugin-example/partial/main.hbs +++ b/test/fixture/dmd-plugin-example/partial/main.hbs @@ -2,3 +2,5 @@ {{>all-docs~}} **documentation generated on {{generatedDate}}** + +The system helper `\{{#orphans}}` was overrided in this example.