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

Commit

Permalink
Merge pull request #6 from mattly/master
Browse files Browse the repository at this point in the history
Add per-page markdown `env` setting feature
  • Loading branch information
mayo committed Dec 17, 2015
2 parents 43c6a6a + 37de641 commit fa9ec3d
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 4 deletions.
16 changes: 16 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-markdownit');

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-markdownit');

metalsmith.use(markdown('default').env(function(page){ return page; }))
```

## License

MIT
15 changes: 12 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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());
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];
Expand All @@ -53,6 +57,11 @@ function plugin(preset, options){
}
});

plugin.env = function(setter){
envSetter = setter;
return plugin;
}

plugin.withParser = function(fn){
fn(markdown);
return plugin;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/env-plugin/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Shall we render the This is my page title now? Btw, we're on The test build site.</p>
5 changes: 5 additions & 0 deletions test/fixtures/env-plugin/src/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: This is my page title
---

Shall we render the @title now? Btw, we're on @siteName site.
35 changes: 35 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,39 @@ describe('metalsmith-markdown', function(){
done();
});
});

it('should be able to set the rendering environment per-page', function(done){
Metalsmith('test/fixtures/env-plugin')
.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');
done();
})
})
});

0 comments on commit fa9ec3d

Please sign in to comment.