Skip to content
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ lint:
./node_modules/.bin/eslint --reset .

test: lint
./node_modules/.bin/mocha -R spec
./node_modules/.bin/mocha -R spec --require coffee-script/register --compilers coffee:coffee-script/register

coverage:
rm -rf coverage
Expand Down
87 changes: 53 additions & 34 deletions dist/markdown-it-checkbox.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,35 @@
/*! markdown-it-checkbox 1.0.0 https://github.com//mcecot/markdown-it-checkbox @license MIT */(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.markdownitCheckbox = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
"use strict"
/*! markdown-it-checkbox 1.1.1 https://github.com//mcecot/markdown-it-checkbox @license MIT */(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.markdownitCheckbox = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var checkboxReplace;

function checkboxReplace (md) {

var arrayReplaceAt, lastId, options, pattern, splitTextToken;
checkboxReplace = function(md, options, Token) {
"use strict";
var arrayReplaceAt, createTokens, defaults, lastId, pattern, splitTextToken;
arrayReplaceAt = md.utils.arrayReplaceAt;
lastId = 0;
options = {
divWrap: false
defaults = {
divWrap: false,
divClass: 'checkbox',
idPrefix: 'checkbox'
};
options = md.utils.assign(defaults, options);
pattern = /\[(X|\s|\_|\-)\]\s(.*)/i;

function splitTextToken(original, Token) {
var checked, id, label, matches, nodes, ref, text, token, value;
text = original.content;
createTokens = function(checked, label, original, Token) {
var child, i, id, len, nodes, ref, token;
nodes = [];
matches = text.match(pattern);
value = matches[1];
label = matches[2];
checked = (ref = value === "X" || value === "x") != null ? ref : {
"true": false
};

/**
* <div class="checkbox">
*/
if (options.divWrap) {
token = new Token("checkbox_open", "div", 1);
token.attrs = [["class", "checkbox"]];
token.attrs = [["class", options.divClass]];
nodes.push(token);
}

/**
* <input type="checkbox" id="checkbox{n}" checked="true">
*/
id = "checkbox" + lastId;
id = options.idPrefix + lastId;
lastId += 1;
token = new Token("checkbox_input", "input", 0);
token.attrs = [["type", "checkbox"], ["id", id]];
Expand All @@ -55,20 +50,46 @@ function checkboxReplace (md) {
*/
token = new Token("text", "", 0);
token.content = label;
nodes.push(token);
original.children[0].content = label;
ref = original.children;
for (i = 0, len = ref.length; i < len; i++) {
child = ref[i];
nodes.push(child);
}

/**
* closing tags
*/
nodes.push(new Token("label_close", "label", -1));
if (options.div_wrap) {
if (options.divWrap) {
nodes.push(new Token("checkbox_close", "div", -1));
}
return nodes;
original.children = nodes;
return original;
};
splitTextToken = function(original, Token) {
var checked, first_node, label, matches, text, value;
if (original.children != null) {
first_node = original.children[0];
if (first_node != null) {
text = first_node.content;
matches = text.match(pattern);
if (matches === null) {
return original;
}
checked = false;
value = matches[1];
label = matches[2];
if (value === "X" || value === "x") {
checked = true;
}
return createTokens(checked, label, original, Token);
}
}
return original;
};

return function(state) {
var blockTokens, i, j, l, token, tokens;
var blockTokens, j, l, token, tokens;
blockTokens = state.tokens;
j = 0;
l = blockTokens.length;
Expand All @@ -78,21 +99,19 @@ function checkboxReplace (md) {
continue;
}
tokens = blockTokens[j].children;
i = tokens.length - 1;
while (i >= 0) {
token = tokens[i];
if (token.type === "text" && pattern.test(token.content)) {
blockTokens[j].children = tokens = arrayReplaceAt(tokens, i, splitTextToken(token, state.Token));
}
i--;
}
token = blockTokens[j];
arrayReplaceAt(blockTokens, j, splitTextToken(token, state.Token));
j++;
}
};
};

module.exports = function checkbox_plugin(md) {
md.core.ruler.push("checkbox", checkboxReplace(md));

/*global module */

module.exports = function(md, options) {
"use strict";
md.core.ruler.push("checkbox", checkboxReplace(md, options));
};

},{}]},{},[1])(1)
Expand Down
4 changes: 2 additions & 2 deletions dist/markdown-it-checkbox.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 25 additions & 28 deletions index.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
_ = require 'underscore'

# Checkbox replacement logic.
#

Expand All @@ -13,11 +11,11 @@ checkboxReplace = (md, options, Token) ->
divClass: 'checkbox'
idPrefix: 'checkbox'

options = _.extend defaults, options
options = md.utils.assign(defaults, options)
pattern = /\[(X|\s|\_|\-)\]\s(.*)/i


createTokens = (checked, label, Token) ->
createTokens = (checked, label, original, Token) ->
nodes = []
###*
# <div class="checkbox">
Expand Down Expand Up @@ -50,34 +48,40 @@ checkboxReplace = (md, options, Token) ->
###
token = new Token("text", "", 0)
token.content = label
nodes.push token

original.children[0].content = label
nodes.push(child) for child in original.children

###*
# closing tags
###
nodes.push new Token("label_close", "label", -1)
if options.divWrap
nodes.push new Token("checkbox_close", "div", -1)

return nodes

original.children = nodes
return original

splitTextToken = (original, Token) ->

text = original.content
matches = text.match pattern

if matches == null
return original
if original.children?
first_node = original.children[0]
if first_node?
text = first_node.content
matches = text.match pattern

checked = false
value = matches[1]
label = matches[2]
if matches == null
return original

if (value == "X" || value == "x")
checked = true
checked = false
value = matches[1]
label = matches[2]

return createTokens(checked, label, Token)
if (value == "X" || value == "x")
checked = true

return createTokens(checked, label, original, Token)

return original

return (state) ->
blockTokens = state.tokens
Expand All @@ -88,15 +92,8 @@ checkboxReplace = (md, options, Token) ->
j++
continue
tokens = blockTokens[j].children
# We scan from the end, to keep position when new tags added.
# Use reversed logic in links start/end match
i = tokens.length - 1
while i >= 0
token = tokens[i]
blockTokens[j].children = tokens = arrayReplaceAt(
tokens, i, splitTextToken(token, state.Token)
)
i--
token = blockTokens[j]
arrayReplaceAt(blockTokens, j, splitTextToken(token, state.Token))
j++
return

Expand Down
60 changes: 33 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
var _, checkboxReplace;

_ = require('underscore');
var checkboxReplace;

checkboxReplace = function(md, options, Token) {
"use strict";
Expand All @@ -12,10 +10,10 @@ checkboxReplace = function(md, options, Token) {
divClass: 'checkbox',
idPrefix: 'checkbox'
};
options = _.extend(defaults, options);
options = md.utils.assign(defaults, options);
pattern = /\[(X|\s|\_|\-)\]\s(.*)/i;
createTokens = function(checked, label, Token) {
var id, nodes, token;
createTokens = function(checked, label, original, Token) {
var child, i, id, len, nodes, ref, token;
nodes = [];

/**
Expand Down Expand Up @@ -51,7 +49,12 @@ checkboxReplace = function(md, options, Token) {
*/
token = new Token("text", "", 0);
token.content = label;
nodes.push(token);
original.children[0].content = label;
ref = original.children;
for (i = 0, len = ref.length; i < len; i++) {
child = ref[i];
nodes.push(child);
}

/**
* closing tags
Expand All @@ -60,25 +63,32 @@ checkboxReplace = function(md, options, Token) {
if (options.divWrap) {
nodes.push(new Token("checkbox_close", "div", -1));
}
return nodes;
original.children = nodes;
return original;
};
splitTextToken = function(original, Token) {
var checked, label, matches, text, value;
text = original.content;
matches = text.match(pattern);
if (matches === null) {
return original;
}
checked = false;
value = matches[1];
label = matches[2];
if (value === "X" || value === "x") {
checked = true;
var checked, first_node, label, matches, text, value;
if (original.children != null) {
first_node = original.children[0];
if (first_node != null) {
text = first_node.content;
matches = text.match(pattern);
if (matches === null) {
return original;
}
checked = false;
value = matches[1];
label = matches[2];
if (value === "X" || value === "x") {
checked = true;
}
return createTokens(checked, label, original, Token);
}
}
return createTokens(checked, label, Token);
return original;
};
return function(state) {
var blockTokens, i, j, l, token, tokens;
var blockTokens, j, l, token, tokens;
blockTokens = state.tokens;
j = 0;
l = blockTokens.length;
Expand All @@ -88,12 +98,8 @@ checkboxReplace = function(md, options, Token) {
continue;
}
tokens = blockTokens[j].children;
i = tokens.length - 1;
while (i >= 0) {
token = tokens[i];
blockTokens[j].children = tokens = arrayReplaceAt(tokens, i, splitTextToken(token, state.Token));
i--;
}
token = blockTokens[j];
arrayReplaceAt(blockTokens, j, splitTextToken(token, state.Token));
j++;
}
};
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/checkbox.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@
<li><input type="checkbox" id="checkbox5" checked="true"><label for="checkbox5">checked</label></li>
</ul>
.

.
- [X] checked _with_ [link](http://example.com)
.
<ul>
<li><input type="checkbox" id="checkbox6" checked="true"><label for="checkbox6">checked <em>with</em> <a href="http://example.com">link</a></label></li>
</ul>
.