@smartcat/sanity-plugin 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_chunks-cjs/index.cjs +18 -5
- package/dist/_chunks-cjs/index.cjs.map +1 -1
- package/dist/_chunks-cjs/index2.cjs +8 -6
- package/dist/_chunks-cjs/index2.cjs.map +1 -1
- package/dist/_chunks-es/index.js +18 -5
- package/dist/_chunks-es/index.js.map +1 -1
- package/dist/_chunks-es/index2.js +8 -6
- package/dist/_chunks-es/index2.js.map +1 -1
- package/dist/export.cjs +21 -4
- package/dist/export.cjs.map +1 -1
- package/dist/export.d.cts +2 -0
- package/dist/export.d.ts +2 -0
- package/dist/export.js +22 -5
- package/dist/export.js.map +1 -1
- package/dist/index.cjs +156 -39
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +160 -43
- package/dist/index.js.map +1 -1
- package/dist/locjson.d.cts +15 -0
- package/dist/locjson.d.ts +15 -0
- package/dist/progress.d.cts +0 -19
- package/dist/progress.d.ts +0 -19
- package/dist/smartcat.cjs +14 -2
- package/dist/smartcat.cjs.map +1 -1
- package/dist/smartcat.d.cts +10 -1
- package/dist/smartcat.d.ts +10 -1
- package/dist/smartcat.js +14 -2
- package/dist/smartcat.js.map +1 -1
- package/package.json +1 -1
- package/src/actions/AddToProjectAction.tsx +23 -2
- package/src/export/export.test.ts +42 -5
- package/src/export/index.ts +63 -8
- package/src/lib/locjson/filename.ts +7 -2
- package/src/lib/locjson/locjson.test.ts +64 -0
- package/src/lib/locjson/serialize.ts +38 -2
- package/src/progress/core.ts +44 -3
- package/src/progress/progress.test.ts +40 -0
- package/src/schema/translationProject.ts +19 -0
- package/src/smartcat/client.ts +15 -1
- package/src/tool/ProjectView.tsx +108 -44
- package/src/tool/data.ts +7 -0
- package/src/tool/processing.ts +110 -4
|
@@ -55,8 +55,13 @@ function hasConfirmedContent(stages) {
|
|
|
55
55
|
function targetPercent(stages) {
|
|
56
56
|
return stages.length === 0 ? 0 : Math.round(stages.reduce((sum, s) => sum + s.percent, 0) / stages.length);
|
|
57
57
|
}
|
|
58
|
+
function escapeRegExp(value) {
|
|
59
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
60
|
+
}
|
|
58
61
|
function sourceIdPrefix(doc) {
|
|
59
|
-
|
|
62
|
+
let noExt = ((doc.name || doc.filename || doc.fullPath || "").split("/").pop() ?? "").replace(/\.locjson$/i, "");
|
|
63
|
+
const lang = doc.targetLanguage;
|
|
64
|
+
return lang && (noExt = noExt.replace(new RegExp(`\\(${escapeRegExp(lang)}\\)$`), ""), noExt = noExt.replace(new RegExp(`_${escapeRegExp(lang)}$`), "")), noExt.replace(/-draft$/, "").slice(-8);
|
|
60
65
|
}
|
|
61
66
|
function buildProgress(items, scProject, previous, now, mappings) {
|
|
62
67
|
const stageNames = buildStageNameMap(scProject), importedSet = /* @__PURE__ */ new Set(), titleByDoc = /* @__PURE__ */ new Map();
|
|
@@ -65,12 +70,17 @@ function buildProgress(items, scProject, previous, now, mappings) {
|
|
|
65
70
|
for (const target of doc.targets)
|
|
66
71
|
target.imported && importedSet.add(`${doc.sourceDocId}__${target.language}`);
|
|
67
72
|
}
|
|
68
|
-
const itemByPrefix = /* @__PURE__ */ new Map();
|
|
69
|
-
for (const item of items)
|
|
73
|
+
const itemByPrefix = /* @__PURE__ */ new Map(), itemById = /* @__PURE__ */ new Map();
|
|
74
|
+
for (const item of items)
|
|
75
|
+
itemByPrefix.set(stripDraft(item._id).slice(0, 8), item), itemById.set(stripDraft(item._id), item);
|
|
76
|
+
const sourceDocIdByScId = /* @__PURE__ */ new Map();
|
|
77
|
+
for (const doc of previous ?? [])
|
|
78
|
+
for (const target of doc.targets)
|
|
79
|
+
target.smartcatDocumentId && sourceDocIdByScId.set(target.smartcatDocumentId, doc.sourceDocId);
|
|
70
80
|
const byDoc = /* @__PURE__ */ new Map();
|
|
71
81
|
for (const scDoc of scProject.documents ?? []) {
|
|
72
82
|
if (!scDoc.targetLanguage) continue;
|
|
73
|
-
const item = itemByPrefix.get(sourceIdPrefix(scDoc));
|
|
83
|
+
const storedSourceDocId = scDoc.id ? sourceDocIdByScId.get(scDoc.id) : void 0, item = (storedSourceDocId ? itemById.get(stripDraft(storedSourceDocId)) : void 0) ?? itemByPrefix.get(sourceIdPrefix(scDoc));
|
|
74
84
|
if (!item) continue;
|
|
75
85
|
const language = toSanityLanguage(mappings, scDoc.targetLanguage);
|
|
76
86
|
let docProgress = byDoc.get(item._id);
|
|
@@ -89,8 +99,10 @@ function buildProgress(items, scProject, previous, now, mappings) {
|
|
|
89
99
|
syncedAt: now
|
|
90
100
|
});
|
|
91
101
|
}
|
|
92
|
-
const result = [];
|
|
102
|
+
const result = [], seen = /* @__PURE__ */ new Set();
|
|
93
103
|
for (const item of items) {
|
|
104
|
+
if (seen.has(item._id)) continue;
|
|
105
|
+
seen.add(item._id);
|
|
94
106
|
const docProgress = byDoc.get(item._id);
|
|
95
107
|
docProgress && (docProgress.targets.sort((a, b) => a.language.localeCompare(b.language)), result.push(docProgress));
|
|
96
108
|
}
|
|
@@ -170,5 +182,6 @@ exports.stagesFromDocument = stagesFromDocument;
|
|
|
170
182
|
exports.summarizeCompletion = summarizeCompletion;
|
|
171
183
|
exports.targetKey = targetKey;
|
|
172
184
|
exports.targetPercent = targetPercent;
|
|
185
|
+
exports.toSanityLanguage = toSanityLanguage;
|
|
173
186
|
exports.toSmartcatLanguage = toSmartcatLanguage;
|
|
174
187
|
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../../src/lib/languageMap.ts","../../src/lib/projectItems.ts","../../src/progress/core.ts"],"sourcesContent":["/**\n * Sanity ↔ Smartcat language-code mapping.\n *\n * Sanity locale tags (`id`) don't always match the codes Smartcat understands\n * (e.g. Sanity \"PL-PL\" vs Smartcat \"pl\"). The browser resolves the mapping from\n * plugin config and stamps the relevant pairs onto the project; the thin\n * Functions read them back. This module is dependency-free so it bundles into\n * both the Studio and the Functions.\n *\n * The invariant: everything stored in Sanity uses the Sanity `id`; only the\n * Smartcat HTTP calls use `smartcatLanguage`. So callers map forward (id → code)\n * when talking to Smartcat and invert (code → id) when storing what comes back.\n */\n\nexport interface SmartcatLanguageMapping {\n sanityId: string\n smartcatLanguage: string\n}\n\n/** Minimal language shape needed to build mappings (a subset of SmartcatLanguage). */\ninterface MappableLanguage {\n id: string\n smartcatLanguage?: string\n}\n\n/**\n * Builds the mapping pairs for the given Sanity ids, resolving each to its\n * configured `smartcatLanguage` (falling back to the id when not configured).\n */\nexport function buildLanguageMappings(\n languages: MappableLanguage[],\n ids: string[],\n): SmartcatLanguageMapping[] {\n const byId = new Map(languages.map((l) => [l.id, l.smartcatLanguage || l.id]))\n return ids.map((id) => ({sanityId: id, smartcatLanguage: byId.get(id) || id}))\n}\n\n/** Sanity id → Smartcat code. Identity when no mapping is found. */\nexport function toSmartcatLanguage(\n mappings: SmartcatLanguageMapping[] | undefined,\n sanityId: string,\n): string {\n return mappings?.find((m) => m.sanityId === sanityId)?.smartcatLanguage ?? sanityId\n}\n\n/** Smartcat code → Sanity id. Identity when no mapping is found. */\nexport function toSanityLanguage(\n mappings: SmartcatLanguageMapping[] | undefined,\n smartcatLanguage: string,\n): string {\n return mappings?.find((m) => m.smartcatLanguage === smartcatLanguage)?.sanityId ?? smartcatLanguage\n}\n\n/**\n * Finds Smartcat codes that more than one Sanity id maps to. A non-empty result\n * means the inverse (code → id) is ambiguous, so translations can't be routed\n * back — callers should reject such a configuration.\n */\nexport function findDuplicateSmartcatLanguages(\n mappings: SmartcatLanguageMapping[],\n): {smartcatLanguage: string; sanityIds: string[]}[] {\n const byCode = new Map<string, string[]>()\n for (const m of mappings) {\n const ids = byCode.get(m.smartcatLanguage) ?? []\n ids.push(m.sanityId)\n byCode.set(m.smartcatLanguage, ids)\n }\n return [...byCode.entries()]\n .filter(([, ids]) => ids.length > 1)\n .map(([smartcatLanguage, sanityIds]) => ({smartcatLanguage, sanityIds}))\n}\n","/**\n * GROQ fragments for reading a translation project's `items`.\n *\n * An item is either the current `smartcat.projectItem` shape (`{docId, docType}`)\n * or a legacy weak reference (`{_ref}`) from before the migration. These\n * fragments normalize across both so the old/new tolerance lives in one place —\n * interpolate them into item queries rather than re-writing `coalesce(...)`.\n */\n\n/** Normalized item id, evaluated in the item's own scope (inside `items[...]`). */\nexport const ITEM_ID = 'coalesce(docId, _ref)'\n\n/** Normalized item id, evaluated from a subquery nested under an item (via `^`). */\nexport const ITEM_ID_FROM_SUBQUERY = 'coalesce(^.docId, ^._ref)'\n\n/** Dereference an item's document, with an optional projection e.g. `{_id, _type}`. */\nexport const itemDocDeref = (projection = ''): string => `*[_id == ${ITEM_ID_FROM_SUBQUERY}][0]${projection}`\n\n/**\n * Dereference an item's document, honoring where it was added from: items added\n * in the **published** perspective (`sourceIsPublished == true`) resolve the\n * published document only; every other item (including legacy items with no\n * flag) prefers the **draft**, falling back to published. This makes the export\n * translate the content the editor was actually viewing, and gives pre-existing\n * projects the draft-preferring default for free.\n */\nexport const itemDocDerefRespectingDraft = (projection = ''): string =>\n `select(` +\n `sourceIsPublished == true => *[_id == ${ITEM_ID_FROM_SUBQUERY}][0]${projection}, ` +\n `coalesce(*[_id == \"drafts.\" + ${ITEM_ID_FROM_SUBQUERY}][0]${projection}, *[_id == ${ITEM_ID_FROM_SUBQUERY}][0]${projection})` +\n `)`\n","import type {SmartcatProject, SmartcatDocument, RequestLogger} from '../smartcat/types'\nimport {toSanityLanguage, type SmartcatLanguageMapping} from '../lib/languageMap'\nimport {ITEM_ID, itemDocDerefRespectingDraft} from '../lib/projectItems'\nimport type {LogLine} from '../lib/log'\n\n/**\n * Per-document, per-language, per-stage translation progress mirrored from\n * Smartcat onto a `smartcat.translationProject`.\n *\n * This module is intentionally **dependency-free** (no DOM, no @sanity/* runtime)\n * so it bundles cleanly into the thin `smartcat-progress` / `smartcat-import`\n * Functions. All keys are deterministic, so repeated refreshes patch the same\n * array members in place instead of churning `_key`s.\n */\n\nexport interface StageProgress {\n /** Stable key for the Sanity array item (always set by `stagesFromDocument`). */\n _key?: string\n name: string\n /** 0–100, rounded. */\n percent: number\n}\n\nexport interface TargetProgress {\n _key: string\n language: string\n /** Smartcat target document id (`<fileGuid>_<languageNumber>`). */\n smartcatDocumentId?: string\n stages: StageProgress[]\n /** All stages at 100%. */\n complete: boolean\n /** A locale variant has been built from this target (set by the browser). */\n imported: boolean\n syncedAt?: string\n}\n\nexport interface DocProgress {\n _key: string\n sourceDocId: string\n title?: string\n targets: TargetProgress[]\n}\n\n/** Sanitizes an arbitrary string into a stable, Sanity-safe array `_key`. */\nexport function keyify(value: string): string {\n return value.replace(/[^a-zA-Z0-9_-]/g, '-') || 'x'\n}\n\n/** Builds a progress target's `_key` from its document + language. */\nexport function targetKey(sourceDocId: string, language: string): string {\n return keyify(`${sourceDocId}__${language}`)\n}\n\nfunction clampPercent(value: number | undefined): number {\n if (typeof value !== 'number' || Number.isNaN(value)) return 0\n return Math.max(0, Math.min(100, Math.round(value)))\n}\n\n/** Strips a `drafts.` prefix from a Sanity document id. */\nfunction stripDraft(id: string): string {\n return id.replace(/^drafts\\./, '')\n}\n\nfunction titleCase(value: string): string {\n return value ? value.charAt(0).toUpperCase() + value.slice(1) : value\n}\n\n/**\n * Builds a stage-id → display-name map from a project's `workflowStages`. On\n * Smartcat, document-level stages carry only `{id, progress}` — the human name\n * (`stageType`, e.g. \"translation\") lives on the project-level stages.\n */\nexport function buildStageNameMap(scProject: SmartcatProject): Map<string, string> {\n const map = new Map<string, string>()\n ;(scProject.workflowStages ?? []).forEach((stage, i) => {\n if (stage.id) map.set(stage.id, titleCase(stage.stageType || stage.stageName || `Stage ${i + 1}`))\n })\n return map\n}\n\n/** Maps a Smartcat document's workflow stages to display-ready stage progress. */\nexport function stagesFromDocument(\n doc: SmartcatDocument,\n stageNames: Map<string, string>,\n): StageProgress[] {\n const stages = doc.workflowStages ?? []\n return stages.map((stage, i) => ({\n _key: stage.id || `s${i}`,\n name:\n (stage.id && stageNames.get(stage.id)) ||\n titleCase(stage.stageType || stage.stageName || `Stage ${i + 1}`),\n percent: clampPercent(stage.progress),\n }))\n}\n\n/** A target is complete when it has stages and every one is at 100%. */\nexport function isTargetComplete(stages: StageProgress[]): boolean {\n return stages.length > 0 && stages.every((s) => s.percent >= 100)\n}\n\n/**\n * Whether a target has any content confirmed at any stage — i.e. there is\n * something worth importing. Used to gate `mode=confirmed` downloads so we skip\n * targets with nothing confirmed yet (which would otherwise come back as source).\n */\nexport function hasConfirmedContent(stages: StageProgress[]): boolean {\n return stages.some((s) => s.percent > 0)\n}\n\n/** Average completion across a target's stages (0–100). */\nexport function targetPercent(stages: StageProgress[]): number {\n if (stages.length === 0) return 0\n return Math.round(stages.reduce((sum, s) => sum + s.percent, 0) / stages.length)\n}\n\n/** A project item we correlate Smartcat documents back to. */\nexport interface ProjectItem {\n _id: string\n title?: string\n}\n\n/**\n * Extracts the 8-char source-id prefix Smartcat preserves in a document's\n * filename (built as `<title>-<idPrefix>.locjson`, or `<title>-<idPrefix>-draft.locjson`\n * when the content came from the document's draft). Used to correlate a Smartcat\n * document back to a Sanity source document.\n *\n * A trailing `-draft` marker is stripped first, so a draft-sourced file resolves\n * to the same id as its published counterpart. The prefix is then always the\n * final 8 characters before the (marker and) extension, so take the tail rather\n * than splitting on a dash — the title or the id prefix itself can\n * contain dashes (e.g. a custom id like `demo-pdpA` → prefix `demo-pdp`), which\n * would make a last-dash split return the wrong fragment.\n *\n * Only a literal `.locjson` extension is stripped (Smartcat's API often returns\n * the name without it). A generic strip-after-last-dot would eat the tail of any\n * DOTTED title — e.g. micro-copy keys like `aiAssistantModal.resetChat-11AKrV8G`\n * — leaving the wrong 8 chars, so those documents silently never correlate,\n * never import, and re-syncs duplicate them instead of updating.\n */\nexport function sourceIdPrefix(doc: SmartcatDocument): string {\n const base = doc.name || doc.filename || doc.fullPath || ''\n const leaf = base.split('/').pop() ?? ''\n const noExt = leaf.replace(/\\.locjson$/i, '')\n // Draft-sourced files carry a trailing `-draft` marker (informational only, so\n // a project never holds both a document's draft and its published version).\n // Strip it before taking the tail so a draft file correlates to the same source\n // id as its published counterpart — the id prefix is what identifies the item.\n const noDraft = noExt.replace(/-draft$/, '')\n return noDraft.slice(-8)\n}\n\n/**\n * Builds the full progress array from a freshly fetched Smartcat project and the\n * project's items — independent of any export-time skeleton, so it works for\n * projects exported before progress tracking existed.\n *\n * Documents are correlated to items by the 8-char id prefix in their filename;\n * `imported` flags are carried over from the previous progress (by source doc +\n * language) so importing isn't forgotten on refresh.\n *\n * `mappings` invert Smartcat's returned target codes back to Sanity ids, so all\n * stored progress uses Sanity locale tags. When omitted, codes pass through\n * unchanged (identity) — keeping legacy projects working.\n */\nexport function buildProgress(\n items: ProjectItem[],\n scProject: SmartcatProject,\n previous: DocProgress[] | undefined,\n now: string,\n mappings?: SmartcatLanguageMapping[],\n): DocProgress[] {\n const stageNames = buildStageNameMap(scProject)\n\n const importedSet = new Set<string>()\n // Titles are computed Sanity-native in the Studio and shipped on the outbox, so\n // they may not be re-derivable server-side. Carry the previously-stored title\n // forward so a progress sync (which only has the raw `doc.title`) can't clobber\n // it with an \"Untitled\" for types that have no literal title field.\n const titleByDoc = new Map<string, string>()\n for (const doc of previous ?? []) {\n if (doc.title) titleByDoc.set(doc.sourceDocId, doc.title)\n for (const target of doc.targets) {\n if (target.imported) importedSet.add(`${doc.sourceDocId}__${target.language}`)\n }\n }\n\n const itemByPrefix = new Map<string, ProjectItem>()\n for (const item of items) itemByPrefix.set(stripDraft(item._id).slice(0, 8), item)\n\n const byDoc = new Map<string, DocProgress>()\n for (const scDoc of scProject.documents ?? []) {\n if (!scDoc.targetLanguage) continue\n const item = itemByPrefix.get(sourceIdPrefix(scDoc))\n if (!item) continue\n\n // Store the Sanity locale tag, not Smartcat's code.\n const language = toSanityLanguage(mappings, scDoc.targetLanguage)\n\n let docProgress = byDoc.get(item._id)\n if (!docProgress) {\n const title = item.title || titleByDoc.get(item._id)\n docProgress = {_key: targetKey(item._id, 'doc'), sourceDocId: item._id, title, targets: []}\n byDoc.set(item._id, docProgress)\n }\n const stages = stagesFromDocument(scDoc, stageNames)\n docProgress.targets.push({\n _key: targetKey(item._id, language),\n language,\n smartcatDocumentId: scDoc.id,\n stages,\n complete: isTargetComplete(stages),\n imported: importedSet.has(`${item._id}__${language}`),\n syncedAt: now,\n })\n }\n\n // Preserve item order; sort each doc's targets by language for stable display.\n const result: DocProgress[] = []\n for (const item of items) {\n const docProgress = byDoc.get(item._id)\n if (!docProgress) continue\n docProgress.targets.sort((a, b) => a.language.localeCompare(b.language))\n result.push(docProgress)\n }\n return result\n}\n\nexport interface CompletionSummary {\n /** Total (document × language) targets. */\n total: number\n /** Targets whose stages are all at 100%. */\n complete: number\n /** Targets a variant has been built for. */\n imported: number\n /** True when every target is complete and imported (and there is ≥1 target). */\n allDone: boolean\n /** True when at least one stage shows any progress. */\n anyProgress: boolean\n}\n\n/** Summarizes a progress array for status decisions and the dashboard. */\nexport function summarizeCompletion(progress: DocProgress[]): CompletionSummary {\n let total = 0\n let complete = 0\n let imported = 0\n let anyProgress = false\n for (const doc of progress) {\n for (const target of doc.targets) {\n total++\n if (target.complete) complete++\n if (target.imported) imported++\n if (target.stages.some((s) => s.percent > 0)) anyProgress = true\n }\n }\n return {\n total,\n complete,\n imported,\n allDone: total > 0 && complete === total && imported === total,\n anyProgress,\n }\n}\n\n// ---------------------------------------------------------------------------\n// Function-side orchestration: refresh progress from Smartcat\n// ---------------------------------------------------------------------------\n\n/** Minimal structural subset of @sanity/client used by the progress refresh. */\nexport interface SanityLikeClient {\n fetch<T = unknown>(query: string, params?: Record<string, unknown>): Promise<T>\n patch(id: string): {\n set(attrs: Record<string, unknown>): {commit(): Promise<unknown>}\n }\n}\n\n/** Structural subset of SmartcatClient the progress refresh needs. */\nexport interface SmartcatProgressClient {\n getProject(projectId: string): Promise<SmartcatProject>\n /** Optional: trace every HTTP call so a timeout/error reaches the functionLog. */\n setRequestLogger?(logger: RequestLogger): void\n}\n\nexport interface RunProgressSyncOptions {\n sanity: SanityLikeClient\n smartcat: SmartcatProgressClient\n /** `_id` of the `smartcat.translationProject` to refresh. */\n projectId: string\n now?: () => string\n}\n\nexport interface RunProgressSyncResult {\n summary: CompletionSummary\n /** Set when the Smartcat fetch failed (e.g. timed out); the run was recorded, not thrown. */\n error?: string\n}\n\nconst PROGRESS_QUERY = `*[_id == $id][0]{\n _id,\n status,\n smartcatProjectId,\n smartcatLanguages[]{sanityId, smartcatLanguage},\n \"items\": items[]{ \"_id\": ${ITEM_ID}, \"title\": ${itemDocDerefRespectingDraft('.title')} },\n progress[]{_key, sourceDocId, title, targets[]{_key, language, smartcatDocumentId, stages, complete, imported, syncedAt}}\n}`\n\ninterface ProgressProjectData {\n _id: string\n status?: string\n smartcatProjectId?: string\n smartcatLanguages?: SmartcatLanguageMapping[]\n items?: ProjectItemRef[]\n progress?: DocProgress[]\n}\n\n/** A project item as read for correlation: its stored id (always present) plus a\n * best-effort title. `doc` is the legacy shape — some callers still project it. */\ninterface ProjectItemRef {\n _id?: string\n title?: string\n doc?: {_id?: string; title?: string} | null\n}\n\n/**\n * Resolves a project's items into the lightweight shape we correlate on. The id\n * is the item's stored `docId` — NOT a dereferenced document — so an item whose\n * document exists only as a draft (never published) still correlates instead of\n * being silently dropped. Tolerates the legacy `{doc}` projection.\n */\nexport function itemsFromProject(project: {items?: ProjectItemRef[]}): ProjectItem[] {\n const result: ProjectItem[] = []\n for (const item of project.items ?? []) {\n const _id = item?._id ?? item?.doc?._id\n if (!_id) continue\n result.push({_id, title: item?.title ?? item?.doc?.title ?? undefined})\n }\n return result\n}\n\n/**\n * Thin progress step run by the `smartcat-progress` Function: fetches the\n * Smartcat project and mirrors its per-stage progress onto the project's\n * `progress` array. Read-only with respect to Smartcat — it never downloads or\n * creates variants, so it is cheap to run on every dashboard page load.\n */\nexport async function runProgressSync(\n options: RunProgressSyncOptions,\n): Promise<RunProgressSyncResult> {\n const {sanity, smartcat, projectId, now = () => new Date().toISOString()} = options\n\n const project = await sanity.fetch<ProgressProjectData | null>(PROGRESS_QUERY, {id: projectId})\n if (!project) throw new Error(`Translation project ${projectId} not found`)\n if (!project.smartcatProjectId) {\n throw new Error('Project has no smartcatProjectId — export it first')\n }\n\n const timestamp = now()\n\n // Trace HTTP failures into a functionLog so a timeout/error surfaces in the UI.\n // Successful calls are left unlogged: this Function runs on every page load, and\n // functionLog is shared with export/import — writing it on every success would\n // wipe a useful export/import log.\n const log: LogLine[] = []\n smartcat.setRequestLogger?.(({method, path, status, body}) => {\n if (status >= 200 && status < 300) return\n log.push({\n level: 'error',\n message: status ? `Error ${status} while performing ${method} ${path}` : `${method} ${path} did not complete`,\n })\n if (body) log.push({level: 'error', indent: true, message: body})\n })\n\n try {\n const scProject = await smartcat.getProject(project.smartcatProjectId)\n const progress = buildProgress(\n itemsFromProject(project),\n scProject,\n project.progress,\n timestamp,\n project.smartcatLanguages,\n )\n const summary = summarizeCompletion(progress)\n\n const patch: Record<string, unknown> = {progress, progressSyncedAt: timestamp, lastError: null}\n // Gentle one-way nudge: once Smartcat reports any work, reflect it in the badge.\n // Completion remains owned by the import flow.\n if (project.status === 'sent' && summary.anyProgress) {\n patch.status = 'translating'\n }\n\n await sanity.patch(projectId).set(patch).commit()\n\n return {summary}\n } catch (err) {\n // Record the failure and return instead of throwing: a throw would let the\n // platform retry, and a timed-out fetch has nothing transient to gain. Leave\n // the existing `progress` untouched (no zeroing-out) and only stamp the error\n // plus a fresh `progressSyncedAt`, which releases the UI refresh spinner and\n // makes the log land where the dashboard reads it.\n const message = err instanceof Error ? err.message : String(err)\n if (!log.some((l) => l.level === 'error')) log.push({level: 'error', message})\n await sanity\n .patch(projectId)\n .set({lastError: message, functionLog: JSON.stringify(log), progressSyncedAt: timestamp})\n .commit()\n .catch(() => {})\n return {summary: summarizeCompletion(project.progress ?? []), error: message}\n }\n}\n"],"names":[],"mappings":";AA6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M;AAAA,IAAC,CAAC,GACV,EAAC,SAAS,oBAAoB,QAAQ,YAAY,CAAA,CAAE,GAAG,OAAO,QAAA;AAAA,EACvE;AACF;;;;;;;;;;;;;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../../src/lib/languageMap.ts","../../src/lib/projectItems.ts","../../src/progress/core.ts"],"sourcesContent":["/**\n * Sanity ↔ Smartcat language-code mapping.\n *\n * Sanity locale tags (`id`) don't always match the codes Smartcat understands\n * (e.g. Sanity \"PL-PL\" vs Smartcat \"pl\"). The browser resolves the mapping from\n * plugin config and stamps the relevant pairs onto the project; the thin\n * Functions read them back. This module is dependency-free so it bundles into\n * both the Studio and the Functions.\n *\n * The invariant: everything stored in Sanity uses the Sanity `id`; only the\n * Smartcat HTTP calls use `smartcatLanguage`. So callers map forward (id → code)\n * when talking to Smartcat and invert (code → id) when storing what comes back.\n */\n\nexport interface SmartcatLanguageMapping {\n sanityId: string\n smartcatLanguage: string\n}\n\n/** Minimal language shape needed to build mappings (a subset of SmartcatLanguage). */\ninterface MappableLanguage {\n id: string\n smartcatLanguage?: string\n}\n\n/**\n * Builds the mapping pairs for the given Sanity ids, resolving each to its\n * configured `smartcatLanguage` (falling back to the id when not configured).\n */\nexport function buildLanguageMappings(\n languages: MappableLanguage[],\n ids: string[],\n): SmartcatLanguageMapping[] {\n const byId = new Map(languages.map((l) => [l.id, l.smartcatLanguage || l.id]))\n return ids.map((id) => ({sanityId: id, smartcatLanguage: byId.get(id) || id}))\n}\n\n/** Sanity id → Smartcat code. Identity when no mapping is found. */\nexport function toSmartcatLanguage(\n mappings: SmartcatLanguageMapping[] | undefined,\n sanityId: string,\n): string {\n return mappings?.find((m) => m.sanityId === sanityId)?.smartcatLanguage ?? sanityId\n}\n\n/** Smartcat code → Sanity id. Identity when no mapping is found. */\nexport function toSanityLanguage(\n mappings: SmartcatLanguageMapping[] | undefined,\n smartcatLanguage: string,\n): string {\n return mappings?.find((m) => m.smartcatLanguage === smartcatLanguage)?.sanityId ?? smartcatLanguage\n}\n\n/**\n * Finds Smartcat codes that more than one Sanity id maps to. A non-empty result\n * means the inverse (code → id) is ambiguous, so translations can't be routed\n * back — callers should reject such a configuration.\n */\nexport function findDuplicateSmartcatLanguages(\n mappings: SmartcatLanguageMapping[],\n): {smartcatLanguage: string; sanityIds: string[]}[] {\n const byCode = new Map<string, string[]>()\n for (const m of mappings) {\n const ids = byCode.get(m.smartcatLanguage) ?? []\n ids.push(m.sanityId)\n byCode.set(m.smartcatLanguage, ids)\n }\n return [...byCode.entries()]\n .filter(([, ids]) => ids.length > 1)\n .map(([smartcatLanguage, sanityIds]) => ({smartcatLanguage, sanityIds}))\n}\n","/**\n * GROQ fragments for reading a translation project's `items`.\n *\n * An item is either the current `smartcat.projectItem` shape (`{docId, docType}`)\n * or a legacy weak reference (`{_ref}`) from before the migration. These\n * fragments normalize across both so the old/new tolerance lives in one place —\n * interpolate them into item queries rather than re-writing `coalesce(...)`.\n */\n\n/** Normalized item id, evaluated in the item's own scope (inside `items[...]`). */\nexport const ITEM_ID = 'coalesce(docId, _ref)'\n\n/** Normalized item id, evaluated from a subquery nested under an item (via `^`). */\nexport const ITEM_ID_FROM_SUBQUERY = 'coalesce(^.docId, ^._ref)'\n\n/** Dereference an item's document, with an optional projection e.g. `{_id, _type}`. */\nexport const itemDocDeref = (projection = ''): string => `*[_id == ${ITEM_ID_FROM_SUBQUERY}][0]${projection}`\n\n/**\n * Dereference an item's document, honoring where it was added from: items added\n * in the **published** perspective (`sourceIsPublished == true`) resolve the\n * published document only; every other item (including legacy items with no\n * flag) prefers the **draft**, falling back to published. This makes the export\n * translate the content the editor was actually viewing, and gives pre-existing\n * projects the draft-preferring default for free.\n */\nexport const itemDocDerefRespectingDraft = (projection = ''): string =>\n `select(` +\n `sourceIsPublished == true => *[_id == ${ITEM_ID_FROM_SUBQUERY}][0]${projection}, ` +\n `coalesce(*[_id == \"drafts.\" + ${ITEM_ID_FROM_SUBQUERY}][0]${projection}, *[_id == ${ITEM_ID_FROM_SUBQUERY}][0]${projection})` +\n `)`\n","import type {SmartcatProject, SmartcatDocument, RequestLogger} from '../smartcat/types'\nimport {toSanityLanguage, type SmartcatLanguageMapping} from '../lib/languageMap'\nimport {ITEM_ID, itemDocDerefRespectingDraft} from '../lib/projectItems'\nimport type {LogLine} from '../lib/log'\n\n/**\n * Per-document, per-language, per-stage translation progress mirrored from\n * Smartcat onto a `smartcat.translationProject`.\n *\n * This module is intentionally **dependency-free** (no DOM, no @sanity/* runtime)\n * so it bundles cleanly into the thin `smartcat-progress` / `smartcat-import`\n * Functions. All keys are deterministic, so repeated refreshes patch the same\n * array members in place instead of churning `_key`s.\n */\n\nexport interface StageProgress {\n /** Stable key for the Sanity array item (always set by `stagesFromDocument`). */\n _key?: string\n name: string\n /** 0–100, rounded. */\n percent: number\n}\n\nexport interface TargetProgress {\n _key: string\n language: string\n /** Smartcat target document id (`<fileGuid>_<languageNumber>`). */\n smartcatDocumentId?: string\n stages: StageProgress[]\n /** All stages at 100%. */\n complete: boolean\n /** A locale variant has been built from this target (set by the browser). */\n imported: boolean\n syncedAt?: string\n}\n\nexport interface DocProgress {\n _key: string\n sourceDocId: string\n title?: string\n targets: TargetProgress[]\n}\n\n/** Sanitizes an arbitrary string into a stable, Sanity-safe array `_key`. */\nexport function keyify(value: string): string {\n return value.replace(/[^a-zA-Z0-9_-]/g, '-') || 'x'\n}\n\n/** Builds a progress target's `_key` from its document + language. */\nexport function targetKey(sourceDocId: string, language: string): string {\n return keyify(`${sourceDocId}__${language}`)\n}\n\nfunction clampPercent(value: number | undefined): number {\n if (typeof value !== 'number' || Number.isNaN(value)) return 0\n return Math.max(0, Math.min(100, Math.round(value)))\n}\n\n/** Strips a `drafts.` prefix from a Sanity document id. */\nfunction stripDraft(id: string): string {\n return id.replace(/^drafts\\./, '')\n}\n\nfunction titleCase(value: string): string {\n return value ? value.charAt(0).toUpperCase() + value.slice(1) : value\n}\n\n/**\n * Builds a stage-id → display-name map from a project's `workflowStages`. On\n * Smartcat, document-level stages carry only `{id, progress}` — the human name\n * (`stageType`, e.g. \"translation\") lives on the project-level stages.\n */\nexport function buildStageNameMap(scProject: SmartcatProject): Map<string, string> {\n const map = new Map<string, string>()\n ;(scProject.workflowStages ?? []).forEach((stage, i) => {\n if (stage.id) map.set(stage.id, titleCase(stage.stageType || stage.stageName || `Stage ${i + 1}`))\n })\n return map\n}\n\n/** Maps a Smartcat document's workflow stages to display-ready stage progress. */\nexport function stagesFromDocument(\n doc: SmartcatDocument,\n stageNames: Map<string, string>,\n): StageProgress[] {\n const stages = doc.workflowStages ?? []\n return stages.map((stage, i) => ({\n _key: stage.id || `s${i}`,\n name:\n (stage.id && stageNames.get(stage.id)) ||\n titleCase(stage.stageType || stage.stageName || `Stage ${i + 1}`),\n percent: clampPercent(stage.progress),\n }))\n}\n\n/** A target is complete when it has stages and every one is at 100%. */\nexport function isTargetComplete(stages: StageProgress[]): boolean {\n return stages.length > 0 && stages.every((s) => s.percent >= 100)\n}\n\n/**\n * Whether a target has any content confirmed at any stage — i.e. there is\n * something worth importing. Used to gate `mode=confirmed` downloads so we skip\n * targets with nothing confirmed yet (which would otherwise come back as source).\n */\nexport function hasConfirmedContent(stages: StageProgress[]): boolean {\n return stages.some((s) => s.percent > 0)\n}\n\n/** Average completion across a target's stages (0–100). */\nexport function targetPercent(stages: StageProgress[]): number {\n if (stages.length === 0) return 0\n return Math.round(stages.reduce((sum, s) => sum + s.percent, 0) / stages.length)\n}\n\n/** A project item we correlate Smartcat documents back to. */\nexport interface ProjectItem {\n _id: string\n title?: string\n}\n\n/**\n * Extracts the 8-char source-id prefix Smartcat preserves in a document's\n * filename (built as `<title>-<idPrefix>.locjson`, or `<title>-<idPrefix>-draft.locjson`\n * when the content came from the document's draft). Used to correlate a Smartcat\n * document back to a Sanity source document.\n *\n * A trailing `-draft` marker is stripped first, so a draft-sourced file resolves\n * to the same id as its published counterpart. The prefix is then always the\n * final 8 characters before the (marker and) extension, so take the tail rather\n * than splitting on a dash — the title or the id prefix itself can\n * contain dashes (e.g. a custom id like `demo-pdpA` → prefix `demo-pdp`), which\n * would make a last-dash split return the wrong fragment.\n *\n * Only a literal `.locjson` extension is stripped (Smartcat's API often returns\n * the name without it). A generic strip-after-last-dot would eat the tail of any\n * DOTTED title — e.g. micro-copy keys like `aiAssistantModal.resetChat-11AKrV8G`\n * — leaving the wrong 8 chars, so those documents silently never correlate,\n * never import, and re-syncs duplicate them instead of updating.\n */\nfunction escapeRegExp(value: string): string {\n return value.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&')\n}\n\nexport function sourceIdPrefix(doc: SmartcatDocument): string {\n const base = doc.name || doc.filename || doc.fullPath || ''\n const leaf = base.split('/').pop() ?? ''\n let noExt = leaf.replace(/\\.locjson$/i, '')\n // Per-language bilingual files (send-existing flow) are named\n // `<title>-<idPrefix>_<lang>.locjson`, and Smartcat's export echoes a trailing\n // `(<lang>)`. Strip both — keyed to *this* document's known target language, so\n // the language marker doesn't occupy the id-prefix tail. Legacy source-only\n // names (`<title>-<idPrefix>.locjson`) have neither, so are left untouched; the\n // key guard also stops a title that legitimately ends in `_xx`/`(xx)` from being\n // mangled unless `xx` is exactly this doc's target language.\n const lang = doc.targetLanguage\n if (lang) {\n noExt = noExt.replace(new RegExp(`\\\\(${escapeRegExp(lang)}\\\\)$`), '')\n noExt = noExt.replace(new RegExp(`_${escapeRegExp(lang)}$`), '')\n }\n // Draft-sourced files carry a trailing `-draft` marker (informational only, so\n // a project never holds both a document's draft and its published version).\n // Strip it before taking the tail so a draft file correlates to the same source\n // id as its published counterpart — the id prefix is what identifies the item.\n const noDraft = noExt.replace(/-draft$/, '')\n return noDraft.slice(-8)\n}\n\n/**\n * Builds the full progress array from a freshly fetched Smartcat project and the\n * project's items — independent of any export-time skeleton, so it works for\n * projects exported before progress tracking existed.\n *\n * Documents are correlated to items by the 8-char id prefix in their filename;\n * `imported` flags are carried over from the previous progress (by source doc +\n * language) so importing isn't forgotten on refresh.\n *\n * `mappings` invert Smartcat's returned target codes back to Sanity ids, so all\n * stored progress uses Sanity locale tags. When omitted, codes pass through\n * unchanged (identity) — keeping legacy projects working.\n */\nexport function buildProgress(\n items: ProjectItem[],\n scProject: SmartcatProject,\n previous: DocProgress[] | undefined,\n now: string,\n mappings?: SmartcatLanguageMapping[],\n): DocProgress[] {\n const stageNames = buildStageNameMap(scProject)\n\n const importedSet = new Set<string>()\n // Titles are computed Sanity-native in the Studio and shipped on the outbox, so\n // they may not be re-derivable server-side. Carry the previously-stored title\n // forward so a progress sync (which only has the raw `doc.title`) can't clobber\n // it with an \"Untitled\" for types that have no literal title field.\n const titleByDoc = new Map<string, string>()\n for (const doc of previous ?? []) {\n if (doc.title) titleByDoc.set(doc.sourceDocId, doc.title)\n for (const target of doc.targets) {\n if (target.imported) importedSet.add(`${doc.sourceDocId}__${target.language}`)\n }\n }\n\n const itemByPrefix = new Map<string, ProjectItem>()\n const itemById = new Map<string, ProjectItem>()\n for (const item of items) {\n itemByPrefix.set(stripDraft(item._id).slice(0, 8), item)\n itemById.set(stripDraft(item._id), item)\n }\n\n // Rename-proof correlation: recover a document's item from a previously stored\n // `smartcatDocumentId`. The Smartcat document id (`<fileGuid>_<langNum>`) is\n // stable across file renames in Smartcat, whereas its filename (which\n // `sourceIdPrefix` parses) is not. Prefer the stored id; fall back to the\n // filename prefix for documents not yet stored (first sync) or legacy projects.\n const sourceDocIdByScId = new Map<string, string>()\n for (const doc of previous ?? []) {\n for (const target of doc.targets) {\n if (target.smartcatDocumentId) sourceDocIdByScId.set(target.smartcatDocumentId, doc.sourceDocId)\n }\n }\n\n const byDoc = new Map<string, DocProgress>()\n for (const scDoc of scProject.documents ?? []) {\n if (!scDoc.targetLanguage) continue\n const storedSourceDocId = scDoc.id ? sourceDocIdByScId.get(scDoc.id) : undefined\n const item =\n (storedSourceDocId ? itemById.get(stripDraft(storedSourceDocId)) : undefined) ??\n itemByPrefix.get(sourceIdPrefix(scDoc))\n if (!item) continue\n\n // Store the Sanity locale tag, not Smartcat's code.\n const language = toSanityLanguage(mappings, scDoc.targetLanguage)\n\n let docProgress = byDoc.get(item._id)\n if (!docProgress) {\n const title = item.title || titleByDoc.get(item._id)\n docProgress = {_key: targetKey(item._id, 'doc'), sourceDocId: item._id, title, targets: []}\n byDoc.set(item._id, docProgress)\n }\n const stages = stagesFromDocument(scDoc, stageNames)\n docProgress.targets.push({\n _key: targetKey(item._id, language),\n language,\n smartcatDocumentId: scDoc.id,\n stages,\n complete: isTargetComplete(stages),\n imported: importedSet.has(`${item._id}__${language}`),\n syncedAt: now,\n })\n }\n\n // Preserve item order; sort each doc's targets by language for stable display.\n // Dedupe by id: the send-existing flow yields one item per (source × language),\n // so `items` can list a source more than once — its single grouped docProgress\n // must still appear only once.\n const result: DocProgress[] = []\n const seen = new Set<string>()\n for (const item of items) {\n if (seen.has(item._id)) continue\n seen.add(item._id)\n const docProgress = byDoc.get(item._id)\n if (!docProgress) continue\n docProgress.targets.sort((a, b) => a.language.localeCompare(b.language))\n result.push(docProgress)\n }\n return result\n}\n\nexport interface CompletionSummary {\n /** Total (document × language) targets. */\n total: number\n /** Targets whose stages are all at 100%. */\n complete: number\n /** Targets a variant has been built for. */\n imported: number\n /** True when every target is complete and imported (and there is ≥1 target). */\n allDone: boolean\n /** True when at least one stage shows any progress. */\n anyProgress: boolean\n}\n\n/** Summarizes a progress array for status decisions and the dashboard. */\nexport function summarizeCompletion(progress: DocProgress[]): CompletionSummary {\n let total = 0\n let complete = 0\n let imported = 0\n let anyProgress = false\n for (const doc of progress) {\n for (const target of doc.targets) {\n total++\n if (target.complete) complete++\n if (target.imported) imported++\n if (target.stages.some((s) => s.percent > 0)) anyProgress = true\n }\n }\n return {\n total,\n complete,\n imported,\n allDone: total > 0 && complete === total && imported === total,\n anyProgress,\n }\n}\n\n// ---------------------------------------------------------------------------\n// Function-side orchestration: refresh progress from Smartcat\n// ---------------------------------------------------------------------------\n\n/** Minimal structural subset of @sanity/client used by the progress refresh. */\nexport interface SanityLikeClient {\n fetch<T = unknown>(query: string, params?: Record<string, unknown>): Promise<T>\n patch(id: string): {\n set(attrs: Record<string, unknown>): {commit(): Promise<unknown>}\n }\n}\n\n/** Structural subset of SmartcatClient the progress refresh needs. */\nexport interface SmartcatProgressClient {\n getProject(projectId: string): Promise<SmartcatProject>\n /** Optional: trace every HTTP call so a timeout/error reaches the functionLog. */\n setRequestLogger?(logger: RequestLogger): void\n}\n\nexport interface RunProgressSyncOptions {\n sanity: SanityLikeClient\n smartcat: SmartcatProgressClient\n /** `_id` of the `smartcat.translationProject` to refresh. */\n projectId: string\n now?: () => string\n}\n\nexport interface RunProgressSyncResult {\n summary: CompletionSummary\n /** Set when the Smartcat fetch failed (e.g. timed out); the run was recorded, not thrown. */\n error?: string\n}\n\nconst PROGRESS_QUERY = `*[_id == $id][0]{\n _id,\n status,\n smartcatProjectId,\n smartcatLanguages[]{sanityId, smartcatLanguage},\n \"items\": items[]{ \"_id\": ${ITEM_ID}, \"title\": ${itemDocDerefRespectingDraft('.title')} },\n progress[]{_key, sourceDocId, title, targets[]{_key, language, smartcatDocumentId, stages, complete, imported, syncedAt}}\n}`\n\ninterface ProgressProjectData {\n _id: string\n status?: string\n smartcatProjectId?: string\n smartcatLanguages?: SmartcatLanguageMapping[]\n items?: ProjectItemRef[]\n progress?: DocProgress[]\n}\n\n/** A project item as read for correlation: its stored id (always present) plus a\n * best-effort title. `doc` is the legacy shape — some callers still project it. */\ninterface ProjectItemRef {\n _id?: string\n title?: string\n doc?: {_id?: string; title?: string} | null\n}\n\n/**\n * Resolves a project's items into the lightweight shape we correlate on. The id\n * is the item's stored `docId` — NOT a dereferenced document — so an item whose\n * document exists only as a draft (never published) still correlates instead of\n * being silently dropped. Tolerates the legacy `{doc}` projection.\n */\nexport function itemsFromProject(project: {items?: ProjectItemRef[]}): ProjectItem[] {\n const result: ProjectItem[] = []\n for (const item of project.items ?? []) {\n const _id = item?._id ?? item?.doc?._id\n if (!_id) continue\n result.push({_id, title: item?.title ?? item?.doc?.title ?? undefined})\n }\n return result\n}\n\n/**\n * Thin progress step run by the `smartcat-progress` Function: fetches the\n * Smartcat project and mirrors its per-stage progress onto the project's\n * `progress` array. Read-only with respect to Smartcat — it never downloads or\n * creates variants, so it is cheap to run on every dashboard page load.\n */\nexport async function runProgressSync(\n options: RunProgressSyncOptions,\n): Promise<RunProgressSyncResult> {\n const {sanity, smartcat, projectId, now = () => new Date().toISOString()} = options\n\n const project = await sanity.fetch<ProgressProjectData | null>(PROGRESS_QUERY, {id: projectId})\n if (!project) throw new Error(`Translation project ${projectId} not found`)\n if (!project.smartcatProjectId) {\n throw new Error('Project has no smartcatProjectId — export it first')\n }\n\n const timestamp = now()\n\n // Trace HTTP failures into a functionLog so a timeout/error surfaces in the UI.\n // Successful calls are left unlogged: this Function runs on every page load, and\n // functionLog is shared with export/import — writing it on every success would\n // wipe a useful export/import log.\n const log: LogLine[] = []\n smartcat.setRequestLogger?.(({method, path, status, body}) => {\n if (status >= 200 && status < 300) return\n log.push({\n level: 'error',\n message: status ? `Error ${status} while performing ${method} ${path}` : `${method} ${path} did not complete`,\n })\n if (body) log.push({level: 'error', indent: true, message: body})\n })\n\n try {\n const scProject = await smartcat.getProject(project.smartcatProjectId)\n const progress = buildProgress(\n itemsFromProject(project),\n scProject,\n project.progress,\n timestamp,\n project.smartcatLanguages,\n )\n const summary = summarizeCompletion(progress)\n\n const patch: Record<string, unknown> = {progress, progressSyncedAt: timestamp, lastError: null}\n // Gentle one-way nudge: once Smartcat reports any work, reflect it in the badge.\n // Completion remains owned by the import flow.\n if (project.status === 'sent' && summary.anyProgress) {\n patch.status = 'translating'\n }\n\n await sanity.patch(projectId).set(patch).commit()\n\n return {summary}\n } catch (err) {\n // Record the failure and return instead of throwing: a throw would let the\n // platform retry, and a timed-out fetch has nothing transient to gain. Leave\n // the existing `progress` untouched (no zeroing-out) and only stamp the error\n // plus a fresh `progressSyncedAt`, which releases the UI refresh spinner and\n // makes the log land where the dashboard reads it.\n const message = err instanceof Error ? err.message : String(err)\n if (!log.some((l) => l.level === 'error')) log.push({level: 'error', message})\n await sanity\n .patch(projectId)\n .set({lastError: message, functionLog: JSON.stringify(log), progressSyncedAt: timestamp})\n .commit()\n .catch(() => {})\n return {summary: summarizeCompletion(project.progress ?? []), error: message}\n }\n}\n"],"names":[],"mappings":";AA6BO,SAAS,sBACd,WACA,KAC2B;AAC3B,QAAM,OAAO,IAAI,IAAI,UAAU,IAAI,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,oBAAoB,EAAE,EAAE,CAAC,CAAC;AAC7E,SAAO,IAAI,IAAI,CAAC,QAAQ,EAAC,UAAU,IAAI,kBAAkB,KAAK,IAAI,EAAE,KAAK,KAAI;AAC/E;AAGO,SAAS,mBACd,UACA,UACQ;AACR,SAAO,UAAU,KAAK,CAAC,MAAM,EAAE,aAAa,QAAQ,GAAG,oBAAoB;AAC7E;AAGO,SAAS,iBACd,UACA,kBACQ;AACR,SAAO,UAAU,KAAK,CAAC,MAAM,EAAE,qBAAqB,gBAAgB,GAAG,YAAY;AACrF;AAOO,SAAS,+BACd,UACmD;AACnD,QAAM,6BAAa,IAAA;AACnB,aAAW,KAAK,UAAU;AACxB,UAAM,MAAM,OAAO,IAAI,EAAE,gBAAgB,KAAK,CAAA;AAC9C,QAAI,KAAK,EAAE,QAAQ,GACnB,OAAO,IAAI,EAAE,kBAAkB,GAAG;AAAA,EACpC;AACA,SAAO,CAAC,GAAG,OAAO,QAAA,CAAS,EACxB,OAAO,CAAC,CAAA,EAAG,GAAG,MAAM,IAAI,SAAS,CAAC,EAClC,IAAI,CAAC,CAAC,kBAAkB,SAAS,OAAO,EAAC,kBAAkB,UAAA,EAAW;AAC3E;AC5DO,MAAM,UAAU,yBAGV,wBAAwB,6BAGxB,eAAe,CAAC,aAAa,OAAe,YAAY,qBAAqB,OAAO,UAAU,IAU9F,8BAA8B,CAAC,aAAa,OACvD,gDACyC,qBAAqB,OAAO,UAAU,mCAC9C,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A,MACP,OAAO;AAAA,MACP,SAAS,SAAS,SAAS,MAAM,qBAAqB,MAAM,IAAI,IAAI,KAAK,GAAG,MAAM,IAAI,IAAI;AAAA,IAAA,CAC3F,GACG,QAAM,IAAI,KAAK,EAAC,OAAO,SAAS,QAAQ,IAAM,SAAS,KAAA,CAAK;AAAA,EAClE,CAAC;AAED,MAAI;AACF,UAAM,YAAY,MAAM,SAAS,WAAW,QAAQ,iBAAiB,GAC/D,WAAW;AAAA,MACf,iBAAiB,OAAO;AAAA,MACxB;AAAA,MACA,QAAQ;AAAA,MACR;AAAA,MACA,QAAQ;AAAA,IAAA,GAEJ,UAAU,oBAAoB,QAAQ,GAEtC,QAAiC,EAAC,UAAU,kBAAkB,WAAW,WAAW,KAAA;AAG1F,WAAI,QAAQ,WAAW,UAAU,QAAQ,gBACvC,MAAM,SAAS,gBAGjB,MAAM,OAAO,MAAM,SAAS,EAAE,IAAI,KAAK,EAAE,OAAA,GAElC,EAAC,QAAA;AAAA,EACV,SAAS,KAAK;AAMZ,UAAM,UAAU,eAAe,QAAQ,IAAI,UAAU,OAAO,GAAG;AAC/D,WAAK,IAAI,KAAK,CAAC,MAAM,EAAE,UAAU,OAAO,KAAG,IAAI,KAAK,EAAC,OAAO,SAAS,QAAA,CAAQ,GAC7E,MAAM,OACH,MAAM,SAAS,EACf,IAAI,EAAC,WAAW,SAAS,aAAa,KAAK,UAAU,GAAG,GAAG,kBAAkB,UAAA,CAAU,EACvF,OAAA,EACA,MAAM,MAAM;AAAA,IAAC,CAAC,GACV,EAAC,SAAS,oBAAoB,QAAQ,YAAY,CAAA,CAAE,GAAG,OAAO,QAAA;AAAA,EACvE;AACF;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -249,9 +249,9 @@ function htmlToPortableText(html, blockContentType, parseHtml) {
|
|
|
249
249
|
function serializeToLocjson(doc, fields, options) {
|
|
250
250
|
const units = [];
|
|
251
251
|
for (const field of fields) {
|
|
252
|
-
const source = field.localization === "field" ? memberValue(getAtPath(doc, field.path), options.sourceLanguage, options.fieldLanguageKey) : getAtPath(doc, field.path);
|
|
252
|
+
const source = field.localization === "field" ? memberValue(getAtPath(doc, field.path), options.sourceLanguage, options.fieldLanguageKey) : getAtPath(doc, field.path), targetOpt = options.target, targetSource = targetOpt ? field.localization === "field" ? memberValue(getAtPath(doc, field.path), targetOpt.language, options.fieldLanguageKey) : getAtPath(targetOpt.doc, field.path) : void 0;
|
|
253
253
|
for (const leaf of enumerateLeaves(field, source)) {
|
|
254
|
-
const value = leaf.valuePath ? getAtPath(source, leaf.valuePath) : source, comments = leaf.title ? [leaf.title] : void 0;
|
|
254
|
+
const value = leaf.valuePath ? getAtPath(source, leaf.valuePath) : source, targetValue = targetSource === void 0 ? void 0 : leaf.valuePath ? getAtPath(targetSource, leaf.valuePath) : targetSource, comments = leaf.title ? [leaf.title] : void 0;
|
|
255
255
|
if (leaf.kind === "plain") {
|
|
256
256
|
if (typeof value != "string" || value.trim().length === 0) continue;
|
|
257
257
|
units.push({
|
|
@@ -260,7 +260,8 @@ function serializeToLocjson(doc, fields, options) {
|
|
|
260
260
|
// Split into line segments but keep the trailing newline on each, so
|
|
261
261
|
// concatenating the segments (LocJSON's '' join) reconstructs the original.
|
|
262
262
|
source: value.split(new RegExp("(?<=\\n)")),
|
|
263
|
-
|
|
263
|
+
// Existing translation (parallel to source), or empty for Smartcat to fill.
|
|
264
|
+
target: typeof targetValue == "string" && targetValue.length > 0 ? targetValue.split(new RegExp("(?<=\\n)")) : []
|
|
264
265
|
});
|
|
265
266
|
} else {
|
|
266
267
|
const blocks = value, unserializable = findUnserializableBlockTypes(blocks);
|
|
@@ -270,11 +271,12 @@ function serializeToLocjson(doc, fields, options) {
|
|
|
270
271
|
}
|
|
271
272
|
const html = portableTextToHtml(blocks);
|
|
272
273
|
if (!html || portableTextToPlainText(blocks).trim().length === 0) continue;
|
|
274
|
+
const targetBlocks = targetValue, targetHtml = targetSource === void 0 || findUnserializableBlockTypes(targetBlocks).length > 0 || portableTextToPlainText(targetBlocks).trim().length === 0 ? "" : portableTextToHtml(targetBlocks);
|
|
273
275
|
units.push({
|
|
274
276
|
key: leaf.fullKey,
|
|
275
277
|
properties: { comments, "x-smartcat-format": "html", "x-smartcat-split": "full", "x-sanity": { fieldPath: leaf.fullKey } },
|
|
276
278
|
source: [html],
|
|
277
|
-
target: []
|
|
279
|
+
target: targetHtml ? [targetHtml] : []
|
|
278
280
|
});
|
|
279
281
|
}
|
|
280
282
|
}
|
|
@@ -298,8 +300,8 @@ function memberValue(field, language, languageKey) {
|
|
|
298
300
|
)?.value : void 0;
|
|
299
301
|
}
|
|
300
302
|
function buildLocjsonFilename(documentType, documentId, title, options = {}) {
|
|
301
|
-
const idPrefix = stripDraft(documentId).slice(0, 8) || "unknown", safeTitle = sanitizeSegment(title || "Untitled"), safeType = sanitizeSegment(documentType) || "document", draftMarker = options.draft ? "-draft" : "";
|
|
302
|
-
return `${safeType}/${safeTitle}-${idPrefix}${draftMarker}.locjson`;
|
|
303
|
+
const idPrefix = stripDraft(documentId).slice(0, 8) || "unknown", safeTitle = sanitizeSegment(title || "Untitled"), safeType = sanitizeSegment(documentType) || "document", draftMarker = options.draft ? "-draft" : "", langMarker = options.language ? `_${sanitizeSegment(options.language)}` : "";
|
|
304
|
+
return `${safeType}/${safeTitle}-${idPrefix}${draftMarker}${langMarker}.locjson`;
|
|
303
305
|
}
|
|
304
306
|
function stripDraft(id) {
|
|
305
307
|
return id.replace(/^drafts\./, "");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index2.cjs","sources":["../../src/lib/locjson/fields.ts","../../src/lib/locjson/paths.ts","../../src/lib/locjson/portableText.ts","../../src/lib/locjson/serialize.ts","../../src/lib/locjson/filename.ts","../../src/lib/locjson/deserialize.ts"],"sourcesContent":["import type {LocalizationMode, TranslatableField, TranslatableLeaf, ValueShape} from './types'\n\nexport interface GetTranslatableFieldsOptions {\n /**\n * Field names to exclude (e.g. the i18n language field, passed by the caller\n * as the configured `documentI18nPluginLanguageField`). Defaults to none.\n */\n exclude?: string[]\n}\n\ninterface CompiledType {\n jsonType?: string\n name?: string\n title?: string\n of?: CompiledType[]\n fields?: CompiledField[]\n options?: {\n documentInternationalization?: {exclude?: boolean}\n smartcatTranslation?: {include?: boolean}\n }\n}\n\n/**\n * True when a field is marked non-translatable via document-internationalization's\n * own field option (`options.documentInternationalization.exclude`). Honoring it\n * means consumers exclude fields per type using the i18n plugin's native marker —\n * no separate config here.\n */\nfunction isExcludedByDocumentI18n(field: CompiledField): boolean {\n return field.type?.options?.documentInternationalization?.exclude === true\n}\n\ninterface CompiledField {\n name: string\n type?: CompiledType\n}\n\n/**\n * True for a Sanity **system** type — the reserved `sanity.` namespace, notably\n * the `sanity.imageAsset` / `sanity.fileAsset` documents that image/file fields\n * reference. Their string fields (`originalFilename`, `path`, `url`, …) look\n * translatable but must never be sent: assets are shared across locales, and\n * translating a filename or CDN path corrupts the reference. Excluding them here\n * keeps them out of both the linked-document crawl and export serialization,\n * since both decide translatability via {@link getTranslatableFields}.\n */\nexport function isSystemType(typeName: unknown): boolean {\n return typeof typeName === 'string' && typeName.startsWith('sanity.')\n}\n\n/**\n * True for a `sanity-plugin-internationalized-array` array type (e.g.\n * `internationalizedArrayString`). Detected structurally by naming convention so\n * we never need a dependency on that plugin.\n */\nexport function isInternationalizedArrayType(type?: CompiledType): boolean {\n return (\n type?.jsonType === 'array' &&\n typeof type.name === 'string' &&\n type.name.startsWith('internationalizedArray')\n )\n}\n\n/** True for an array member `_type` like `internationalizedArrayStringValue`. */\nexport function isInternationalizedArrayMemberType(typeName: unknown): boolean {\n return typeof typeName === 'string' && /^internationalizedArray.+Value$/.test(typeName)\n}\n\n/** The inner value type of an internationalized-array field (its member's `value` field). */\nexport function innerValueType(arrayType?: CompiledType): CompiledType | undefined {\n return arrayType?.of?.[0]?.fields?.find((f) => f.name === 'value')?.type\n}\n\n/**\n * Localization mode of a **compiled** schema type: `field` if it declares any\n * internationalized-array field (in which case only those fields are\n * translatable), otherwise `document`. A pure function of the type, so callers\n * should compute it once per type.\n */\nexport function localizationMode(compiledType: unknown): LocalizationMode {\n const fields = (compiledType as CompiledType)?.fields\n if (!Array.isArray(fields)) return 'document'\n return fields.some((f) => isInternationalizedArrayType(f.type)) ? 'field' : 'document'\n}\n\n/**\n * Selects translatable fields from a **compiled** Sanity schema type.\n *\n * Document-mode types (default policy, installation-agnostic):\n * - `string` and `text` fields -> plain units\n * - Portable Text (arrays containing `block` members) -> HTML units\n * - everything else (slug, url, datetime, number, boolean, reference, image,\n * non-block arrays, objects, system `_` fields, excluded names) is excluded.\n *\n * Field-mode types (any internationalized-array field present): only the\n * internationalized-array fields are translatable; each is classified by its\n * inner `value` type (string/text -> plain, Portable Text -> HTML).\n *\n * The compiled schema shape isn't strongly typed publicly, so this reads it\n * defensively. Callers that need overrides can post-filter the result.\n */\nexport function getTranslatableFields(\n compiledType: unknown,\n options: GetTranslatableFieldsOptions = {},\n): TranslatableField[] {\n const rootType = compiledType as CompiledType\n if (isSystemType(rootType?.name)) return []\n if (!Array.isArray(rootType?.fields)) return []\n const result: TranslatableField[] = []\n walkFields(rootType, '', localizationMode(rootType), new Set(options.exclude ?? []), result)\n return result\n}\n\n/**\n * Recursively selects translatable fields. In field-mode it descends through\n * plain object fields (so nested internationalized-array fields, e.g.\n * `benefits.items`, are reached) and classifies each i18n field's member value\n * shape. Document-mode keeps the flat top-level behavior.\n */\nfunction walkFields(\n compiledType: CompiledType | undefined,\n prefix: string,\n mode: LocalizationMode,\n exclude: Set<string>,\n out: TranslatableField[],\n): void {\n for (const field of compiledType?.fields ?? []) {\n // `exclude` (the i18n language field) only ever appears at the top level.\n if (!field?.name || field.name.startsWith('_') || (prefix === '' && exclude.has(field.name))) continue\n const forceInclude = field.type?.options?.smartcatTranslation?.include === true\n if (isExcludedByDocumentI18n(field) && !forceInclude) continue\n const path = prefix ? `${prefix}.${field.name}` : field.name\n const title = field.type?.title || titleCase(field.name)\n\n if (mode === 'field') {\n if (isInternationalizedArrayType(field.type)) {\n const inner = innerValueType(field.type)\n const shape = classifyValueShape(inner) ?? (forceInclude ? classifyValueShapeByJsonType(inner) : null)\n if (shape) out.push({path, localization: 'field', kind: shape.container === 'scalar' ? shape.kind : 'plain', title, value: shape})\n else if (forceInclude) warnUntranslatableInclude(field.name, inner)\n } else if (isPlainObject(field.type)) {\n walkFields(field.type, path, mode, exclude, out)\n }\n } else {\n const kind = classifyField(field.type) ?? (forceInclude ? classifyByJsonType(field.type) : null)\n if (kind) out.push({path, localization: 'document', kind, title, value: {container: 'scalar', kind}})\n else if (forceInclude) warnUntranslatableInclude(field.name, field.type)\n }\n }\n}\n\n/** One translatable leaf of a field, expanded against a concrete source value. */\nexport interface FieldLeaf {\n /** Unique unit key / x-sanity fieldPath. */\n fullKey: string\n /** Path within the member value to the leaf (empty = the value itself). */\n valuePath: string\n kind: TranslatableField['kind']\n title?: string\n}\n\n/**\n * Expand a field into its translatable leaves against a concrete source value\n * (the source-language member value for field-mode). Serialize and import both\n * call this with the SAME source value, so the unit keys they compute match.\n */\nexport function enumerateLeaves(field: TranslatableField, sourceValue: unknown): FieldLeaf[] {\n const base = field.path\n const shape: ValueShape = field.value ?? {container: 'scalar', kind: field.kind}\n if (shape.container === 'scalar') {\n return [{fullKey: base, valuePath: '', kind: shape.kind, title: field.title}]\n }\n if (shape.container === 'arrayOfScalar') {\n const arr = Array.isArray(sourceValue) ? sourceValue : []\n return arr.map((_, i) => ({fullKey: `${base}[${i}]`, valuePath: `[${i}]`, kind: shape.kind, title: field.title}))\n }\n if (shape.container === 'object') {\n return shape.leaves.map((l) => ({fullKey: `${base}.${l.subPath}`, valuePath: l.subPath, kind: l.kind, title: l.title ?? field.title}))\n }\n const arr = Array.isArray(sourceValue) ? sourceValue : []\n const out: FieldLeaf[] = []\n for (const item of arr) {\n const key = (item as Record<string, unknown>)?._key\n if (typeof key !== 'string') continue\n for (const l of shape.leaves) {\n const valuePath = l.subPath ? `[${key}].${l.subPath}` : `[${key}]`\n out.push({fullKey: `${base}${valuePath}`, valuePath, kind: l.kind, title: l.title ?? field.title})\n }\n }\n return out\n}\n\n/** A recursable plain object (not a reference/image/file/slug/geopoint). */\nfunction isPlainObject(type?: CompiledType): boolean {\n if (type?.jsonType !== 'object' || !Array.isArray(type.fields)) return false\n if (type.fields.some((f) => f.name === '_ref')) return false\n return !['reference', 'image', 'file', 'slug', 'geopoint'].includes(type.name ?? '')\n}\n\n/** Classify an internationalized-array member's `value` type into a ValueShape. */\nfunction classifyValueShape(inner?: CompiledType): ValueShape | null {\n if (!inner) return null\n const scalar = classifyField(inner)\n if (scalar) return {container: 'scalar', kind: scalar}\n if (inner.jsonType === 'array' && Array.isArray(inner.of)) {\n const member = inner.of[0]\n if (member?.jsonType === 'string' && (member.name === 'string' || member.name === 'text')) {\n return {container: 'arrayOfScalar', kind: 'plain'}\n }\n if (isPlainObject(member)) {\n const leaves = collectLeaves(member, '')\n if (leaves.length) return {container: 'arrayOfObject', leaves}\n }\n return null\n }\n if (isPlainObject(inner)) {\n const leaves = collectLeaves(inner, '')\n if (leaves.length) return {container: 'object', leaves}\n }\n return null\n}\n\n/** ValueShape by jsonType only (for `smartcatTranslation.include` overrides). */\nfunction classifyValueShapeByJsonType(inner?: CompiledType): ValueShape | null {\n const kind = classifyByJsonType(inner)\n return kind ? {container: 'scalar', kind} : null\n}\n\n/** Translatable string/text/Portable-Text leaves within an object (recursing plain objects). */\nfunction collectLeaves(objType: CompiledType | undefined, prefix: string): TranslatableLeaf[] {\n const leaves: TranslatableLeaf[] = []\n for (const f of objType?.fields ?? []) {\n if (!f?.name || f.name.startsWith('_')) continue\n if (isExcludedByDocumentI18n(f) && f.type?.options?.smartcatTranslation?.include !== true) continue\n const subPath = prefix ? `${prefix}.${f.name}` : f.name\n const kind = classifyField(f.type)\n if (kind) leaves.push({subPath, kind, title: f.type?.title || titleCase(f.name)})\n else if (isPlainObject(f.type)) leaves.push(...collectLeaves(f.type, subPath))\n }\n return leaves\n}\n\n/** True for an array whose members include a Portable Text `block` type. */\nfunction isPortableTextArray(type: CompiledType): boolean {\n return (\n type.jsonType === 'array' &&\n Array.isArray(type.of) &&\n type.of.some((member) => member?.name === 'block' || member?.jsonType === 'block')\n )\n}\n\nfunction classifyField(type?: CompiledType): TranslatableField['kind'] | null {\n if (!type) return null\n\n // Plain text: only the `string` and `text` base types (not url, slug, datetime…).\n if (type.jsonType === 'string' && (type.name === 'string' || type.name === 'text')) {\n return 'plain'\n }\n\n if (isPortableTextArray(type)) return 'portableText'\n\n return null\n}\n\n/**\n * Classify by base `jsonType` only, ignoring the type name. Used for fields\n * explicitly opted in with `smartcatTranslation.include`, where the name isn't in\n * the default allowlist (e.g. a custom string alias) but the underlying shape is\n * still translatable.\n */\nfunction classifyByJsonType(type?: CompiledType): TranslatableField['kind'] | null {\n if (!type) return null\n if (type.jsonType === 'string') return 'plain'\n if (isPortableTextArray(type)) return 'portableText'\n return null\n}\n\nfunction warnUntranslatableInclude(fieldName: string, type?: CompiledType): void {\n // eslint-disable-next-line no-console\n console.warn(\n `[smartcat-translation] Field \"${fieldName}\" is marked smartcatTranslation.include ` +\n `but its type (name: ${JSON.stringify(type?.name)}, jsonType: ${JSON.stringify(type?.jsonType)}) ` +\n `has no translatable representation; skipping.`,\n )\n}\n\nfunction titleCase(name: string): string {\n return name.replace(/([A-Z])/g, ' $1').replace(/^./, (c) => c.toUpperCase())\n}\n\n/**\n * Selects translatable fields from a document's **data** (no schema needed) —\n * for use in Sanity Functions, which run server-side without the Studio schema.\n *\n * Applies the same homogeneity rule as {@link getTranslatableFields}: if any\n * field holds internationalized-array members, the document is field-mode and\n * only those fields are selected (classified by a sample member's `value`);\n * otherwise top-level `string`/Portable Text fields are selected (document-mode).\n */\nexport function inferTranslatableFields(\n doc: Record<string, unknown>,\n options: GetTranslatableFieldsOptions = {},\n): TranslatableField[] {\n if (isSystemType(doc._type)) return []\n const exclude = new Set(options.exclude ?? [])\n\n const isI18nArray = (value: unknown): value is Array<{value?: unknown}> =>\n Array.isArray(value) &&\n value.some((m) => m && typeof m === 'object' && isInternationalizedArrayMemberType((m as {_type?: unknown})._type))\n\n const entries = Object.entries(doc).filter(([key]) => !key.startsWith('_') && !exclude.has(key))\n const fieldMode = entries.some(([, value]) => isI18nArray(value))\n\n const result: TranslatableField[] = []\n for (const [key, value] of entries) {\n if (fieldMode) {\n if (!isI18nArray(value)) continue\n const sample = value.find((m) => m && typeof m === 'object')?.value\n const kind = sampleKind(sample)\n if (kind) result.push({path: key, kind, localization: 'field', title: titleCase(key)})\n } else if (typeof value === 'string') {\n if (value.length > 0) result.push({path: key, kind: 'plain', localization: 'document', title: titleCase(key)})\n } else if (Array.isArray(value) && value.some(isPortableTextBlock)) {\n result.push({path: key, kind: 'portableText', localization: 'document', title: titleCase(key)})\n }\n }\n return result\n}\n\nfunction sampleKind(value: unknown): TranslatableField['kind'] | null {\n if (typeof value === 'string') return 'plain'\n if (Array.isArray(value) && value.some(isPortableTextBlock)) return 'portableText'\n return null\n}\n\nfunction isPortableTextBlock(value: unknown): boolean {\n return Boolean(value && typeof value === 'object' && (value as {_type?: string})._type === 'block')\n}\n\n/**\n * Reorders fields to match `orderedPaths` (e.g. a schema's field order). Fields\n * not listed keep their original relative order and go last. Stable.\n */\nexport function orderFieldsBy(\n fields: TranslatableField[],\n orderedPaths: string[] | undefined,\n): TranslatableField[] {\n if (!orderedPaths || orderedPaths.length === 0) return fields\n const rank = new Map(orderedPaths.map((path, i) => [path, i]))\n const at = (path: string) => (rank.has(path) ? (rank.get(path) as number) : Number.MAX_SAFE_INTEGER)\n return fields\n .map((field, i) => ({field, i}))\n .sort((a, b) => at(a.field.path) - at(b.field.path) || a.i - b.i)\n .map((x) => x.field)\n}\n","/**\n * Tiny path grammar for addressing translatable leaves nested inside a field's\n * value: `.field` for object properties, `[key]` for array members by `_key`,\n * `[n]` for array members by index. Used to expand array/object member values\n * into per-leaf LocJSON units and to overlay translations back onto a cloned\n * source value.\n *\n * e.g. `items[b1].value`, `tabs[2]`, `` (empty = the value itself).\n */\nexport type PathSeg = {field: string} | {key: string} | {index: number}\n\nexport function parsePath(path: string): PathSeg[] {\n const segs: PathSeg[] = []\n const re = /\\.?([^.[\\]]+)|\\[([^\\]]+)\\]/g\n let m: RegExpExecArray | null\n while ((m = re.exec(path)) !== null) {\n if (m[1] !== undefined) segs.push({field: m[1]})\n else if (/^\\d+$/.test(m[2])) segs.push({index: Number(m[2])})\n else segs.push({key: m[2]})\n }\n return segs\n}\n\n/** Append a segment to a base path string, producing a valid path. */\nexport function joinPath(base: string, seg: PathSeg): string {\n if ('field' in seg) return base ? `${base}.${seg.field}` : seg.field\n if ('index' in seg) return `${base}[${seg.index}]`\n return `${base}[${seg.key}]`\n}\n\nfunction child(container: unknown, seg: PathSeg): unknown {\n if (container == null || typeof container !== 'object') return undefined\n if ('field' in seg) return (container as Record<string, unknown>)[seg.field]\n if ('index' in seg) return Array.isArray(container) ? container[seg.index] : undefined\n return Array.isArray(container)\n ? container.find((m) => m && typeof m === 'object' && (m as Record<string, unknown>)._key === seg.key)\n : undefined\n}\n\n/** Read the value at a path from a root value (undefined if any segment misses). */\nexport function getAtPath(root: unknown, path: string): unknown {\n let cur = root\n for (const seg of parsePath(path)) {\n cur = child(cur, seg)\n if (cur === undefined) return undefined\n }\n return cur\n}\n\n/**\n * Return a deep clone of `root` with the leaf at `path` replaced by `value`.\n * An empty path replaces the whole value. Missing intermediate segments are a\n * no-op (the clone is returned unchanged), so a template that lacks a leaf can't\n * be corrupted.\n */\nexport function setAtPath<T>(root: T, path: string, value: unknown): T {\n const segs = parsePath(path)\n if (segs.length === 0) return value as T\n const clone = structuredClone(root)\n let cur: unknown = clone\n for (let i = 0; i < segs.length - 1; i++) {\n cur = child(cur, segs[i])\n if (cur == null || typeof cur !== 'object') return clone\n }\n const last = segs[segs.length - 1]\n if ('field' in last) (cur as Record<string, unknown>)[last.field] = value\n else if ('index' in last && Array.isArray(cur)) cur[last.index] = value\n else if ('key' in last && Array.isArray(cur)) {\n const idx = cur.findIndex((m) => m && typeof m === 'object' && (m as Record<string, unknown>)._key === last.key)\n if (idx >= 0) cur[idx] = value\n }\n return clone\n}\n","import {escapeHTML, toHTML} from '@portabletext/to-html'\nimport {htmlToBlocks, type DeserializerRule} from '@sanity/block-tools'\n\n/** A Portable Text block (loosely typed — shape varies by schema). */\nexport type PortableTextBlock = Record<string, unknown>\n\n/**\n * Custom (non-`block`) top-level Portable Text types that we *can* round-trip\n * through HTML, and so must NOT treat as unserializable. Each needs both a\n * `toHTML` component (below) and an `htmlToBlocks` rule (see {@link tableRule})\n * so the block survives export → translation → import intact.\n *\n * Currently just `@sanity/table`: `{_type:'table', rows:[{_type:'tableRow',\n * cells: string[]}]}` — plain-string cells, no header row, no col/row spans.\n */\nconst SERIALIZABLE_CUSTOM_TYPES = new Set(['table'])\n\n/** Renders a `@sanity/table` block to an HTML `<table>` for translation. */\nfunction tableToHtml(value: unknown): string {\n const rows = Array.isArray((value as {rows?: unknown})?.rows) ? (value as {rows: unknown[]}).rows : []\n const trs = rows\n .map((row) => {\n const cells = Array.isArray((row as {cells?: unknown})?.cells) ? (row as {cells: unknown[]}).cells : []\n const tds = cells.map((cell) => `<td>${escapeHTML(typeof cell === 'string' ? cell : '')}</td>`).join('')\n return `<tr>${tds}</tr>`\n })\n .join('')\n return `<table><tbody>${trs}</tbody></table>`\n}\n\n/**\n * An `htmlToBlocks` rule that turns a translated `<table>` back into a\n * `@sanity/table` block. Rows get deterministic index-based `_key`s (the whole\n * field value is replaced on import, so keys need only be unique within the\n * table); cells are plain strings, matching the schema.\n */\nconst tableRule: DeserializerRule = {\n deserialize(el, _next, createBlock) {\n if ((el as Element).tagName?.toLowerCase() !== 'table') return undefined\n const rows = Array.from((el as Element).querySelectorAll('tr')).map((tr, i) => ({\n _type: 'tableRow',\n _key: `row${i}`,\n cells: Array.from(tr.querySelectorAll('td, th')).map((cell) => cell.textContent ?? ''),\n }))\n return createBlock({_type: 'table', rows}) as never\n },\n}\n\n/** Parses an HTML string into a DOM Document. Provided by the environment. */\nexport type ParseHtml = (html: string) => Document\n\n/**\n * Renders Portable Text to an HTML string for translation.\n * Whole-field: the entire block array becomes one HTML document fragment.\n */\nexport function portableTextToHtml(blocks: PortableTextBlock[] | undefined | null): string {\n if (!blocks || blocks.length === 0) return ''\n return toHTML(blocks as never, {\n // Fallback only. Serialization skips any field containing an unserializable\n // block up front (see findUnserializableBlockTypes), so to-html should never\n // meet an unknown type here. If one slips through, render it as empty rather\n // than let @portabletext/to-html emit its default placeholder — a hidden\n // `<div style=\"display:none\">Unknown block type \"…\"</div>` whose English text\n // would otherwise be sent to Smartcat and translated into every locale.\n // (`onMissingComponent: false` only silences the warning; it does NOT stop the\n // placeholder — the `unknownType` override is what suppresses it.)\n onMissingComponent: false,\n components: {\n unknownType: () => '',\n // Serializable custom block types get real markup so their text is\n // translatable and can be parsed back (see SERIALIZABLE_CUSTOM_TYPES).\n types: {table: ({value}) => tableToHtml(value)},\n },\n })\n}\n\n/**\n * Distinct `_type`s in a Portable Text value that `@portabletext/to-html` cannot\n * serialize: any top-level member that isn't a standard `block` (and isn't in\n * {@link SERIALIZABLE_CUSTOM_TYPES}, e.g. `table`, which we render explicitly),\n * or any inline child that isn't a `span` (e.g. an inline object). Such nodes\n * render to nothing, so a field containing them can't survive the HTML\n * round-trip — the caller skips the whole field and warns rather than silently\n * drop or corrupt it. Marks/annotations are intentionally not included: their\n * text survives (only the annotation would be lost).\n */\nexport function findUnserializableBlockTypes(\n blocks: PortableTextBlock[] | undefined | null,\n): string[] {\n if (!Array.isArray(blocks)) return []\n const types = new Set<string>()\n for (const block of blocks) {\n if (!block || typeof block !== 'object') continue\n const type = (block as {_type?: unknown})._type\n if (type !== 'block') {\n if (typeof type === 'string' && !SERIALIZABLE_CUSTOM_TYPES.has(type)) types.add(type)\n continue\n }\n const children = (block as {children?: unknown}).children\n if (!Array.isArray(children)) continue\n for (const child of children) {\n const childType = (child as {_type?: unknown} | null)?._type\n if (typeof childType === 'string' && childType !== 'span') types.add(childType)\n }\n }\n return [...types].sort()\n}\n\n/**\n * Extracts the visible text of Portable Text — the concatenation of every span's\n * `text` across all blocks (recursing into `children`). Used to decide whether a\n * field carries anything translatable: structural-but-textless blocks render to\n * non-empty HTML (`<p></p>`, `<br/>`, `<ul><li></li></ul>`), so emptiness must be\n * judged from the source blocks, not from stripping tags/entities out of the HTML\n * (which would mishandle ` ` and void elements).\n */\nexport function portableTextToPlainText(blocks: PortableTextBlock[] | undefined | null): string {\n if (!Array.isArray(blocks)) return ''\n let text = ''\n const visit = (node: unknown): void => {\n if (Array.isArray(node)) {\n node.forEach(visit)\n } else if (node && typeof node === 'object') {\n const value = node as {text?: unknown; children?: unknown; rows?: unknown}\n if (typeof value.text === 'string') text += value.text\n if (Array.isArray(value.children)) visit(value.children)\n // `table` blocks carry text in `rows[].cells[]` (plain strings), not spans;\n // count it so a table-only field isn't judged empty and skipped on export.\n if (Array.isArray(value.rows)) {\n for (const row of value.rows) {\n const cells = (row as {cells?: unknown})?.cells\n if (Array.isArray(cells)) for (const cell of cells) if (typeof cell === 'string') text += cell\n }\n }\n }\n }\n visit(blocks)\n return text\n}\n\n/**\n * Parses translated HTML back into Portable Text using the field's compiled\n * block content type. `parseHtml` supplies a DOM parser:\n * - Studio: (html) => new DOMParser().parseFromString(html, 'text/html')\n * - Functions: (html) => new JSDOM(html).window.document\n */\nexport function htmlToPortableText(\n html: string,\n blockContentType: unknown,\n parseHtml: ParseHtml,\n): PortableTextBlock[] {\n if (!html.trim()) return []\n return htmlToBlocks(html, blockContentType as never, {\n parseHtml,\n rules: [tableRule],\n }) as unknown as PortableTextBlock[]\n}\n","import {\n findUnserializableBlockTypes,\n portableTextToHtml,\n portableTextToPlainText,\n type PortableTextBlock,\n} from './portableText'\nimport {enumerateLeaves} from './fields'\nimport {getAtPath} from './paths'\nimport type {LocJSON, LocUnit, TranslatableField} from './types'\n\nexport interface SerializeOptions {\n sourceLanguage: string\n /**\n * How internationalized-array members identify their locale: a field name\n * (e.g. `'language'`) or `'_key'` (v4.x and lower). Resolved by the caller (see\n * `resolveConfig`); no default is assumed here.\n */\n fieldLanguageKey: string\n /**\n * Called when a Portable Text field is skipped because it contains block types\n * that can't be serialized (see {@link findUnserializableBlockTypes}). The\n * whole field is left out so its content isn't dropped or corrupted on the\n * round-trip; the caller surfaces this to the user's log.\n */\n onSkippedField?: (info: {fieldPath: string; title?: string; types: string[]}) => void\n}\n\n/** A minimal view of a Sanity document needed for serialization. */\nexport interface SerializableDocument {\n _id: string\n _type: string\n _rev?: string\n [key: string]: unknown\n}\n\n/**\n * Serializes a Sanity document into a LocJSON file: one unit per translatable\n * field. Plain fields become plain units (segments split on newlines); Portable\n * Text fields become whole-field HTML units (Smartcat segments them internally).\n *\n * Fields with no value are skipped, so the file only contains real source text.\n */\nexport function serializeToLocjson(\n doc: SerializableDocument,\n fields: TranslatableField[],\n options: SerializeOptions,\n): LocJSON {\n const units: LocUnit[] = []\n\n for (const field of fields) {\n // For field-level (internationalized-array) fields the source content is the\n // source-language member's `value` (the field may be nested through objects).\n const source =\n field.localization === 'field'\n ? memberValue(getAtPath(doc, field.path), options.sourceLanguage, options.fieldLanguageKey)\n : getAtPath(doc, field.path)\n\n // A scalar field yields one leaf; array/object member values yield one leaf\n // per translatable string inside (addressed by valuePath).\n for (const leaf of enumerateLeaves(field, source)) {\n const value = leaf.valuePath ? getAtPath(source, leaf.valuePath) : source\n const comments = leaf.title ? [leaf.title] : undefined\n\n if (leaf.kind === 'plain') {\n // Skip when there's no translatable text. Trim only for the check — the\n // stored source keeps its original whitespace so the round-trip is exact.\n if (typeof value !== 'string' || value.trim().length === 0) continue\n units.push({\n key: leaf.fullKey,\n properties: {comments, 'x-smartcat-split': 'full', 'x-sanity': {fieldPath: leaf.fullKey}},\n // Split into line segments but keep the trailing newline on each, so\n // concatenating the segments (LocJSON's '' join) reconstructs the original.\n source: value.split(/(?<=\\n)/),\n target: [],\n })\n } else {\n const blocks = value as PortableTextBlock[] | undefined\n // A field with a block to-html can't serialize (e.g. a custom `table`)\n // is skipped whole: partial HTML would drop that block on the round-trip.\n const unserializable = findUnserializableBlockTypes(blocks)\n if (unserializable.length > 0) {\n options.onSkippedField?.({fieldPath: field.path, title: field.title, types: unserializable})\n continue\n }\n // `html` is non-empty for structural-but-textless blocks (e.g. `<p></p>`,\n // `<br/>`), so also require actual visible text before emitting a unit.\n const html = portableTextToHtml(blocks)\n if (!html || portableTextToPlainText(blocks).trim().length === 0) continue\n units.push({\n key: leaf.fullKey,\n properties: {comments, 'x-smartcat-format': 'html', 'x-smartcat-split': 'full', 'x-sanity': {fieldPath: leaf.fullKey}},\n source: [html],\n target: [],\n })\n }\n }\n }\n\n return {\n properties: {\n version: 1,\n 'x-sanity': {\n documentId: doc._id,\n documentType: doc._type,\n rev: doc._rev,\n sourceLanguage: options.sourceLanguage,\n },\n },\n units,\n }\n}\n\n/**\n * Reads the `value` of an internationalized-array field's member for a given\n * language. Members are identified strictly by `languageKey` — a field name\n * (`'language'`, v5+) or `'_key'` (v4.x and lower).\n */\nfunction memberValue(field: unknown, language: string, languageKey: string): unknown {\n if (!Array.isArray(field)) return undefined\n const member = field.find(\n (m) => m && typeof m === 'object' && (m as Record<string, unknown>)[languageKey] === language,\n )\n return (member as {value?: unknown} | undefined)?.value\n}\n","/**\n * Builds the Smartcat document filename for a Sanity document:\n * \"<type>/<sanitized title>-<id prefix>.locjson\"\n * e.g. \"page/About Us-3f14f092.locjson\".\n *\n * The type acts as a folder; the id prefix guarantees uniqueness. Identity on\n * import comes from the file's `x-sanity` metadata, not this name, so renames\n * are safe. When the exported content came from the document's draft, a `-draft`\n * marker is appended so it's visible in Smartcat (`…-3f14f092-draft.locjson`).\n */\nexport function buildLocjsonFilename(\n documentType: string,\n documentId: string,\n title?: string,\n options: {draft?: boolean} = {},\n): string {\n const idPrefix = stripDraft(documentId).slice(0, 8) || 'unknown'\n const safeTitle = sanitizeSegment(title || 'Untitled')\n const safeType = sanitizeSegment(documentType) || 'document'\n const draftMarker = options.draft ? '-draft' : ''\n return `${safeType}/${safeTitle}-${idPrefix}${draftMarker}.locjson`\n}\n\nfunction stripDraft(id: string): string {\n return id.replace(/^drafts\\./, '')\n}\n\n/** Removes characters unsafe for file paths while keeping it human-readable. */\nfunction sanitizeSegment(value: string): string {\n return value\n .replace(/[/\\\\?%*:|\"<>]/g, ' ') // path-unsafe characters\n .replace(/\\s+/g, ' ')\n .trim()\n}\n","import {htmlToPortableText, type ParseHtml} from './portableText'\nimport type {LocJSON} from './types'\n\nexport interface DeserializeOptions {\n /** DOM parser for HTML units (DOMParser in Studio, jsdom in Functions). */\n parseHtml: ParseHtml\n /** Returns the compiled block content type for a Portable Text field path. */\n getBlockContentType: (fieldPath: string) => unknown\n}\n\nexport interface DeserializedDocument {\n documentId?: string\n documentType?: string\n sourceLanguage?: string\n /** Translated field values, keyed by field path. */\n fields: Record<string, unknown>\n /**\n * Source (pre-translation) text per field path, as the raw joined string. Used\n * to decide whether a field is JSON (so its translation can be validated before\n * import). Always the string form, even for HTML units.\n */\n sources: Record<string, string>\n}\n\n/**\n * Parses a (translated) LocJSON file back into Sanity field values.\n *\n * Uses each unit's `target` (falling back to `source` when a unit hasn't been\n * translated yet, so the result is always coherent). HTML units are converted\n * back to Portable Text via the field's compiled block content type.\n */\nexport function deserializeFromLocjson(locjson: LocJSON, options: DeserializeOptions): DeserializedDocument {\n const fileMeta = locjson.properties?.['x-sanity']\n const fields: Record<string, unknown> = {}\n const sources: Record<string, string> = {}\n\n for (const unit of locjson.units ?? []) {\n const fieldPath = unit.properties?.['x-sanity']?.fieldPath ?? unit.key\n const text = (unit.target?.length ? unit.target : unit.source) ?? []\n\n // LocJSON splits a string into arbitrary chunks with no implied delimiter,\n // so the original is always reconstructed by concatenating the segments.\n const value = text.join('')\n sources[fieldPath] = (unit.source ?? []).join('')\n\n if (unit.properties?.['x-smartcat-format'] === 'html') {\n fields[fieldPath] = htmlToPortableText(value, options.getBlockContentType(fieldPath), options.parseHtml)\n } else {\n fields[fieldPath] = value\n }\n }\n\n return {\n documentId: fileMeta?.documentId,\n documentType: fileMeta?.documentType,\n sourceLanguage: fileMeta?.sourceLanguage,\n fields,\n sources,\n }\n}\n"],"names":["escapeHTML","toHTML","child","htmlToBlocks"],"mappings":";;AA4BA,SAAS,yBAAyB,OAA+B;AAC/D,SAAO,MAAM,MAAM,SAAS,8BAA8B,YAAY;AACxE;AAgBO,SAAS,aAAa,UAA4B;AACvD,SAAO,OAAO,YAAa,YAAY,SAAS,WAAW,SAAS;AACtE;AAOO,SAAS,6BAA6B,MAA8B;AACzE,SACE,MAAM,aAAa,WACnB,OAAO,KAAK,QAAS,YACrB,KAAK,KAAK,WAAW,wBAAwB;AAEjD;AAGO,SAAS,mCAAmC,UAA4B;AAC7E,SAAO,OAAO,YAAa,YAAY,kCAAkC,KAAK,QAAQ;AACxF;AAGO,SAAS,eAAe,WAAoD;AACjF,SAAO,WAAW,KAAK,CAAC,GAAG,QAAQ,KAAK,CAAC,MAAM,EAAE,SAAS,OAAO,GAAG;AACtE;AAQO,SAAS,iBAAiB,cAAyC;AACxE,QAAM,SAAU,cAA+B;AAC/C,SAAK,MAAM,QAAQ,MAAM,KAClB,OAAO,KAAK,CAAC,MAAM,6BAA6B,EAAE,IAAI,CAAC,IAAI,UAD/B;AAErC;AAkBO,SAAS,sBACd,cACA,UAAwC,IACnB;AACrB,QAAM,WAAW;AACjB,MAAI,aAAa,UAAU,IAAI,UAAU,CAAA;AACzC,MAAI,CAAC,MAAM,QAAQ,UAAU,MAAM,UAAU,CAAA;AAC7C,QAAM,SAA8B,CAAA;AACpC,SAAA,WAAW,UAAU,IAAI,iBAAiB,QAAQ,GAAG,IAAI,IAAI,QAAQ,WAAW,CAAA,CAAE,GAAG,MAAM,GACpF;AACT;AAQA,SAAS,WACP,cACA,QACA,MACA,SACA,KACM;AACN,aAAW,SAAS,cAAc,UAAU,CAAA,GAAI;AAE9C,QAAI,CAAC,OAA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AAA,GAAI,KAAK,EAAE,GAE5C,KAAK,aAAa,mBAAmB,MAAM,SAC7C,OAAO,SAAS,IAAI,mBAAmB,OAAO,QAAQ,oBAAoB,SAAS,GAAG,QAAQ,SAAS,IAEvG,OAAO,SAAS,IAAI;AAAA,EAExB;AAEA,SAAO;AAAA,IACL,YAAY,UAAU;AAAA,IACtB,cAAc,UAAU;AAAA,IACxB,gBAAgB,UAAU;AAAA,IAC1B;AAAA,IACA;AAAA,EAAA;AAEJ;;;;;;;;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"index2.cjs","sources":["../../src/lib/locjson/fields.ts","../../src/lib/locjson/paths.ts","../../src/lib/locjson/portableText.ts","../../src/lib/locjson/serialize.ts","../../src/lib/locjson/filename.ts","../../src/lib/locjson/deserialize.ts"],"sourcesContent":["import type {LocalizationMode, TranslatableField, TranslatableLeaf, ValueShape} from './types'\n\nexport interface GetTranslatableFieldsOptions {\n /**\n * Field names to exclude (e.g. the i18n language field, passed by the caller\n * as the configured `documentI18nPluginLanguageField`). Defaults to none.\n */\n exclude?: string[]\n}\n\ninterface CompiledType {\n jsonType?: string\n name?: string\n title?: string\n of?: CompiledType[]\n fields?: CompiledField[]\n options?: {\n documentInternationalization?: {exclude?: boolean}\n smartcatTranslation?: {include?: boolean}\n }\n}\n\n/**\n * True when a field is marked non-translatable via document-internationalization's\n * own field option (`options.documentInternationalization.exclude`). Honoring it\n * means consumers exclude fields per type using the i18n plugin's native marker —\n * no separate config here.\n */\nfunction isExcludedByDocumentI18n(field: CompiledField): boolean {\n return field.type?.options?.documentInternationalization?.exclude === true\n}\n\ninterface CompiledField {\n name: string\n type?: CompiledType\n}\n\n/**\n * True for a Sanity **system** type — the reserved `sanity.` namespace, notably\n * the `sanity.imageAsset` / `sanity.fileAsset` documents that image/file fields\n * reference. Their string fields (`originalFilename`, `path`, `url`, …) look\n * translatable but must never be sent: assets are shared across locales, and\n * translating a filename or CDN path corrupts the reference. Excluding them here\n * keeps them out of both the linked-document crawl and export serialization,\n * since both decide translatability via {@link getTranslatableFields}.\n */\nexport function isSystemType(typeName: unknown): boolean {\n return typeof typeName === 'string' && typeName.startsWith('sanity.')\n}\n\n/**\n * True for a `sanity-plugin-internationalized-array` array type (e.g.\n * `internationalizedArrayString`). Detected structurally by naming convention so\n * we never need a dependency on that plugin.\n */\nexport function isInternationalizedArrayType(type?: CompiledType): boolean {\n return (\n type?.jsonType === 'array' &&\n typeof type.name === 'string' &&\n type.name.startsWith('internationalizedArray')\n )\n}\n\n/** True for an array member `_type` like `internationalizedArrayStringValue`. */\nexport function isInternationalizedArrayMemberType(typeName: unknown): boolean {\n return typeof typeName === 'string' && /^internationalizedArray.+Value$/.test(typeName)\n}\n\n/** The inner value type of an internationalized-array field (its member's `value` field). */\nexport function innerValueType(arrayType?: CompiledType): CompiledType | undefined {\n return arrayType?.of?.[0]?.fields?.find((f) => f.name === 'value')?.type\n}\n\n/**\n * Localization mode of a **compiled** schema type: `field` if it declares any\n * internationalized-array field (in which case only those fields are\n * translatable), otherwise `document`. A pure function of the type, so callers\n * should compute it once per type.\n */\nexport function localizationMode(compiledType: unknown): LocalizationMode {\n const fields = (compiledType as CompiledType)?.fields\n if (!Array.isArray(fields)) return 'document'\n return fields.some((f) => isInternationalizedArrayType(f.type)) ? 'field' : 'document'\n}\n\n/**\n * Selects translatable fields from a **compiled** Sanity schema type.\n *\n * Document-mode types (default policy, installation-agnostic):\n * - `string` and `text` fields -> plain units\n * - Portable Text (arrays containing `block` members) -> HTML units\n * - everything else (slug, url, datetime, number, boolean, reference, image,\n * non-block arrays, objects, system `_` fields, excluded names) is excluded.\n *\n * Field-mode types (any internationalized-array field present): only the\n * internationalized-array fields are translatable; each is classified by its\n * inner `value` type (string/text -> plain, Portable Text -> HTML).\n *\n * The compiled schema shape isn't strongly typed publicly, so this reads it\n * defensively. Callers that need overrides can post-filter the result.\n */\nexport function getTranslatableFields(\n compiledType: unknown,\n options: GetTranslatableFieldsOptions = {},\n): TranslatableField[] {\n const rootType = compiledType as CompiledType\n if (isSystemType(rootType?.name)) return []\n if (!Array.isArray(rootType?.fields)) return []\n const result: TranslatableField[] = []\n walkFields(rootType, '', localizationMode(rootType), new Set(options.exclude ?? []), result)\n return result\n}\n\n/**\n * Recursively selects translatable fields. In field-mode it descends through\n * plain object fields (so nested internationalized-array fields, e.g.\n * `benefits.items`, are reached) and classifies each i18n field's member value\n * shape. Document-mode keeps the flat top-level behavior.\n */\nfunction walkFields(\n compiledType: CompiledType | undefined,\n prefix: string,\n mode: LocalizationMode,\n exclude: Set<string>,\n out: TranslatableField[],\n): void {\n for (const field of compiledType?.fields ?? []) {\n // `exclude` (the i18n language field) only ever appears at the top level.\n if (!field?.name || field.name.startsWith('_') || (prefix === '' && exclude.has(field.name))) continue\n const forceInclude = field.type?.options?.smartcatTranslation?.include === true\n if (isExcludedByDocumentI18n(field) && !forceInclude) continue\n const path = prefix ? `${prefix}.${field.name}` : field.name\n const title = field.type?.title || titleCase(field.name)\n\n if (mode === 'field') {\n if (isInternationalizedArrayType(field.type)) {\n const inner = innerValueType(field.type)\n const shape = classifyValueShape(inner) ?? (forceInclude ? classifyValueShapeByJsonType(inner) : null)\n if (shape) out.push({path, localization: 'field', kind: shape.container === 'scalar' ? shape.kind : 'plain', title, value: shape})\n else if (forceInclude) warnUntranslatableInclude(field.name, inner)\n } else if (isPlainObject(field.type)) {\n walkFields(field.type, path, mode, exclude, out)\n }\n } else {\n const kind = classifyField(field.type) ?? (forceInclude ? classifyByJsonType(field.type) : null)\n if (kind) out.push({path, localization: 'document', kind, title, value: {container: 'scalar', kind}})\n else if (forceInclude) warnUntranslatableInclude(field.name, field.type)\n }\n }\n}\n\n/** One translatable leaf of a field, expanded against a concrete source value. */\nexport interface FieldLeaf {\n /** Unique unit key / x-sanity fieldPath. */\n fullKey: string\n /** Path within the member value to the leaf (empty = the value itself). */\n valuePath: string\n kind: TranslatableField['kind']\n title?: string\n}\n\n/**\n * Expand a field into its translatable leaves against a concrete source value\n * (the source-language member value for field-mode). Serialize and import both\n * call this with the SAME source value, so the unit keys they compute match.\n */\nexport function enumerateLeaves(field: TranslatableField, sourceValue: unknown): FieldLeaf[] {\n const base = field.path\n const shape: ValueShape = field.value ?? {container: 'scalar', kind: field.kind}\n if (shape.container === 'scalar') {\n return [{fullKey: base, valuePath: '', kind: shape.kind, title: field.title}]\n }\n if (shape.container === 'arrayOfScalar') {\n const arr = Array.isArray(sourceValue) ? sourceValue : []\n return arr.map((_, i) => ({fullKey: `${base}[${i}]`, valuePath: `[${i}]`, kind: shape.kind, title: field.title}))\n }\n if (shape.container === 'object') {\n return shape.leaves.map((l) => ({fullKey: `${base}.${l.subPath}`, valuePath: l.subPath, kind: l.kind, title: l.title ?? field.title}))\n }\n const arr = Array.isArray(sourceValue) ? sourceValue : []\n const out: FieldLeaf[] = []\n for (const item of arr) {\n const key = (item as Record<string, unknown>)?._key\n if (typeof key !== 'string') continue\n for (const l of shape.leaves) {\n const valuePath = l.subPath ? `[${key}].${l.subPath}` : `[${key}]`\n out.push({fullKey: `${base}${valuePath}`, valuePath, kind: l.kind, title: l.title ?? field.title})\n }\n }\n return out\n}\n\n/** A recursable plain object (not a reference/image/file/slug/geopoint). */\nfunction isPlainObject(type?: CompiledType): boolean {\n if (type?.jsonType !== 'object' || !Array.isArray(type.fields)) return false\n if (type.fields.some((f) => f.name === '_ref')) return false\n return !['reference', 'image', 'file', 'slug', 'geopoint'].includes(type.name ?? '')\n}\n\n/** Classify an internationalized-array member's `value` type into a ValueShape. */\nfunction classifyValueShape(inner?: CompiledType): ValueShape | null {\n if (!inner) return null\n const scalar = classifyField(inner)\n if (scalar) return {container: 'scalar', kind: scalar}\n if (inner.jsonType === 'array' && Array.isArray(inner.of)) {\n const member = inner.of[0]\n if (member?.jsonType === 'string' && (member.name === 'string' || member.name === 'text')) {\n return {container: 'arrayOfScalar', kind: 'plain'}\n }\n if (isPlainObject(member)) {\n const leaves = collectLeaves(member, '')\n if (leaves.length) return {container: 'arrayOfObject', leaves}\n }\n return null\n }\n if (isPlainObject(inner)) {\n const leaves = collectLeaves(inner, '')\n if (leaves.length) return {container: 'object', leaves}\n }\n return null\n}\n\n/** ValueShape by jsonType only (for `smartcatTranslation.include` overrides). */\nfunction classifyValueShapeByJsonType(inner?: CompiledType): ValueShape | null {\n const kind = classifyByJsonType(inner)\n return kind ? {container: 'scalar', kind} : null\n}\n\n/** Translatable string/text/Portable-Text leaves within an object (recursing plain objects). */\nfunction collectLeaves(objType: CompiledType | undefined, prefix: string): TranslatableLeaf[] {\n const leaves: TranslatableLeaf[] = []\n for (const f of objType?.fields ?? []) {\n if (!f?.name || f.name.startsWith('_')) continue\n if (isExcludedByDocumentI18n(f) && f.type?.options?.smartcatTranslation?.include !== true) continue\n const subPath = prefix ? `${prefix}.${f.name}` : f.name\n const kind = classifyField(f.type)\n if (kind) leaves.push({subPath, kind, title: f.type?.title || titleCase(f.name)})\n else if (isPlainObject(f.type)) leaves.push(...collectLeaves(f.type, subPath))\n }\n return leaves\n}\n\n/** True for an array whose members include a Portable Text `block` type. */\nfunction isPortableTextArray(type: CompiledType): boolean {\n return (\n type.jsonType === 'array' &&\n Array.isArray(type.of) &&\n type.of.some((member) => member?.name === 'block' || member?.jsonType === 'block')\n )\n}\n\nfunction classifyField(type?: CompiledType): TranslatableField['kind'] | null {\n if (!type) return null\n\n // Plain text: only the `string` and `text` base types (not url, slug, datetime…).\n if (type.jsonType === 'string' && (type.name === 'string' || type.name === 'text')) {\n return 'plain'\n }\n\n if (isPortableTextArray(type)) return 'portableText'\n\n return null\n}\n\n/**\n * Classify by base `jsonType` only, ignoring the type name. Used for fields\n * explicitly opted in with `smartcatTranslation.include`, where the name isn't in\n * the default allowlist (e.g. a custom string alias) but the underlying shape is\n * still translatable.\n */\nfunction classifyByJsonType(type?: CompiledType): TranslatableField['kind'] | null {\n if (!type) return null\n if (type.jsonType === 'string') return 'plain'\n if (isPortableTextArray(type)) return 'portableText'\n return null\n}\n\nfunction warnUntranslatableInclude(fieldName: string, type?: CompiledType): void {\n // eslint-disable-next-line no-console\n console.warn(\n `[smartcat-translation] Field \"${fieldName}\" is marked smartcatTranslation.include ` +\n `but its type (name: ${JSON.stringify(type?.name)}, jsonType: ${JSON.stringify(type?.jsonType)}) ` +\n `has no translatable representation; skipping.`,\n )\n}\n\nfunction titleCase(name: string): string {\n return name.replace(/([A-Z])/g, ' $1').replace(/^./, (c) => c.toUpperCase())\n}\n\n/**\n * Selects translatable fields from a document's **data** (no schema needed) —\n * for use in Sanity Functions, which run server-side without the Studio schema.\n *\n * Applies the same homogeneity rule as {@link getTranslatableFields}: if any\n * field holds internationalized-array members, the document is field-mode and\n * only those fields are selected (classified by a sample member's `value`);\n * otherwise top-level `string`/Portable Text fields are selected (document-mode).\n */\nexport function inferTranslatableFields(\n doc: Record<string, unknown>,\n options: GetTranslatableFieldsOptions = {},\n): TranslatableField[] {\n if (isSystemType(doc._type)) return []\n const exclude = new Set(options.exclude ?? [])\n\n const isI18nArray = (value: unknown): value is Array<{value?: unknown}> =>\n Array.isArray(value) &&\n value.some((m) => m && typeof m === 'object' && isInternationalizedArrayMemberType((m as {_type?: unknown})._type))\n\n const entries = Object.entries(doc).filter(([key]) => !key.startsWith('_') && !exclude.has(key))\n const fieldMode = entries.some(([, value]) => isI18nArray(value))\n\n const result: TranslatableField[] = []\n for (const [key, value] of entries) {\n if (fieldMode) {\n if (!isI18nArray(value)) continue\n const sample = value.find((m) => m && typeof m === 'object')?.value\n const kind = sampleKind(sample)\n if (kind) result.push({path: key, kind, localization: 'field', title: titleCase(key)})\n } else if (typeof value === 'string') {\n if (value.length > 0) result.push({path: key, kind: 'plain', localization: 'document', title: titleCase(key)})\n } else if (Array.isArray(value) && value.some(isPortableTextBlock)) {\n result.push({path: key, kind: 'portableText', localization: 'document', title: titleCase(key)})\n }\n }\n return result\n}\n\nfunction sampleKind(value: unknown): TranslatableField['kind'] | null {\n if (typeof value === 'string') return 'plain'\n if (Array.isArray(value) && value.some(isPortableTextBlock)) return 'portableText'\n return null\n}\n\nfunction isPortableTextBlock(value: unknown): boolean {\n return Boolean(value && typeof value === 'object' && (value as {_type?: string})._type === 'block')\n}\n\n/**\n * Reorders fields to match `orderedPaths` (e.g. a schema's field order). Fields\n * not listed keep their original relative order and go last. Stable.\n */\nexport function orderFieldsBy(\n fields: TranslatableField[],\n orderedPaths: string[] | undefined,\n): TranslatableField[] {\n if (!orderedPaths || orderedPaths.length === 0) return fields\n const rank = new Map(orderedPaths.map((path, i) => [path, i]))\n const at = (path: string) => (rank.has(path) ? (rank.get(path) as number) : Number.MAX_SAFE_INTEGER)\n return fields\n .map((field, i) => ({field, i}))\n .sort((a, b) => at(a.field.path) - at(b.field.path) || a.i - b.i)\n .map((x) => x.field)\n}\n","/**\n * Tiny path grammar for addressing translatable leaves nested inside a field's\n * value: `.field` for object properties, `[key]` for array members by `_key`,\n * `[n]` for array members by index. Used to expand array/object member values\n * into per-leaf LocJSON units and to overlay translations back onto a cloned\n * source value.\n *\n * e.g. `items[b1].value`, `tabs[2]`, `` (empty = the value itself).\n */\nexport type PathSeg = {field: string} | {key: string} | {index: number}\n\nexport function parsePath(path: string): PathSeg[] {\n const segs: PathSeg[] = []\n const re = /\\.?([^.[\\]]+)|\\[([^\\]]+)\\]/g\n let m: RegExpExecArray | null\n while ((m = re.exec(path)) !== null) {\n if (m[1] !== undefined) segs.push({field: m[1]})\n else if (/^\\d+$/.test(m[2])) segs.push({index: Number(m[2])})\n else segs.push({key: m[2]})\n }\n return segs\n}\n\n/** Append a segment to a base path string, producing a valid path. */\nexport function joinPath(base: string, seg: PathSeg): string {\n if ('field' in seg) return base ? `${base}.${seg.field}` : seg.field\n if ('index' in seg) return `${base}[${seg.index}]`\n return `${base}[${seg.key}]`\n}\n\nfunction child(container: unknown, seg: PathSeg): unknown {\n if (container == null || typeof container !== 'object') return undefined\n if ('field' in seg) return (container as Record<string, unknown>)[seg.field]\n if ('index' in seg) return Array.isArray(container) ? container[seg.index] : undefined\n return Array.isArray(container)\n ? container.find((m) => m && typeof m === 'object' && (m as Record<string, unknown>)._key === seg.key)\n : undefined\n}\n\n/** Read the value at a path from a root value (undefined if any segment misses). */\nexport function getAtPath(root: unknown, path: string): unknown {\n let cur = root\n for (const seg of parsePath(path)) {\n cur = child(cur, seg)\n if (cur === undefined) return undefined\n }\n return cur\n}\n\n/**\n * Return a deep clone of `root` with the leaf at `path` replaced by `value`.\n * An empty path replaces the whole value. Missing intermediate segments are a\n * no-op (the clone is returned unchanged), so a template that lacks a leaf can't\n * be corrupted.\n */\nexport function setAtPath<T>(root: T, path: string, value: unknown): T {\n const segs = parsePath(path)\n if (segs.length === 0) return value as T\n const clone = structuredClone(root)\n let cur: unknown = clone\n for (let i = 0; i < segs.length - 1; i++) {\n cur = child(cur, segs[i])\n if (cur == null || typeof cur !== 'object') return clone\n }\n const last = segs[segs.length - 1]\n if ('field' in last) (cur as Record<string, unknown>)[last.field] = value\n else if ('index' in last && Array.isArray(cur)) cur[last.index] = value\n else if ('key' in last && Array.isArray(cur)) {\n const idx = cur.findIndex((m) => m && typeof m === 'object' && (m as Record<string, unknown>)._key === last.key)\n if (idx >= 0) cur[idx] = value\n }\n return clone\n}\n","import {escapeHTML, toHTML} from '@portabletext/to-html'\nimport {htmlToBlocks, type DeserializerRule} from '@sanity/block-tools'\n\n/** A Portable Text block (loosely typed — shape varies by schema). */\nexport type PortableTextBlock = Record<string, unknown>\n\n/**\n * Custom (non-`block`) top-level Portable Text types that we *can* round-trip\n * through HTML, and so must NOT treat as unserializable. Each needs both a\n * `toHTML` component (below) and an `htmlToBlocks` rule (see {@link tableRule})\n * so the block survives export → translation → import intact.\n *\n * Currently just `@sanity/table`: `{_type:'table', rows:[{_type:'tableRow',\n * cells: string[]}]}` — plain-string cells, no header row, no col/row spans.\n */\nconst SERIALIZABLE_CUSTOM_TYPES = new Set(['table'])\n\n/** Renders a `@sanity/table` block to an HTML `<table>` for translation. */\nfunction tableToHtml(value: unknown): string {\n const rows = Array.isArray((value as {rows?: unknown})?.rows) ? (value as {rows: unknown[]}).rows : []\n const trs = rows\n .map((row) => {\n const cells = Array.isArray((row as {cells?: unknown})?.cells) ? (row as {cells: unknown[]}).cells : []\n const tds = cells.map((cell) => `<td>${escapeHTML(typeof cell === 'string' ? cell : '')}</td>`).join('')\n return `<tr>${tds}</tr>`\n })\n .join('')\n return `<table><tbody>${trs}</tbody></table>`\n}\n\n/**\n * An `htmlToBlocks` rule that turns a translated `<table>` back into a\n * `@sanity/table` block. Rows get deterministic index-based `_key`s (the whole\n * field value is replaced on import, so keys need only be unique within the\n * table); cells are plain strings, matching the schema.\n */\nconst tableRule: DeserializerRule = {\n deserialize(el, _next, createBlock) {\n if ((el as Element).tagName?.toLowerCase() !== 'table') return undefined\n const rows = Array.from((el as Element).querySelectorAll('tr')).map((tr, i) => ({\n _type: 'tableRow',\n _key: `row${i}`,\n cells: Array.from(tr.querySelectorAll('td, th')).map((cell) => cell.textContent ?? ''),\n }))\n return createBlock({_type: 'table', rows}) as never\n },\n}\n\n/** Parses an HTML string into a DOM Document. Provided by the environment. */\nexport type ParseHtml = (html: string) => Document\n\n/**\n * Renders Portable Text to an HTML string for translation.\n * Whole-field: the entire block array becomes one HTML document fragment.\n */\nexport function portableTextToHtml(blocks: PortableTextBlock[] | undefined | null): string {\n if (!blocks || blocks.length === 0) return ''\n return toHTML(blocks as never, {\n // Fallback only. Serialization skips any field containing an unserializable\n // block up front (see findUnserializableBlockTypes), so to-html should never\n // meet an unknown type here. If one slips through, render it as empty rather\n // than let @portabletext/to-html emit its default placeholder — a hidden\n // `<div style=\"display:none\">Unknown block type \"…\"</div>` whose English text\n // would otherwise be sent to Smartcat and translated into every locale.\n // (`onMissingComponent: false` only silences the warning; it does NOT stop the\n // placeholder — the `unknownType` override is what suppresses it.)\n onMissingComponent: false,\n components: {\n unknownType: () => '',\n // Serializable custom block types get real markup so their text is\n // translatable and can be parsed back (see SERIALIZABLE_CUSTOM_TYPES).\n types: {table: ({value}) => tableToHtml(value)},\n },\n })\n}\n\n/**\n * Distinct `_type`s in a Portable Text value that `@portabletext/to-html` cannot\n * serialize: any top-level member that isn't a standard `block` (and isn't in\n * {@link SERIALIZABLE_CUSTOM_TYPES}, e.g. `table`, which we render explicitly),\n * or any inline child that isn't a `span` (e.g. an inline object). Such nodes\n * render to nothing, so a field containing them can't survive the HTML\n * round-trip — the caller skips the whole field and warns rather than silently\n * drop or corrupt it. Marks/annotations are intentionally not included: their\n * text survives (only the annotation would be lost).\n */\nexport function findUnserializableBlockTypes(\n blocks: PortableTextBlock[] | undefined | null,\n): string[] {\n if (!Array.isArray(blocks)) return []\n const types = new Set<string>()\n for (const block of blocks) {\n if (!block || typeof block !== 'object') continue\n const type = (block as {_type?: unknown})._type\n if (type !== 'block') {\n if (typeof type === 'string' && !SERIALIZABLE_CUSTOM_TYPES.has(type)) types.add(type)\n continue\n }\n const children = (block as {children?: unknown}).children\n if (!Array.isArray(children)) continue\n for (const child of children) {\n const childType = (child as {_type?: unknown} | null)?._type\n if (typeof childType === 'string' && childType !== 'span') types.add(childType)\n }\n }\n return [...types].sort()\n}\n\n/**\n * Extracts the visible text of Portable Text — the concatenation of every span's\n * `text` across all blocks (recursing into `children`). Used to decide whether a\n * field carries anything translatable: structural-but-textless blocks render to\n * non-empty HTML (`<p></p>`, `<br/>`, `<ul><li></li></ul>`), so emptiness must be\n * judged from the source blocks, not from stripping tags/entities out of the HTML\n * (which would mishandle ` ` and void elements).\n */\nexport function portableTextToPlainText(blocks: PortableTextBlock[] | undefined | null): string {\n if (!Array.isArray(blocks)) return ''\n let text = ''\n const visit = (node: unknown): void => {\n if (Array.isArray(node)) {\n node.forEach(visit)\n } else if (node && typeof node === 'object') {\n const value = node as {text?: unknown; children?: unknown; rows?: unknown}\n if (typeof value.text === 'string') text += value.text\n if (Array.isArray(value.children)) visit(value.children)\n // `table` blocks carry text in `rows[].cells[]` (plain strings), not spans;\n // count it so a table-only field isn't judged empty and skipped on export.\n if (Array.isArray(value.rows)) {\n for (const row of value.rows) {\n const cells = (row as {cells?: unknown})?.cells\n if (Array.isArray(cells)) for (const cell of cells) if (typeof cell === 'string') text += cell\n }\n }\n }\n }\n visit(blocks)\n return text\n}\n\n/**\n * Parses translated HTML back into Portable Text using the field's compiled\n * block content type. `parseHtml` supplies a DOM parser:\n * - Studio: (html) => new DOMParser().parseFromString(html, 'text/html')\n * - Functions: (html) => new JSDOM(html).window.document\n */\nexport function htmlToPortableText(\n html: string,\n blockContentType: unknown,\n parseHtml: ParseHtml,\n): PortableTextBlock[] {\n if (!html.trim()) return []\n return htmlToBlocks(html, blockContentType as never, {\n parseHtml,\n rules: [tableRule],\n }) as unknown as PortableTextBlock[]\n}\n","import {\n findUnserializableBlockTypes,\n portableTextToHtml,\n portableTextToPlainText,\n type PortableTextBlock,\n} from './portableText'\nimport {enumerateLeaves} from './fields'\nimport {getAtPath} from './paths'\nimport type {LocJSON, LocUnit, TranslatableField} from './types'\n\nexport interface SerializeOptions {\n sourceLanguage: string\n /**\n * How internationalized-array members identify their locale: a field name\n * (e.g. `'language'`) or `'_key'` (v4.x and lower). Resolved by the caller (see\n * `resolveConfig`); no default is assumed here.\n */\n fieldLanguageKey: string\n /**\n * Called when a Portable Text field is skipped because it contains block types\n * that can't be serialized (see {@link findUnserializableBlockTypes}). The\n * whole field is left out so its content isn't dropped or corrupted on the\n * round-trip; the caller surfaces this to the user's log.\n */\n onSkippedField?: (info: {fieldPath: string; title?: string; types: string[]}) => void\n /**\n * When set, produces a **bilingual** file for a single target language: each\n * unit's `target` is populated from the existing translation, in parallel with\n * its `source`. Used by the \"send existing translations\" flow. The target value\n * of a field is read the same way as the source, but for the target language:\n * - field-level: the target-language member's `value` on the same document;\n * - document-level: the same field path on `doc` (the target-locale sibling).\n * `language` is the Sanity locale (matches internationalized-array members).\n */\n target?: {\n language: string\n /** The target-locale sibling document (document-level only). */\n doc?: SerializableDocument\n }\n}\n\n/** A minimal view of a Sanity document needed for serialization. */\nexport interface SerializableDocument {\n _id: string\n _type: string\n _rev?: string\n [key: string]: unknown\n}\n\n/**\n * Serializes a Sanity document into a LocJSON file: one unit per translatable\n * field. Plain fields become plain units (segments split on newlines); Portable\n * Text fields become whole-field HTML units (Smartcat segments them internally).\n *\n * Fields with no value are skipped, so the file only contains real source text.\n */\nexport function serializeToLocjson(\n doc: SerializableDocument,\n fields: TranslatableField[],\n options: SerializeOptions,\n): LocJSON {\n const units: LocUnit[] = []\n\n for (const field of fields) {\n // For field-level (internationalized-array) fields the source content is the\n // source-language member's `value` (the field may be nested through objects).\n const source =\n field.localization === 'field'\n ? memberValue(getAtPath(doc, field.path), options.sourceLanguage, options.fieldLanguageKey)\n : getAtPath(doc, field.path)\n\n // The existing translation for this field (bilingual mode only): read the same\n // way as the source, but for the target language / target-locale document.\n const targetOpt = options.target\n const targetSource =\n !targetOpt\n ? undefined\n : field.localization === 'field'\n ? memberValue(getAtPath(doc, field.path), targetOpt.language, options.fieldLanguageKey)\n : getAtPath(targetOpt.doc, field.path)\n\n // A scalar field yields one leaf; array/object member values yield one leaf\n // per translatable string inside (addressed by valuePath).\n for (const leaf of enumerateLeaves(field, source)) {\n const value = leaf.valuePath ? getAtPath(source, leaf.valuePath) : source\n const targetValue =\n targetSource === undefined ? undefined : leaf.valuePath ? getAtPath(targetSource, leaf.valuePath) : targetSource\n const comments = leaf.title ? [leaf.title] : undefined\n\n if (leaf.kind === 'plain') {\n // Skip when there's no translatable text. Trim only for the check — the\n // stored source keeps its original whitespace so the round-trip is exact.\n if (typeof value !== 'string' || value.trim().length === 0) continue\n units.push({\n key: leaf.fullKey,\n properties: {comments, 'x-smartcat-split': 'full', 'x-sanity': {fieldPath: leaf.fullKey}},\n // Split into line segments but keep the trailing newline on each, so\n // concatenating the segments (LocJSON's '' join) reconstructs the original.\n source: value.split(/(?<=\\n)/),\n // Existing translation (parallel to source), or empty for Smartcat to fill.\n target: typeof targetValue === 'string' && targetValue.length > 0 ? targetValue.split(/(?<=\\n)/) : [],\n })\n } else {\n const blocks = value as PortableTextBlock[] | undefined\n // A field with a block to-html can't serialize (e.g. a custom `table`)\n // is skipped whole: partial HTML would drop that block on the round-trip.\n const unserializable = findUnserializableBlockTypes(blocks)\n if (unserializable.length > 0) {\n options.onSkippedField?.({fieldPath: field.path, title: field.title, types: unserializable})\n continue\n }\n // `html` is non-empty for structural-but-textless blocks (e.g. `<p></p>`,\n // `<br/>`), so also require actual visible text before emitting a unit.\n const html = portableTextToHtml(blocks)\n if (!html || portableTextToPlainText(blocks).trim().length === 0) continue\n // Existing translation as HTML — only when the target-locale value is itself\n // serializable and carries visible text; otherwise leave it for Smartcat.\n const targetBlocks = targetValue as PortableTextBlock[] | undefined\n const targetHtml =\n targetSource === undefined ||\n findUnserializableBlockTypes(targetBlocks).length > 0 ||\n portableTextToPlainText(targetBlocks).trim().length === 0\n ? ''\n : portableTextToHtml(targetBlocks)\n units.push({\n key: leaf.fullKey,\n properties: {comments, 'x-smartcat-format': 'html', 'x-smartcat-split': 'full', 'x-sanity': {fieldPath: leaf.fullKey}},\n source: [html],\n target: targetHtml ? [targetHtml] : [],\n })\n }\n }\n }\n\n return {\n properties: {\n version: 1,\n 'x-sanity': {\n documentId: doc._id,\n documentType: doc._type,\n rev: doc._rev,\n sourceLanguage: options.sourceLanguage,\n },\n },\n units,\n }\n}\n\n/**\n * Reads the `value` of an internationalized-array field's member for a given\n * language. Members are identified strictly by `languageKey` — a field name\n * (`'language'`, v5+) or `'_key'` (v4.x and lower).\n */\nfunction memberValue(field: unknown, language: string, languageKey: string): unknown {\n if (!Array.isArray(field)) return undefined\n const member = field.find(\n (m) => m && typeof m === 'object' && (m as Record<string, unknown>)[languageKey] === language,\n )\n return (member as {value?: unknown} | undefined)?.value\n}\n","/**\n * Builds the Smartcat document filename for a Sanity document:\n * \"<type>/<sanitized title>-<id prefix>.locjson\"\n * e.g. \"page/About Us-3f14f092.locjson\".\n *\n * The type acts as a folder; the id prefix guarantees uniqueness. Identity on\n * import comes from the file's `x-sanity` metadata, not this name, so renames\n * are safe. When the exported content came from the document's draft, a `-draft`\n * marker is appended so it's visible in Smartcat (`…-3f14f092-draft.locjson`).\n */\nexport function buildLocjsonFilename(\n documentType: string,\n documentId: string,\n title?: string,\n options: {draft?: boolean; language?: string} = {},\n): string {\n const idPrefix = stripDraft(documentId).slice(0, 8) || 'unknown'\n const safeTitle = sanitizeSegment(title || 'Untitled')\n const safeType = sanitizeSegment(documentType) || 'document'\n const draftMarker = options.draft ? '-draft' : ''\n // Per-language bilingual files (send-existing flow) append `_<lang>` so Smartcat\n // groups the languages of one source as a single multilingual document, and so\n // `sourceIdPrefix` can strip the marker back off. The language sits after the\n // draft marker; correlation strips them in reverse.\n const langMarker = options.language ? `_${sanitizeSegment(options.language)}` : ''\n return `${safeType}/${safeTitle}-${idPrefix}${draftMarker}${langMarker}.locjson`\n}\n\nfunction stripDraft(id: string): string {\n return id.replace(/^drafts\\./, '')\n}\n\n/** Removes characters unsafe for file paths while keeping it human-readable. */\nfunction sanitizeSegment(value: string): string {\n return value\n .replace(/[/\\\\?%*:|\"<>]/g, ' ') // path-unsafe characters\n .replace(/\\s+/g, ' ')\n .trim()\n}\n","import {htmlToPortableText, type ParseHtml} from './portableText'\nimport type {LocJSON} from './types'\n\nexport interface DeserializeOptions {\n /** DOM parser for HTML units (DOMParser in Studio, jsdom in Functions). */\n parseHtml: ParseHtml\n /** Returns the compiled block content type for a Portable Text field path. */\n getBlockContentType: (fieldPath: string) => unknown\n}\n\nexport interface DeserializedDocument {\n documentId?: string\n documentType?: string\n sourceLanguage?: string\n /** Translated field values, keyed by field path. */\n fields: Record<string, unknown>\n /**\n * Source (pre-translation) text per field path, as the raw joined string. Used\n * to decide whether a field is JSON (so its translation can be validated before\n * import). Always the string form, even for HTML units.\n */\n sources: Record<string, string>\n}\n\n/**\n * Parses a (translated) LocJSON file back into Sanity field values.\n *\n * Uses each unit's `target` (falling back to `source` when a unit hasn't been\n * translated yet, so the result is always coherent). HTML units are converted\n * back to Portable Text via the field's compiled block content type.\n */\nexport function deserializeFromLocjson(locjson: LocJSON, options: DeserializeOptions): DeserializedDocument {\n const fileMeta = locjson.properties?.['x-sanity']\n const fields: Record<string, unknown> = {}\n const sources: Record<string, string> = {}\n\n for (const unit of locjson.units ?? []) {\n const fieldPath = unit.properties?.['x-sanity']?.fieldPath ?? unit.key\n const text = (unit.target?.length ? unit.target : unit.source) ?? []\n\n // LocJSON splits a string into arbitrary chunks with no implied delimiter,\n // so the original is always reconstructed by concatenating the segments.\n const value = text.join('')\n sources[fieldPath] = (unit.source ?? []).join('')\n\n if (unit.properties?.['x-smartcat-format'] === 'html') {\n fields[fieldPath] = htmlToPortableText(value, options.getBlockContentType(fieldPath), options.parseHtml)\n } else {\n fields[fieldPath] = value\n }\n }\n\n return {\n documentId: fileMeta?.documentId,\n documentType: fileMeta?.documentType,\n sourceLanguage: fileMeta?.sourceLanguage,\n fields,\n sources,\n }\n}\n"],"names":["escapeHTML","toHTML","child","htmlToBlocks"],"mappings":";;AA4BA,SAAS,yBAAyB,OAA+B;AAC/D,SAAO,MAAM,MAAM,SAAS,8BAA8B,YAAY;AACxE;AAgBO,SAAS,aAAa,UAA4B;AACvD,SAAO,OAAO,YAAa,YAAY,SAAS,WAAW,SAAS;AACtE;AAOO,SAAS,6BAA6B,MAA8B;AACzE,SACE,MAAM,aAAa,WACnB,OAAO,KAAK,QAAS,YACrB,KAAK,KAAK,WAAW,wBAAwB;AAEjD;AAGO,SAAS,mCAAmC,UAA4B;AAC7E,SAAO,OAAO,YAAa,YAAY,kCAAkC,KAAK,QAAQ;AACxF;AAGO,SAAS,eAAe,WAAoD;AACjF,SAAO,WAAW,KAAK,CAAC,GAAG,QAAQ,KAAK,CAAC,MAAM,EAAE,SAAS,OAAO,GAAG;AACtE;AAQO,SAAS,iBAAiB,cAAyC;AACxE,QAAM,SAAU,cAA+B;AAC/C,SAAK,MAAM,QAAQ,MAAM,KAClB,OAAO,KAAK,CAAC,MAAM,6BAA6B,EAAE,IAAI,CAAC,IA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gB,UAAU;AAAA,IAC1B;AAAA,IACA;AAAA,EAAA;AAEJ;;;;;;;;;;;;;;;"}
|
package/dist/_chunks-es/index.js
CHANGED
|
@@ -54,8 +54,13 @@ function hasConfirmedContent(stages) {
|
|
|
54
54
|
function targetPercent(stages) {
|
|
55
55
|
return stages.length === 0 ? 0 : Math.round(stages.reduce((sum, s) => sum + s.percent, 0) / stages.length);
|
|
56
56
|
}
|
|
57
|
+
function escapeRegExp(value) {
|
|
58
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
59
|
+
}
|
|
57
60
|
function sourceIdPrefix(doc) {
|
|
58
|
-
|
|
61
|
+
let noExt = ((doc.name || doc.filename || doc.fullPath || "").split("/").pop() ?? "").replace(/\.locjson$/i, "");
|
|
62
|
+
const lang = doc.targetLanguage;
|
|
63
|
+
return lang && (noExt = noExt.replace(new RegExp(`\\(${escapeRegExp(lang)}\\)$`), ""), noExt = noExt.replace(new RegExp(`_${escapeRegExp(lang)}$`), "")), noExt.replace(/-draft$/, "").slice(-8);
|
|
59
64
|
}
|
|
60
65
|
function buildProgress(items, scProject, previous, now, mappings) {
|
|
61
66
|
const stageNames = buildStageNameMap(scProject), importedSet = /* @__PURE__ */ new Set(), titleByDoc = /* @__PURE__ */ new Map();
|
|
@@ -64,12 +69,17 @@ function buildProgress(items, scProject, previous, now, mappings) {
|
|
|
64
69
|
for (const target of doc.targets)
|
|
65
70
|
target.imported && importedSet.add(`${doc.sourceDocId}__${target.language}`);
|
|
66
71
|
}
|
|
67
|
-
const itemByPrefix = /* @__PURE__ */ new Map();
|
|
68
|
-
for (const item of items)
|
|
72
|
+
const itemByPrefix = /* @__PURE__ */ new Map(), itemById = /* @__PURE__ */ new Map();
|
|
73
|
+
for (const item of items)
|
|
74
|
+
itemByPrefix.set(stripDraft(item._id).slice(0, 8), item), itemById.set(stripDraft(item._id), item);
|
|
75
|
+
const sourceDocIdByScId = /* @__PURE__ */ new Map();
|
|
76
|
+
for (const doc of previous ?? [])
|
|
77
|
+
for (const target of doc.targets)
|
|
78
|
+
target.smartcatDocumentId && sourceDocIdByScId.set(target.smartcatDocumentId, doc.sourceDocId);
|
|
69
79
|
const byDoc = /* @__PURE__ */ new Map();
|
|
70
80
|
for (const scDoc of scProject.documents ?? []) {
|
|
71
81
|
if (!scDoc.targetLanguage) continue;
|
|
72
|
-
const item = itemByPrefix.get(sourceIdPrefix(scDoc));
|
|
82
|
+
const storedSourceDocId = scDoc.id ? sourceDocIdByScId.get(scDoc.id) : void 0, item = (storedSourceDocId ? itemById.get(stripDraft(storedSourceDocId)) : void 0) ?? itemByPrefix.get(sourceIdPrefix(scDoc));
|
|
73
83
|
if (!item) continue;
|
|
74
84
|
const language = toSanityLanguage(mappings, scDoc.targetLanguage);
|
|
75
85
|
let docProgress = byDoc.get(item._id);
|
|
@@ -88,8 +98,10 @@ function buildProgress(items, scProject, previous, now, mappings) {
|
|
|
88
98
|
syncedAt: now
|
|
89
99
|
});
|
|
90
100
|
}
|
|
91
|
-
const result = [];
|
|
101
|
+
const result = [], seen = /* @__PURE__ */ new Set();
|
|
92
102
|
for (const item of items) {
|
|
103
|
+
if (seen.has(item._id)) continue;
|
|
104
|
+
seen.add(item._id);
|
|
93
105
|
const docProgress = byDoc.get(item._id);
|
|
94
106
|
docProgress && (docProgress.targets.sort((a, b) => a.language.localeCompare(b.language)), result.push(docProgress));
|
|
95
107
|
}
|
|
@@ -170,6 +182,7 @@ export {
|
|
|
170
182
|
summarizeCompletion,
|
|
171
183
|
targetKey,
|
|
172
184
|
targetPercent,
|
|
185
|
+
toSanityLanguage,
|
|
173
186
|
toSmartcatLanguage
|
|
174
187
|
};
|
|
175
188
|
//# sourceMappingURL=index.js.map
|