Files
RuneStone/src/lib/features/pm/storage/legacy.ts
T
Knotty Wyvern e66f089176 Make Project Management a native feature
- Move the storage and model of the plugin into src/lib/features/pm/ as pure TypeScript, so the skill can still bundle them.
- Move the DOM interface onto the host menus, the shared tooltip, and the host icons.
- A ribbon button opens Project Management. Board tabs keep the owner runestone.pm and the board-<hex> view IDs through view patterns.
- Read only .runestone/pm/. Offer the plugin migration, with backups, for legacy notes.
- A save, move, or delete reads only that card again. Watcher paths reach the feature.
- Add Settings → Project Management and the projectManagement section. View states move to localStorage.
- Background reads the boards from workspace.json. Remove api.backgrounds and the boards service.
- Add yaml 2.9.1. Use the host markdown-it.
- Move 18 plugin test files to tests/unit/features/pm/ and add native tests.
2026-10-29 20:54:29 -07:00

79 lines
4.3 KiB
TypeScript

// The legacy format: boards and task notes in visible Markdown files, and the root label file.
// The native feature reads only `.runestone/pm/`. When it opens a workspace that still has data in the legacy format,
// it offers the migration (`migrateStorage` in `migration.ts`), and it migrates only after the user chooses it.
import type { Workspace } from "../model/model.ts";
import { readCatalog } from "./catalog.ts";
import { cardPath } from "./cards.ts";
import { readDeletedCards } from "./deleted-cards.ts";
import { labelsPath, legacyLabelsPath } from "./labels.ts";
import { split } from "./notes.ts";
import { cardsFolder, readOptional } from "./persistence.ts";
import { markdownPaths, readSources } from "./scan.ts";
/** The legacy data that the migration has not copied to `.runestone/pm/` yet. */
export interface LegacyContent {
/** The paths of board notes. */
boards: string[];
/** The paths of task notes. */
cards: string[];
/** The paths of notes with invalid Project Management properties. The migration reports them. */
invalid: string[];
/** True when `runestone-pm-labels.json` exists and `.runestone/pm/labels.json` does not. */
labels: boolean;
}
/** The YAML properties at the start of a note, up to a line that starts with `runestone_pm:`. The closing line stops it. */
const properties = /^---[ \t]*\r?\n(?:(?!(?:---|\.\.\.)[ \t]*\r?\n)[^\n]*\n)*?runestone_pm[ \t]*:/;
/** True when the YAML properties of a note can contain Project Management data. It needs no YAML parse. */
export function mentionsProjectData(source: string): boolean {
return properties.test(source.charCodeAt(0) === 0xfeff ? source.slice(1) : source);
}
/**
* Find the legacy data of a workspace. Only notes that name `runestone_pm` in their properties get a YAML parse.
* A board or a card that the migration copied, or that the user deleted, does not count.
*/
export async function findLegacy(workspace: Workspace, check: () => void = () => {}): Promise<LegacyContent> {
const result: LegacyContent = { boards: [], cards: [], invalid: [], labels: false };
const paths = await markdownPaths(workspace, check);
const found: { path: string; kind: "board" | "task"; id: string }[] = [];
// One batch at a time, so the texts of all notes are never in memory together.
for (let offset = 0; offset < paths.length; offset += 64) {
const sources = await readSources(workspace, paths.slice(offset, offset + 64));
check();
for (const [path, file] of sources) {
if (file.source === undefined || !mentionsProjectData(file.source)) continue;
try {
const value = split(file.source).doc.toJS({ maxAliasCount: 50 }).runestone_pm;
if (value === undefined) continue;
if (!value || typeof value.id !== "string" || !value.id.trim() || (value.kind !== "board" && value.kind !== "task")) throw new Error("Invalid data.");
found.push({ path, kind: value.kind, id: value.id });
} catch { result.invalid.push(path); }
}
}
let saved = new Set<string>(), deletedBoards = new Set<string>(), deletedCards = new Set<string>();
try {
const catalog = await readCatalog(workspace);
saved = new Set(catalog.value.boards.map(board => board.value.id));
deletedBoards = new Set(catalog.value.deletedBoardIds ?? []);
} catch { /* The migration reports a damaged catalog. */ }
try { deletedCards = new Set((await readDeletedCards(workspace)).ids); } catch { /* The migration reports it. */ }
let cards = new Set<string>();
if (found.some(item => item.kind === "task")) {
try { cards = new Set((await workspace.list(cardsFolder)).map(entry => entry.rel_path)); } catch { /* No card folder yet. */ }
}
for (const item of found) {
if (item.kind === "board" && !saved.has(item.id) && !deletedBoards.has(item.id)) result.boards.push(item.path);
if (item.kind === "task" && !cards.has(cardPath(item.id)) && !deletedCards.has(item.id)) result.cards.push(item.path);
}
result.labels = (await readOptional(workspace, legacyLabelsPath)) !== null && (await readOptional(workspace, labelsPath)) === null;
for (const list of [result.boards, result.cards, result.invalid]) list.sort();
return result;
}
/** True when the workspace has legacy data to migrate. */
export function hasLegacy(content: LegacyContent): boolean {
return content.boards.length > 0 || content.cards.length > 0 || content.invalid.length > 0 || content.labels;
}