diff --git a/demo/md-plugins.ts b/demo/md-plugins.ts index 19e494b1..9dcffb89 100644 --- a/demo/md-plugins.ts +++ b/demo/md-plugins.ts @@ -10,6 +10,7 @@ const color = require('markdown-it-color').colorPlugin; import math from 'markdown-it-katex'; import meta from '@doc-tools/transform/lib/plugins/meta'; +import checkbox from '@doc-tools/transform/lib/plugins/checkbox'; import deflist from '@doc-tools/transform/lib/plugins/deflist'; import anchors from '@doc-tools/transform/lib/plugins/anchors'; import cut from '@doc-tools/transform/lib/plugins/cut'; @@ -35,6 +36,7 @@ const defaultPlugins: PluginWithParams[] = [ yfmTable, file, imsize, + checkbox, ]; const extendedPlugins = defaultPlugins.concat(sub, ins, mark, math, color); diff --git a/src/extensions/yfm/Checkbox/Checkbox.test.ts b/src/extensions/yfm/Checkbox/Checkbox.test.ts index 8d3859bb..feccc9ef 100644 --- a/src/extensions/yfm/Checkbox/Checkbox.test.ts +++ b/src/extensions/yfm/Checkbox/Checkbox.test.ts @@ -6,12 +6,17 @@ import {CheckboxNode} from './const'; import {Checkbox} from './index'; const {schema, parser, serializer} = new ExtensionsManager({ - extensions: (builder) => builder.use(BaseSchema, {}).use(Checkbox, {}), + extensions: (builder) => + builder + .use(BaseSchema, {}) + .use(Checkbox, {checkboxLabelPlaceholder: 'checkbox-placeholder'}), }).buildDeps(); const { doc, checkbox, + cbInput, + cbLabel, checkboxInput0, checkboxInput1, checkboxInput2, @@ -22,6 +27,8 @@ const { doc: {nodeType: BaseNode.Doc}, p: {nodeType: BaseNode.Paragraph}, checkbox: {nodeType: CheckboxNode.Checkbox}, + cbInput: {nodeType: CheckboxNode.Input}, + cbLabel: {nodeType: CheckboxNode.Label}, checkboxInput0: {nodeType: CheckboxNode.Input, id: 'yfm-editor-checkbox0'}, checkboxInput1: { nodeType: CheckboxNode.Input, @@ -36,6 +43,8 @@ const { | 'doc' | 'p' | 'checkbox' + | 'cbInput' + | 'cbLabel' | 'checkboxInput0' | 'checkboxInput1' | 'checkboxInput2' @@ -44,7 +53,7 @@ const { | 'checkboxLabel2' >; -const {same} = createMarkupChecker({parser, serializer}); +const {same, serialize} = createMarkupChecker({parser, serializer}); describe('Checkbox extension', () => { it('should parse unchecked checkbox', () => @@ -55,4 +64,15 @@ describe('Checkbox extension', () => { it('should not escape characters', () => same('[ ] abobo +', doc(checkbox(checkboxInput2(), checkboxLabel2('abobo +'))))); + + it('should substitute placeholder when label is empty', () => { + serialize(doc(checkbox(cbInput(), cbLabel())), '[ ] checkbox-placeholder'); + }); + + it('should substitute placeholder when label contains only whitespace characters', () => { + serialize( + doc(checkbox(cbInput(), cbLabel(' \t \n '))), + '[ ] checkbox-placeholder', + ); + }); }); diff --git a/src/extensions/yfm/Checkbox/toYfm.ts b/src/extensions/yfm/Checkbox/toYfm.ts index f8bdec5d..b5a69b4f 100644 --- a/src/extensions/yfm/Checkbox/toYfm.ts +++ b/src/extensions/yfm/Checkbox/toYfm.ts @@ -14,7 +14,7 @@ export const toYfm: Record = { }, [CheckboxNode.Label]: (state, node) => { - if (!node.content.size) { + if (!node.content.size || node.textContent.trim().length === 0) { state.write(getPlaceholderContent(node)); return; }