Skip to content

Commit

Permalink
Merge 91e00d0 into 852f5e9
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas-C committed Jan 4, 2019
2 parents 852f5e9 + 91e00d0 commit ebf2874
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 32 deletions.
7 changes: 5 additions & 2 deletions .travis.yml
@@ -1,7 +1,10 @@
language: node_js
node_js:
- '0.10'
- '0.11'
- '4'
- '5'
- '6'
- '7'
- '8'
before_script:
- npm config set coverage true
script: npm test
Expand Down
33 changes: 19 additions & 14 deletions index.coffee
Expand Up @@ -14,11 +14,18 @@ checkboxReplace = (md, options, Token) ->
idPrefix: 'checkbox'

options = _.extend defaults, options
pattern = /\[(X|\s|\_|\-)\]\s(.*)/i
# Split string into: textBefore checkboxValue textAfter:
pattern = /(.*?)\s?\[(X|\s|\_|\-)\]\s?(.*)/i


createTokens = (checked, label, Token) ->
nodes = []
createTokens = (nodes, matches, Token) ->
checked = (matches[2] == "X" || matches[2] == "x")
label = matches[3] || ''
# We recurse:
matches = label.match(pattern)
if matches != null
label = matches[1]

###*
# <div class="checkbox">
###
Expand Down Expand Up @@ -59,24 +66,22 @@ checkboxReplace = (md, options, Token) ->
if options.divWrap
nodes.push new Token("checkbox_close", "div", -1)

return nodes
if(matches != null)
createTokens(nodes, matches, Token)

splitTextToken = (original, Token) ->

text = original.content
matches = text.match pattern
matches = original.content.match(pattern)

if matches == null
return original

checked = false
value = matches[1]
label = matches[2]

if (value == "X" || value == "x")
checked = true
nodes = []
if matches[1] != null
original.content = matches[1]
nodes = [original]

return createTokens(checked, label, Token)
createTokens(nodes, matches, Token)
return nodes


return (state) ->
Expand Down
34 changes: 20 additions & 14 deletions index.js 100644 → 100755
Expand Up @@ -13,10 +13,15 @@ checkboxReplace = function(md, options, Token) {
idPrefix: 'checkbox'
};
options = _.extend(defaults, options);
pattern = /\[(X|\s|\_|\-)\]\s(.*)/i;
createTokens = function(checked, label, Token) {
var id, nodes, token;
nodes = [];
pattern = /(.*?)\s?\[(X|\s|\_|\-)\]\s?(.*)/i;
createTokens = function(nodes, matches, Token) {
var checked, id, label, token;
checked = matches[2] === "X" || matches[2] === "x";
label = matches[3] || '';
matches = label.match(pattern);
if (matches !== null) {
label = matches[1];
}

/**
* <div class="checkbox">
Expand Down Expand Up @@ -60,22 +65,23 @@ checkboxReplace = function(md, options, Token) {
if (options.divWrap) {
nodes.push(new Token("checkbox_close", "div", -1));
}
return nodes;
if (matches !== null) {
return createTokens(nodes, matches, Token);
}
};
splitTextToken = function(original, Token) {
var checked, label, matches, text, value;
text = original.content;
matches = text.match(pattern);
var matches, nodes;
matches = original.content.match(pattern);
if (matches === null) {
return original;
}
checked = false;
value = matches[1];
label = matches[2];
if (value === "X" || value === "x") {
checked = true;
nodes = [];
if (matches[1] !== null) {
original.content = matches[1];
nodes = [original];
}
return createTokens(checked, label, Token);
createTokens(nodes, matches, Token);
return nodes;
};
return function(state) {
var blockTokens, i, j, l, token, tokens;
Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -6,7 +6,7 @@
"license": "MIT",
"scripts": {
"prepublish": "gulp coffee --require coffee-script/register",
"test": "coffeelint gulpfile.coffee index.coffee test -f ./coffeelint.json && istanbul test _mocha --report lcov -- ./test/*.coffee --require coffee-script/register --reporter spec",
"test": "coffeelint gulpfile.coffee index.coffee test -f ./coffeelint.json && istanbul test node_modules/mocha/bin/_mocha --report lcov -- ./test/*.coffee --require coffee-script/register --reporter spec",
"coveralls": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
},
"keywords": [
Expand All @@ -26,7 +26,7 @@
"gulp-bump": "^0.3.0",
"gulp-coffee": "^2.1.2",
"gulp-filter": "^2.0.2",
"gulp-git": "^1.1.0",
"gulp-git": "^2.8.0",
"gulp-tag-version": "^1.2.1",
"gulp-util": "^3.0.4",
"istanbul": "*",
Expand Down
32 changes: 32 additions & 0 deletions test/test.coffee
Expand Up @@ -53,3 +53,35 @@ describe 'markdown-it-checkbox', ->
'<label for="cb0">test written</label>' +
'</p>\n'
done()

it 'should render a checkbox when it is a the end of a line', (done) ->
md = require('markdown-it')()
md.use plugin, {idPrefix: 'cb'}
res = md.render('[ ]')
res.toString().should.be.eql '<p>' +
'<input type="checkbox" id="cb0">' +
'<label for="cb0"></label>' +
'</p>\n'
done()

it 'should preserve text before & after', (done) ->
md = require('markdown-it')()
md.use plugin, {idPrefix: 'cb'}
res = md.render('before [ ] after')
res.toString().should.be.eql '<p>' +
'before<input type="checkbox" id="cb0">' +
'<label for="cb0">after</label>' +
'</p>\n'
done()

it 'should render two consecutive checkboxes', (done) ->
md = require('markdown-it')()
md.use plugin, {idPrefix: 'cb'}
res = md.render('[ ] one [ ] two')
res.toString().should.be.eql '<p>' +
'<input type="checkbox" id="cb0">' +
'<label for="cb0">one</label>' +
'<input type="checkbox" id="cb1">' +
'<label for="cb1">two</label>' +
'</p>\n'
done()

0 comments on commit ebf2874

Please sign in to comment.