feat(workspace): numbered lists and reliable markdown paste
MindSift CI & Docker Release / Build, Test in Container & Push (push) Successful in 2m5s
MindSift CI & Docker Release / Build, Test in Container & Push (push) Successful in 2m5s
- block type 'numbered' (/number): 1., a., i. by nesting, continues on Enter, an empty item ends the list; markdown export/import as 1. - pasting: parser moved to lib/pasteOutline with tests; blank lines split paragraphs, numbered lists keep their type, indentation levels follow the widths in use (2, 3 or 4 spaces, tabs) - text that stays in the focused block after pasting (a paragraph with line breaks) was dropped; the block now takes it Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9008d7c915
commit
2bfe74e66b
13 files changed
+286
-176
No files matched your search
@@ -0,0 +1,27 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { listLabel, numberedLabels } from '../outlineList';
|
||||
import type { DocumentBlock } from '../types';
|
||||
|
||||
const blk = (id: string, level: number, type: DocumentBlock['type'] = 'numbered'): DocumentBlock => ({
|
||||
id, documentId: 'd', type, content: id, level, position: 0,
|
||||
});
|
||||
|
||||
describe('numbered lists', () => {
|
||||
it('formats numbers, letters and roman numerals by depth', () => {
|
||||
expect(listLabel(3, 0)).toBe('3.');
|
||||
expect(listLabel(2, 1)).toBe('b.');
|
||||
expect(listLabel(27, 1)).toBe('aa.');
|
||||
expect(listLabel(4, 2)).toBe('iv.');
|
||||
expect(listLabel(1, 3)).toBe('1.');
|
||||
});
|
||||
|
||||
it('counts siblings, nests and restarts after other blocks', () => {
|
||||
const labels = numberedLabels([
|
||||
blk('a', 0), blk('a1', 1), blk('a2', 1), blk('a2i', 2), blk('b', 0),
|
||||
blk('note', 0, 'text'), blk('c', 0), blk('plain', 1, 'text'), blk('c1', 2),
|
||||
]);
|
||||
expect(Object.fromEntries(labels)).toEqual({
|
||||
a: '1.', a1: 'a.', a2: 'b.', a2i: 'i.', b: '2.', c: '1.', c1: 'a.',
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,38 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { parsePastedLines } from '../pasteOutline';
|
||||
|
||||
const parse = (text: string) => parsePastedLines(text.split('\n'));
|
||||
|
||||
describe('parsePastedLines', () => {
|
||||
it('splits paragraphs at blank lines and keeps wrapped lines together', () => {
|
||||
expect(parse('Erster Absatz\nnoch erster\n\nZweiter Absatz')).toEqual([
|
||||
{ type: 'text', content: 'Erster Absatz\nnoch erster', indent: 0 },
|
||||
{ type: 'text', content: 'Zweiter Absatz', indent: 0 },
|
||||
]);
|
||||
});
|
||||
|
||||
it('keeps numbered lists with nesting (3-space indent)', () => {
|
||||
expect(parse('1. Eins\n 1. Unterpunkt\n 1. Tiefer\n2. Zwei')).toEqual([
|
||||
{ type: 'numbered', content: 'Eins', indent: 0 },
|
||||
{ type: 'numbered', content: 'Unterpunkt', indent: 1 },
|
||||
{ type: 'numbered', content: 'Tiefer', indent: 2 },
|
||||
{ type: 'numbered', content: 'Zwei', indent: 0 },
|
||||
]);
|
||||
});
|
||||
|
||||
it('reads headings, todos, bullets and dividers', () => {
|
||||
expect(parse('# Titel\nText danach\n- [x] erledigt\n- Punkt\n---')).toEqual([
|
||||
{ type: 'h1', content: 'Titel', indent: 0 },
|
||||
{ type: 'text', content: 'Text danach', indent: 0 },
|
||||
{ type: 'todo', checked: true, content: 'erledigt', indent: 0 },
|
||||
{ type: 'text', content: 'Punkt', indent: 0 },
|
||||
{ type: 'divider', content: '---', indent: 0 },
|
||||
]);
|
||||
});
|
||||
|
||||
it('keeps fenced code as one block', () => {
|
||||
const items = parse('```go\nfunc main() {}\n\n```\nDanach');
|
||||
expect(items[0].content).toBe('```go\nfunc main() {}\n\n```');
|
||||
expect(items[1]).toEqual({ type: 'text', content: 'Danach', indent: 0 });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,65 @@
|
||||
import type { DocumentBlock } from './types';
|
||||
|
||||
// Numbered lists in the outliner: consecutive 'numbered' siblings count 1, 2, 3 …; a numbered list
|
||||
// inside another one uses a, b, c, the next level i, ii, iii, then numbers again.
|
||||
|
||||
function alpha(n: number): string {
|
||||
let s = '';
|
||||
while (n > 0) {
|
||||
n--;
|
||||
s = String.fromCharCode(97 + (n % 26)) + s;
|
||||
n = Math.floor(n / 26);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
const ROMAN: [number, string][] = [
|
||||
[1000, 'm'], [900, 'cm'], [500, 'd'], [400, 'cd'], [100, 'c'], [90, 'xc'],
|
||||
[50, 'l'], [40, 'xl'], [10, 'x'], [9, 'ix'], [5, 'v'], [4, 'iv'], [1, 'i'],
|
||||
];
|
||||
|
||||
function roman(n: number): string {
|
||||
let s = '';
|
||||
for (const [v, r] of ROMAN) {
|
||||
while (n >= v) {
|
||||
s += r;
|
||||
n -= v;
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
export function listLabel(n: number, depth: number): string {
|
||||
switch (depth % 3) {
|
||||
case 1:
|
||||
return `${alpha(n)}.`;
|
||||
case 2:
|
||||
return `${roman(n)}.`;
|
||||
default:
|
||||
return `${n}.`;
|
||||
}
|
||||
}
|
||||
|
||||
/** Label ("1.", "b.", "iii.") for every numbered block, in document order. */
|
||||
export function numberedLabels(blocks: DocumentBlock[]): Map<string, string> {
|
||||
const labels = new Map<string, string>();
|
||||
const runs = new Map<number, number>(); // level -> count of the current run
|
||||
const chain: DocumentBlock[] = []; // last block seen per level = ancestors of the next one
|
||||
for (const b of blocks) {
|
||||
const level = Math.max(0, b.level || 0);
|
||||
for (const l of Array.from(runs.keys())) {
|
||||
if (l > level) runs.delete(l);
|
||||
}
|
||||
chain.length = Math.min(chain.length, level);
|
||||
if (b.type === 'numbered') {
|
||||
const n = (runs.get(level) ?? 0) + 1;
|
||||
runs.set(level, n);
|
||||
const depth = chain.filter((a) => a && a.type === 'numbered').length;
|
||||
labels.set(b.id, listLabel(n, depth));
|
||||
} else {
|
||||
runs.delete(level);
|
||||
}
|
||||
chain[level] = b;
|
||||
}
|
||||
return labels;
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
import type { BlockType } from './types';
|
||||
|
||||
// Turns pasted (Markdown) text into outline items: headings, todos, bullets, numbered items, dividers,
|
||||
// fenced code and paragraphs. A blank line ends a paragraph; indentation becomes the nesting level.
|
||||
|
||||
export interface PastedItem {
|
||||
type: BlockType;
|
||||
checked?: boolean;
|
||||
content: string;
|
||||
indent: number;
|
||||
}
|
||||
|
||||
function leadingWidth(line: string): { width: number; rest: string } {
|
||||
let width = 0;
|
||||
let i = 0;
|
||||
while (i < line.length && (line[i] === ' ' || line[i] === '\t')) {
|
||||
width += line[i] === '\t' ? 4 : 1;
|
||||
i++;
|
||||
}
|
||||
return { width, rest: line.slice(i) };
|
||||
}
|
||||
|
||||
export function parsePastedLines(rawLines: string[]): PastedItem[] {
|
||||
// Indentation steps differ (2, 3 or 4 spaces, tabs): the distinct widths in use are the levels
|
||||
const widths = Array.from(
|
||||
new Set(rawLines.filter((l) => l.trim() !== '').map((l) => leadingWidth(l).width))
|
||||
).sort((a, b) => a - b);
|
||||
const levelOf = (w: number) => Math.max(0, widths.indexOf(w));
|
||||
|
||||
const items: PastedItem[] = [];
|
||||
let current: PastedItem | null = null;
|
||||
let inCode = false;
|
||||
let afterHeading = false;
|
||||
|
||||
const flush = () => {
|
||||
if (current) {
|
||||
current.content = current.content.trimEnd();
|
||||
items.push(current);
|
||||
current = null;
|
||||
}
|
||||
};
|
||||
|
||||
for (const rawLine of rawLines) {
|
||||
if (inCode) {
|
||||
if (current) (current as PastedItem).content += '\n' + rawLine;
|
||||
if (rawLine.trim().startsWith('```')) {
|
||||
inCode = false;
|
||||
flush();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (rawLine.trim().startsWith('```')) {
|
||||
inCode = true;
|
||||
flush();
|
||||
current = { type: 'text', content: rawLine, indent: 0 };
|
||||
afterHeading = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
const { width, rest } = leadingWidth(rawLine);
|
||||
const indent = levelOf(width);
|
||||
const line = rest.trimEnd();
|
||||
|
||||
// A blank line ends the paragraph or list item
|
||||
if (line === '') {
|
||||
flush();
|
||||
continue;
|
||||
}
|
||||
|
||||
const heading = line.match(/^(#{1,3})\s+(.*)$/);
|
||||
if (heading) {
|
||||
flush();
|
||||
afterHeading = true;
|
||||
items.push({ type: (['h1', 'h2', 'h3'] as const)[heading[1].length - 1], content: heading[2], indent });
|
||||
continue;
|
||||
}
|
||||
if (/^(?:-{3,}|\*{3,}|_{3,})$/.test(line)) {
|
||||
flush();
|
||||
afterHeading = false;
|
||||
items.push({ type: 'divider', content: '---', indent });
|
||||
continue;
|
||||
}
|
||||
|
||||
const todo = line.match(/^[-*+]\s*\[([ xX])\]\s*(.*)$/);
|
||||
const bullet = line.match(/^[-*+•]\s+(.*)$/);
|
||||
const numbered = line.match(/^\d+[.)]\s+(.*)$/);
|
||||
if (todo || bullet || numbered) {
|
||||
flush();
|
||||
afterHeading = false;
|
||||
current = todo
|
||||
? { type: 'todo', checked: todo[1].toLowerCase() === 'x', content: todo[2], indent }
|
||||
: numbered
|
||||
? { type: 'numbered', content: numbered[1], indent }
|
||||
: { type: 'text', content: bullet![1], indent };
|
||||
continue;
|
||||
}
|
||||
|
||||
// Plain text: continues the paragraph or list item above (a wrapped line), else starts a paragraph
|
||||
if (current && !afterHeading) {
|
||||
(current as PastedItem).content += '\n' + line;
|
||||
} else {
|
||||
flush();
|
||||
current = { type: 'text', content: line, indent };
|
||||
}
|
||||
afterHeading = false;
|
||||
}
|
||||
flush();
|
||||
return items;
|
||||
}
|
||||
@@ -261,7 +261,7 @@ export const DATA_SECTIONS: { id: DataSection; label: string }[] = [
|
||||
{ id: 'themes', label: 'Themes' },
|
||||
];
|
||||
|
||||
export type BlockType = 'h1' | 'h2' | 'h3' | 'paragraph' | 'text' | 'todo' | 'bullet' | 'code' | 'callout' | 'divider' | 'table' | 'progress' | 'image' | 'audio';
|
||||
export type BlockType = 'h1' | 'h2' | 'h3' | 'paragraph' | 'text' | 'todo' | 'bullet' | 'numbered' | 'code' | 'callout' | 'divider' | 'table' | 'progress' | 'image' | 'audio';
|
||||
|
||||
export interface DocumentBlock {
|
||||
id: string;
|
||||
|
||||
Reference in new issue
Block a user