feat(git-sync): CommonJS build + §13.1 editor-ext idempotency gate (Phase A.2)
Make @docmost/git-sync natively consumable by the CommonJS server (and jest):
build to CommonJS (tsconfig module CommonJS, drop type:module, strip .js from
relative imports), and lazy-load the only ESM-only dep (marked) via the dynamic
Function('import()') trick (mirrors docmost-client.loader.ts) with a require()
fallback so vitest's evaluator works too. git-sync tests stay green (314 pass,
3 expected fail).
Add the §13.1 idempotency gate (apps/server .../git-sync-converter-gate.spec.ts):
13 editor-ext docs (paragraphs/headings, marks, links, bullet/ordered/task lists,
blockquote, callouts, code block, hr, table, nested mix) round-trip
content(editor-ext) -> convertProseMirrorToMarkdown -> markdownToProseMirror ->
TiptapTransformer.toYdoc/fromYdoc(tiptapExtensions) -> canonicalize and assert
docsCanonicallyEqual. All green => the vendored converter's docmost-schema is
schema-compatible with editor-ext (no node/mark/attr loss), which the plan §13.1
requires before Phase B. The one intrinsic markdown-image lossiness (width/height
/align can't ride plain ) is isolated in a KNOWN DIVERGENCE block, not
hidden. Server tsc clean.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -21,7 +21,7 @@ import { getSchema } from "@tiptap/core";
|
||||
import { Node } from "@tiptap/pm/model";
|
||||
import { ChangeSet, simplifyChanges } from "@tiptap/pm/changeset";
|
||||
import { recreateTransform } from "@fellow/prosemirror-recreate-transform";
|
||||
import { docmostExtensions } from "./docmost-schema.js";
|
||||
import { docmostExtensions } from "./docmost-schema";
|
||||
|
||||
/** A single inserted/deleted change with its containing-block context. */
|
||||
export interface DiffChange {
|
||||
|
||||
@@ -14,14 +14,14 @@ export {
|
||||
serializeDocmostMarkdown,
|
||||
parseDocmostMarkdown,
|
||||
serializeDocmostMarkdownBody,
|
||||
} from "./markdown-document.js";
|
||||
export type { DocmostMdMeta } from "./markdown-document.js";
|
||||
} from "./markdown-document";
|
||||
export type { DocmostMdMeta } from "./markdown-document";
|
||||
|
||||
export { convertProseMirrorToMarkdown } from "./markdown-converter.js";
|
||||
export { convertProseMirrorToMarkdown } from "./markdown-converter";
|
||||
|
||||
export { markdownToProseMirror } from "./markdown-to-prosemirror.js";
|
||||
export { markdownToProseMirror } from "./markdown-to-prosemirror";
|
||||
|
||||
export {
|
||||
canonicalizeContent,
|
||||
docsCanonicallyEqual,
|
||||
} from "./canonicalize.js";
|
||||
} from "./canonicalize";
|
||||
|
||||
@@ -9,10 +9,59 @@
|
||||
* lives in the same upstream file is intentionally NOT vendored — the gitmost
|
||||
* server writes page bodies natively through the collab gateway (plan §3.3).
|
||||
*/
|
||||
import { marked } from "marked";
|
||||
import { generateJSON } from "@tiptap/html";
|
||||
import { JSDOM } from "jsdom";
|
||||
import { docmostExtensions } from "./docmost-schema.js";
|
||||
import { docmostExtensions } from "./docmost-schema";
|
||||
|
||||
/**
|
||||
* Structural type for the bits of the `marked` ESM module we use: just the
|
||||
* `marked` named export's `parse` method (markdown -> HTML string).
|
||||
*/
|
||||
interface MarkedModule {
|
||||
marked: { parse(markdown: string): string | Promise<string> };
|
||||
}
|
||||
|
||||
// `marked` is ESM-only. Under this package's CommonJS build TS would otherwise
|
||||
// downlevel a literal `import()` to `require()`, which cannot load an ESM-only
|
||||
// module. Indirect through `Function` so the real dynamic `import()` survives
|
||||
// compilation and loads ESM from CommonJS at runtime in Node (same trick as
|
||||
// apps/server/src/core/ai-chat/tools/docmost-client.loader.ts).
|
||||
const esmImport = new Function(
|
||||
"specifier",
|
||||
"return import(specifier)",
|
||||
) as (specifier: string) => Promise<unknown>;
|
||||
|
||||
// Memoize the in-flight/loaded module so the dynamic import runs at most once.
|
||||
let markedPromise: Promise<MarkedModule> | null = null;
|
||||
|
||||
/**
|
||||
* Lazily load the ESM-only `marked` module (cached).
|
||||
*
|
||||
* In the built CommonJS package (Node, jest with ts-jest) the `esmImport`
|
||||
* Function trick performs a real dynamic `import()` of the ESM module. Under
|
||||
* vitest, however, the transformed module is evaluated without a dynamic-import
|
||||
* callback, so `new Function('return import(...)')` throws "A dynamic import
|
||||
* callback was not specified"; there `require('marked')` succeeds because the
|
||||
* test runner's loader interops ESM. We therefore try the Function import first
|
||||
* and fall back to `require` so BOTH runtimes resolve `marked` transparently.
|
||||
*/
|
||||
async function loadMarked(): Promise<MarkedModule["marked"]> {
|
||||
if (!markedPromise) {
|
||||
markedPromise = (esmImport("marked") as Promise<MarkedModule>)
|
||||
.catch(() => {
|
||||
// Function-trick import is unavailable (e.g. under vitest's evaluator):
|
||||
// fall back to require, which the test runner can interop for ESM.
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
return require("marked") as MarkedModule;
|
||||
})
|
||||
.catch((err) => {
|
||||
// Do not cache a rejected import — allow the next call to retry.
|
||||
markedPromise = null;
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
return (await markedPromise).marked;
|
||||
}
|
||||
|
||||
// Setup DOM environment for Tiptap HTML parsing in Node.js
|
||||
const dom = new JSDOM("<!DOCTYPE html><html><body></body></html>");
|
||||
@@ -61,6 +110,8 @@ async function preprocessCallouts(markdown: string): Promise<string> {
|
||||
return markdown;
|
||||
}
|
||||
|
||||
const marked = await loadMarked();
|
||||
|
||||
// Recursively transform a slice of lines, converting top-level callouts in
|
||||
// that slice into <div> blocks and rendering their inner content (which may
|
||||
// itself contain nested callouts) through this same function.
|
||||
@@ -290,6 +341,7 @@ function bridgeTaskLists(html: string): string {
|
||||
export async function markdownToProseMirror(
|
||||
markdownContent: string,
|
||||
): Promise<any> {
|
||||
const marked = await loadMarked();
|
||||
const withCallouts = await preprocessCallouts(markdownContent);
|
||||
const html = await marked.parse(withCallouts);
|
||||
const bridged = bridgeTaskLists(html);
|
||||
|
||||
Reference in New Issue
Block a user