|
| 1 | +/*! markdown-it-task-lists 1.1.0 https://github.com/revin/markdown-it-task-lists#readme by @license ISC */ |
| 2 | +(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.markdownitTaskLists = 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){ |
| 3 | +// Markdown-it plugin to render GitHub-style task lists; see |
| 4 | +// |
| 5 | +// https://github.com/blog/1375-task-lists-in-gfm-issues-pulls-comments |
| 6 | +// https://github.com/blog/1825-task-lists-in-all-markdown-documents |
| 7 | + |
| 8 | +module.exports = function(md, options) { |
| 9 | +md.core.ruler.after('inline', 'github-task-lists', function(state) { |
| 10 | +var tokens = state.tokens; |
| 11 | +for (var i = 2; i < tokens.length; i++) { |
| 12 | +if (isTodoItem(tokens, i)) { |
| 13 | +todoify(tokens[i], state.Token); |
| 14 | +tokens[i-2].attrSet('class', 'task-list-item'); |
| 15 | +tokens[parentToken(tokens, i-2)].attrSet('class', 'task-list'); |
| 16 | +} |
| 17 | +} |
| 18 | +}); |
| 19 | +}; |
| 20 | + |
| 21 | +function parentToken(tokens, index) { |
| 22 | +var targetLevel = tokens[index].level - 1; |
| 23 | +for (var i = index - 1; i >= 0; i--) { |
| 24 | +if (tokens[i].level === targetLevel) { |
| 25 | +return i; |
| 26 | +} |
| 27 | +} |
| 28 | +return -1; |
| 29 | +} |
| 30 | + |
| 31 | +function isTodoItem(tokens, index) { |
| 32 | +return isInline(tokens[index]) && |
| 33 | + isParagraph(tokens[index - 1]) && |
| 34 | + isListItem(tokens[index - 2]) && |
| 35 | + startsWithTodoMarkdown(tokens[index]); |
| 36 | +} |
| 37 | + |
| 38 | +function todoify(token, TokenConstructor) { |
| 39 | +token.children.unshift(makeCheckbox(token, TokenConstructor)); |
| 40 | +token.children[1].content = token.children[1].content.slice(3); |
| 41 | +token.content = token.content.slice(3); |
| 42 | +} |
| 43 | + |
| 44 | +function makeCheckbox(token, TokenConstructor) { |
| 45 | +var checkbox = new TokenConstructor('html_inline', '', 0); |
| 46 | +if (token.content.indexOf('[ ]') === 0) { |
| 47 | +checkbox.content = '<input class="task-list-item-checkbox" disabled="" type="checkbox">'; |
| 48 | +} else if (token.content.indexOf('[x]') === 0) { |
| 49 | +checkbox.content = '<input class="task-list-item-checkbox" checked="" disabled="" type="checkbox">'; |
| 50 | +} |
| 51 | +return checkbox; |
| 52 | +} |
| 53 | + |
| 54 | +function isInline(token) { return token.type === 'inline'; } |
| 55 | +function isParagraph(token) { return token.type === 'paragraph_open'; } |
| 56 | +function isListItem(token) { return token.type === 'list_item_open'; } |
| 57 | + |
| 58 | +function startsWithTodoMarkdown(token) { |
| 59 | +// leading whitespace in a list item is already trimmed off by markdown-it |
| 60 | +return token.content.indexOf('[ ]') === 0 || token.content.indexOf('[x]') === 0; |
| 61 | +} |
| 62 | + |
| 63 | +},{}]},{},[1])(1) |
| 64 | +}); |
0 commit comments