markdown-it-checkbox
Version:
Plugin to create checkboxes for markdown-it markdown parser
99 lines (88 loc) • 3.3 kB
JavaScript
/*! 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){
function checkboxReplace (md) {
var arrayReplaceAt, lastId, options, pattern, splitTextToken;
arrayReplaceAt = md.utils.arrayReplaceAt;
lastId = 0;
options = {
divWrap: false
};
pattern = /\[(X|\s|\_|\-)\]\s(.*)/i;
function splitTextToken(original, Token) {
var checked, id, label, matches, nodes, ref, text, token, value;
text = original.content;
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"]];
nodes.push(token);
}
/**
* <input type="checkbox" id="checkbox{n}" checked="true">
*/
id = "checkbox" + lastId;
lastId += 1;
token = new Token("checkbox_input", "input", 0);
token.attrs = [["type", "checkbox"], ["id", id]];
if (checked === true) {
token.attrs.push(["checked", "true"]);
}
nodes.push(token);
/**
* <label for="checkbox{n}">
*/
token = new Token("label_open", "label", 1);
token.attrs = [["for", id]];
nodes.push(token);
/**
* content of label tag
*/
token = new Token("text", "", 0);
token.content = label;
nodes.push(token);
/**
* closing tags
*/
nodes.push(new Token("label_close", "label", -1));
if (options.div_wrap) {
nodes.push(new Token("checkbox_close", "div", -1));
}
return nodes;
};
return function(state) {
var blockTokens, i, j, l, token, tokens;
blockTokens = state.tokens;
j = 0;
l = blockTokens.length;
while (j < l) {
if (blockTokens[j].type !== "inline") {
j++;
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--;
}
j++;
}
};
};
module.exports = function checkbox_plugin(md) {
md.core.ruler.push("checkbox", checkboxReplace(md));
};
},{}]},{},[1])(1)
});