mirror of
https://github.com/usememos/memos.git
synced 2025-12-16 21:59:25 +08:00
fix: wrong checkbox toggled when preceded by a codeblock checkbox
This commit is contained in:
parent
47a57dd326
commit
9be1fd8585
1 changed files with 33 additions and 0 deletions
|
|
@ -24,6 +24,39 @@ export function toggleTaskAtLine(markdown: string, lineNumber: number, checked:
|
|||
|
||||
return lines.join("\n");
|
||||
}
|
||||
export function isInsideCodeBlock(lines :string [] , index : number):boolean{
|
||||
let inside = false;
|
||||
for(let i = 0 ; i <= index ; i++){
|
||||
if(lines[i].startsWith("```")){
|
||||
inside = !inside;
|
||||
}
|
||||
}
|
||||
return inside;
|
||||
}
|
||||
export function toggleTaskAtIndex(markdown: string, taskIndex: number, checked: boolean): string {
|
||||
const lines = markdown.split("\n");
|
||||
const taskPattern = /^(\s*[-*+]\s+)\[([ xX])\](\s+.*)$/;
|
||||
|
||||
let currentTaskIndex = 0;
|
||||
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
if (isInsideCodeBlock(lines, i)) continue;
|
||||
const line = lines[i];
|
||||
const match = line.match(taskPattern);
|
||||
|
||||
if (match) {
|
||||
if (currentTaskIndex === taskIndex) {
|
||||
const [, prefix, , suffix] = match;
|
||||
const newCheckmark = checked ? "x" : " ";
|
||||
lines[i] = `${prefix}[${newCheckmark}]${suffix}`;
|
||||
break;
|
||||
}
|
||||
currentTaskIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
export function removeCompletedTasks(markdown: string): string {
|
||||
const lines = markdown.split("\n");
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue