From 679c76dfb2c7cf7b39c9838c12b6df3b270f75e8 Mon Sep 17 00:00:00 2001 From: Matthew Lyon Date: Tue, 15 Dec 2015 20:19:24 -0800 Subject: [PATCH 1/4] Add per-page markdown env-setting feature This would come in really handy for a markdownit plugin I'm working on. --- Readme.md | 16 ++++++++++ lib/index.js | 11 +++++-- test/fixtures/env-plugin/expected/index.html | 1 + test/fixtures/env-plugin/src/index.md | 5 +++ test/index.js | 32 ++++++++++++++++++++ 5 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/env-plugin/expected/index.html create mode 100644 test/fixtures/env-plugin/src/index.md diff --git a/Readme.md b/Readme.md index 9704004..8cd2900 100644 --- a/Readme.md +++ b/Readme.md @@ -60,6 +60,22 @@ md.parser.enable(['embpahsis', 'html_block', 'html_tag']); metalsmith.use(md); ``` +The parser's `enable`, `disable`, `use` and `set` methods are proxied on the metalsmith plugin, so you may access them like so: + +```js +var markdown = require('metalsmith-multimarkdownit'); + +metalsmith.use(markdown('zero', {html: true}).enable('emphasis', 'html_block', 'html_tag')) +``` + +You may provide a function to set the parser & renderer's environment on a per-page basis, should you need to: + +```js +var markdown = require('metalsmith-multimarkdownit'); + +metalsmith.use(markdown('default').env(function(page){ return page; })) +``` + ## License MIT diff --git a/lib/index.js b/lib/index.js index a08c447..90c7fa1 100644 --- a/lib/index.js +++ b/lib/index.js @@ -19,8 +19,8 @@ module.exports = plugin; */ function plugin(preset, options){ - var markdown = new markdownIt(preset, options); - + var markdown = new markdownIt(preset, options), + envSetter = function(){}; var plugin = function(files, metalsmith, done){ setImmediate(done); @@ -33,7 +33,7 @@ function plugin(preset, options){ if ('.' != dir) html = dir + '/' + html; debug('converting file: %s', file); - var str = markdown.render(data.contents.toString()); + var str = markdown.render(data.contents.toString(), envSetter(data)); data.contents = new Buffer(str); delete files[file]; @@ -53,6 +53,11 @@ function plugin(preset, options){ } }); + plugin.env = function(setter){ + envSetter = setter; + return plugin; + } + plugin.withParser = function(fn){ fn(markdown); return plugin; diff --git a/test/fixtures/env-plugin/expected/index.html b/test/fixtures/env-plugin/expected/index.html new file mode 100644 index 0000000..c61aaf3 --- /dev/null +++ b/test/fixtures/env-plugin/expected/index.html @@ -0,0 +1 @@ +

Shall we render the This is my page title now?

diff --git a/test/fixtures/env-plugin/src/index.md b/test/fixtures/env-plugin/src/index.md new file mode 100644 index 0000000..de07be8 --- /dev/null +++ b/test/fixtures/env-plugin/src/index.md @@ -0,0 +1,5 @@ +--- +title: This is my page title +--- + +Shall we render the @title now? diff --git a/test/index.js b/test/index.js index e1978d0..7ff1a6f 100644 --- a/test/index.js +++ b/test/index.js @@ -89,4 +89,36 @@ describe('metalsmith-markdown', function(){ done(); }); }); + + it('should be able to set the rendering environment per-page', function(done){ + Metalsmith('test/fixtures/env-plugin') + .use(markdown('default').env(function(page){ return page; }).use(function(md){ + md.inline.ruler.push('vars', function(state, silent){ + var pos = state.pos, + max = state.posMax; + if (state.src.charCodeAt(pos) !== '@'.charCodeAt(0)) { return false; } + while (pos < max) { + pos++; + if (state.md.utils.isSpace(state.src.charCodeAt(pos))){break;} + } + if (!silent) { + var name = state.src.slice(state.pos + 1, pos), + token = state.push('vars', '', 0); + token.markup = "@" + name; + token.content = state.env[name]; + } + state.pos = pos; + return true; + }); + md.renderer.rules['vars'] = function(tokens, id, options, env, self){ + var token = tokens[id]; + return token.content; + } + })) + .build(function(err){ + if (err) return done(err); + equal('test/fixtures/env-plugin/expected', 'test/fixtures/env-plugin/build'); + done(); + }) + }) }); From f33d177efdc70543ebf9e3804b74a2b2fc78b349 Mon Sep 17 00:00:00 2001 From: Matthew Lyon Date: Tue, 15 Dec 2015 20:20:22 -0800 Subject: [PATCH 2/4] version bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 428df95..999683a 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "metalsmith-markdownit", "description": "A Metalsmith plugin to convert markdown files.", "repository": "git://github.com/mayo/metalsmith-markdownit.git", - "version": "0.2.0", + "version": "0.3.0", "license": "MIT", "main": "lib/index.js", "dependencies": { From fd968838b1d9e9acda9780d40ce0eb0c3e80ecae Mon Sep 17 00:00:00 2001 From: Matthew Lyon Date: Wed, 16 Dec 2015 08:43:29 -0800 Subject: [PATCH 3/4] provide site metadata to env setter --- lib/index.js | 6 ++- test/fixtures/env-plugin/expected/index.html | 2 +- test/fixtures/env-plugin/src/index.md | 2 +- test/index.js | 49 +++++++++++--------- 4 files changed, 33 insertions(+), 26 deletions(-) diff --git a/lib/index.js b/lib/index.js index 90c7fa1..fea3cc2 100644 --- a/lib/index.js +++ b/lib/index.js @@ -33,7 +33,11 @@ function plugin(preset, options){ if ('.' != dir) html = dir + '/' + html; debug('converting file: %s', file); - var str = markdown.render(data.contents.toString(), envSetter(data)); + var env = {}; + if (envSetter) { + env = envSetter(data, metalsmith.metadata()); + } + var str = markdown.render(data.contents.toString(), env); data.contents = new Buffer(str); delete files[file]; diff --git a/test/fixtures/env-plugin/expected/index.html b/test/fixtures/env-plugin/expected/index.html index c61aaf3..18712b3 100644 --- a/test/fixtures/env-plugin/expected/index.html +++ b/test/fixtures/env-plugin/expected/index.html @@ -1 +1 @@ -

Shall we render the This is my page title now?

+

Shall we render the This is my page title now? Btw, we're on The test build site.

diff --git a/test/fixtures/env-plugin/src/index.md b/test/fixtures/env-plugin/src/index.md index de07be8..457237d 100644 --- a/test/fixtures/env-plugin/src/index.md +++ b/test/fixtures/env-plugin/src/index.md @@ -2,4 +2,4 @@ title: This is my page title --- -Shall we render the @title now? +Shall we render the @title now? Btw, we're on @siteName site. diff --git a/test/index.js b/test/index.js index 7ff1a6f..78eb33a 100644 --- a/test/index.js +++ b/test/index.js @@ -92,29 +92,32 @@ describe('metalsmith-markdown', function(){ it('should be able to set the rendering environment per-page', function(done){ Metalsmith('test/fixtures/env-plugin') - .use(markdown('default').env(function(page){ return page; }).use(function(md){ - md.inline.ruler.push('vars', function(state, silent){ - var pos = state.pos, - max = state.posMax; - if (state.src.charCodeAt(pos) !== '@'.charCodeAt(0)) { return false; } - while (pos < max) { - pos++; - if (state.md.utils.isSpace(state.src.charCodeAt(pos))){break;} - } - if (!silent) { - var name = state.src.slice(state.pos + 1, pos), - token = state.push('vars', '', 0); - token.markup = "@" + name; - token.content = state.env[name]; - } - state.pos = pos; - return true; - }); - md.renderer.rules['vars'] = function(tokens, id, options, env, self){ - var token = tokens[id]; - return token.content; - } - })) + .metadata({siteName: 'The test build'}) + .use(markdown('default') + .env(function(page, metadata){ return {title: page.title, siteName: metadata.siteName}; }) + .use(function(md){ + md.inline.ruler.push('vars', function(state, silent){ + var pos = state.pos, + max = state.posMax; + if (state.src.charCodeAt(pos) !== '@'.charCodeAt(0)) { return false; } + while (pos < max) { + pos++; + if (state.md.utils.isSpace(state.src.charCodeAt(pos))){break;} + } + if (!silent) { + var name = state.src.slice(state.pos + 1, pos), + token = state.push('vars', '', 0); + token.markup = "@" + name; + token.content = state.env[name]; + } + state.pos = pos; + return true; + }); + md.renderer.rules['vars'] = function(tokens, id, options, env, self){ + var token = tokens[id]; + return token.content; + } + })) .build(function(err){ if (err) return done(err); equal('test/fixtures/env-plugin/expected', 'test/fixtures/env-plugin/build'); From 37de6417e6776d1a357e4bb774356337d216550a Mon Sep 17 00:00:00 2001 From: Matthew Lyon Date: Wed, 16 Dec 2015 14:48:07 -0800 Subject: [PATCH 4/4] Update Readme.md --- Readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Readme.md b/Readme.md index 8cd2900..fe37e82 100644 --- a/Readme.md +++ b/Readme.md @@ -63,7 +63,7 @@ metalsmith.use(md); The parser's `enable`, `disable`, `use` and `set` methods are proxied on the metalsmith plugin, so you may access them like so: ```js -var markdown = require('metalsmith-multimarkdownit'); +var markdown = require('metalsmith-markdownit'); metalsmith.use(markdown('zero', {html: true}).enable('emphasis', 'html_block', 'html_tag')) ``` @@ -71,7 +71,7 @@ metalsmith.use(markdown('zero', {html: true}).enable('emphasis', 'html_block', ' You may provide a function to set the parser & renderer's environment on a per-page basis, should you need to: ```js -var markdown = require('metalsmith-multimarkdownit'); +var markdown = require('metalsmith-markdownit'); metalsmith.use(markdown('default').env(function(page){ return page; })) ```