@prenta/import 0.5.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/CHANGELOG.md +686 -0
- package/LICENSE +21 -0
- package/README.md +24 -0
- package/dist/adapter.d.ts +47 -0
- package/dist/adapter.d.ts.map +1 -0
- package/dist/adapter.js +60 -0
- package/dist/adapter.js.map +1 -0
- package/dist/adapters/contentful/asset.d.ts +47 -0
- package/dist/adapters/contentful/asset.d.ts.map +1 -0
- package/dist/adapters/contentful/asset.js +88 -0
- package/dist/adapters/contentful/asset.js.map +1 -0
- package/dist/adapters/contentful/index.d.ts +24 -0
- package/dist/adapters/contentful/index.d.ts.map +1 -0
- package/dist/adapters/contentful/index.js +242 -0
- package/dist/adapters/contentful/index.js.map +1 -0
- package/dist/adapters/contentful/locale.d.ts +66 -0
- package/dist/adapters/contentful/locale.d.ts.map +1 -0
- package/dist/adapters/contentful/locale.js +125 -0
- package/dist/adapters/contentful/locale.js.map +1 -0
- package/dist/adapters/contentful/map.d.ts +69 -0
- package/dist/adapters/contentful/map.d.ts.map +1 -0
- package/dist/adapters/contentful/map.js +174 -0
- package/dist/adapters/contentful/map.js.map +1 -0
- package/dist/adapters/contentful/rich-text.d.ts +52 -0
- package/dist/adapters/contentful/rich-text.d.ts.map +1 -0
- package/dist/adapters/contentful/rich-text.js +95 -0
- package/dist/adapters/contentful/rich-text.js.map +1 -0
- package/dist/adapters/contentful/types.d.ts +189 -0
- package/dist/adapters/contentful/types.d.ts.map +1 -0
- package/dist/adapters/contentful/types.js +26 -0
- package/dist/adapters/contentful/types.js.map +1 -0
- package/dist/adapters/identity.d.ts +27 -0
- package/dist/adapters/identity.d.ts.map +1 -0
- package/dist/adapters/identity.js +116 -0
- package/dist/adapters/identity.js.map +1 -0
- package/dist/adapters/sanity/asset.d.ts +45 -0
- package/dist/adapters/sanity/asset.d.ts.map +1 -0
- package/dist/adapters/sanity/asset.js +79 -0
- package/dist/adapters/sanity/asset.js.map +1 -0
- package/dist/adapters/sanity/index.d.ts +19 -0
- package/dist/adapters/sanity/index.d.ts.map +1 -0
- package/dist/adapters/sanity/index.js +165 -0
- package/dist/adapters/sanity/index.js.map +1 -0
- package/dist/adapters/sanity/map.d.ts +75 -0
- package/dist/adapters/sanity/map.d.ts.map +1 -0
- package/dist/adapters/sanity/map.js +245 -0
- package/dist/adapters/sanity/map.js.map +1 -0
- package/dist/adapters/sanity/parse.d.ts +64 -0
- package/dist/adapters/sanity/parse.d.ts.map +1 -0
- package/dist/adapters/sanity/parse.js +71 -0
- package/dist/adapters/sanity/parse.js.map +1 -0
- package/dist/adapters/sanity/portable-text.d.ts +64 -0
- package/dist/adapters/sanity/portable-text.d.ts.map +1 -0
- package/dist/adapters/sanity/portable-text.js +112 -0
- package/dist/adapters/sanity/portable-text.js.map +1 -0
- package/dist/adapters/sanity/types.d.ts +182 -0
- package/dist/adapters/sanity/types.d.ts.map +1 -0
- package/dist/adapters/sanity/types.js +17 -0
- package/dist/adapters/sanity/types.js.map +1 -0
- package/dist/adapters/strapi/asset.d.ts +56 -0
- package/dist/adapters/strapi/asset.d.ts.map +1 -0
- package/dist/adapters/strapi/asset.js +94 -0
- package/dist/adapters/strapi/asset.js.map +1 -0
- package/dist/adapters/strapi/index.d.ts +20 -0
- package/dist/adapters/strapi/index.d.ts.map +1 -0
- package/dist/adapters/strapi/index.js +205 -0
- package/dist/adapters/strapi/index.js.map +1 -0
- package/dist/adapters/strapi/links.d.ts +79 -0
- package/dist/adapters/strapi/links.d.ts.map +1 -0
- package/dist/adapters/strapi/links.js +98 -0
- package/dist/adapters/strapi/links.js.map +1 -0
- package/dist/adapters/strapi/loader.d.ts +68 -0
- package/dist/adapters/strapi/loader.d.ts.map +1 -0
- package/dist/adapters/strapi/loader.js +177 -0
- package/dist/adapters/strapi/loader.js.map +1 -0
- package/dist/adapters/strapi/map.d.ts +53 -0
- package/dist/adapters/strapi/map.d.ts.map +1 -0
- package/dist/adapters/strapi/map.js +193 -0
- package/dist/adapters/strapi/map.js.map +1 -0
- package/dist/adapters/strapi/types.d.ts +174 -0
- package/dist/adapters/strapi/types.d.ts.map +1 -0
- package/dist/adapters/strapi/types.js +31 -0
- package/dist/adapters/strapi/types.js.map +1 -0
- package/dist/adapters/strapi/uid.d.ts +44 -0
- package/dist/adapters/strapi/uid.d.ts.map +1 -0
- package/dist/adapters/strapi/uid.js +58 -0
- package/dist/adapters/strapi/uid.js.map +1 -0
- package/dist/adapters/wordpress/attachments.d.ts +37 -0
- package/dist/adapters/wordpress/attachments.d.ts.map +1 -0
- package/dist/adapters/wordpress/attachments.js +134 -0
- package/dist/adapters/wordpress/attachments.js.map +1 -0
- package/dist/adapters/wordpress/authors.d.ts +24 -0
- package/dist/adapters/wordpress/authors.d.ts.map +1 -0
- package/dist/adapters/wordpress/authors.js +40 -0
- package/dist/adapters/wordpress/authors.js.map +1 -0
- package/dist/adapters/wordpress/index.d.ts +19 -0
- package/dist/adapters/wordpress/index.d.ts.map +1 -0
- package/dist/adapters/wordpress/index.js +281 -0
- package/dist/adapters/wordpress/index.js.map +1 -0
- package/dist/adapters/wordpress/map.d.ts +61 -0
- package/dist/adapters/wordpress/map.d.ts.map +1 -0
- package/dist/adapters/wordpress/map.js +265 -0
- package/dist/adapters/wordpress/map.js.map +1 -0
- package/dist/adapters/wordpress/parse.d.ts +50 -0
- package/dist/adapters/wordpress/parse.d.ts.map +1 -0
- package/dist/adapters/wordpress/parse.js +141 -0
- package/dist/adapters/wordpress/parse.js.map +1 -0
- package/dist/adapters/wordpress/terms.d.ts +46 -0
- package/dist/adapters/wordpress/terms.d.ts.map +1 -0
- package/dist/adapters/wordpress/terms.js +87 -0
- package/dist/adapters/wordpress/terms.js.map +1 -0
- package/dist/adapters/wordpress/types.d.ts +201 -0
- package/dist/adapters/wordpress/types.d.ts.map +1 -0
- package/dist/adapters/wordpress/types.js +24 -0
- package/dist/adapters/wordpress/types.js.map +1 -0
- package/dist/cli.d.ts +16 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +266 -0
- package/dist/cli.js.map +1 -0
- package/dist/formatter.d.ts +12 -0
- package/dist/formatter.d.ts.map +1 -0
- package/dist/formatter.js +48 -0
- package/dist/formatter.js.map +1 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/migrate.d.ts +151 -0
- package/dist/migrate.d.ts.map +1 -0
- package/dist/migrate.js +255 -0
- package/dist/migrate.js.map +1 -0
- package/dist/registry.d.ts +39 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +62 -0
- package/dist/registry.js.map +1 -0
- package/dist/runner.d.ts +33 -0
- package/dist/runner.d.ts.map +1 -0
- package/dist/runner.js +61 -0
- package/dist/runner.js.map +1 -0
- package/dist/types.d.ts +173 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +17 -0
- package/dist/types.js.map +1 -0
- package/package.json +59 -0
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `sanity` — adapter for Sanity dataset NDJSON exports.
|
|
3
|
+
*
|
|
4
|
+
* Reads the output of `sanity dataset export <name> <file.tar.gz>`
|
|
5
|
+
* (the NDJSON `data.ndjson` inside the archive) and produces
|
|
6
|
+
* Prenta-shaped `ImportedDocument`s. Asset binaries inside the
|
|
7
|
+
* `.tar.gz` aren't read by this adapter — the operator either
|
|
8
|
+
* uploads them separately or configures asset URL resolution via
|
|
9
|
+
* `projectId` + `dataset` / `assetBaseUrl`.
|
|
10
|
+
*
|
|
11
|
+
* Two-pass: the adapter walks the records once to build the
|
|
12
|
+
* resolution context (doc id → target collection, asset id → URL),
|
|
13
|
+
* then walks again invoking `mapDocument` per-record. That order
|
|
14
|
+
* means in-export references can resolve in either direction
|
|
15
|
+
* regardless of how the records are arranged in the file.
|
|
16
|
+
*/
|
|
17
|
+
import { readFileSync } from 'node:fs';
|
|
18
|
+
import { buildResult } from '../../adapter.js';
|
|
19
|
+
import { mapDocument, resolveCollection } from './map.js';
|
|
20
|
+
import { parseNdjson } from './parse.js';
|
|
21
|
+
const DEFAULT_IGNORE_TYPES = ['sanity.imageAsset', 'sanity.fileAsset', 'sanity.imageMetadata'];
|
|
22
|
+
export const sanityAdapter = {
|
|
23
|
+
name: 'sanity',
|
|
24
|
+
description: 'Import a Sanity dataset NDJSON export.',
|
|
25
|
+
docsUrl: 'https://github.com/actuate-media/prenta/blob/main/docs/import.md#sanity-adapter',
|
|
26
|
+
run(options) {
|
|
27
|
+
const config = readConfig(options.config);
|
|
28
|
+
const raw = readFileSync(options.input, 'utf-8');
|
|
29
|
+
const parse = parseNdjson(raw);
|
|
30
|
+
const warnings = [];
|
|
31
|
+
const unresolved = [];
|
|
32
|
+
for (const err of parse.errors) {
|
|
33
|
+
unresolved.push({
|
|
34
|
+
source: { field: `<line ${err.line}>` },
|
|
35
|
+
code: 'invalid-ndjson-line',
|
|
36
|
+
reason: `Could not parse line ${err.line}: ${err.message}`,
|
|
37
|
+
rawValue: err.preview,
|
|
38
|
+
hint: 'Inspect the source export at the reported line. Sanity exports are NDJSON: one JSON object per line.',
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
const ignored = new Set([...DEFAULT_IGNORE_TYPES, ...(config.ignoreTypes ?? [])]);
|
|
42
|
+
// First pass: skip drafts (if configured), separate asset docs
|
|
43
|
+
// for URL lookup, and build the doc id → collection map used
|
|
44
|
+
// for reference resolution.
|
|
45
|
+
const importable = [];
|
|
46
|
+
let skipped = 0;
|
|
47
|
+
const context = {
|
|
48
|
+
collectionByDocId: new Map(),
|
|
49
|
+
assetUrlsById: new Map(),
|
|
50
|
+
};
|
|
51
|
+
const dropDrafts = config.dropDrafts !== false;
|
|
52
|
+
for (const record of parse.records) {
|
|
53
|
+
// The parser returns loose records (`Record<string, unknown>`),
|
|
54
|
+
// so the narrowing-to-SanityDoc check happens here. Records
|
|
55
|
+
// missing the system fields land in `unresolved` as
|
|
56
|
+
// `invalid-shape` instead of crashing downstream.
|
|
57
|
+
if (typeof record._id !== 'string' || typeof record._type !== 'string') {
|
|
58
|
+
unresolved.push({
|
|
59
|
+
source: { id: typeof record._id === 'string' ? record._id : undefined, field: '<root>' },
|
|
60
|
+
code: 'invalid-shape',
|
|
61
|
+
reason: `Record is missing _id or _type.`,
|
|
62
|
+
rawValue: record,
|
|
63
|
+
hint: 'Sanity exports always include _id + _type. The source file may be truncated or hand-edited.',
|
|
64
|
+
});
|
|
65
|
+
skipped++;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
const doc = record;
|
|
69
|
+
if (dropDrafts && doc._id.startsWith('drafts.')) {
|
|
70
|
+
skipped++;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (doc._type === 'sanity.imageAsset' || doc._type === 'sanity.fileAsset') {
|
|
74
|
+
// Asset docs feed `assetUrlsById` so inline image / file refs
|
|
75
|
+
// surface their canonical URL even without projectId config.
|
|
76
|
+
const url = doc.url;
|
|
77
|
+
if (typeof url === 'string') {
|
|
78
|
+
context.assetUrlsById.set(doc._id, url);
|
|
79
|
+
}
|
|
80
|
+
skipped++;
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
if (ignored.has(doc._type)) {
|
|
84
|
+
skipped++;
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (options.shouldInclude && !options.shouldInclude({ type: doc._type, id: doc._id })) {
|
|
88
|
+
skipped++;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
// Pre-populate the collection map so references resolve
|
|
92
|
+
// forward-OR-backward in the file regardless of order.
|
|
93
|
+
const { collection } = resolveCollection(doc._type, config);
|
|
94
|
+
context.collectionByDocId.set(doc._id, collection);
|
|
95
|
+
importable.push(doc);
|
|
96
|
+
}
|
|
97
|
+
const documents = [];
|
|
98
|
+
for (const record of importable) {
|
|
99
|
+
const result = mapDocument(record, context, config);
|
|
100
|
+
warnings.push(...result.warnings);
|
|
101
|
+
unresolved.push(...result.unresolved);
|
|
102
|
+
documents.push({
|
|
103
|
+
collection: result.collection,
|
|
104
|
+
...(result.slug !== undefined ? { slug: result.slug } : {}),
|
|
105
|
+
sourceId: result.sourceId,
|
|
106
|
+
sourceType: result.sourceType,
|
|
107
|
+
data: result.data,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
return buildResult({
|
|
111
|
+
adapter: sanityAdapter.name,
|
|
112
|
+
documents,
|
|
113
|
+
warnings,
|
|
114
|
+
unresolved,
|
|
115
|
+
totals: {
|
|
116
|
+
// `totalRead` is "lines we *tried* to read", not "lines we
|
|
117
|
+
// managed to parse" — otherwise a half-corrupt export reports
|
|
118
|
+
// fewer records than it really contained and the operator
|
|
119
|
+
// never notices the truncation. Parse errors are reported as
|
|
120
|
+
// unresolved fields above, so this number plus the unresolved
|
|
121
|
+
// count tells the full story.
|
|
122
|
+
totalRead: parse.records.length + parse.errors.length,
|
|
123
|
+
converted: documents.length,
|
|
124
|
+
skipped,
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* Validate the user-supplied `--config` JSON into the
|
|
131
|
+
* adapter-specific shape. Anything we can't make sense of is
|
|
132
|
+
* silently dropped rather than throwing — the operator already
|
|
133
|
+
* gets a clearer error from the CLI's `--config` parsing if the
|
|
134
|
+
* file is malformed, and we don't want to spend cycles policing
|
|
135
|
+
* keys we don't know about (forward-compat for adapter additions).
|
|
136
|
+
*/
|
|
137
|
+
function readConfig(raw) {
|
|
138
|
+
if (raw === null || typeof raw !== 'object' || Array.isArray(raw))
|
|
139
|
+
return {};
|
|
140
|
+
const r = raw;
|
|
141
|
+
const out = {};
|
|
142
|
+
if (r.typeMap && typeof r.typeMap === 'object' && !Array.isArray(r.typeMap)) {
|
|
143
|
+
const map = {};
|
|
144
|
+
for (const [k, v] of Object.entries(r.typeMap)) {
|
|
145
|
+
if (typeof v === 'string')
|
|
146
|
+
map[k] = v;
|
|
147
|
+
}
|
|
148
|
+
out.typeMap = map;
|
|
149
|
+
}
|
|
150
|
+
if (typeof r.dropDrafts === 'boolean')
|
|
151
|
+
out.dropDrafts = r.dropDrafts;
|
|
152
|
+
if (typeof r.includeSystemFields === 'boolean')
|
|
153
|
+
out.includeSystemFields = r.includeSystemFields;
|
|
154
|
+
if (typeof r.projectId === 'string')
|
|
155
|
+
out.projectId = r.projectId;
|
|
156
|
+
if (typeof r.dataset === 'string')
|
|
157
|
+
out.dataset = r.dataset;
|
|
158
|
+
if (typeof r.assetBaseUrl === 'string')
|
|
159
|
+
out.assetBaseUrl = r.assetBaseUrl;
|
|
160
|
+
if (Array.isArray(r.ignoreTypes)) {
|
|
161
|
+
out.ignoreTypes = r.ignoreTypes.filter((s) => typeof s === 'string');
|
|
162
|
+
}
|
|
163
|
+
return out;
|
|
164
|
+
}
|
|
165
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/adapters/sanity/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAEtC,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAS9C,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAA;AAEzD,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAGxC,MAAM,oBAAoB,GAAG,CAAC,mBAAmB,EAAE,kBAAkB,EAAE,sBAAsB,CAAC,CAAA;AAE9F,MAAM,CAAC,MAAM,aAAa,GAAkB;IAC1C,IAAI,EAAE,QAAQ;IACd,WAAW,EAAE,wCAAwC;IACrD,OAAO,EAAE,iFAAiF;IAC1F,GAAG,CAAC,OAAuB;QACzB,MAAM,MAAM,GAAG,UAAU,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;QACzC,MAAM,GAAG,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YAAY,KAAK,QAAQ;QAAE,GAAG,CAAC,YAAY,GAAG,CAAC,CAAC,YAAY,CAAA;IACzE,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,WAAW,CAAC,EAAE,CAAC;QACjC,GAAG,CAAC,WAAW,GAAG,CAAC,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,EAAe,EAAE,CAAC,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAA;IACnF,CAAC;IACD,OAAO,GAAG,CAAA;AACZ,CAAC"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Map a single Sanity document into an Prenta-shaped payload.
|
|
3
|
+
*
|
|
4
|
+
* The mapper walks every field on the document, applying these
|
|
5
|
+
* rules in order:
|
|
6
|
+
*
|
|
7
|
+
* 1. **System fields** (`_id`, `_type`, `_rev`, `_createdAt`,
|
|
8
|
+
* `_updatedAt`) are stripped from `data` and lifted into
|
|
9
|
+
* `sourceId` / `sourceType`. Set `includeSystemFields: true` in
|
|
10
|
+
* config to keep them on `data`.
|
|
11
|
+
* 2. **Slug objects** (`{ _type: 'slug', current: 'x' }`) are
|
|
12
|
+
* flattened to the string `'x'`. The first slug encountered
|
|
13
|
+
* becomes the top-level `slug` on the document.
|
|
14
|
+
* 3. **Reference objects** (`{ _type: 'reference', _ref }`) are
|
|
15
|
+
* rewritten to `{ collection: '<target>', sourceId: '<ref>' }`
|
|
16
|
+
* when the ref resolves to a doc inside the same export.
|
|
17
|
+
* Unresolved refs produce a warning and the ref is preserved
|
|
18
|
+
* so a follow-up tool can re-attempt resolution.
|
|
19
|
+
* 4. **Image / file objects** are rewritten to include a `url`
|
|
20
|
+
* (when config provides projectId+dataset or assetBaseUrl)
|
|
21
|
+
* plus the parsed `width`, `height`, `extension`.
|
|
22
|
+
* 5. **Portable Text arrays** are passed through unchanged. The
|
|
23
|
+
* adapter walks them once to surface broken refs, but the
|
|
24
|
+
* content shape is preserved so downstream renderers
|
|
25
|
+
* (`@portabletext/to-html` etc.) work as-is.
|
|
26
|
+
* 6. Everything else is recursively mapped (objects/arrays) or
|
|
27
|
+
* copied verbatim (primitives).
|
|
28
|
+
*
|
|
29
|
+
* The mapper is pure: it takes a doc + resolution context + config
|
|
30
|
+
* and returns a payload + warnings + unresolved. The adapter
|
|
31
|
+
* orchestrates the I/O and accumulates totals.
|
|
32
|
+
*/
|
|
33
|
+
import type { ImportWarning, UnresolvedField } from '../../types.js';
|
|
34
|
+
import type { SanityAdapterConfig, SanityDoc } from './types.js';
|
|
35
|
+
/**
|
|
36
|
+
* What the field mapper knows about the rest of the export. The
|
|
37
|
+
* adapter populates these maps from a first pass over the records
|
|
38
|
+
* before invoking the mapper, so per-document mapping can be a pure
|
|
39
|
+
* lookup.
|
|
40
|
+
*/
|
|
41
|
+
export interface MappingContext {
|
|
42
|
+
/** `_id` → resolved Prenta collection for that document. */
|
|
43
|
+
collectionByDocId: Map<string, string>;
|
|
44
|
+
/**
|
|
45
|
+
* Asset `_id` → URL declared inside the asset document (when
|
|
46
|
+
* Sanity exports include them; legacy exports often don't).
|
|
47
|
+
*/
|
|
48
|
+
assetUrlsById: Map<string, string>;
|
|
49
|
+
}
|
|
50
|
+
export interface MapResult {
|
|
51
|
+
collection: string;
|
|
52
|
+
slug?: string;
|
|
53
|
+
data: Record<string, unknown>;
|
|
54
|
+
sourceId: string;
|
|
55
|
+
sourceType: string;
|
|
56
|
+
warnings: ImportWarning[];
|
|
57
|
+
unresolved: UnresolvedField[];
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Resolve the target Prenta collection for a Sanity `_type`.
|
|
61
|
+
*
|
|
62
|
+
* - With a `typeMap` configured: types in the map win; types NOT
|
|
63
|
+
* in the map fall back to using `_type` as the collection slug
|
|
64
|
+
* AND surface a `warned: true` so the adapter emits an
|
|
65
|
+
* `unmapped-type` warning the operator can act on.
|
|
66
|
+
* - Without a `typeMap`: every type maps to `_type` directly,
|
|
67
|
+
* silently. The operator opted into untyped passthrough mode by
|
|
68
|
+
* not configuring one.
|
|
69
|
+
*/
|
|
70
|
+
export declare function resolveCollection(sourceType: string, config: SanityAdapterConfig): {
|
|
71
|
+
collection: string;
|
|
72
|
+
warned: boolean;
|
|
73
|
+
};
|
|
74
|
+
export declare function mapDocument(doc: SanityDoc, context: MappingContext, config: SanityAdapterConfig): MapResult;
|
|
75
|
+
//# sourceMappingURL=map.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"map.d.ts","sourceRoot":"","sources":["../../../src/adapters/sanity/map.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAA;AAGpE,OAAO,KAAK,EAAE,mBAAmB,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAEhE;;;;;GAKG;AACH,MAAM,WAAW,cAAc;IAC7B,4DAA4D;IAC5D,iBAAiB,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IACtC;;;OAGG;IACH,aAAa,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CACnC;AAED,MAAM,WAAW,SAAS;IACxB,UAAU,EAAE,MAAM,CAAA;IAClB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAC7B,QAAQ,EAAE,MAAM,CAAA;IAChB,UAAU,EAAE,MAAM,CAAA;IAClB,QAAQ,EAAE,aAAa,EAAE,CAAA;IACzB,UAAU,EAAE,eAAe,EAAE,CAAA;CAC9B;AAID;;;;;;;;;;GAUG;AACH,wBAAgB,iBAAiB,CAC/B,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,mBAAmB,GAC1B;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,OAAO,CAAA;CAAE,CAKzC;AAED,wBAAgB,WAAW,CACzB,GAAG,EAAE,SAAS,EACd,OAAO,EAAE,cAAc,EACvB,MAAM,EAAE,mBAAmB,GAC1B,SAAS,CA+CX"}
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Map a single Sanity document into an Prenta-shaped payload.
|
|
3
|
+
*
|
|
4
|
+
* The mapper walks every field on the document, applying these
|
|
5
|
+
* rules in order:
|
|
6
|
+
*
|
|
7
|
+
* 1. **System fields** (`_id`, `_type`, `_rev`, `_createdAt`,
|
|
8
|
+
* `_updatedAt`) are stripped from `data` and lifted into
|
|
9
|
+
* `sourceId` / `sourceType`. Set `includeSystemFields: true` in
|
|
10
|
+
* config to keep them on `data`.
|
|
11
|
+
* 2. **Slug objects** (`{ _type: 'slug', current: 'x' }`) are
|
|
12
|
+
* flattened to the string `'x'`. The first slug encountered
|
|
13
|
+
* becomes the top-level `slug` on the document.
|
|
14
|
+
* 3. **Reference objects** (`{ _type: 'reference', _ref }`) are
|
|
15
|
+
* rewritten to `{ collection: '<target>', sourceId: '<ref>' }`
|
|
16
|
+
* when the ref resolves to a doc inside the same export.
|
|
17
|
+
* Unresolved refs produce a warning and the ref is preserved
|
|
18
|
+
* so a follow-up tool can re-attempt resolution.
|
|
19
|
+
* 4. **Image / file objects** are rewritten to include a `url`
|
|
20
|
+
* (when config provides projectId+dataset or assetBaseUrl)
|
|
21
|
+
* plus the parsed `width`, `height`, `extension`.
|
|
22
|
+
* 5. **Portable Text arrays** are passed through unchanged. The
|
|
23
|
+
* adapter walks them once to surface broken refs, but the
|
|
24
|
+
* content shape is preserved so downstream renderers
|
|
25
|
+
* (`@portabletext/to-html` etc.) work as-is.
|
|
26
|
+
* 6. Everything else is recursively mapped (objects/arrays) or
|
|
27
|
+
* copied verbatim (primitives).
|
|
28
|
+
*
|
|
29
|
+
* The mapper is pure: it takes a doc + resolution context + config
|
|
30
|
+
* and returns a payload + warnings + unresolved. The adapter
|
|
31
|
+
* orchestrates the I/O and accumulates totals.
|
|
32
|
+
*/
|
|
33
|
+
import { assetUrlFrom, parseAssetRef } from './asset.js';
|
|
34
|
+
import { collectReferences, isPortableTextArray, isReference } from './portable-text.js';
|
|
35
|
+
const SYSTEM_FIELDS = new Set(['_id', '_type', '_rev', '_createdAt', '_updatedAt']);
|
|
36
|
+
/**
|
|
37
|
+
* Resolve the target Prenta collection for a Sanity `_type`.
|
|
38
|
+
*
|
|
39
|
+
* - With a `typeMap` configured: types in the map win; types NOT
|
|
40
|
+
* in the map fall back to using `_type` as the collection slug
|
|
41
|
+
* AND surface a `warned: true` so the adapter emits an
|
|
42
|
+
* `unmapped-type` warning the operator can act on.
|
|
43
|
+
* - Without a `typeMap`: every type maps to `_type` directly,
|
|
44
|
+
* silently. The operator opted into untyped passthrough mode by
|
|
45
|
+
* not configuring one.
|
|
46
|
+
*/
|
|
47
|
+
export function resolveCollection(sourceType, config) {
|
|
48
|
+
if (!config.typeMap)
|
|
49
|
+
return { collection: sourceType, warned: false };
|
|
50
|
+
const mapped = config.typeMap[sourceType];
|
|
51
|
+
if (mapped)
|
|
52
|
+
return { collection: mapped, warned: false };
|
|
53
|
+
return { collection: sourceType, warned: true };
|
|
54
|
+
}
|
|
55
|
+
export function mapDocument(doc, context, config) {
|
|
56
|
+
const { collection, warned: usedFallbackCollection } = resolveCollection(doc._type, config);
|
|
57
|
+
const warnings = [];
|
|
58
|
+
const unresolved = [];
|
|
59
|
+
const data = {};
|
|
60
|
+
const source = { type: doc._type, id: doc._id };
|
|
61
|
+
let slug;
|
|
62
|
+
if (usedFallbackCollection) {
|
|
63
|
+
warnings.push({
|
|
64
|
+
source,
|
|
65
|
+
code: 'unmapped-type',
|
|
66
|
+
message: `No typeMap entry for '${doc._type}' — using as-is as the collection slug.`,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
for (const [key, rawValue] of Object.entries(doc)) {
|
|
70
|
+
if (SYSTEM_FIELDS.has(key) && !config.includeSystemFields)
|
|
71
|
+
continue;
|
|
72
|
+
const visited = visit(rawValue, {
|
|
73
|
+
context,
|
|
74
|
+
config,
|
|
75
|
+
source,
|
|
76
|
+
path: key,
|
|
77
|
+
warnings,
|
|
78
|
+
unresolved,
|
|
79
|
+
});
|
|
80
|
+
// First slug field on the document becomes top-level `slug`.
|
|
81
|
+
// Sanity doesn't enforce "exactly one slug per doc" but the
|
|
82
|
+
// convention is overwhelming.
|
|
83
|
+
if (slug === undefined && typeof visited === 'string' && isSlugField(rawValue)) {
|
|
84
|
+
slug = visited;
|
|
85
|
+
}
|
|
86
|
+
data[key] = visited;
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
collection,
|
|
90
|
+
...(slug !== undefined ? { slug } : {}),
|
|
91
|
+
data,
|
|
92
|
+
sourceId: doc._id,
|
|
93
|
+
sourceType: doc._type,
|
|
94
|
+
warnings,
|
|
95
|
+
unresolved,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
function visit(value, ctx) {
|
|
99
|
+
if (value === null || value === undefined)
|
|
100
|
+
return value;
|
|
101
|
+
if (typeof value !== 'object')
|
|
102
|
+
return value;
|
|
103
|
+
if (Array.isArray(value)) {
|
|
104
|
+
if (isPortableTextArray(value)) {
|
|
105
|
+
// Walk for broken refs but pass the array through unchanged.
|
|
106
|
+
const hits = collectReferences(value, ctx.path);
|
|
107
|
+
for (const hit of hits) {
|
|
108
|
+
if (ctx.context.collectionByDocId.has(hit.ref))
|
|
109
|
+
continue;
|
|
110
|
+
if (hit.weak)
|
|
111
|
+
continue;
|
|
112
|
+
ctx.warnings.push({
|
|
113
|
+
source: { ...ctx.source },
|
|
114
|
+
code: 'unresolved-portable-text-ref',
|
|
115
|
+
message: `Portable Text reference at ${hit.path} → '${hit.ref}' not present in this export.`,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
return value;
|
|
119
|
+
}
|
|
120
|
+
return value.map((item, i) => visit(item, { ...ctx, path: `${ctx.path}[${i}]` }));
|
|
121
|
+
}
|
|
122
|
+
// Tagged object — Sanity uses `_type` as the discriminator on
|
|
123
|
+
// everything that needs structural rewriting (slug, reference,
|
|
124
|
+
// image, file).
|
|
125
|
+
const tagged = value;
|
|
126
|
+
const typeTag = typeof tagged._type === 'string' ? tagged._type : null;
|
|
127
|
+
if (typeTag === 'slug' && typeof tagged.current === 'string') {
|
|
128
|
+
return tagged.current;
|
|
129
|
+
}
|
|
130
|
+
if (typeTag === 'reference' && isReference(value)) {
|
|
131
|
+
return resolveReference(value, ctx);
|
|
132
|
+
}
|
|
133
|
+
if (typeTag === 'image') {
|
|
134
|
+
return rewriteAsset(tagged, 'image', ctx);
|
|
135
|
+
}
|
|
136
|
+
if (typeTag === 'file') {
|
|
137
|
+
return rewriteAsset(tagged, 'file', ctx);
|
|
138
|
+
}
|
|
139
|
+
// Plain object — recurse, preserving any `_type` tag verbatim so
|
|
140
|
+
// downstream consumers can still discriminate on it.
|
|
141
|
+
const out = {};
|
|
142
|
+
for (const [k, v] of Object.entries(tagged)) {
|
|
143
|
+
out[k] = visit(v, { ...ctx, path: `${ctx.path}.${k}` });
|
|
144
|
+
}
|
|
145
|
+
return out;
|
|
146
|
+
}
|
|
147
|
+
function isSlugField(raw) {
|
|
148
|
+
if (raw === null || typeof raw !== 'object' || Array.isArray(raw))
|
|
149
|
+
return false;
|
|
150
|
+
const r = raw;
|
|
151
|
+
return r._type === 'slug' && typeof r.current === 'string';
|
|
152
|
+
}
|
|
153
|
+
function resolveReference(ref, ctx) {
|
|
154
|
+
const targetCollection = ctx.context.collectionByDocId.get(ref._ref);
|
|
155
|
+
if (targetCollection) {
|
|
156
|
+
return {
|
|
157
|
+
collection: targetCollection,
|
|
158
|
+
sourceId: ref._ref,
|
|
159
|
+
...(ref._weak ? { weak: true } : {}),
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
// Weak refs are allowed to dangle by Sanity convention — surface
|
|
163
|
+
// them as a low-noise warning rather than an unresolved field.
|
|
164
|
+
if (ref._weak) {
|
|
165
|
+
ctx.warnings.push({
|
|
166
|
+
source: { ...ctx.source },
|
|
167
|
+
code: 'weak-reference-unresolved',
|
|
168
|
+
message: `Weak reference at ${ctx.path} → '${ref._ref}' not present in this export.`,
|
|
169
|
+
});
|
|
170
|
+
return ref;
|
|
171
|
+
}
|
|
172
|
+
ctx.unresolved.push({
|
|
173
|
+
source: { ...ctx.source, field: ctx.path },
|
|
174
|
+
code: 'unresolved-reference',
|
|
175
|
+
reason: `Reference → '${ref._ref}' not present in this export.`,
|
|
176
|
+
rawValue: ref,
|
|
177
|
+
hint: 'Include the target document in the export, or mark the field weak in Sanity.',
|
|
178
|
+
});
|
|
179
|
+
return ref;
|
|
180
|
+
}
|
|
181
|
+
function rewriteAsset(raw, kind, ctx) {
|
|
182
|
+
const asset = raw.asset;
|
|
183
|
+
if (!isReference(asset)) {
|
|
184
|
+
// `image` / `file` blocks should always carry an asset; if
|
|
185
|
+
// they don't, leave the object untouched (no point losing
|
|
186
|
+
// alt-text / other metadata) and warn.
|
|
187
|
+
ctx.warnings.push({
|
|
188
|
+
source: { ...ctx.source },
|
|
189
|
+
code: 'asset-missing',
|
|
190
|
+
message: `${kind} at ${ctx.path} is missing an asset ref — passing through unchanged.`,
|
|
191
|
+
});
|
|
192
|
+
return raw;
|
|
193
|
+
}
|
|
194
|
+
const out = { ...raw, assetId: asset._ref };
|
|
195
|
+
// Always try the explicit-URL lookup first — Sanity exports
|
|
196
|
+
// sometimes include URLs on the asset docs even when the asset
|
|
197
|
+
// ref doesn't match the canonical encoded format.
|
|
198
|
+
const declaredUrl = ctx.context.assetUrlsById.get(asset._ref);
|
|
199
|
+
// Parse the ref for dimensions / extension. Failure is non-fatal:
|
|
200
|
+
// we still emit the assetId + any declared URL, just without the
|
|
201
|
+
// derived metadata.
|
|
202
|
+
const parsed = parseAssetRef(asset._ref);
|
|
203
|
+
if (parsed) {
|
|
204
|
+
if (parsed.kind === 'image') {
|
|
205
|
+
if (parsed.width)
|
|
206
|
+
out.width = parsed.width;
|
|
207
|
+
if (parsed.height)
|
|
208
|
+
out.height = parsed.height;
|
|
209
|
+
}
|
|
210
|
+
out.extension = parsed.extension;
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
ctx.warnings.push({
|
|
214
|
+
source: { ...ctx.source },
|
|
215
|
+
code: 'asset-ref-unrecognized',
|
|
216
|
+
message: `${kind} asset ref '${asset._ref}' at ${ctx.path} doesn't match Sanity's encoded format — emitting assetId only.`,
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
if (declaredUrl) {
|
|
220
|
+
out.url = declaredUrl;
|
|
221
|
+
}
|
|
222
|
+
else if (parsed) {
|
|
223
|
+
const derived = assetUrlFrom(parsed, ctx.config);
|
|
224
|
+
if (derived) {
|
|
225
|
+
out.url = derived;
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
ctx.warnings.push({
|
|
229
|
+
source: { ...ctx.source },
|
|
230
|
+
code: 'asset-url-unresolved',
|
|
231
|
+
message: `Cannot derive URL for ${kind} '${asset._ref}' at ${ctx.path}. Set config.projectId + dataset, config.assetBaseUrl, or include the asset URL in the export.`,
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
// No declared URL, no parseable ref — operator must intervene.
|
|
237
|
+
ctx.warnings.push({
|
|
238
|
+
source: { ...ctx.source },
|
|
239
|
+
code: 'asset-url-unresolved',
|
|
240
|
+
message: `Cannot derive URL for ${kind} '${asset._ref}' at ${ctx.path}. Set config.projectId + dataset, config.assetBaseUrl, or include the asset URL in the export.`,
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
return out;
|
|
244
|
+
}
|
|
245
|
+
//# sourceMappingURL=map.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"map.js","sourceRoot":"","sources":["../../../src/adapters/sanity/map.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAGH,OAAO,EAA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AE,GAAG,GAAG,CAAC,MAAM,EAAE;gBACzB,IAAI,EAAE,sBAAsB;gBAC5B,OAAO,EAAE,yBAAyB,IAAI,KAAK,KAAK,CAAC,IAAI,QAAQ,GAAG,CAAC,IAAI,gGAAgG;aACtK,CAAC,CAAA;QACJ,CAAC;IACH,CAAC;SAAM,CAAC;QACN,+DAA+D;QAC/D,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC;YAChB,MAAM,EAAE,EAAE,GAAG,GAAG,CAAC,MAAM,EAAE;YACzB,IAAI,EAAE,sBAAsB;YAC5B,OAAO,EAAE,yBAAyB,IAAI,KAAK,KAAK,CAAC,IAAI,QAAQ,GAAG,CAAC,IAAI,gGAAgG;SACtK,CAAC,CAAA;IACJ,CAAC;IACD,OAAO,GAAG,CAAA;AACZ,CAAC"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NDJSON parser for Sanity dataset exports.
|
|
3
|
+
*
|
|
4
|
+
* Sanity exports are newline-delimited JSON: one document per line.
|
|
5
|
+
* Real exports tend to be 100s of MB, so the parser is deliberately
|
|
6
|
+
* tolerant — a single bad line surfaces as a per-line error instead
|
|
7
|
+
* of aborting the whole import.
|
|
8
|
+
*
|
|
9
|
+
* Returns the parsed records alongside any per-line errors so the
|
|
10
|
+
* adapter can report them as `UnresolvedField` entries.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Shape the parser is willing to commit to: a string-keyed object.
|
|
14
|
+
* The narrow `SanityDoc` type (which requires `_id` and `_type` to be
|
|
15
|
+
* strings) is *not* used here — the parser doesn't validate those
|
|
16
|
+
* fields, so claiming it returned a `SanityDoc` would be a type lie.
|
|
17
|
+
* The adapter narrows `LooseRecord` to `SanityDoc` itself once it
|
|
18
|
+
* has verified the system fields.
|
|
19
|
+
*/
|
|
20
|
+
export type LooseRecord = Record<string, unknown>;
|
|
21
|
+
export interface NdjsonParseError {
|
|
22
|
+
/** 1-based line number in the source file. */
|
|
23
|
+
line: number;
|
|
24
|
+
/** Message from `JSON.parse`. */
|
|
25
|
+
message: string;
|
|
26
|
+
/** First ~120 chars of the offending line so reports stay readable. */
|
|
27
|
+
preview: string;
|
|
28
|
+
}
|
|
29
|
+
export interface NdjsonParseResult {
|
|
30
|
+
/**
|
|
31
|
+
* Records the parser successfully turned into objects. The shape
|
|
32
|
+
* is intentionally loose; the adapter does its own `_id`/`_type`
|
|
33
|
+
* shape check and downgrades non-conforming records to
|
|
34
|
+
* `invalid-shape` unresolved entries.
|
|
35
|
+
*/
|
|
36
|
+
records: LooseRecord[];
|
|
37
|
+
/**
|
|
38
|
+
* Per-line parse failures. The adapter reports each as an
|
|
39
|
+
* `invalid-ndjson-line` unresolved entry and includes them in
|
|
40
|
+
* `summary.totalRead` so the count of read-attempts isn't silently
|
|
41
|
+
* deflated when lines are malformed.
|
|
42
|
+
*/
|
|
43
|
+
errors: NdjsonParseError[];
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Parse an NDJSON string into loose records. Blank lines (and lines
|
|
47
|
+
* that are only whitespace) are silently ignored — Sanity sometimes
|
|
48
|
+
* includes trailing newlines and we shouldn't turn those into
|
|
49
|
+
* errors.
|
|
50
|
+
*
|
|
51
|
+
* Records that parse but don't look like Sanity docs (missing `_id`
|
|
52
|
+
* / `_type`) are still returned; the adapter classifies them as
|
|
53
|
+
* `invalid-shape` rather than the parser pre-filtering them. That
|
|
54
|
+
* way the per-line error report only contains JSON-syntax errors,
|
|
55
|
+
* which is easier to reason about.
|
|
56
|
+
*/
|
|
57
|
+
export declare function parseNdjson(raw: string): NdjsonParseResult;
|
|
58
|
+
/**
|
|
59
|
+
* Convenience for the very common test pattern of "given a single
|
|
60
|
+
* record, behave as if it was the only line of an NDJSON file."
|
|
61
|
+
* Adapter code never uses this — only tests.
|
|
62
|
+
*/
|
|
63
|
+
export declare function toNdjson(records: object[]): string;
|
|
64
|
+
//# sourceMappingURL=parse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../../../src/adapters/sanity/parse.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH;;;;;;;GAOG;AACH,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;AAEjD,MAAM,WAAW,gBAAgB;IAC/B,8CAA8C;IAC9C,IAAI,EAAE,MAAM,CAAA;IACZ,iCAAiC;IACjC,OAAO,EAAE,MAAM,CAAA;IACf,uEAAuE;IACvE,OAAO,EAAE,MAAM,CAAA;CAChB;AAED,MAAM,WAAW,iBAAiB;IAChC;;;;;OAKG;IACH,OAAO,EAAE,WAAW,EAAE,CAAA;IACtB;;;;;OAKG;IACH,MAAM,EAAE,gBAAgB,EAAE,CAAA;CAC3B;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,iBAAiB,CAiC1D;AAOD;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,MAAM,CAElD"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NDJSON parser for Sanity dataset exports.
|
|
3
|
+
*
|
|
4
|
+
* Sanity exports are newline-delimited JSON: one document per line.
|
|
5
|
+
* Real exports tend to be 100s of MB, so the parser is deliberately
|
|
6
|
+
* tolerant — a single bad line surfaces as a per-line error instead
|
|
7
|
+
* of aborting the whole import.
|
|
8
|
+
*
|
|
9
|
+
* Returns the parsed records alongside any per-line errors so the
|
|
10
|
+
* adapter can report them as `UnresolvedField` entries.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Parse an NDJSON string into loose records. Blank lines (and lines
|
|
14
|
+
* that are only whitespace) are silently ignored — Sanity sometimes
|
|
15
|
+
* includes trailing newlines and we shouldn't turn those into
|
|
16
|
+
* errors.
|
|
17
|
+
*
|
|
18
|
+
* Records that parse but don't look like Sanity docs (missing `_id`
|
|
19
|
+
* / `_type`) are still returned; the adapter classifies them as
|
|
20
|
+
* `invalid-shape` rather than the parser pre-filtering them. That
|
|
21
|
+
* way the per-line error report only contains JSON-syntax errors,
|
|
22
|
+
* which is easier to reason about.
|
|
23
|
+
*/
|
|
24
|
+
export function parseNdjson(raw) {
|
|
25
|
+
const records = [];
|
|
26
|
+
const errors = [];
|
|
27
|
+
// Tolerate both LF and CRLF (Windows-edited exports happen).
|
|
28
|
+
const lines = raw.split(/\r?\n/);
|
|
29
|
+
for (let i = 0; i < lines.length; i++) {
|
|
30
|
+
const line = lines[i];
|
|
31
|
+
if (line.trim().length === 0)
|
|
32
|
+
continue;
|
|
33
|
+
try {
|
|
34
|
+
const parsed = JSON.parse(line);
|
|
35
|
+
if (parsed !== null && typeof parsed === 'object' && !Array.isArray(parsed)) {
|
|
36
|
+
records.push(parsed);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
// JSON-valid but not an object — exceedingly rare in Sanity
|
|
40
|
+
// exports, but worth surfacing so the operator sees it
|
|
41
|
+
// explicitly rather than getting a confused warning later.
|
|
42
|
+
errors.push({
|
|
43
|
+
line: i + 1,
|
|
44
|
+
message: 'NDJSON line is not a JSON object',
|
|
45
|
+
preview: preview(line),
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
catch (err) {
|
|
50
|
+
errors.push({
|
|
51
|
+
line: i + 1,
|
|
52
|
+
message: err.message,
|
|
53
|
+
preview: preview(line),
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return { records, errors };
|
|
58
|
+
}
|
|
59
|
+
function preview(line) {
|
|
60
|
+
const trimmed = line.length > 120 ? line.slice(0, 117) + '...' : line;
|
|
61
|
+
return trimmed;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Convenience for the very common test pattern of "given a single
|
|
65
|
+
* record, behave as if it was the only line of an NDJSON file."
|
|
66
|
+
* Adapter code never uses this — only tests.
|
|
67
|
+
*/
|
|
68
|
+
export function toNdjson(records) {
|
|
69
|
+
return records.map((r) => JSON.stringify(r)).join('\n') + '\n';
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=parse.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse.js","sourceRoot":"","sources":["../../../src/adapters/sanity/parse.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAsCH;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,WAAW,CAAC,GAAW;IACrC,MAAM,OAAO,GAAkB,EAAE,CAAA;IACjC,MAAM,MAAM,GAAuB,EAAE,CAAA;IACrC,6DAA6D;IAC7D,MAAM,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;IAEhC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACtC,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAE,CAAA;QACtB,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC,MAAM,KAAK,CAAC;YAAE,SAAQ;QACtC,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAY,CAAA;YAC1C,IAAI,MAAM,KAAK,IAAI,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;gBAC5E,OAAO,CAAC,IAAI,CAAC,MAAqB,CAAC,CAAA;YACrC,CAAC;iBAAM,CAAC;gBACN,4DAA4D;gBAC5D,uDAAuD;gBACvD,2DAA2D;gBAC3D,MAAM,CAAC,IAAI,CAAC;oBACV,IAAI,EAAE,CAAC,GAAG,CAAC;oBACX,OAAO,EAAE,kCAAkC;oBAC3C,OAAO,EAAE,OAAO,CAAC,IAAI,CAAC;iBACvB,CAAC,CAAA;YACJ,CAAC;QACH,C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