Skip to content

Commit

Permalink
allow optional alternative id provision. fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfr3000 committed Sep 18, 2015
1 parent 676ae39 commit 0553653
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
10 changes: 6 additions & 4 deletions renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@ var uuid = require('uuid');
var md = require('markdown-it');
var Plugin = require('markdown-it-regexp');

function makePlugin(onAnswerOption) {
function makePlugin(onAnswerOption, opts) {
opts = opts || {};
return Plugin(
// regexp to match
/\[([\sx])\]/,

// this function will be called when something matches
function(match, utils) {
var checked = match[1] === 'x';
var id = uuid.v4();
var id = opts.getId ? opts.getId() : uuid.v4();
console.log(opts);
onAnswerOption(id, checked);
return '<input id="' + id + '" type="checkbox">';
}
);
}

module.exports = function render(input, optionsHandler) {
module.exports = function render(input, optionsHandler, opts) {
return md()
.use(makePlugin(optionsHandler))
.use(makePlugin(optionsHandler, opts))
.render(input);
};
29 changes: 24 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
var test = require("tape");
var renderer = require("../renderer");

var opts = {
getId: function() {
return "42";
}
};
test("should convert [x] to an checked HTML checkbox element", function(t){
var html = renderer('- [x] hello').replace(/\n/g, '');
t.equal(html, '<ul><li><input type="checkbox" checked> hello</li></ul>');
var html = renderer('- [x] hello', function(){}, opts).replace(/\n/g, '');
t.equal(html, '<ul><li><input id="42" type="checkbox"> hello</li></ul>');
t.end();
});
test("should convert [ ] to an unchecked HTML checkbox element", function(t){
var html = renderer('- [ ] hello').replace(/\n/g, '');
t.equal(html, '<ul><li><input type="checkbox"> hello</li></ul>');
var html = renderer('- [ ] hello', function(){}, opts).replace(/\n/g, '');
t.equal(html, '<ul><li><input id="42" type="checkbox"> hello</li></ul>');
t.end();
});

test("should call onAnswerOption with question id and its correctness", function(t){
renderer('- [ ] hello', function(id, correct){
t.equal(id, "42");
t.equal(correct, false);
t.end();
}, opts);
});
test("should call onAnswerOption with question id and its correctness", function(t){
renderer('- [x] hello', function(id, correct){
t.equal(id, "42");
t.equal(correct,true);
t.end();
}, opts);
});

0 comments on commit 0553653

Please sign in to comment.