@sanity/import
Version:
Import documents to a Sanity dataset
65 lines (64 loc) • 2.46 kB
JavaScript
import { pathToFileURL } from 'node:url';
import { extractWithPath } from '@sanity/mutator';
import { serializePath } from './serializePath.js';
import { deepGet, deepSet, deepUnset } from './util/deepGet.js';
const assetKey = '_sanityAsset';
const assetMatcher = /^(file|image)@([a-z]+:\/\/.*)/;
// Note: mutates in-place
export function unsetAssetRefs(doc) {
for (const path of findAssetRefs(doc)){
const parentPath = path.slice(0, -1);
const parent = deepGet(doc, parentPath);
// If the only key in the object is `_sanityAsset`, unset the whole thing,
// as we will be using a `setIfMissing({[path]: {}})` patch to enforce it.
// Prevents empty objects from appearing while import is running
const isOnlyKey = parent && Object.keys(parent).length === 1 && parent[assetKey];
const unsetPath = isOnlyKey ? parentPath : path;
deepUnset(doc, unsetPath);
}
return doc;
}
// Note: mutates in-place
export function absolutifyPaths(doc, absPath) {
if (!absPath) {
return doc;
}
const modifier = (value)=>value.replace(/file:\/\/\.\//i, `${pathToFileURL(absPath).href}/`).replace(/(https?):\/\/\.\//, `$1://${absPath}/`);
for (const path of findAssetRefs(doc)){
const value = deepGet(doc, path);
deepSet(doc, path, modifier(value));
}
return doc;
}
export function getAssetRefs(doc) {
return findAssetRefs(doc).map((path)=>validateAssetImportKey(path, doc)).map((path)=>{
const value = deepGet(doc, path);
return {
documentId: doc._id,
path: serializePath({
path: path.filter((segment)=>isNotAssetKey(segment))
}),
type: value.replace(assetMatcher, '$1'),
url: value.replace(assetMatcher, '$2')
};
});
}
function isNotAssetKey(segment) {
return segment !== assetKey;
}
function findAssetRefs(doc) {
return extractWithPath(`..[${assetKey}]`, doc).map((match)=>match.path);
}
function validateAssetImportKey(path, doc) {
if (!assetMatcher.test(deepGet(doc, path))) {
throw new Error([
'Asset type is not specified.',
'`_sanityAsset` values must be prefixed with a type, eg image@url or file@url.',
`See document with ID "${doc._id}", path: ${serializePath({
path
})}`
].join('\n'));
}
return path;
}
//# sourceMappingURL=assetRefs.js.map