First step of docs/git-sync-plan.md. New workspace package @docmost/git-sync vendoring the PURE parts from docmost-sync (HEAD b03eb35): - lib: markdown-converter, markdown-document, canonicalize, docmost-schema, node-ops, diff, and an extracted markdown-to-prosemirror (only the pure marked->HTML->generateJSON path from upstream collaboration.ts; no websocket). - engine (pure, no IO): reconcile, layout, sanitize, stabilize, loop-guard. Ported the upstream pure-module + round-trip corpus tests (vitest): 314 pass, 3 expected upstream known-limitation fails. tsc clean. No server wiring yet. docmost-schema inlines getStyleProperty (as packages/mcp does — @tiptap/core 3.20.4 doesn't export it). IO engine (pull/push/git/settings) deferred to later Phase A/B steps; the editor-ext idempotency gate (plan §13.1) is the next step. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
59 lines
2.3 KiB
TypeScript
59 lines
2.3 KiB
TypeScript
/**
|
|
* Normalize-on-write helper (SPEC §11 "Резолюция").
|
|
*
|
|
* git diffs byte-for-byte, so writing a page in a NON-fixpoint markdown form
|
|
* would make the next pull re-export it to a slightly different (but stable)
|
|
* form and produce a phantom diff -> churny commits. The converter has a couple
|
|
* of known one-pass asymmetries (a block image after a paragraph adds an empty
|
|
* paragraph; a diagram materializes `data-align`), all of which converge to a
|
|
* fixpoint after ONE `export -> import -> export` round-trip.
|
|
*
|
|
* So at write time we run exactly that one pass and persist the fixpoint form.
|
|
* Already-stable content is unaffected (the pass is idempotent), so re-pulls of
|
|
* unchanged pages produce identical bytes and git sees no diff.
|
|
*/
|
|
import {
|
|
convertProseMirrorToMarkdown,
|
|
markdownToProseMirror,
|
|
serializeDocmostMarkdownBody,
|
|
type DocmostMdMeta,
|
|
} from "../lib/index.js";
|
|
|
|
/**
|
|
* Meta object as `exportPageBody` builds it (SPEC §4). Kept byte-for-byte
|
|
* compatible so files produced here match `exportPageBody`'s output exactly.
|
|
*/
|
|
export interface PageMeta {
|
|
version: 1;
|
|
pageId: string;
|
|
slugId: string;
|
|
title: string;
|
|
spaceId: string;
|
|
parentPageId: string | null;
|
|
}
|
|
|
|
/**
|
|
* Produce the self-contained `.md` file text for a page from its raw
|
|
* ProseMirror `content` + identity meta, in the verified fixpoint form.
|
|
*
|
|
* md1 = convertProseMirrorToMarkdown(content)
|
|
* doc2 = markdownToProseMirror(md1) // one import...
|
|
* stableBody = convertProseMirrorToMarkdown(doc2) // ...and re-export
|
|
* file = serializeDocmostMarkdownBody(meta, stableBody)
|
|
*
|
|
* The single export->import->export pass is the verified fixpoint (SPEC §11):
|
|
* idempotent for already-stable content, and the convergence point for the
|
|
* known converter asymmetries.
|
|
*/
|
|
export async function stabilizePageFile(
|
|
content: unknown,
|
|
meta: PageMeta,
|
|
): Promise<string> {
|
|
const md1 = convertProseMirrorToMarkdown(content);
|
|
const doc2 = await markdownToProseMirror(md1);
|
|
const stableBody = convertProseMirrorToMarkdown(doc2);
|
|
// The meta shape is exactly what `exportPageBody` writes; cast to the lib's
|
|
// DocmostMdMeta (a superset with optional fields) for the serializer.
|
|
return serializeDocmostMarkdownBody(meta as DocmostMdMeta, stableBody);
|
|
}
|