Skip to content

Commit

Permalink
fix(Checkbox): write placeholder content when label constains only wh…
Browse files Browse the repository at this point in the history
…itespace characters (#32)
  • Loading branch information
d3m1d0v authored Nov 7, 2022
1 parent 1b0d44a commit 3ded049
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
2 changes: 2 additions & 0 deletions demo/md-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -35,6 +36,7 @@ const defaultPlugins: PluginWithParams[] = [
yfmTable,
file,
imsize,
checkbox,
];
const extendedPlugins = defaultPlugins.concat(sub, ins, mark, math, color);

Expand Down
24 changes: 22 additions & 2 deletions src/extensions/yfm/Checkbox/Checkbox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -36,6 +43,8 @@ const {
| 'doc'
| 'p'
| 'checkbox'
| 'cbInput'
| 'cbLabel'
| 'checkboxInput0'
| 'checkboxInput1'
| 'checkboxInput2'
Expand All @@ -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', () =>
Expand All @@ -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',
);
});
});
2 changes: 1 addition & 1 deletion src/extensions/yfm/Checkbox/toYfm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const toYfm: Record<CheckboxNode, SerializerNodeToken> = {
},

[CheckboxNode.Label]: (state, node) => {
if (!node.content.size) {
if (!node.content.size || node.textContent.trim().length === 0) {
state.write(getPlaceholderContent(node));
return;
}
Expand Down

0 comments on commit 3ded049

Please sign in to comment.