diff --git a/.gitignore b/.gitignore index d0cd323..20c473a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules test/fixtures/*/build/* -.DS_Store \ No newline at end of file +.DS_Store +*.log diff --git a/lib/index.js b/lib/index.js index 0049d08..71bcfe3 100644 --- a/lib/index.js +++ b/lib/index.js @@ -6,6 +6,8 @@ var extname = require('path').extname; var join = require('path').join; var markdownIt = require('markdown-it'); var minimatch = require('minimatch'); +var multimatch = require('multimatch'); +var _ = require('deepdash')(require('lodash')); /** * Expose `plugin`. @@ -44,7 +46,7 @@ function plugin(preset, options){ if (pluginOpts) { // merge defaults with supplied options - for (var prop in pluginOpts) { + for (var prop in pluginOpts) { pluginOptions[prop] = pluginOpts[prop]; } } @@ -68,12 +70,15 @@ function plugin(preset, options){ } debug('converting file: %s', file); - pluginOptions.fields.forEach(function(field){ - debug('- checking field: %s', field); - if (!data[field]) return - debug('- converting field: %s', field); - var str = markdown.render(data[field].toString(), env); - data[field] = new Buffer(str); + all_paths = _.paths(data ,{ leavesOnly: false }); + fields_path = multimatch(all_paths, pluginOptions.fields, {matchBase: true }); + debug('- searching for the fields: %s', pluginOptions.fields); + debug('- fields found to convert: %s', fields_path); + + fields_path.forEach(function(path){ + debug('- converting field: %s', path); + var str = markdown.render(_.get(data, path).toString(), env); + _.set(data, path, new Buffer(str)); }) delete files[file]; @@ -104,4 +109,4 @@ function plugin(preset, options){ } return plugin; -} \ No newline at end of file +} diff --git a/package.json b/package.json index 2927968..2e68ed6 100644 --- a/package.json +++ b/package.json @@ -6,12 +6,13 @@ "license": "MIT", "main": "lib/index.js", "dependencies": { - "debug": "^3.2.6", + "debug": "^4.1.1", + "deepdash": "^3.1.3", "markdown-it": "^8.4.2", - "minimatch": "^3.0.4" + "multimatch": "^4.0.0" }, "devDependencies": { - "mocha": "5.x", + "mocha": "6.x", "metalsmith": "2.x", "assert-dir-equal": "1.x", "markdown-it-abbr": "^1.0.4" diff --git a/test/fixtures/plugin-options-multisubfield/expected/index.htm b/test/fixtures/plugin-options-multisubfield/expected/index.htm new file mode 100644 index 0000000..67dd1d5 --- /dev/null +++ b/test/fixtures/plugin-options-multisubfield/expected/index.htm @@ -0,0 +1,6 @@ +
With some "amazing", riveting, coooonnnntent.
+ +The excerpt has bold text in it!
+Another bold text !
+This is the submain.subfield Text mith markdown!
diff --git a/test/fixtures/plugin-options-multisubfield/src/index.html b/test/fixtures/plugin-options-multisubfield/src/index.html new file mode 100644 index 0000000..77b29ce --- /dev/null +++ b/test/fixtures/plugin-options-multisubfield/src/index.html @@ -0,0 +1,14 @@ +--- +title: Test +excerpt: +- + subfield: "The excerpt has **bold** text in it!" +- + subfield: "Another **bold** text !" +main: + submain: + subfield: "This is the submain.subfield Text mith **markdown**!" +--- +# A Markdown Post + +With some "amazing", _riveting_, **coooonnnntent**. diff --git a/test/fixtures/plugin-options-subfield/expected/index.htm b/test/fixtures/plugin-options-subfield/expected/index.htm new file mode 100644 index 0000000..886c6cf --- /dev/null +++ b/test/fixtures/plugin-options-subfield/expected/index.htm @@ -0,0 +1,4 @@ +With some "amazing", riveting, coooonnnntent.
+ +The excerpt has bold text in it!
diff --git a/test/fixtures/plugin-options-subfield/src/index.html b/test/fixtures/plugin-options-subfield/src/index.html new file mode 100644 index 0000000..3f6f6b0 --- /dev/null +++ b/test/fixtures/plugin-options-subfield/src/index.html @@ -0,0 +1,8 @@ +--- +title: Test +excerpt: + subfield: "The excerpt has **bold** text in it!" +--- +# A Markdown Post + +With some "amazing", _riveting_, **coooonnnntent**. diff --git a/test/index.js b/test/index.js index 3faf69b..06c20a2 100644 --- a/test/index.js +++ b/test/index.js @@ -10,7 +10,9 @@ describe('metalsmith-markdown', function(){ .use(markdown()) .build(function(err){ if (err) return done(err); - equal('test/fixtures/basic/expected', 'test/fixtures/basic/build'); + assert.doesNotThrow(function(){ + equal('test/fixtures/basic/build', 'test/fixtures/basic/expected'); + }); done(); }); }); @@ -20,7 +22,9 @@ describe('metalsmith-markdown', function(){ .use(markdown('default')) .build(function(err){ if (err) return done(err); - equal('test/fixtures/preset/expected', 'test/fixtures/preset/build'); + assert.doesNotThrow(function(){ + equal('test/fixtures/preset/build', 'test/fixtures/preset/expected'); + }); done(); }); }); @@ -30,7 +34,9 @@ describe('metalsmith-markdown', function(){ .use(markdown({ html: true })) .build(function(err){ if (err) return done(err); - equal('test/fixtures/options/expected', 'test/fixtures/options/build'); + assert.doesNotThrow(function(){ + equal('test/fixtures/options/build', 'test/fixtures/options/expected'); + }); done(); }); }); @@ -40,7 +46,9 @@ describe('metalsmith-markdown', function(){ .use(markdown('default', { typographer: true })) .build(function(err){ if (err) return done(err); - equal('test/fixtures/combo/expected', 'test/fixtures/combo/build'); + assert.doesNotThrow(function(){ + equal('test/fixtures/combo/build', 'test/fixtures/combo/expected'); + }); done(); }); }); @@ -53,7 +61,9 @@ describe('metalsmith-markdown', function(){ .use(md) .build(function(err){ if (err) return done(err); - equal('test/fixtures/parser/expected', 'test/fixtures/parser/build'); + assert.doesNotThrow(function(){ + equal('test/fixtures/parser/build', 'test/fixtures/parser/expected'); + }); done(); }); }); @@ -63,7 +73,9 @@ describe('metalsmith-markdown', function(){ .use(markdown('zero').enable('emphasis')) .build(function(err){ if (err) return done(err); - equal('test/fixtures/parser/expected', 'test/fixtures/parser/build'); + assert.doesNotThrow(function(){ + equal('test/fixtures/parser/build', 'test/fixtures/parser/expected'); + }); done(); }); }); @@ -75,7 +87,9 @@ describe('metalsmith-markdown', function(){ .use(md) .build(function(err){ if (err) return done(err); - equal('test/fixtures/plugin/expected', 'test/fixtures/plugin/build'); + assert.doesNotThrow(function(){ + equal('test/fixtures/plugin/build', 'test/fixtures/plugin/expected'); + }); done() }); }); @@ -85,7 +99,9 @@ describe('metalsmith-markdown', function(){ .use(markdown('default').use(require('markdown-it-abbr'))) .build(function(err){ if (err) return done(err); - equal('test/fixtures/plugin/expected', 'test/fixtures/plugin/build'); + assert.doesNotThrow(function(){ + equal('test/fixtures/plugin/build', 'test/fixtures/plugin/expected'); + }); done(); }); }); @@ -120,7 +136,9 @@ describe('metalsmith-markdown', function(){ })) .build(function(err){ if (err) return done(err); - equal('test/fixtures/env-plugin/expected', 'test/fixtures/env-plugin/build'); + assert.doesNotThrow(function(){ + equal('test/fixtures/env-plugin/build', 'test/fixtures/env-plugin/expected'); + }); done(); }) }) @@ -142,7 +160,9 @@ describe('metalsmith-markdown', function(){ }) .build(function(err){ if (err) return done(err); - equal('test/fixtures/plugin-options/expected', 'test/fixtures/plugin-options/build'); + assert.doesNotThrow(function(){ + equal('test/fixtures/plugin-options/build', 'test/fixtures/plugin-options/expected'); + }); done(); }); }); @@ -164,7 +184,57 @@ describe('metalsmith-markdown', function(){ }) .build(function(err){ if (err) return done(err); - equal('test/fixtures/plugin-options-preset/expected', 'test/fixtures/plugin-options/build'); + assert.doesNotThrow(function(){ + equal('test/fixtures/plugin-options-preset/build', 'test/fixtures/plugin-options/expected'); + }); + done(); + }); + }); + + it('should accept plugin options for subfields', function(done){ + Metalsmith('test/fixtures/plugin-options-subfield') + .use(markdown({ + plugin: { + pattern: '**/*.html', + fields: ['contents', 'excerpt.subfield'], + extension: 'htm' + } + })) + .use(function(files, metalsmith, done){ + var f = files['index.htm']; + // concat the excerpt into the main content + f.contents = f.contents.toString() + '\n' + f.excerpt.subfield.toString() + done() + }) + .build(function(err){ + if (err) return done(err); + assert.doesNotThrow(function(){ + equal('test/fixtures/plugin-options-subfield/build', 'test/fixtures/plugin-options-subfield/expected'); + }); + done(); + }); + }); + + it('should accept plugin options for multiple subfields', function(done){ + Metalsmith('test/fixtures/plugin-options-multisubfield') + .use(markdown({ + plugin: { + pattern: '**/*.html', + fields: ['contents', '*.subfield'], + extension: 'htm' + } + })) + .use(function(files, metalsmith, done){ + var f = files['index.htm']; + // concat the excerpt into the main content + f.contents = f.contents.toString() + '\n' + f.excerpt[0].subfield.toString() + f.excerpt[1].subfield.toString() + f.main.submain.subfield.toString() + done() + }) + .build(function(err){ + if (err) return done(err); + assert.doesNotThrow(function(){ + equal('test/fixtures/plugin-options-multisubfield/build', 'test/fixtures/plugin-options-multisubfield/expected'); + }); done(); }); });