mirror of
https://github.com/usememos/memos.git
synced 2026-11-19 10:29:11 +01:00
fix(web): toggle numbered task list items correctly (#6346)
Co-authored-by: kalelooz <212041863+kalelooz@users.noreply.github.com>
This commit is contained in:
@@ -46,8 +46,8 @@ export function toggleTaskAtLine(markdown: string, lineNumber: number, checked:
|
||||
|
||||
const line = lines[lineNumber];
|
||||
|
||||
// Match task list patterns: - [ ], - [x], - [X], etc.
|
||||
const taskPattern = /^(\s*[-*+]\s+)\[([ xX])\](\s+.*)$/;
|
||||
// Match bullet and numbered tasks, retaining any trailing carriage return.
|
||||
const taskPattern = /^(\s*(?:[-*+]|\d+[.)])\s+)\[([ xX])\](\s+.*)$/s;
|
||||
const match = line.match(taskPattern);
|
||||
|
||||
if (!match) {
|
||||
|
||||
@@ -1,5 +1,30 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { findAnchorTarget, findMemoAnchorTarget, slugify } from "@/utils/markdown-manipulation";
|
||||
import { findAnchorTarget, findMemoAnchorTarget, slugify, toggleTaskAtIndex } from "@/utils/markdown-manipulation";
|
||||
|
||||
describe("toggleTaskAtIndex", () => {
|
||||
it.each(["1.", "1)", "42.", "42)", "-", "*", "+"])("checks and unchecks %s tasks", (marker) => {
|
||||
expect(toggleTaskAtIndex(`${marker} [ ] Buy milk`, 0, true)).toBe(`${marker} [x] Buy milk`);
|
||||
expect(toggleTaskAtIndex(`${marker} [x] Buy milk`, 0, false)).toBe(`${marker} [ ] Buy milk`);
|
||||
expect(toggleTaskAtIndex(`${marker} [X] Buy milk`, 0, false)).toBe(`${marker} [ ] Buy milk`);
|
||||
});
|
||||
|
||||
it.each(["\n", "\r\n"])("preserves formatting and targets only the selected nested task with %j line endings", (eol) => {
|
||||
const lines = ["Intro", "", "- [ ] First", " 1) [ ] Second ", " 2) [x] Third", "", "1. [ ] Last", ""];
|
||||
const markdown = lines.join(eol);
|
||||
lines[3] = " 1) [x] Second ";
|
||||
expect(toggleTaskAtIndex(markdown, 1, true)).toBe(lines.join(eol));
|
||||
expect(toggleTaskAtIndex(lines.join(eol), 1, false)).toBe(markdown);
|
||||
});
|
||||
|
||||
it("ignores checkbox-looking text in fenced, indented, and inline code", () => {
|
||||
const lines = ["```md", "1. [ ] Fenced", "```", "", " 1) [ ] Indented", "", "`1. [ ] Inline`", "", "1) [ ] Real"];
|
||||
const markdown = lines.join("\n");
|
||||
lines[8] = "1) [x] Real";
|
||||
expect(toggleTaskAtIndex(markdown, 0, true)).toBe(lines.join("\n"));
|
||||
expect(toggleTaskAtIndex(markdown, 1, true)).toBe(markdown);
|
||||
expect(toggleTaskAtIndex(markdown, -1, true)).toBe(markdown);
|
||||
});
|
||||
});
|
||||
|
||||
describe("slugify", () => {
|
||||
it("keeps non-Latin headings addressable", () => {
|
||||
|
||||
Reference in New Issue
Block a user