todo.js 6.6 KB

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