// 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 { 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(), deletedBoards = new Set(), deletedCards = new Set(); 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(); 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; }