From 8f5ad313b89ba0c59621cd2f0f7e04b98394e41a Mon Sep 17 00:00:00 2001 From: Eduard Mann Date: Fri, 19 Apr 2019 09:02:00 +0200 Subject: [PATCH 1/8] .gitignore logfiles --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 From 0dd96863990cf9029fd17344cc99664840cf86b3 Mon Sep 17 00:00:00 2001 From: Eduard Mann Date: Fri, 19 Apr 2019 09:03:14 +0200 Subject: [PATCH 2/8] Added testcase for subfileds - options->fields --- .../expected/index.htm | 4 ++++ .../plugin-options-subfield/src/index.html | 8 +++++++ test/index.js | 22 +++++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 test/fixtures/plugin-options-subfield/expected/index.htm create mode 100644 test/fixtures/plugin-options-subfield/src/index.html 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 @@ +

A Markdown Post

+

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..279884d --- /dev/null +++ b/test/fixtures/plugin-options-subfield/src/index.html @@ -0,0 +1,8 @@ +--- +title: Test +excerpt: + sub: "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..cdb2678 100644 --- a/test/index.js +++ b/test/index.js @@ -169,4 +169,26 @@ describe('metalsmith-markdown', function(){ }); }); + it('should accept plugin options for subfields', function(done){ + Metalsmith('test/fixtures/plugin-options-subfield') + .use(markdown({ + plugin: { + pattern: '**/*.html', + fields: ['contents', 'excerpt.sub'], + 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.sub.toString() + done() + }) + .build(function(err){ + if (err) return done(err); + equal('test/fixtures/plugin-options-subfield/expected', 'test/fixtures/plugin-options-subfield/build'); + done(); + }); + }); + }); From 0b3e559348a5f2214868aff20a757a737c6850f8 Mon Sep 17 00:00:00 2001 From: Eduard Mann Date: Fri, 19 Apr 2019 09:09:02 +0200 Subject: [PATCH 3/8] mocha and debug upgrade dependencies --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 2927968..4fd9189 100644 --- a/package.json +++ b/package.json @@ -6,12 +6,12 @@ "license": "MIT", "main": "lib/index.js", "dependencies": { - "debug": "^3.2.6", + "debug": "^4.1.1", "markdown-it": "^8.4.2", "minimatch": "^3.0.4" }, "devDependencies": { - "mocha": "5.x", + "mocha": "6.x", "metalsmith": "2.x", "assert-dir-equal": "1.x", "markdown-it-abbr": "^1.0.4" From e6dfabd50b55bb1875892e6a7fadee0039a766a8 Mon Sep 17 00:00:00 2001 From: Eduard Mann Date: Fri, 19 Apr 2019 23:01:58 +0200 Subject: [PATCH 4/8] fixed the testcase. --- test/fixtures/plugin-options-subfield/src/index.html | 2 +- test/index.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/fixtures/plugin-options-subfield/src/index.html b/test/fixtures/plugin-options-subfield/src/index.html index 279884d..3f6f6b0 100644 --- a/test/fixtures/plugin-options-subfield/src/index.html +++ b/test/fixtures/plugin-options-subfield/src/index.html @@ -1,7 +1,7 @@ --- title: Test excerpt: - sub: "The excerpt has **bold** text in it!"" + subfield: "The excerpt has **bold** text in it!" --- # A Markdown Post diff --git a/test/index.js b/test/index.js index cdb2678..beccf4f 100644 --- a/test/index.js +++ b/test/index.js @@ -174,14 +174,14 @@ describe('metalsmith-markdown', function(){ .use(markdown({ plugin: { pattern: '**/*.html', - fields: ['contents', 'excerpt.sub'], + 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.sub.toString() + f.contents = f.contents.toString() + '\n' + f.excerpt.subfield.toString() done() }) .build(function(err){ From 958cfd3272830ef39873c73078024c3f47a00a54 Mon Sep 17 00:00:00 2001 From: Eduard Mann Date: Fri, 19 Apr 2019 23:02:43 +0200 Subject: [PATCH 5/8] implemented subfield rendering, using loadash --- lib/index.js | 13 +++++++------ package.json | 1 + 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/index.js b/lib/index.js index 0049d08..6fd239b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -6,6 +6,7 @@ var extname = require('path').extname; var join = require('path').join; var markdownIt = require('markdown-it'); var minimatch = require('minimatch'); +var _ = require('lodash'); /** * Expose `plugin`. @@ -44,7 +45,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]; } } @@ -70,10 +71,10 @@ 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); + if (!_.has(data, field)) return + debug('- converting field: %s', field); + var str = markdown.render(_.get(data, field).toString(), env); + _.set(data, field, new Buffer(str)); }) delete files[file]; @@ -104,4 +105,4 @@ function plugin(preset, options){ } return plugin; -} \ No newline at end of file +} diff --git a/package.json b/package.json index 4fd9189..9eccc81 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "main": "lib/index.js", "dependencies": { "debug": "^4.1.1", + "lodash": "^4.17.11", "markdown-it": "^8.4.2", "minimatch": "^3.0.4" }, From f30401c4e0bc838d4dd1caaefe73626af27758a2 Mon Sep 17 00:00:00 2001 From: Eduard Mann Date: Mon, 22 Apr 2019 22:29:58 +0200 Subject: [PATCH 6/8] added testecase for multiple subfields --- .../expected/index.htm | 6 +++++ .../src/index.html | 14 ++++++++++++ test/index.js | 22 +++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 test/fixtures/plugin-options-multisubfield/expected/index.htm create mode 100644 test/fixtures/plugin-options-multisubfield/src/index.html 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 @@ +

A Markdown Post

+

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/index.js b/test/index.js index beccf4f..a76aba7 100644 --- a/test/index.js +++ b/test/index.js @@ -191,4 +191,26 @@ describe('metalsmith-markdown', function(){ }); }); + 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() + '\n' + f.excerpt[1].subfield.toString() + '\n' + f.main.submain.subfield.toString() + done() + }) + .build(function(err){ + if (err) return done(err); + equal('test/fixtures/plugin-options-multisubfield/expected', 'test/fixtures/plugin-options-multisubfield/build'); + done(); + }); + }); + }); From 652788301ca6a196ad156bdb4a681d9f4def3c88 Mon Sep 17 00:00:00 2001 From: Eduard Mann Date: Wed, 24 Apr 2019 22:41:25 +0200 Subject: [PATCH 7/8] adjusted the order of equal function, added "doesNotThrow" --- test/index.js | 56 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/test/index.js b/test/index.js index a76aba7..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,9 @@ 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(); }); }); @@ -186,7 +208,9 @@ describe('metalsmith-markdown', function(){ }) .build(function(err){ if (err) return done(err); - equal('test/fixtures/plugin-options-subfield/expected', 'test/fixtures/plugin-options-subfield/build'); + assert.doesNotThrow(function(){ + equal('test/fixtures/plugin-options-subfield/build', 'test/fixtures/plugin-options-subfield/expected'); + }); done(); }); }); @@ -196,19 +220,21 @@ describe('metalsmith-markdown', function(){ .use(markdown({ plugin: { pattern: '**/*.html', - fields: ['contents', 'subfield'], + 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() + '\n' + f.excerpt[1].subfield.toString() + '\n' + f.main.submain.subfield.toString() + 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); - equal('test/fixtures/plugin-options-multisubfield/expected', 'test/fixtures/plugin-options-multisubfield/build'); + assert.doesNotThrow(function(){ + equal('test/fixtures/plugin-options-multisubfield/build', 'test/fixtures/plugin-options-multisubfield/expected'); + }); done(); }); }); From f44451de890a152a03e7f8012fb58d37cf847d58 Mon Sep 17 00:00:00 2001 From: Eduard Mann Date: Wed, 24 Apr 2019 22:47:12 +0200 Subject: [PATCH 8/8] added capability for multiple filds --- lib/index.js | 18 +++++++++++------- package.json | 4 ++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/index.js b/lib/index.js index 6fd239b..71bcfe3 100644 --- a/lib/index.js +++ b/lib/index.js @@ -6,7 +6,8 @@ var extname = require('path').extname; var join = require('path').join; var markdownIt = require('markdown-it'); var minimatch = require('minimatch'); -var _ = require('lodash'); +var multimatch = require('multimatch'); +var _ = require('deepdash')(require('lodash')); /** * Expose `plugin`. @@ -69,12 +70,15 @@ function plugin(preset, options){ } debug('converting file: %s', file); - pluginOptions.fields.forEach(function(field){ - debug('- checking field: %s', field); - if (!_.has(data, field)) return - debug('- converting field: %s', field); - var str = markdown.render(_.get(data, field).toString(), env); - _.set(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]; diff --git a/package.json b/package.json index 9eccc81..2e68ed6 100644 --- a/package.json +++ b/package.json @@ -7,9 +7,9 @@ "main": "lib/index.js", "dependencies": { "debug": "^4.1.1", - "lodash": "^4.17.11", + "deepdash": "^3.1.3", "markdown-it": "^8.4.2", - "minimatch": "^3.0.4" + "multimatch": "^4.0.0" }, "devDependencies": { "mocha": "6.x",