Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions server/collab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5713,6 +5713,20 @@ async function seedLegacyDocumentToPersistedYjsAsync(
applyMarksMapDiff(ydoc.getMap('marks'), marks);
}, 'legacy-seed-markdown');
await seedFragmentFromLegacyMarkdown(ydoc, markdown);
// Sync the document row's markdown to the YDoc-derived version so that
// sameAuthoritativeContent() comparisons pass. Without this, API-created
// documents have a permanent mismatch (raw vs Milkdown-normalized markdown)
// that forces mutation_ready: false and breaks share view hydration.
const derivedMarkdown = await deriveMarkdownProjectionFromFragment(ydoc);
if (derivedMarkdown && derivedMarkdown.trim()) {
const derivedMarks = canonicalizeStoredMarks(encodeMarksMap(ydoc.getMap('marks')));
updateDocument(slug, derivedMarkdown, derivedMarks);
// Re-read the row so persistCanonicalYjsBaseline uses the normalized markdown
const updatedRow = getDocumentBySlug(slug);
if (updatedRow) {
return persistCanonicalYjsBaseline(slug, updatedRow, ydoc);
}
}
return persistCanonicalYjsBaseline(slug, row, ydoc);
}

Expand Down
6 changes: 5 additions & 1 deletion server/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
syncCanonicalDocumentStateToCollab,
stripEphemeralCollabSpans,
acquireRewriteLock,
ensureCanonicalYjsBaselineForDocument,
} from './collab.js';
import { getSnapshotPublicUrl, refreshSnapshotForSlug } from './snapshot.js';
import { executeCanonicalRewrite, mutateCanonicalDocument } from './canonical-document.js';
Expand Down Expand Up @@ -784,7 +785,7 @@ function deriveShareCapabilities(role: ShareRole, shareState: string): {
}

// Create a shared document
apiRoutes.post('/documents', (req: Request, res: Response) => {
apiRoutes.post('/documents', async (req: Request, res: Response) => {
const legacyCreateMode = resolveLegacyCreateMode(getPublicBaseUrl(req));
if (legacyCreateMode === 'disabled') {
recordLegacyCreateRouteTelemetry(req, legacyCreateMode, 'blocked_disabled');
Expand Down Expand Up @@ -827,6 +828,9 @@ apiRoutes.post('/documents', (req: Request, res: Response) => {
const ownerSecret = randomUUID();
const normalizedMarks = canonicalizeStoredMarks(marks ?? {});
const doc = createDocument(slug, sanitizedMarkdown, normalizedMarks, title, ownerId, ownerSecret);
// Bootstrap YDoc so the collab system can hydrate the share view.
// Without this, API-created documents have no YDoc and the share view loads empty.
await ensureCanonicalYjsBaselineForDocument(slug);
const defaultAccess = createDocumentAccessToken(slug, 'editor');
const links = buildShareLink(req, doc.slug);
const shareUrlWithToken = withShareToken(links.shareUrl, defaultAccess.secret);
Expand Down