Skip to content

Commit 0fc5c0c

Browse files
committed
feat: improve checklist paste handling and examples
Refine logic to detect whether the current line's checklist is empty and only strip checklist markers pasted text when the target line is an empty checklist. When the current line contains checklist content, convert pasted checklist lines into a new checklist item ("- [ ] ...") instead of removing their markers. This prevents losing checklist structure and ensures pasted content is placed appropriately. Add numerous example builders covering: - pasting into empty vs non-empty checklists (including blockquotes), - multi-line paste behavior for empty and non-empty checklist targets, - handling of non-standard checklist markers like "- [!]", - indented empty checklist cases, and a case that standardizes incoming non-standard markers to "- [ ]". Update Chinese locale strings to more accurately describe the rule as operating on task-list markers and only removing markers for empty task-list lines.
1 parent b8f4168 commit 0fc5c0c

File tree

2 files changed

+90
-6
lines changed

2 files changed

+90
-6
lines changed

src/lang/locale/zh-cn.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,8 @@ export default {
568568
},
569569
// prevent-double-checklist-indicator-on-paste.ts
570570
'prevent-double-checklist-indicator-on-paste': {
571-
'name': '防止重复的清单标记',
572-
'description': '粘贴时,如果光标所在行有清单标记,则从要粘贴的文本中移除清单标记',
571+
'name': '防止重复的任务列表标记',
572+
'description': '粘贴时,如果光标所在行有空的任务列表标记,则从要粘贴的文本中移除任务列表标记',
573573
},
574574
// prevent-double-list-item-indicator-on-paste.ts
575575
'prevent-double-list-item-indicator-on-paste': {

src/rules/prevent-double-checklist-indicator-on-paste.ts

Lines changed: 88 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@ export default class PreventDoubleChecklistIndicatorOnPaste extends RuleBuilder<
3131
return text;
3232
}
3333

34-
return text.replace(nonBlockquoteChecklistRegex, '');
34+
const checklistContentMatch = options.lineContent.match(/^(\s*(>\s*)*-\s*\[.\]\s*)(.*)$/);
35+
const hasContent = checklistContentMatch && checklistContentMatch[3].trim().length > 0;
36+
37+
if (!hasContent) {
38+
return text.replace(nonBlockquoteChecklistRegex, '');
39+
} else {
40+
const cleanContent = text.replace(nonBlockquoteChecklistRegex, '');
41+
return '\n- [ ] ' + cleanContent.trim();
42+
}
3543
}
3644
get exampleBuilders(): ExampleBuilder<PreventDoubleChecklistIndicatorOnPasteOptions>[] {
3745
return [
@@ -64,7 +72,7 @@ export default class PreventDoubleChecklistIndicatorOnPaste extends RuleBuilder<
6472
},
6573
}),
6674
new ExampleBuilder({
67-
description: 'Line being pasted into a blockquote with a checklist indicator has its checklist indicator removed when current line is: `> - [x] `',
75+
description: 'Line being pasted into a blockquote with empty checklist has its checklist indicator removed when current line is: `> - [x] `',
6876
before: dedent`
6977
- [ ] Checklist item contents here
7078
More content here
@@ -79,7 +87,7 @@ export default class PreventDoubleChecklistIndicatorOnPaste extends RuleBuilder<
7987
},
8088
}),
8189
new ExampleBuilder({
82-
description: 'Line being pasted with a checklist indicator has its checklist indicator removed when current line is: `- [ ] `',
90+
description: 'Line being pasted into empty checklist has its checklist indicator removed when current line is: `- [ ] `',
8391
before: dedent`
8492
- [x] Checklist item 1
8593
- [ ] Checklist item 2
@@ -93,8 +101,57 @@ export default class PreventDoubleChecklistIndicatorOnPaste extends RuleBuilder<
93101
selectedText: '',
94102
},
95103
}),
104+
new ExampleBuilder({
105+
description: 'Line being pasted into non-empty checklist creates a new line when current line is: `- [ ] Existing content`',
106+
before: dedent`
107+
- [x] New checklist item
108+
`,
109+
after: dedent`
110+
111+
- [ ] New checklist item
112+
`,
113+
options: {
114+
lineContent: '- [ ] Existing content',
115+
selectedText: '',
116+
},
117+
}),
118+
new ExampleBuilder({
119+
description: 'Multi-line paste into empty checklist: only first line checkbox removed, rest lines unchanged when current line is: `- [ ] `',
120+
before: dedent`
121+
- [x] First line content
122+
- [ ] Second line content
123+
Regular text line
124+
`,
125+
after: dedent`
126+
First line content
127+
- [ ] Second line content
128+
Regular text line
129+
`,
130+
options: {
131+
lineContent: '- [ ] ',
132+
selectedText: '',
133+
},
134+
}),
135+
new ExampleBuilder({
136+
description: 'Multi-line paste into non-empty checklist: creates new line, only first line becomes checklist when current line is: `- [ ] Existing`',
137+
before: dedent`
138+
- [x] First line content
139+
- [ ] Second line content
140+
Regular text line
141+
`,
142+
after: dedent`
143+
144+
- [ ] First line content
145+
- [ ] Second line content
146+
Regular text line
147+
`,
148+
options: {
149+
lineContent: '- [ ] Existing',
150+
selectedText: '',
151+
},
152+
}),
96153
new ExampleBuilder({ // accounts for https://github.com/platers/obsidian-linter/issues/748
97-
description: 'Line being pasted as a checklist indicator has its checklist indicator removed when current line is: `- [!] `',
154+
description: 'Line being pasted into empty checklist has its checklist indicator removed when current line is: `- [!] `',
98155
before: dedent`
99156
- [x] Checklist item 1
100157
- [ ] Checklist item 2
@@ -123,6 +180,33 @@ export default class PreventDoubleChecklistIndicatorOnPaste extends RuleBuilder<
123180
selectedText: '- [!] Some text here',
124181
},
125182
}),
183+
new ExampleBuilder({
184+
description: 'Non-empty checklist creates new line with standard checkbox: `- [!] Non-empty` with `- [x] Content`',
185+
before: dedent`
186+
- [x] Content to paste
187+
`,
188+
after: dedent`
189+
190+
- [ ] Content to paste
191+
`,
192+
options: {
193+
lineContent: '- [!] Non-empty',
194+
selectedText: '',
195+
},
196+
}),
197+
new ExampleBuilder({
198+
description: 'Handles indented empty checklist correctly: ` - [ ] ` (with 2 spaces)',
199+
before: dedent`
200+
- [x] Indented content
201+
`,
202+
after: dedent`
203+
Indented content
204+
`,
205+
options: {
206+
lineContent: ' - [ ] ',
207+
selectedText: '',
208+
},
209+
}),
126210
];
127211
}
128212
get optionBuilders(): OptionBuilderBase<PreventDoubleChecklistIndicatorOnPasteOptions>[] {

0 commit comments

Comments
 (0)