Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

Multiple subfields rendering #21

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
test/fixtures/*/build/*
.DS_Store
.DS_Store
*.log
21 changes: 13 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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];
}
}
Expand All @@ -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];
Expand Down Expand Up @@ -104,4 +109,4 @@ function plugin(preset, options){
}

return plugin;
}
}
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/plugin-options-multisubfield/expected/index.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>A Markdown Post</h1>
<p>With some &quot;amazing&quot;, <em>riveting</em>, <strong>coooonnnntent</strong>.</p>

<p>The excerpt has <strong>bold</strong> text in it!</p>
<p>Another <strong>bold</strong> text !</p>
<p>This is the submain.subfield Text mith <strong>markdown</strong>!</p>
14 changes: 14 additions & 0 deletions test/fixtures/plugin-options-multisubfield/src/index.html
Original file line number Diff line number Diff line change
@@ -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**.
4 changes: 4 additions & 0 deletions test/fixtures/plugin-options-subfield/expected/index.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h1>A Markdown Post</h1>
<p>With some &quot;amazing&quot;, <em>riveting</em>, <strong>coooonnnntent</strong>.</p>

<p>The excerpt has <strong>bold</strong> text in it!</p>
8 changes: 8 additions & 0 deletions test/fixtures/plugin-options-subfield/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: Test
excerpt:
subfield: "The excerpt has **bold** text in it!"
---
# A Markdown Post

With some "amazing", _riveting_, **coooonnnntent**.
92 changes: 81 additions & 11 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Expand All @@ -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();
});
});
Expand All @@ -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();
});
});
Expand All @@ -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();
});
});
Expand All @@ -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();
});
});
Expand All @@ -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();
});
});
Expand All @@ -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()
});
});
Expand All @@ -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();
});
});
Expand Down Expand Up @@ -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();
})
})
Expand All @@ -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();
});
});
Expand All @@ -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();
});
});
Expand Down