Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/lang/locale/zh-cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,8 @@ export default {
},
// prevent-double-checklist-indicator-on-paste.ts
'prevent-double-checklist-indicator-on-paste': {
'name': '防止重复的清单标记',
'description': '粘贴时,如果光标所在行有清单标记,则从要粘贴的文本中移除清单标记',
'name': '防止重复的任务列表标记',
'description': '粘贴时,如果光标所在行有空的任务列表标记,则从要粘贴的文本中移除任务列表标记',
},
// prevent-double-list-item-indicator-on-paste.ts
'prevent-double-list-item-indicator-on-paste': {
Expand Down
92 changes: 88 additions & 4 deletions src/rules/prevent-double-checklist-indicator-on-paste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@ export default class PreventDoubleChecklistIndicatorOnPaste extends RuleBuilder<
return text;
}

return text.replace(nonBlockquoteChecklistRegex, '');
const checklistContentMatch = options.lineContent.match(/^(\s*(>\s*)*-\s*\[.\]\s*)(.*)$/);
const hasContent = checklistContentMatch && checklistContentMatch[3].trim().length > 0;

if (!hasContent) {
return text.replace(nonBlockquoteChecklistRegex, '');
} else {
const cleanContent = text.replace(nonBlockquoteChecklistRegex, '');
return '\n- [ ] ' + cleanContent.trim();
}
}
get exampleBuilders(): ExampleBuilder<PreventDoubleChecklistIndicatorOnPasteOptions>[] {
return [
Expand Down Expand Up @@ -64,7 +72,7 @@ export default class PreventDoubleChecklistIndicatorOnPaste extends RuleBuilder<
},
}),
new ExampleBuilder({
description: 'Line being pasted into a blockquote with a checklist indicator has its checklist indicator removed when current line is: `> - [x] `',
description: 'Line being pasted into a blockquote with empty checklist has its checklist indicator removed when current line is: `> - [x] `',
before: dedent`
- [ ] Checklist item contents here
More content here
Expand All @@ -79,7 +87,7 @@ export default class PreventDoubleChecklistIndicatorOnPaste extends RuleBuilder<
},
}),
new ExampleBuilder({
description: 'Line being pasted with a checklist indicator has its checklist indicator removed when current line is: `- [ ] `',
description: 'Line being pasted into empty checklist has its checklist indicator removed when current line is: `- [ ] `',
before: dedent`
- [x] Checklist item 1
- [ ] Checklist item 2
Expand All @@ -93,8 +101,57 @@ export default class PreventDoubleChecklistIndicatorOnPaste extends RuleBuilder<
selectedText: '',
},
}),
new ExampleBuilder({
description: 'Line being pasted into non-empty checklist creates a new line when current line is: `- [ ] Existing content`',
before: dedent`
- [x] New checklist item
`,
after: dedent`

- [ ] New checklist item
`,
options: {
lineContent: '- [ ] Existing content',
selectedText: '',
},
}),
new ExampleBuilder({
description: 'Multi-line paste into empty checklist: only first line checkbox removed, rest lines unchanged when current line is: `- [ ] `',
before: dedent`
- [x] First line content
- [ ] Second line content
Regular text line
`,
after: dedent`
First line content
- [ ] Second line content
Regular text line
`,
options: {
lineContent: '- [ ] ',
selectedText: '',
},
}),
new ExampleBuilder({
description: 'Multi-line paste into non-empty checklist: creates new line, only first line becomes checklist when current line is: `- [ ] Existing`',
before: dedent`
- [x] First line content
- [ ] Second line content
Regular text line
`,
after: dedent`

- [ ] First line content
- [ ] Second line content
Regular text line
`,
options: {
lineContent: '- [ ] Existing',
selectedText: '',
},
}),
new ExampleBuilder({ // accounts for https://github.com/platers/obsidian-linter/issues/748
description: 'Line being pasted as a checklist indicator has its checklist indicator removed when current line is: `- [!] `',
description: 'Line being pasted into empty checklist has its checklist indicator removed when current line is: `- [!] `',
before: dedent`
- [x] Checklist item 1
- [ ] Checklist item 2
Expand Down Expand Up @@ -123,6 +180,33 @@ export default class PreventDoubleChecklistIndicatorOnPaste extends RuleBuilder<
selectedText: '- [!] Some text here',
},
}),
new ExampleBuilder({
description: 'Non-empty checklist creates new line with standard checkbox: `- [!] Non-empty` with `- [x] Content`',
before: dedent`
- [x] Content to paste
`,
after: dedent`

- [ ] Content to paste
`,
options: {
lineContent: '- [!] Non-empty',
selectedText: '',
},
}),
new ExampleBuilder({
description: 'Handles indented empty checklist correctly: ` - [ ] ` (with 2 spaces)',
before: dedent`
- [x] Indented content
`,
after: dedent`
Indented content
`,
options: {
lineContent: ' - [ ] ',
selectedText: '',
},
}),
];
}
get optionBuilders(): OptionBuilderBase<PreventDoubleChecklistIndicatorOnPasteOptions>[] {
Expand Down
14 changes: 11 additions & 3 deletions src/rules/prevent-double-list-item-indicator-on-paste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,21 @@ export default class PreventDoubleListItemIndicatorOnPaste extends RuleBuilder<P
const listRegex = /^\s*[*+-] /;

const isListLine = indentedOrBlockquoteNestedListIndicatorRegex.test(options.lineContent);
const selectedStartsWithListItem = indentedOrBlockquoteNestedListIndicatorRegex.test(options.selectedText);
const selectedTextStartsWithListItem = indentedOrBlockquoteNestedListIndicatorRegex.test(options.selectedText);
const isListClipboard = listRegex.test(text);
if (selectedStartsWithListItem || !isListLine || !isListClipboard) {
if (!isListLine || !isListClipboard || selectedTextStartsWithListItem) {
return text;
}

return text.replace(listRegex, '');
const listContentMatch = options.lineContent.match(/^(\s*(>\s*)*[*+-]\s*)(.*)$/);
const hasContent = listContentMatch && listContentMatch[3].trim().length > 0;

if (!hasContent) {
return text.replace(listRegex, '');
} else {
const cleanContent = text.replace(listRegex, '');
return '\n- ' + cleanContent.trim();
}
}
get exampleBuilders(): ExampleBuilder<PreventDoubleListItemIndicatorOnPasteOptions>[] {
return [
Expand Down