markdown-it-todoList.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*! markdown-it-task-lists 2.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(){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}return e})()({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. var disableCheckboxes = true;
  8. var useLabelWrapper = false;
  9. var useLabelAfter = false;
  10. module.exports = function(md, options) {
  11. if (options) {
  12. disableCheckboxes = !options.enabled;
  13. useLabelWrapper = !!options.label;
  14. useLabelAfter = !!options.labelAfter;
  15. }
  16. md.core.ruler.after('inline', 'github-task-lists', function(state) {
  17. var tokens = state.tokens;
  18. // 用于寻找关闭标签的数组
  19. let tagPaired = (index)=>{
  20. let targetItem,
  21. item = tokens[index],
  22. tag = item.tag,
  23. level = item.level,
  24. targetType = item.type === 'list_item_open' ? 'list_item_close' : 'bullet_list_close';
  25. for(let i = index,len=tokens.length; i<len; i++){
  26. let _item = tokens[i];
  27. if(_item.tag === tag && level === _item.level && _item.type === targetType){
  28. targetItem = _item;
  29. break;
  30. };
  31. };
  32. return targetItem;
  33. };
  34. for (var i = 2; i < tokens.length; i++) {
  35. if (isTodoItem(tokens, i)) {
  36. todoify(tokens[i], state.Token);
  37. attrSet(tokens[i-2], 'class', 'task-list-item' + (!disableCheckboxes ? ' enabled' : ''));
  38. attrSet(tokens[parentToken(tokens, i-2)], 'class', 'contains-task-list');
  39. tagPaired(parentToken(tokens, i-2)).tag = 'todogroup';
  40. tokens[parentToken(tokens, i-2)].tag = 'todogroup';
  41. tagPaired(i-2).tag = 'todolist';
  42. tokens[i-2].tag = 'todolist';
  43. };
  44. };
  45. });
  46. };
  47. function attrSet(token, name, value) {
  48. var index = token.attrIndex(name);
  49. var attr = [name, value];
  50. if (index < 0) {
  51. token.attrPush(attr);
  52. } else {
  53. token.attrs[index] = attr;
  54. }
  55. }
  56. function parentToken(tokens, index) {
  57. var targetLevel = tokens[index].level - 1;
  58. for (var i = index - 1; i >= 0; i--) {
  59. if (tokens[i].level === targetLevel) {
  60. return i;
  61. }
  62. }
  63. return -1;
  64. }
  65. function isTodoItem(tokens, index) {
  66. return isInline(tokens[index]) &&
  67. isParagraph(tokens[index - 1]) &&
  68. isListItem(tokens[index - 2]) &&
  69. startsWithTodoMarkdown(tokens[index]);
  70. }
  71. function todoify(token, TokenConstructor) {
  72. token.children.unshift(makeCheckbox(token, TokenConstructor));
  73. token.children[1].content = token.children[1].content.slice(3);
  74. token.content = token.content.slice(3);
  75. if (useLabelWrapper) {
  76. if (useLabelAfter) {
  77. token.children.pop();
  78. // Use large random number as id property of the checkbox.
  79. var id = 'task-item-' + Math.ceil(Math.random() * (10000 * 1000) - 1000);
  80. token.children[0].content = token.children[0].content.slice(0, -1) + ' id="' + id + '">';
  81. token.children.push(afterLabel(token.content, id, TokenConstructor));
  82. } else {
  83. token.children.unshift(beginLabel(TokenConstructor));
  84. token.children.push(endLabel(TokenConstructor));
  85. }
  86. }
  87. }
  88. function makeCheckbox(token, TokenConstructor) {
  89. var checkbox = new TokenConstructor('html_inline', '', 0);
  90. var disabledAttr = disableCheckboxes ? ' disabled="" ' : '';
  91. var value = ' value="' + token.content + '" ';
  92. if (token.content.indexOf('[ ] ') === 0) {
  93. checkbox.content = '<checkbox class="h2w__todoCheckbox task-list-item-checkbox"' + disabledAttr + value + '/>';
  94. } else if (token.content.indexOf('[x] ') === 0 || token.content.indexOf('[X] ') === 0) {
  95. checkbox.content = '<checkbox class="h2w__todoCheckbox task-list-item-checkbox" checked="true"' + disabledAttr + value + '/>';
  96. }
  97. return checkbox;
  98. }
  99. // these next two functions are kind of hacky; probably should really be a
  100. // true block-level token with .tag=='label'
  101. function beginLabel(TokenConstructor) {
  102. var token = new TokenConstructor('html_inline', '', 0);
  103. token.content = '<label>';
  104. return token;
  105. }
  106. function endLabel(TokenConstructor) {
  107. var token = new TokenConstructor('html_inline', '', 0);
  108. token.content = '</label>';
  109. return token;
  110. }
  111. function afterLabel(content, id, TokenConstructor) {
  112. var token = new TokenConstructor('html_inline', '', 0);
  113. token.content = '<label class="task-list-item-label" for="' + id + '">' + content + '</label>';
  114. token.attrs = [{for: id}];
  115. return token;
  116. }
  117. function isInline(token) { return token.type === 'inline'; }
  118. function isParagraph(token) { return token.type === 'paragraph_open'; }
  119. function isListItem(token) { return token.type === 'list_item_open'; }
  120. function startsWithTodoMarkdown(token) {
  121. // leading whitespace in a list item is already trimmed off by markdown-it
  122. return token.content.indexOf('[ ] ') === 0 || token.content.indexOf('[x] ') === 0 || token.content.indexOf('[X] ') === 0;
  123. }
  124. },{}]},{},[1])(1)
  125. });