diff --git a/src/transform/plugins/checkbox/checkbox.ts b/src/transform/plugins/checkbox/checkbox.ts index 7e1d9d8e..86588091 100644 --- a/src/transform/plugins/checkbox/checkbox.ts +++ b/src/transform/plugins/checkbox/checkbox.ts @@ -1,9 +1,19 @@ import type MarkdownIt from 'markdown-it'; +import type Core from 'markdown-it/lib/parser_core'; import type StateCore from 'markdown-it/lib/rules_core/state_core'; import type Token from 'markdown-it/lib/token'; // eslint-disable-next-line no-useless-escape -const pattern = /^\[(X|\s|\_|\-)\]\s(.*)/i; +export const pattern = /^\[(X|\s|\_|\-)\]\s(.*)/i; +export const CheckboxTokenType = { + Checkbox: 'checkbox', + CheckboxOpen: 'checkbox_open', + CheckboxClose: 'checkbox_close', + CheckboxInput: 'checkbox_input', + CheckboxLabel: 'checkbox_label', + CheckboxLabelOpen: 'checkbox_label_open', + CheckboxLabelClose: 'checkbox_label_close', +} as const; function matchOpenToken(tokens: Token[], i: number) { return ( @@ -16,13 +26,16 @@ function matchOpenToken(tokens: Token[], i: number) { export type CheckboxOptions = { idPrefix?: string; divClass?: string; + /** @default true */ + disabled?: boolean; }; -export const checkboxReplace = function (_md: MarkdownIt, opts: CheckboxOptions) { +export const checkboxReplace = function (_md: MarkdownIt, opts?: CheckboxOptions): Core.RuleCore { let lastId = 0; - const defaults = { + const defaults: Required = { divClass: 'checkbox', idPrefix: 'checkbox', + disabled: true, }; const options = Object.assign(defaults, opts); @@ -33,7 +46,7 @@ export const checkboxReplace = function (_md: MarkdownIt, opts: CheckboxOptions) /** *
*/ - token = new state.Token('checkbox_open', 'div', 1); + token = new state.Token(CheckboxTokenType.CheckboxOpen, 'div', 1); token.block = true; token.map = state.tokens[i].map; token.attrs = [['class', options.divClass]]; @@ -44,23 +57,25 @@ export const checkboxReplace = function (_md: MarkdownIt, opts: CheckboxOptions) */ const id = options.idPrefix + lastId; lastId += 1; - token = new state.Token('checkbox_input', 'input', 0); + token = new state.Token(CheckboxTokenType.CheckboxInput, 'input', 0); token.block = true; token.map = state.tokens[i].map; token.attrs = [ ['type', 'checkbox'], ['id', id], - ['disabled', ''], ]; + if (options.disabled) { + token.attrSet('disabled', ''); + } if (checked === true) { - token.attrs.push(['checked', 'true']); + token.attrSet('checked', 'true'); } nodes.push(token); /** *
\n', ); }); + + it('should not set disable attr to checkbox', () => { + const md = new MarkdownIt({}).use(plugin, {disabled: false}); + const res = md.render('[X] check'); + assert.equal( + res, + '
\n' + + '\n' + + '\n' + + '
\n', + ); + }); }); it('should parse inline markup in label', () => {