@mdit/plugin-tasklist 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -16,3 +16,13 @@ npm i -D @mdit/plugin-tasklist
16
16
  # yarn
17
17
  yarn add -D @mdit/plugin-tasklist
18
18
  ```
19
+
20
+ ## Comparison / 对比
21
+
22
+ Compared against the upstream [`markdown-it-task-checkbox`](https://www.npmjs.com/package/markdown-it-task-checkbox) package `1.0.6`.
23
+
24
+ - Correctness: **7/21** cases match the reference exactly (1 reference errors, 1 differences).
25
+ - Throughput: **1.18×** the reference (geometric mean across 6 benchmark cases; above 1× means faster).
26
+ - Entry gzip: **731 B** versus **1.4 kB** for the reference (import cost).
27
+
28
+ See the full comparison report: [English](https://mdit-plugins.github.io/compare.html) · [中文](https://mdit-plugins.github.io/zh/compare.html).
package/dist/cdn.umd.js CHANGED
@@ -1,4 +1,2 @@
1
- (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports):typeof define==`function`&&define.amd?define([`exports`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.mdItPluginTasklist={}))})(this,function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`}),String.raw`\$&`;let t=(e,t,n)=>(n?[e[n].ruler]:[e.core.ruler,e.block.ruler,e.inline.ruler]).some(e=>e.__rules__.some(({name:e})=>e===t));String.raw`\^$.*+?()[]{}|`;let n=(e,t,n)=>{let r=e.attrIndex(t),i=[t,n];r<0?e.attrPush(i):e.attrs[r]=i},r=(e,t)=>{let n=e[t].level-1;for(let r=t-1;r>=0;r--)if(e[r].level===n)return r;
2
- /* istanbul ignore next -- @preserve */
3
- return-1},i=e=>e.type===`inline`,a=e=>e.type===`paragraph_open`,o=e=>e.type===`list_item_open`,s=e=>{let t=e.content;if(t.length<4||t.charCodeAt(0)!==91||t.charCodeAt(2)!==93)return!1;let n=t.charCodeAt(3);if(n!==32&&n!==160)return!1;let r=t.charCodeAt(1);return r===120||r===88?`checked`:r===32||r===160},c=e=>{e[0].type===`text`?e[0].content=e[0].content.slice(3):e.splice(0,3)};e.tasklist=(e,l={})=>{if(t(e,`task_list`,`core`))return;let{disabled:u=!0,label:d=!0,containerClass:f=`task-list-container`,itemClass:p=`task-list-item`,checkboxClass:m=`task-list-item-checkbox`,labelClass:h=`task-list-item-label`}=l;e.core.ruler.after(`inline`,`task_list`,e=>{let t=e.tokens;for(let l=2;l<t.length;l++){let g=t[l];if(!i(g)||!a(t[l-1])||!o(t[l-2]))continue;let _=s(g);if(_===!1)continue;g.children??=[],c(g.children);let v=new e.Token(`checkbox_input`,`input`,0);if(v.attrs=[[`type`,`checkbox`],[`class`,m]],_===`checked`&&v.attrs.push([`checked`,`checked`]),u&&v.attrs.push([`disabled`,`disabled`]),g.children.unshift(v),d){let t=new e.Token(`label_open`,`label`,1);t.attrs=[[`class`,h]],g.children.unshift(t),g.children.push(new e.Token(`label_close`,`label`,-1))}n(t[l-2],`class`,p),n(t[r(t,l-2)],`class`,f)}})}});
1
+ (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports):typeof define==`function`&&define.amd?define([`exports`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.mdItPluginTasklist={}))})(this,function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`}),String.raw`\$&`;let t=(e,t,n)=>(n?[e[n].ruler]:[e.core.ruler,e.block.ruler,e.inline.ruler]).some(e=>e.__rules__.some(({name:e})=>e===t));String.raw`\^$.*+?()[]{}|`;let n=(e,t,n)=>{let r=e.attrIndex(t),i=[t,n];r<0?e.attrPush(i):e.attrs[r]=i},r=e=>e.type===`inline`,i=e=>e.type===`paragraph_open`,a=e=>e.type===`list_item_open`,o=e=>{let t=e.content;if(t.length<4||t.charCodeAt(0)!==91||t.charCodeAt(2)!==93)return!1;let n=t.charCodeAt(3);if(n!==32&&n!==160)return!1;let r=t.charCodeAt(1);return r===120||r===88?`checked`:r===32||r===160},s=e=>{e[0].type===`text`?e[0].content=e[0].content.slice(3):e.splice(0,3)};e.tasklist=(e,c={})=>{if(t(e,`task_list`,`core`))return;let{disabled:l=!0,label:u=!0,containerClass:d=`task-list-container`,itemClass:f=`task-list-item`,checkboxClass:p=`task-list-item-checkbox`,labelClass:m=`task-list-item-label`}=c;e.core.ruler.after(`inline`,`task_list`,e=>{let t=e.tokens,c=[];for(let h=0;h<t.length;h++){let g=t[h];if(c[g.level]=h,h<2||!r(g)||!i(t[h-1])||!a(t[h-2]))continue;let _=o(g);if(_===!1)continue;g.children??=[],s(g.children);let v=new e.Token(`checkbox_input`,`input`,0);if(v.attrs=[[`type`,`checkbox`],[`class`,p]],_===`checked`&&v.attrs.push([`checked`,`checked`]),l&&v.attrs.push([`disabled`,`disabled`]),g.children.unshift(v),u){let t=new e.Token(`label_open`,`label`,1);t.attrs=[[`class`,m]],g.children.unshift(t),g.children.push(new e.Token(`label_close`,`label`,-1))}n(t[h-2],`class`,f),n(t[c[t[h-2].level-1]],`class`,d)}})}});
4
2
  //# sourceMappingURL=cdn.umd.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"cdn.umd.js","names":["hasRule","factory","global","exports","c","setTokenAttr","index","token","name","attr","value","getParentTokenIndex","targetLevel","tokens","i","isInlineToken","isParagraphToken","isListItemToken","startsWithTodoMarkdown","content","spacer","contentChar","removeTodoMarker","children","md","options","state","todoState","checkboxToken","checkboxClass","disabled","label","labelToken","labelClass","itemClass","containerClass"],"sources":["../../helper/dist/index.js","../src/utils.ts","../src/plugin.ts"],"sourcesContent":["const e=e=>{let t=e.split(`\n`),n=t.reduce((e,t)=>{for(let n=0;n<t.length;n++)if(t[n]!==` `&&t[n]!==`\t`)return Math.min(n,e);return e},1/0);return n<1/0?t.map(e=>e.slice(n)).join(`\n`):e},t=/[-/\\\\^$*+?.()|[\\]{}]/g,n=String.raw`\\$&`,r=e=>e.replaceAll(`&`,`&amp;`).replaceAll(`<`,`&lt;`).replaceAll(`>`,`&gt;`).replaceAll(`\"`,`&quot;`).replaceAll(`'`,`&#39;`),i=e=>e.replaceAll(t,n),a=(e,t,n,r)=>{let i=e.bMarks[t]+e.tShift[t],a=e.eMarks[t];if(e.sCount[t]-r>=4||i+3>a)return null;let o=e.src.charCodeAt(i);if(o!==126&&o!==96)return null;let s=i;i=e.skipChars(i,o);let c=i-s;if(c<3)return null;let l=e.src.slice(i,a);if(o===96&&l.includes(\"`\"))return null;let u=t,d=!1;for(;u++,!(u>=n||(i=s=e.bMarks[u]+e.tShift[u],a=e.eMarks[u],i<a&&e.sCount[u]<r));)if(e.src.charCodeAt(i)===o&&!(e.sCount[u]-r>=4)&&(i=e.skipChars(i,o),!(i-s<c)&&(i=e.skipSpaces(i),!(i<a)))){d=!0;break}return u+ +!!d},o=/\\r\\n?|\\n/g,s=/\\\\(?<char>[ \\\\!\"#$%&'()*+,./:;<=>?@[\\]^_`{|}~-])/gu,c=(e,t,n)=>(n?[e[n].ruler]:[e.core.ruler,e.block.ruler,e.inline.ruler]).some(e=>e.__rules__.some(({name:e})=>e===t)),l=String.raw`\\^$.*+?()[]{}|`,u=[`.env`,`.env.*`,`.npmrc`,`.netrc`,`.git-credentials`,`id_rsa*`,`id_ed25519*`,`*.pem`,`*.key`,`*.p12`,`.git/**`,`**/.git/**`,`.ssh/**`,`**/.ssh/**`,`.aws/**`,`**/.aws/**`,`.azure/**`,`**/.azure/**`,`.kube/**`,`**/.kube/**`,`.docker/config.json`,`**/.docker/config.json`,`*.tfstate*`],d=e=>{let t=`^`,{length:n}=e;for(let r=0;r<n;r++){let n=e[r];n===`*`?e.charCodeAt(r+1)===42?(r++,e.charCodeAt(r+1)===47?(r++,t+=`(?:.*/)?`):t+=`.*`):t+=`[^/]*`:t+=n===`?`?`[^/]`:l.includes(n)?`\\\\${n}`:n}return RegExp(`${t}$`)},f=/* @__PURE__ */ new Map,p=e=>{let t=f.get(e);if(t)return t;let n=d(e);return f.set(e,n),n},m=(e,t)=>((e.global||e.sticky)&&(e.lastIndex=0),e.test(t)),h=(e,t=u)=>{let n=e.lastIndexOf(`/`),r=n===-1?e:e.slice(n+1);for(let n of t)if(typeof n==`string`){let t=n.includes(`/`)?e:r;if(p(n).test(t))return!0}else if(m(n,e)||m(n,r))return!0;return!1};export{o as NEWLINE_RE,s as UNESCAPE_RE,e as dedent,u as defaultSensitiveFiles,r as escapeHtml,i as escapeRegExp,c as hasRule,h as isSensitiveFile,a as scanFence};\n//# sourceMappingURL=index.js.map","import type { Token } from \"markdown-it\";\n\nexport const setTokenAttr = (token: Token, name: string, value: string): void => {\n const index = token.attrIndex(name);\n const attr: [string, string] = [name, value];\n\n if (index < 0) token.attrPush(attr);\n // oxlint-disable-next-line typescript/no-non-null-assertion\n else token.attrs![index] = attr;\n};\n\nexport const getParentTokenIndex = (tokens: Token[], index: number): number => {\n const targetLevel = tokens[index].level - 1;\n\n for (let i = index - 1; i >= 0; i--) if (tokens[i].level === targetLevel) return i;\n\n /* istanbul ignore next -- @preserve */\n return -1;\n};\n\nexport const isInlineToken = (token: Token): boolean => token.type === \"inline\";\n\nexport const isParagraphToken = (token: Token): boolean => token.type === \"paragraph_open\";\n\nexport const isListItemToken = (token: Token): boolean => token.type === \"list_item_open\";\n","/** Forked from https://github.com/linsir/markdown-it-task-checkbox/blob/master/index.js */\n\nimport { hasRule } from \"@mdit/helper\";\nimport type { CoreRule, PluginWithOptions } from \"@mdit/helper\";\nimport type { Token } from \"markdown-it\";\n\nimport type { MarkdownItTaskListOptions } from \"./options.js\";\nimport {\n getParentTokenIndex,\n isInlineToken,\n isListItemToken,\n isParagraphToken,\n setTokenAttr,\n} from \"./utils.js\";\n\n// The leading whitespace in a list item (token.content) is already trimmed off by markdown-it.\n// The regex below checks for '[ ] ' or '[x] ' or '[X] ' at the start of the string token.content,\n// where the space is either a normal space or a non-breaking space (character 160 = \\u00A0).\nconst startsWithTodoMarkdown = (token: Token): \"checked\" | boolean => {\n const content = token.content;\n\n // minimal length is 4: \"[ ] \"\n if (content.length < 4) return false;\n\n if (content.charCodeAt(0) !== 91 /* [ */) return false;\n\n if (content.charCodeAt(2) !== 93 /* ] */) return false;\n\n const spacer = content.charCodeAt(3);\n\n if (spacer !== 32 /* space */ && spacer !== 160 /* \\u00A0 */) return false;\n\n const contentChar = content.charCodeAt(1);\n\n if (contentChar === 120 /* x */ || contentChar === 88 /* X */) return \"checked\";\n\n return contentChar === 32 /* space */ || contentChar === 160; /* \\u00A0 */\n};\n\n// The checkbox marker takes the first three characters of the inline content: `[ ]`, `[x]` or `[X]`.\n// It is normally part of the first text token. However, when `[x]` matches a link reference\n// definition, markdown-it parses the marker as a link (`link_open`, text `x`, `link_close`), so\n// that whole link has to be removed instead.\nconst removeTodoMarker = (children: Token[]): void => {\n if (children[0].type === \"text\") {\n children[0].content = children[0].content.slice(3);\n return;\n }\n\n children.splice(0, 3);\n};\n\n/**\n * Task list plugin, rendering `[ ]` and `[x]` list items as checkboxes\n *\n * 任务列表插件,将 `[ ]` 与 `[x]` 列表项渲染为 checkbox\n *\n * @param md - MarkdownIt instance / MarkdownIt 实例\n * @param options - Task list options / 任务列表选项\n */\nexport const tasklist: PluginWithOptions<MarkdownItTaskListOptions> = (md, options = {}) => {\n // Registering the core rule twice would wrap the same inline token again, producing nested\n // labels and duplicated checkboxes.\n if (hasRule(md, \"task_list\", \"core\")) return;\n\n const {\n disabled = true,\n label = true,\n containerClass = \"task-list-container\",\n itemClass = \"task-list-item\",\n checkboxClass = \"task-list-item-checkbox\",\n labelClass = \"task-list-item-label\",\n } = options;\n\n const taskListRule: CoreRule = (state) => {\n const tokens = state.tokens;\n\n for (let index = 2; index < tokens.length; index++) {\n const token = tokens[index];\n\n // check if the token is an inline token inside a list item and starts with todo markdown\n if (\n !isInlineToken(token) ||\n !isParagraphToken(tokens[index - 1]) ||\n !isListItemToken(tokens[index - 2])\n )\n continue;\n\n const todoState = startsWithTodoMarkdown(token);\n\n if (todoState === false) continue;\n\n token.children ??= [];\n\n // remove the checkbox syntax letter\n removeTodoMarker(token.children);\n\n const checkboxToken = new state.Token(\"checkbox_input\", \"input\", 0);\n\n // No `id`/`for` attributes are emitted: ids are per document and would collide when multiple\n // documents are rendered on the same page. Nesting the input inside the label keeps the\n // native label/checkbox association without any document-scoped identifier.\n checkboxToken.attrs = [\n [\"type\", \"checkbox\"],\n [\"class\", checkboxClass],\n ];\n\n // if token.content starts with '[x] ' or '[X] '\n if (todoState === \"checked\") checkboxToken.attrs.push([\"checked\", \"checked\"]);\n\n if (disabled) checkboxToken.attrs.push([\"disabled\", \"disabled\"]);\n\n // checkbox, placed before the label is unshifted so that it ends up inside the label\n token.children.unshift(checkboxToken);\n\n if (label) {\n const labelToken = new state.Token(\"label_open\", \"label\", 1);\n\n labelToken.attrs = [[\"class\", labelClass]];\n\n token.children.unshift(labelToken);\n token.children.push(new state.Token(\"label_close\", \"label\", -1));\n }\n\n setTokenAttr(tokens[index - 2], \"class\", itemClass);\n setTokenAttr(tokens[getParentTokenIndex(tokens, index - 2)], \"class\", containerClass);\n }\n };\n\n md.core.ruler.after(\"inline\", \"task_list\", taskListRule);\n};\n"],"mappings":"C,yRAEkC,OAAO,GAAG,MAAstB,IAAAI,GAAG,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,KAAK,MAAM,EAAE,MAAM,MAAM,EAAE,OAAO,KAAK,EAAA,CAAG,KAAK,GAAG,EAAE,UAAU,MAAM,CAAC,KAAK,KAAK,IAAI,CAAC,CAAC,EAAI,OAAO,GAAG,iBCAn4B,IAAaC,GAAgB,EAAc,EAAc,IAAwB,CAC/E,IAAMC,EAAQC,EAAM,UAAUC,CAAI,EAC5BC,EAAyB,CAACD,EAAME,CAAK,EAEvCJ,EAAQ,EAAGC,EAAM,SAASE,CAAI,EAE7B,EAAM,MAAOH,GAASG,CAC7B,EAEaE,GAAuB,EAAiB,IAA0B,CAC7E,IAAMC,EAAcC,EAAOP,EAAM,CAAC,MAAQ,EAE1C,IAAK,IAAIQ,EAA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BX,CAAK,EAE9C,GAAIoB,IAAc,GAAO,SAEzB,EAAM,WAAa,CAAC,EAGpBL,EAAiBf,EAAM,QAAQ,EAE/B,IAAMqB,EAAgB,IAAIF,EAAM,MAAM,iBAAkB,QAAS,CAAC,EAkBlE,GAbA,EAAc,MAAQ,CACpB,CAAC,OAAQ,UAAU,EACnB,CAAC,QAASG,CAAa,CACzB,EAGIF,IAAc,WAAWC,EAAc,MAAM,KAAK,CAAC,UAAW,SAAS,CAAC,EAExEE,GAAUF,EAAc,MAAM,KAAK,CAAC,WAAY,UAAU,CAAC,EAG/DrB,EAAM,SAAS,QAAQqB,CAAa,EAEhCG,EAAO,CACT,IAAMC,EAAa,IAAIN,EAAM,MAAM,aAAc,QAAS,CAAC,EAE3D,EAAW,MAAQ,CAAC,CAAC,QAASO,CAAU,CAAC,EAEzC1B,EAAM,SAAS,QAAQyB,CAAU,EACjCzB,EAAM,SAAS,KAAK,IAAImB,EAAM,MAAM,cAAe,QAAS,EAAE,CAAC,CACjE,CAEArB,EAAaQ,EAAOP,EAAQ,GAAI,QAAS4B,CAAS,EAClD7B,EAAaQ,EAAOF,EAAoBE,EAAQP,EAAQ,CAAC,GAAI,QAAS6B,CAAc,CACtF,CACF,CAEuD,CACzD,C"}
1
+ {"version":3,"file":"cdn.umd.js","names":["hasRule","factory","global","exports","c","setTokenAttr","index","token","name","attr","value","isInlineToken","isParagraphToken","isListItemToken","startsWithTodoMarkdown","content","spacer","contentChar","removeTodoMarker","children","md","options","tokens","state","lastIndexByLevel","todoState","checkboxToken","checkboxClass","disabled","label","labelToken","labelClass","itemClass","containerClass"],"sources":["../../helper/dist/index.js","../src/utils.ts","../src/plugin.ts"],"sourcesContent":["const e=e=>{let t=e.split(`\n`),n=1/0;for(let r=0;r<t.length;r++){let i=t[r],{length:a}=i;for(let t=0;t<a;t++){let r=i.charCodeAt(t);if(r!==32&&r!==9){if(t===0)return e;t<n&&(n=t);break}}}return n===1/0?e:t.map(e=>e.slice(n)).join(`\n`)},t=/[-/\\\\^$*+?.()|[\\]{}]/g,n=String.raw`\\$&`,r=e=>e.replaceAll(`&`,`&amp;`).replaceAll(`<`,`&lt;`).replaceAll(`>`,`&gt;`).replaceAll(`\"`,`&quot;`).replaceAll(`'`,`&#39;`),i=e=>e.replaceAll(t,n),a=(e,t,n,r)=>{let i=e.bMarks[t]+e.tShift[t],a=e.eMarks[t];if(e.sCount[t]-r>=4||i+3>a)return null;let o=e.src.charCodeAt(i);if(o!==126&&o!==96)return null;let s=i;i=e.skipChars(i,o);let c=i-s;if(c<3)return null;let l=e.src.slice(i,a);if(o===96&&l.includes(\"`\"))return null;let u=t,d=!1;for(;u++,!(u>=n||(i=s=e.bMarks[u]+e.tShift[u],a=e.eMarks[u],i<a&&e.sCount[u]<r));)if(e.src.charCodeAt(i)===o&&!(e.sCount[u]-r>=4)&&(i=e.skipChars(i,o),!(i-s<c)&&(i=e.skipSpaces(i),!(i<a)))){d=!0;break}return u+ +!!d},o=/\\r\\n?|\\n/g,s=/\\\\(?<char>[ \\\\!\"#$%&'()*+,./:;<=>?@[\\]^_`{|}~-])/gu,c=(e,t,n)=>(n?[e[n].ruler]:[e.core.ruler,e.block.ruler,e.inline.ruler]).some(e=>e.__rules__.some(({name:e})=>e===t)),l=String.raw`\\^$.*+?()[]{}|`,u=[`.env`,`.env.*`,`.npmrc`,`.netrc`,`.git-credentials`,`id_rsa*`,`id_ed25519*`,`*.pem`,`*.key`,`*.p12`,`.git/**`,`**/.git/**`,`.ssh/**`,`**/.ssh/**`,`.aws/**`,`**/.aws/**`,`.azure/**`,`**/.azure/**`,`.kube/**`,`**/.kube/**`,`.docker/config.json`,`**/.docker/config.json`,`*.tfstate*`],d=e=>{let t=`^`,{length:n}=e;for(let r=0;r<n;r++){let n=e[r];n===`*`?e.charCodeAt(r+1)===42?(r++,e.charCodeAt(r+1)===47?(r++,t+=`(?:.*/)?`):t+=`.*`):t+=`[^/]*`:t+=n===`?`?`[^/]`:l.includes(n)?`\\\\${n}`:n}return RegExp(`${t}$`)},f=/* @__PURE__ */ new Map,p=e=>{let t=f.get(e);if(t)return t;let n=d(e);return f.set(e,n),n},m=(e,t)=>((e.global||e.sticky)&&(e.lastIndex=0),e.test(t)),h=(e,t=u)=>{let n=e.lastIndexOf(`/`),r=n===-1?e:e.slice(n+1);for(let n of t)if(typeof n==`string`){let t=n.includes(`/`)?e:r;if(p(n).test(t))return!0}else if(m(n,e)||m(n,r))return!0;return!1};export{o as NEWLINE_RE,s as UNESCAPE_RE,e as dedent,u as defaultSensitiveFiles,r as escapeHtml,i as escapeRegExp,c as hasRule,h as isSensitiveFile,a as scanFence};\n//# sourceMappingURL=index.js.map","import type { Token } from \"markdown-it\";\n\nexport const setTokenAttr = (token: Token, name: string, value: string): void => {\n const index = token.attrIndex(name);\n const attr: [string, string] = [name, value];\n\n if (index < 0) token.attrPush(attr);\n // oxlint-disable-next-line typescript/no-non-null-assertion\n else token.attrs![index] = attr;\n};\n\nexport const isInlineToken = (token: Token): boolean => token.type === \"inline\";\n\nexport const isParagraphToken = (token: Token): boolean => token.type === \"paragraph_open\";\n\nexport const isListItemToken = (token: Token): boolean => token.type === \"list_item_open\";\n","/** Forked from https://github.com/linsir/markdown-it-task-checkbox/blob/master/index.js */\n\nimport { hasRule } from \"@mdit/helper\";\nimport type { CoreRule, PluginWithOptions } from \"@mdit/helper\";\nimport type { Token } from \"markdown-it\";\n\nimport type { MarkdownItTaskListOptions } from \"./options.js\";\nimport { isInlineToken, isListItemToken, isParagraphToken, setTokenAttr } from \"./utils.js\";\n\n// The leading whitespace in a list item (token.content) is already trimmed off by markdown-it.\n// The regex below checks for '[ ] ' or '[x] ' or '[X] ' at the start of the string token.content,\n// where the space is either a normal space or a non-breaking space (character 160 = \\u00A0).\nconst startsWithTodoMarkdown = (token: Token): \"checked\" | boolean => {\n const content = token.content;\n\n // minimal length is 4: \"[ ] \"\n if (content.length < 4) return false;\n\n if (content.charCodeAt(0) !== 91 /* [ */) return false;\n\n if (content.charCodeAt(2) !== 93 /* ] */) return false;\n\n const spacer = content.charCodeAt(3);\n\n if (spacer !== 32 /* space */ && spacer !== 160 /* \\u00A0 */) return false;\n\n const contentChar = content.charCodeAt(1);\n\n if (contentChar === 120 /* x */ || contentChar === 88 /* X */) return \"checked\";\n\n return contentChar === 32 /* space */ || contentChar === 160; /* \\u00A0 */\n};\n\n// The checkbox marker takes the first three characters of the inline content: `[ ]`, `[x]` or `[X]`.\n// It is normally part of the first text token. However, when `[x]` matches a link reference\n// definition, markdown-it parses the marker as a link (`link_open`, text `x`, `link_close`), so\n// that whole link has to be removed instead.\nconst removeTodoMarker = (children: Token[]): void => {\n if (children[0].type === \"text\") {\n children[0].content = children[0].content.slice(3);\n return;\n }\n\n children.splice(0, 3);\n};\n\n/**\n * Task list plugin, rendering `[ ]` and `[x]` list items as checkboxes\n *\n * 任务列表插件,将 `[ ]` 与 `[x]` 列表项渲染为 checkbox\n *\n * @param md - MarkdownIt instance / MarkdownIt 实例\n * @param options - Task list options / 任务列表选项\n */\nexport const tasklist: PluginWithOptions<MarkdownItTaskListOptions> = (md, options = {}) => {\n // Registering the core rule twice would wrap the same inline token again, producing nested\n // labels and duplicated checkboxes.\n if (hasRule(md, \"task_list\", \"core\")) return;\n\n const {\n disabled = true,\n label = true,\n containerClass = \"task-list-container\",\n itemClass = \"task-list-item\",\n checkboxClass = \"task-list-item-checkbox\",\n labelClass = \"task-list-item-label\",\n } = options;\n\n const taskListRule: CoreRule = (state) => {\n const tokens = state.tokens;\n // Index of the nearest preceding token for every nesting level. The parent list token of a list\n // item is the nearest preceding token one level up, so tracking it while walking forward once\n // keeps the parent lookup O(1) instead of rescanning the token list for every task item.\n const lastIndexByLevel: number[] = [];\n\n for (let index = 0; index < tokens.length; index++) {\n const token = tokens[index];\n\n lastIndexByLevel[token.level] = index;\n\n // check if the token is an inline token inside a list item and starts with todo markdown\n if (\n index < 2 ||\n !isInlineToken(token) ||\n !isParagraphToken(tokens[index - 1]) ||\n !isListItemToken(tokens[index - 2])\n )\n continue;\n\n const todoState = startsWithTodoMarkdown(token);\n\n if (todoState === false) continue;\n\n token.children ??= [];\n\n // remove the checkbox syntax letter\n removeTodoMarker(token.children);\n\n const checkboxToken = new state.Token(\"checkbox_input\", \"input\", 0);\n\n // No `id`/`for` attributes are emitted: ids are per document and would collide when multiple\n // documents are rendered on the same page. Nesting the input inside the label keeps the\n // native label/checkbox association without any document-scoped identifier.\n checkboxToken.attrs = [\n [\"type\", \"checkbox\"],\n [\"class\", checkboxClass],\n ];\n\n // if token.content starts with '[x] ' or '[X] '\n if (todoState === \"checked\") checkboxToken.attrs.push([\"checked\", \"checked\"]);\n\n if (disabled) checkboxToken.attrs.push([\"disabled\", \"disabled\"]);\n\n // checkbox, placed before the label is unshifted so that it ends up inside the label\n token.children.unshift(checkboxToken);\n\n if (label) {\n const labelToken = new state.Token(\"label_open\", \"label\", 1);\n\n labelToken.attrs = [[\"class\", labelClass]];\n\n token.children.unshift(labelToken);\n token.children.push(new state.Token(\"label_close\", \"label\", -1));\n }\n\n setTokenAttr(tokens[index - 2], \"class\", itemClass);\n setTokenAttr(tokens[lastIndexByLevel[tokens[index - 2].level - 1]], \"class\", containerClass);\n }\n };\n\n md.core.ruler.after(\"inline\", \"task_list\", taskListRule);\n};\n"],"mappings":"C,yRAEgC,OAAO,GAAG,MAAstB,IAAAI,GAAG,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,KAAK,MAAM,EAAE,MAAM,MAAM,EAAE,OAAO,KAAK,EAAA,CAAG,KAAK,GAAG,EAAE,UAAU,MAAM,CAAC,KAAK,KAAK,IAAI,CAAC,CAAC,EAAI,OAAO,GAAG,iBCAj4B,IAAaC,GAAgB,EAAc,EAAc,IAAwB,CAC/E,IAAMC,EAAQC,EAAM,UAAUC,CAAI,EAC5BC,EAAyB,CAACD,EAAME,CAAK,EAEvCJ,EAAQ,EAAGC,EAAM,SAASE,CAAI,EAE7B,EAAM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9C,GAAIkB,IAAc,GAAO,SAEzB,EAAM,WAAa,CAAC,EAGpBP,EAAiBX,EAAM,QAAQ,EAE/B,IAAMmB,EAAgB,IAAIH,EAAM,MAAM,iBAAkB,QAAS,CAAC,EAkBlE,GAbA,EAAc,MAAQ,CACpB,CAAC,OAAQ,UAAU,EACnB,CAAC,QAASI,CAAa,CACzB,EAGIF,IAAc,WAAWC,EAAc,MAAM,KAAK,CAAC,UAAW,SAAS,CAAC,EAExEE,GAAUF,EAAc,MAAM,KAAK,CAAC,WAAY,UAAU,CAAC,EAG/DnB,EAAM,SAAS,QAAQmB,CAAa,EAEhCG,EAAO,CACT,IAAMC,EAAa,IAAIP,EAAM,MAAM,aAAc,QAAS,CAAC,EAE3D,EAAW,MAAQ,CAAC,CAAC,QAASQ,CAAU,CAAC,EAEzCxB,EAAM,SAAS,QAAQuB,CAAU,EACjCvB,EAAM,SAAS,KAAK,IAAIgB,EAAM,MAAM,cAAe,QAAS,EAAE,CAAC,CACjE,CAEAlB,EAAaiB,EAAOhB,EAAQ,GAAI,QAAS0B,CAAS,EAClD3B,EAAaiB,EAAOE,EAAiBF,EAAOhB,EAAQ,EAAE,CAAC,MAAQ,IAAK,QAAS2B,CAAc,CAC7F,CACF,CAEuD,CACzD,C"}
package/dist/index.d.ts CHANGED
@@ -63,5 +63,4 @@ declare const tasklistIdKey: unique symbol;
63
63
  interface TaskListEnv extends Env {
64
64
  [tasklistIdKey]?: number;
65
65
  }
66
- export type { MarkdownItTaskListOptions, TaskListEnv, tasklistIdKey };
67
- //# sourceMappingURL=index.d.ts.map
66
+ export type { MarkdownItTaskListOptions, TaskListEnv, tasklistIdKey };
package/dist/index.js CHANGED
@@ -1,4 +1,2 @@
1
- import{hasRule as e}from"@mdit/helper";const t=(e,t,n)=>{let r=e.attrIndex(t),i=[t,n];r<0?e.attrPush(i):e.attrs[r]=i},n=(e,t)=>{let n=e[t].level-1;for(let r=t-1;r>=0;r--)if(e[r].level===n)return r;
2
- /* istanbul ignore next -- @preserve */
3
- return-1},r=e=>e.type===`inline`,i=e=>e.type===`paragraph_open`,a=e=>e.type===`list_item_open`,o=e=>{let t=e.content;if(t.length<4||t.charCodeAt(0)!==91||t.charCodeAt(2)!==93)return!1;let n=t.charCodeAt(3);if(n!==32&&n!==160)return!1;let r=t.charCodeAt(1);return r===120||r===88?`checked`:r===32||r===160},s=e=>{e[0].type===`text`?e[0].content=e[0].content.slice(3):e.splice(0,3)},c=(c,l={})=>{if(e(c,`task_list`,`core`))return;let{disabled:u=!0,label:d=!0,containerClass:f=`task-list-container`,itemClass:p=`task-list-item`,checkboxClass:m=`task-list-item-checkbox`,labelClass:h=`task-list-item-label`}=l;c.core.ruler.after(`inline`,`task_list`,e=>{let c=e.tokens;for(let l=2;l<c.length;l++){let g=c[l];if(!r(g)||!i(c[l-1])||!a(c[l-2]))continue;let _=o(g);if(_===!1)continue;g.children??=[],s(g.children);let v=new e.Token(`checkbox_input`,`input`,0);if(v.attrs=[[`type`,`checkbox`],[`class`,m]],_===`checked`&&v.attrs.push([`checked`,`checked`]),u&&v.attrs.push([`disabled`,`disabled`]),g.children.unshift(v),d){let t=new e.Token(`label_open`,`label`,1);t.attrs=[[`class`,h]],g.children.unshift(t),g.children.push(new e.Token(`label_close`,`label`,-1))}t(c[l-2],`class`,p),t(c[n(c,l-2)],`class`,f)}})};export{c as tasklist};
1
+ import{hasRule as e}from"@mdit/helper";const t=(e,t,n)=>{let r=e.attrIndex(t),i=[t,n];r<0?e.attrPush(i):e.attrs[r]=i},n=e=>e.type===`inline`,r=e=>e.type===`paragraph_open`,i=e=>e.type===`list_item_open`,a=e=>{let t=e.content;if(t.length<4||t.charCodeAt(0)!==91||t.charCodeAt(2)!==93)return!1;let n=t.charCodeAt(3);if(n!==32&&n!==160)return!1;let r=t.charCodeAt(1);return r===120||r===88?`checked`:r===32||r===160},o=e=>{e[0].type===`text`?e[0].content=e[0].content.slice(3):e.splice(0,3)},s=(s,c={})=>{if(e(s,`task_list`,`core`))return;let{disabled:l=!0,label:u=!0,containerClass:d=`task-list-container`,itemClass:f=`task-list-item`,checkboxClass:p=`task-list-item-checkbox`,labelClass:m=`task-list-item-label`}=c;s.core.ruler.after(`inline`,`task_list`,e=>{let s=e.tokens,c=[];for(let h=0;h<s.length;h++){let g=s[h];if(c[g.level]=h,h<2||!n(g)||!r(s[h-1])||!i(s[h-2]))continue;let _=a(g);if(_===!1)continue;g.children??=[],o(g.children);let v=new e.Token(`checkbox_input`,`input`,0);if(v.attrs=[[`type`,`checkbox`],[`class`,p]],_===`checked`&&v.attrs.push([`checked`,`checked`]),l&&v.attrs.push([`disabled`,`disabled`]),g.children.unshift(v),u){let t=new e.Token(`label_open`,`label`,1);t.attrs=[[`class`,m]],g.children.unshift(t),g.children.push(new e.Token(`label_close`,`label`,-1))}t(s[h-2],`class`,f),t(s[c[s[h-2].level-1]],`class`,d)}})};export{s as tasklist};
4
2
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["setTokenAttr","index","token","name","attr","value","getParentTokenIndex","targetLevel","tokens","i","isInlineToken","isParagraphToken","isListItemToken","startsWithTodoMarkdown","content","spacer","contentChar","removeTodoMarker","children","tasklist","hasRule","md","options","state","todoState","checkboxToken","checkboxClass","disabled","label","labelToken","labelClass","itemClass","containerClass"],"sources":["../src/utils.ts","../src/plugin.ts"],"sourcesContent":["import type { Token } from \"markdown-it\";\n\nexport const setTokenAttr = (token: Token, name: string, value: string): void => {\n const index = token.attrIndex(name);\n const attr: [string, string] = [name, value];\n\n if (index < 0) token.attrPush(attr);\n // oxlint-disable-next-line typescript/no-non-null-assertion\n else token.attrs![index] = attr;\n};\n\nexport const getParentTokenIndex = (tokens: Token[], index: number): number => {\n const targetLevel = tokens[index].level - 1;\n\n for (let i = index - 1; i >= 0; i--) if (tokens[i].level === targetLevel) return i;\n\n /* istanbul ignore next -- @preserve */\n return -1;\n};\n\nexport const isInlineToken = (token: Token): boolean => token.type === \"inline\";\n\nexport const isParagraphToken = (token: Token): boolean => token.type === \"paragraph_open\";\n\nexport const isListItemToken = (token: Token): boolean => token.type === \"list_item_open\";\n","/** Forked from https://github.com/linsir/markdown-it-task-checkbox/blob/master/index.js */\n\nimport { hasRule } from \"@mdit/helper\";\nimport type { CoreRule, PluginWithOptions } from \"@mdit/helper\";\nimport type { Token } from \"markdown-it\";\n\nimport type { MarkdownItTaskListOptions } from \"./options.js\";\nimport {\n getParentTokenIndex,\n isInlineToken,\n isListItemToken,\n isParagraphToken,\n setTokenAttr,\n} from \"./utils.js\";\n\n// The leading whitespace in a list item (token.content) is already trimmed off by markdown-it.\n// The regex below checks for '[ ] ' or '[x] ' or '[X] ' at the start of the string token.content,\n// where the space is either a normal space or a non-breaking space (character 160 = \\u00A0).\nconst startsWithTodoMarkdown = (token: Token): \"checked\" | boolean => {\n const content = token.content;\n\n // minimal length is 4: \"[ ] \"\n if (content.length < 4) return false;\n\n if (content.charCodeAt(0) !== 91 /* [ */) return false;\n\n if (content.charCodeAt(2) !== 93 /* ] */) return false;\n\n const spacer = content.charCodeAt(3);\n\n if (spacer !== 32 /* space */ && spacer !== 160 /* \\u00A0 */) return false;\n\n const contentChar = content.charCodeAt(1);\n\n if (contentChar === 120 /* x */ || contentChar === 88 /* X */) return \"checked\";\n\n return contentChar === 32 /* space */ || contentChar === 160; /* \\u00A0 */\n};\n\n// The checkbox marker takes the first three characters of the inline content: `[ ]`, `[x]` or `[X]`.\n// It is normally part of the first text token. However, when `[x]` matches a link reference\n// definition, markdown-it parses the marker as a link (`link_open`, text `x`, `link_close`), so\n// that whole link has to be removed instead.\nconst removeTodoMarker = (children: Token[]): void => {\n if (children[0].type === \"text\") {\n children[0].content = children[0].content.slice(3);\n return;\n }\n\n children.splice(0, 3);\n};\n\n/**\n * Task list plugin, rendering `[ ]` and `[x]` list items as checkboxes\n *\n * 任务列表插件,将 `[ ]` 与 `[x]` 列表项渲染为 checkbox\n *\n * @param md - MarkdownIt instance / MarkdownIt 实例\n * @param options - Task list options / 任务列表选项\n */\nexport const tasklist: PluginWithOptions<MarkdownItTaskListOptions> = (md, options = {}) => {\n // Registering the core rule twice would wrap the same inline token again, producing nested\n // labels and duplicated checkboxes.\n if (hasRule(md, \"task_list\", \"core\")) return;\n\n const {\n disabled = true,\n label = true,\n containerClass = \"task-list-container\",\n itemClass = \"task-list-item\",\n checkboxClass = \"task-list-item-checkbox\",\n labelClass = \"task-list-item-label\",\n } = options;\n\n const taskListRule: CoreRule = (state) => {\n const tokens = state.tokens;\n\n for (let index = 2; index < tokens.length; index++) {\n const token = tokens[index];\n\n // check if the token is an inline token inside a list item and starts with todo markdown\n if (\n !isInlineToken(token) ||\n !isParagraphToken(tokens[index - 1]) ||\n !isListItemToken(tokens[index - 2])\n )\n continue;\n\n const todoState = startsWithTodoMarkdown(token);\n\n if (todoState === false) continue;\n\n token.children ??= [];\n\n // remove the checkbox syntax letter\n removeTodoMarker(token.children);\n\n const checkboxToken = new state.Token(\"checkbox_input\", \"input\", 0);\n\n // No `id`/`for` attributes are emitted: ids are per document and would collide when multiple\n // documents are rendered on the same page. Nesting the input inside the label keeps the\n // native label/checkbox association without any document-scoped identifier.\n checkboxToken.attrs = [\n [\"type\", \"checkbox\"],\n [\"class\", checkboxClass],\n ];\n\n // if token.content starts with '[x] ' or '[X] '\n if (todoState === \"checked\") checkboxToken.attrs.push([\"checked\", \"checked\"]);\n\n if (disabled) checkboxToken.attrs.push([\"disabled\", \"disabled\"]);\n\n // checkbox, placed before the label is unshifted so that it ends up inside the label\n token.children.unshift(checkboxToken);\n\n if (label) {\n const labelToken = new state.Token(\"label_open\", \"label\", 1);\n\n labelToken.attrs = [[\"class\", labelClass]];\n\n token.children.unshift(labelToken);\n token.children.push(new state.Token(\"label_close\", \"label\", -1));\n }\n\n setTokenAttr(tokens[index - 2], \"class\", itemClass);\n setTokenAttr(tokens[getParentTokenIndex(tokens, index - 2)], \"class\", containerClass);\n }\n };\n\n md.core.ruler.after(\"in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0BAChB,aAAa,wBACXC,EAyDJD,EAAG,KAAK,MAAM,MAAM,SAAU,YAvDE,GAAU,CACxC,IAAMb,EAASe,EAAM,OAErB,IAAK,IAAItB,EAAQ,EAAGA,EAAQO,EAAO,OAAQ,IAAS,CAClD,IAAMN,EAAQM,EAAOP,GAGrB,GACE,CAACS,EAAcR,CAAK,GACpB,CAACS,EAAiBH,EAAOP,EAAQ,EAAE,GACnC,CAACW,EAAgBJ,EAAOP,EAAQ,EAAE,EAElC,SAEF,IAAMuB,EAAYX,EAAuBX,CAAK,EAE9C,GAAIsB,IAAc,GAAO,SAEzB,EAAM,WAAa,CAAC,EAGpBP,EAAiBf,EAAM,QAAQ,EAE/B,IAAMuB,EAAgB,IAAIF,EAAM,MAAM,iBAAkB,QAAS,CAAC,EAkBlE,GAbA,EAAc,MAAQ,CACpB,CAAC,OAAQ,UAAU,EACnB,CAAC,QAASG,CAAa,CACzB,EAGIF,IAAc,WAAWC,EAAc,MAAM,KAAK,CAAC,UAAW,SAAS,CAAC,EAExEE,GAAUF,EAAc,MAAM,KAAK,CAAC,WAAY,UAAU,CAAC,EAG/DvB,EAAM,SAAS,QAAQuB,CAAa,EAEhCG,EAAO,CACT,IAAMC,EAAa,IAAIN,EAAM,MAAM,aAAc,QAAS,CAAC,EAE3D,EAAW,MAAQ,CAAC,CAAC,QAASO,CAAU,CAAC,EAEzC5B,EAAM,SAAS,QAAQ2B,CAAU,EACjC3B,EAAM,SAAS,KAAK,IAAIqB,EAAM,MAAM,cAAe,QAAS,EAAE,CAAC,CACjE,CAEAvB,EAAaQ,EAAOP,EAAQ,GAAI,QAAS8B,CAAS,EAClD/B,EAAaQ,EAAOF,EAAoBE,EAAQP,EAAQ,CAAC,GAAI,QAAS+B,CAAc,CACtF,CACF,CAEuD,CACzD,E"}
1
+ {"version":3,"file":"index.js","names":["setTokenAttr","index","token","name","attr","value","isInlineToken","isParagraphToken","isListItemToken","startsWithTodoMarkdown","content","spacer","contentChar","removeTodoMarker","children","tasklist","hasRule","md","options","tokens","state","lastIndexByLevel","todoState","checkboxToken","checkboxClass","disabled","label","labelToken","labelClass","itemClass","containerClass"],"sources":["../src/utils.ts","../src/plugin.ts"],"sourcesContent":["import type { Token } from \"markdown-it\";\n\nexport const setTokenAttr = (token: Token, name: string, value: string): void => {\n const index = token.attrIndex(name);\n const attr: [string, string] = [name, value];\n\n if (index < 0) token.attrPush(attr);\n // oxlint-disable-next-line typescript/no-non-null-assertion\n else token.attrs![index] = attr;\n};\n\nexport const isInlineToken = (token: Token): boolean => token.type === \"inline\";\n\nexport const isParagraphToken = (token: Token): boolean => token.type === \"paragraph_open\";\n\nexport const isListItemToken = (token: Token): boolean => token.type === \"list_item_open\";\n","/** Forked from https://github.com/linsir/markdown-it-task-checkbox/blob/master/index.js */\n\nimport { hasRule } from \"@mdit/helper\";\nimport type { CoreRule, PluginWithOptions } from \"@mdit/helper\";\nimport type { Token } from \"markdown-it\";\n\nimport type { MarkdownItTaskListOptions } from \"./options.js\";\nimport { isInlineToken, isListItemToken, isParagraphToken, setTokenAttr } from \"./utils.js\";\n\n// The leading whitespace in a list item (token.content) is already trimmed off by markdown-it.\n// The regex below checks for '[ ] ' or '[x] ' or '[X] ' at the start of the string token.content,\n// where the space is either a normal space or a non-breaking space (character 160 = \\u00A0).\nconst startsWithTodoMarkdown = (token: Token): \"checked\" | boolean => {\n const content = token.content;\n\n // minimal length is 4: \"[ ] \"\n if (content.length < 4) return false;\n\n if (content.charCodeAt(0) !== 91 /* [ */) return false;\n\n if (content.charCodeAt(2) !== 93 /* ] */) return false;\n\n const spacer = content.charCodeAt(3);\n\n if (spacer !== 32 /* space */ && spacer !== 160 /* \\u00A0 */) return false;\n\n const contentChar = content.charCodeAt(1);\n\n if (contentChar === 120 /* x */ || contentChar === 88 /* X */) return \"checked\";\n\n return contentChar === 32 /* space */ || contentChar === 160; /* \\u00A0 */\n};\n\n// The checkbox marker takes the first three characters of the inline content: `[ ]`, `[x]` or `[X]`.\n// It is normally part of the first text token. However, when `[x]` matches a link reference\n// definition, markdown-it parses the marker as a link (`link_open`, text `x`, `link_close`), so\n// that whole link has to be removed instead.\nconst removeTodoMarker = (children: Token[]): void => {\n if (children[0].type === \"text\") {\n children[0].content = children[0].content.slice(3);\n return;\n }\n\n children.splice(0, 3);\n};\n\n/**\n * Task list plugin, rendering `[ ]` and `[x]` list items as checkboxes\n *\n * 任务列表插件,将 `[ ]` 与 `[x]` 列表项渲染为 checkbox\n *\n * @param md - MarkdownIt instance / MarkdownIt 实例\n * @param options - Task list options / 任务列表选项\n */\nexport const tasklist: PluginWithOptions<MarkdownItTaskListOptions> = (md, options = {}) => {\n // Registering the core rule twice would wrap the same inline token again, producing nested\n // labels and duplicated checkboxes.\n if (hasRule(md, \"task_list\", \"core\")) return;\n\n const {\n disabled = true,\n label = true,\n containerClass = \"task-list-container\",\n itemClass = \"task-list-item\",\n checkboxClass = \"task-list-item-checkbox\",\n labelClass = \"task-list-item-label\",\n } = options;\n\n const taskListRule: CoreRule = (state) => {\n const tokens = state.tokens;\n // Index of the nearest preceding token for every nesting level. The parent list token of a list\n // item is the nearest preceding token one level up, so tracking it while walking forward once\n // keeps the parent lookup O(1) instead of rescanning the token list for every task item.\n const lastIndexByLevel: number[] = [];\n\n for (let index = 0; index < tokens.length; index++) {\n const token = tokens[index];\n\n lastIndexByLevel[token.level] = index;\n\n // check if the token is an inline token inside a list item and starts with todo markdown\n if (\n index < 2 ||\n !isInlineToken(token) ||\n !isParagraphToken(tokens[index - 1]) ||\n !isListItemToken(tokens[index - 2])\n )\n continue;\n\n const todoState = startsWithTodoMarkdown(token);\n\n if (todoState === false) continue;\n\n token.children ??= [];\n\n // remove the checkbox syntax letter\n removeTodoMarker(token.children);\n\n const checkboxToken = new state.Token(\"checkbox_input\", \"input\", 0);\n\n // No `id`/`for` attributes are emitted: ids are per document and would collide when multiple\n // documents are rendered on the same page. Nesting the input inside the label keeps the\n // native label/checkbox association without any document-scoped identifier.\n checkboxToken.attrs = [\n [\"type\", \"checkbox\"],\n [\"class\", checkboxClass],\n ];\n\n // if token.content starts with '[x] ' or '[X] '\n if (todoState === \"checked\") checkboxToken.attrs.push([\"checked\", \"checked\"]);\n\n if (disabled) checkboxToken.attrs.push([\"disabled\", \"disabled\"]);\n\n // checkbox, placed before the label is unshifted so that it ends up inside the label\n token.children.unshift(checkboxToken);\n\n if (label) {\n const labelToken = new state.Token(\"label_open\", \"label\", 1);\n\n labelToken.attrs = [[\"class\", labelClass]];\n\n token.children.unshift(labelToken);\n token.children.push(new state.Token(\"label_close\", \"label\", -1));\n }\n\n setTokenAttr(tokens[index - 2], \"class\", itemClass);\n setTokenAttr(tokens[lastIndexByLevel[tokens[index - 2].level - 1]], \"class\", containerClass);\n }\n };\n\n md.core.ruler.after(\"inline\", \"task_list\", taskListRule);\n};\n"],"mappings":"A,uCAEA,MAAaA,GAAgB,EAAc,EAAc,IAAwB,CAC/E,IAAMC,EAAQC,EAAM,UAAUC,CAAI,EAC5BC,EAAyB,CAACD,EAAME,CAAK,EAEvCJ,EAAQ,EAAGC,EAAM,SAASE,CAAI,EAE7B,EAAM,MAAOH,GAASG,CAC7B,EAEaE,EAAiB,GAA0BJ,EAAM,OAAS,SAE1DK,EAAoB,GAA0BL,EAAM,OAAS,iBAE7DM,EAAmB,GAA0BN,EAAM,OAAS,iBCHnEO,EAA0B,GAAsC,CACpE,IAAMC,EAAUR,EAAM,QAOtB,GAJIQ,EAAQ,OAAS,GAEjBA,EAAQ,WAAW,CAAC,IAAM,IAE1BA,EAAQ,WAAW,CAAC,IAAM,GAAY,MAAO,GAEjD,IAAMC,EAASD,EAAQ,WAAW,CAAC,EAEnC,GAAIC,IAAW,IAAkBA,IAAW,IAAkB,MAAO,GAErE,IAAMC,EAAcF,EAAQ,WAAW,CAAC,EAIxC,OAFIE,IAAgB,KAAeA,IAAgB,GAAmB,UAE/DA,IAAgB,IAAkBA,IAAgB,GAC3D,EAMMC,EAAoB,GAA4B,CAChDC,EAAS,EAAE,CAAC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ACjCzB,EAAM,SAAS,KAAK,IAAIkB,EAAM,MAAM,cAAe,QAAS,EAAE,CAAC,CACjE,CAEApB,EAAamB,EAAOlB,EAAQ,GAAI,QAAS4B,CAAS,EAClD7B,EAAamB,EAAOE,EAAiBF,EAAOlB,EAAQ,EAAE,CAAC,MAAQ,IAAK,QAAS6B,CAAc,CAC7F,CACF,CAEuD,CACzD,E"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mdit/plugin-tasklist",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Tasklist plugin for MarkdownIt",
5
5
  "keywords": [
6
6
  "markdown-it",
@@ -44,7 +44,7 @@
44
44
  "clean": "rimraf ./dist"
45
45
  },
46
46
  "dependencies": {
47
- "@mdit/helper": "^1.2.0"
47
+ "@mdit/helper": "^1.2.1"
48
48
  },
49
49
  "peerDependencies": {
50
50
  "markdown-it": "^15.0.3"
@@ -57,5 +57,5 @@
57
57
  "engines": {
58
58
  "node": ">=22"
59
59
  },
60
- "gitHead": "34cbd263c63502f4d2ef55ab9f9defe790e7bf88"
60
+ "gitHead": "9f02fe8110ff5ea45f89e24605f9abfba59aefeb"
61
61
  }