@liiift-studio/sanity-font-manager 2.3.19 → 2.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/README.md +437 -437
- package/dist/UploadModal-6LIX7XOK.js +6 -0
- package/dist/UploadModal-NME2W53V.mjs +6 -0
- package/dist/chunk-646WCBRR.mjs +7276 -0
- package/dist/chunk-FH4QKHOH.js +7276 -0
- package/dist/index.js +747 -1675
- package/dist/index.mjs +400 -1237
- package/package.json +85 -85
- package/src/components/BatchUploadFonts.jsx +653 -639
- package/src/components/BulkActions.jsx +99 -0
- package/src/components/ExistingDocumentResolver.jsx +152 -0
- package/src/components/FontReviewCard.jsx +415 -0
- package/src/components/FontScriptUploaderComponent.jsx +463 -463
- package/src/components/GenerateCollectionsPairsComponent.jsx +259 -259
- package/src/components/KeyValueInput.jsx +95 -95
- package/src/components/KeyValueReferenceInput.jsx +254 -254
- package/src/components/NestedObjectArraySelector.jsx +146 -146
- package/src/components/PriceInput.jsx +26 -26
- package/src/components/PrimaryCollectionGeneratorTypeface.jsx +116 -116
- package/src/components/RegenerateSubfamiliesComponent.jsx +185 -185
- package/src/components/SetOTF.jsx +87 -87
- package/src/components/SingleUploaderTool.jsx +672 -673
- package/src/components/StatusDisplay.jsx +26 -26
- package/src/components/StyleCountInput.jsx +16 -16
- package/src/components/UpdateScriptsComponent.jsx +76 -76
- package/src/components/UploadButton.jsx +43 -43
- package/src/components/UploadModal.jsx +268 -0
- package/src/components/UploadScriptsComponent.jsx +539 -537
- package/src/components/UploadStep1Settings.jsx +272 -0
- package/src/components/UploadStep2Review.jsx +472 -0
- package/src/components/UploadStep3Execute.jsx +234 -0
- package/src/components/UploadSummary.jsx +196 -0
- package/src/components/VariableInstanceReferencesInput.jsx +190 -190
- package/src/hooks/useNestedObjects.js +92 -92
- package/src/hooks/useSanityClient.js +9 -9
- package/src/index.js +115 -70
- package/src/schema/openTypeField.js +1945 -1945
- package/src/schema/styleCountField.js +12 -12
- package/src/schema/stylesField.js +268 -268
- package/src/schema/stylisticSetField.js +301 -301
- package/src/utils/buildUploadPlan.js +325 -0
- package/src/utils/executeUploadPlan.js +437 -0
- package/src/utils/executionReducer.js +56 -0
- package/src/utils/fontHelpers.js +267 -0
- package/src/utils/generateCssFile.js +207 -205
- package/src/utils/generateFontData.js +98 -145
- package/src/utils/generateFontFile.js +38 -38
- package/src/utils/generateKeywords.js +185 -185
- package/src/utils/generateSubset.js +45 -45
- package/src/utils/getEmptyFontKit.js +101 -99
- package/src/utils/parseFont.js +55 -0
- package/src/utils/parseVariableFontInstances.js +211 -211
- package/src/utils/planReducer.js +517 -0
- package/src/utils/planTypes.js +183 -0
- package/src/utils/processFontFiles.js +529 -477
- package/src/utils/regenerateFontData.js +146 -146
- package/src/utils/resolveExistingFont.js +87 -0
- package/src/utils/sanitizeForSanityId.js +65 -65
- package/src/utils/updateFontPrices.js +94 -94
- package/src/utils/updateTypefaceDocument.js +149 -160
- package/src/utils/uploadFontFiles.js +405 -316
- package/src/utils/utils.js +24 -24
|
@@ -1,146 +1,146 @@
|
|
|
1
|
-
// Renames font document IDs across a typeface when a typeface slug changes
|
|
2
|
-
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
readFontFile,
|
|
6
|
-
extractFontMetadata,
|
|
7
|
-
} from './processFontFiles';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Re-downloads TTF/OTF files for all fonts in a typeface and re-extracts their metadata,
|
|
11
|
-
* patching each font document's title, slug, weightName, and subfamily.
|
|
12
|
-
*
|
|
13
|
-
* @param {Object} params
|
|
14
|
-
* @param {Object} params.client - Sanity client
|
|
15
|
-
* @param {string} params.typefaceName
|
|
16
|
-
* @param {Object} params.slug - Typeface slug object
|
|
17
|
-
* @param {string[]} params.weightKeywordList
|
|
18
|
-
* @param {string[]} params.italicKeywordList
|
|
19
|
-
* @param {boolean} params.preserveShortenedNames
|
|
20
|
-
* @param {Function} params.setStatus
|
|
21
|
-
* @param {Function} params.setError
|
|
22
|
-
* @returns {Promise<Object>}
|
|
23
|
-
*/
|
|
24
|
-
export const renameFontDocuments = async ({
|
|
25
|
-
client,
|
|
26
|
-
typefaceName,
|
|
27
|
-
slug,
|
|
28
|
-
weightKeywordList,
|
|
29
|
-
italicKeywordList,
|
|
30
|
-
preserveShortenedNames = false,
|
|
31
|
-
setStatus,
|
|
32
|
-
setError,
|
|
33
|
-
}) => {
|
|
34
|
-
try {
|
|
35
|
-
if (!typefaceName) {
|
|
36
|
-
setStatus('Typeface needs a title');
|
|
37
|
-
setError(true);
|
|
38
|
-
console.error('Typeface needs title');
|
|
39
|
-
return { success: false, message: 'Typeface needs title' };
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
setStatus('Fetching font documents...');
|
|
43
|
-
const slugCurrent = slug?.current || typefaceName;
|
|
44
|
-
|
|
45
|
-
const query = await client.fetch(
|
|
46
|
-
`*[_type == "typeface" && slug.current == $slugCurrent][0]{
|
|
47
|
-
"fonts": styles.fonts[]->{ _id, title, subfamily, fileInput, style, weight, _key }
|
|
48
|
-
}`,
|
|
49
|
-
{ slugCurrent }
|
|
50
|
-
);
|
|
51
|
-
const fontDocuments = query?.fonts || [];
|
|
52
|
-
|
|
53
|
-
if (fontDocuments.length === 0) {
|
|
54
|
-
setStatus('No font documents found for this typeface');
|
|
55
|
-
setError(true);
|
|
56
|
-
return { success: false, message: 'No font documents found for this typeface' };
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
setStatus(`Found ${fontDocuments.length} font documents to process...`);
|
|
60
|
-
|
|
61
|
-
let updatedCount = 0;
|
|
62
|
-
let skippedCount = 0;
|
|
63
|
-
let errorCount = 0;
|
|
64
|
-
|
|
65
|
-
for (let i = 0; i < fontDocuments.length; i++) {
|
|
66
|
-
const fontDoc = fontDocuments[i];
|
|
67
|
-
setStatus(`Processing font ${i + 1}/${fontDocuments.length}: ${fontDoc.title}`);
|
|
68
|
-
|
|
69
|
-
if (!fontDoc.fileInput?.ttf?.asset?._ref && !fontDoc.fileInput?.otf?.asset?._ref) {
|
|
70
|
-
console.log(`Skipping ${fontDoc.title} — no TTF or OTF file found`);
|
|
71
|
-
skippedCount++;
|
|
72
|
-
continue;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
try {
|
|
76
|
-
let file;
|
|
77
|
-
let ttfAsset;
|
|
78
|
-
let otfAsset;
|
|
79
|
-
|
|
80
|
-
if (fontDoc.fileInput?.ttf?.asset?._ref) {
|
|
81
|
-
ttfAsset = await client.getDocument(fontDoc.fileInput.ttf.asset._ref);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
if (!ttfAsset?.url) {
|
|
85
|
-
if (fontDoc.fileInput?.otf?.asset?._ref) {
|
|
86
|
-
otfAsset = await client.getDocument(fontDoc.fileInput.otf.asset._ref);
|
|
87
|
-
}
|
|
88
|
-
if (!otfAsset?.url) {
|
|
89
|
-
console.log(`Skipping ${fontDoc.title} — no TTF or OTF URL`);
|
|
90
|
-
skippedCount++;
|
|
91
|
-
continue;
|
|
92
|
-
}
|
|
93
|
-
setStatus(`Fetching OTF file for ${fontDoc.title}...`);
|
|
94
|
-
const res = await fetch(otfAsset.url);
|
|
95
|
-
file = await res.blob();
|
|
96
|
-
} else {
|
|
97
|
-
setStatus(`Fetching TTF file for ${fontDoc.title}...`);
|
|
98
|
-
const res = await fetch(ttfAsset.url);
|
|
99
|
-
file = await res.blob();
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const fontBuffer = await readFontFile(file);
|
|
103
|
-
const font =
|
|
104
|
-
|
|
105
|
-
const { weightName, subfamilyName, fontTitle } = extractFontMetadata(
|
|
106
|
-
font,
|
|
107
|
-
typefaceName,
|
|
108
|
-
weightKeywordList,
|
|
109
|
-
italicKeywordList,
|
|
110
|
-
preserveShortenedNames,
|
|
111
|
-
);
|
|
112
|
-
|
|
113
|
-
const slugValue = fontTitle.toLowerCase().replace(/\s+/g, '-');
|
|
114
|
-
|
|
115
|
-
setStatus(`Updating font ${i + 1}/${fontDocuments.length}: ${fontTitle}`);
|
|
116
|
-
await client.patch(fontDoc._id)
|
|
117
|
-
.set({
|
|
118
|
-
title: fontTitle,
|
|
119
|
-
slug: { _type: 'slug', current: slugValue },
|
|
120
|
-
weightName: weightName,
|
|
121
|
-
subfamily: subfamilyName,
|
|
122
|
-
})
|
|
123
|
-
.commit();
|
|
124
|
-
|
|
125
|
-
updatedCount++;
|
|
126
|
-
} catch (err) {
|
|
127
|
-
console.error(`Error processing ${fontDoc.title}:`, err);
|
|
128
|
-
errorCount++;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
const resultMessage = `Renamed ${updatedCount} of ${fontDocuments.length} font documents (${skippedCount} skipped, ${errorCount} errors)`;
|
|
133
|
-
setStatus(resultMessage);
|
|
134
|
-
|
|
135
|
-
return {
|
|
136
|
-
success: true,
|
|
137
|
-
message: resultMessage,
|
|
138
|
-
stats: { total: fontDocuments.length, updated: updatedCount, skipped: skippedCount, errors: errorCount },
|
|
139
|
-
};
|
|
140
|
-
} catch (err) {
|
|
141
|
-
console.error('Error renaming font documents:', err);
|
|
142
|
-
setError(true);
|
|
143
|
-
setStatus(`Error: ${err.message}`);
|
|
144
|
-
return { success: false, message: `Error: ${err.message}` };
|
|
145
|
-
}
|
|
146
|
-
};
|
|
1
|
+
// Renames font document IDs across a typeface when a typeface slug changes
|
|
2
|
+
|
|
3
|
+
import { parseFont } from './parseFont';
|
|
4
|
+
import {
|
|
5
|
+
readFontFile,
|
|
6
|
+
extractFontMetadata,
|
|
7
|
+
} from './processFontFiles';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Re-downloads TTF/OTF files for all fonts in a typeface and re-extracts their metadata,
|
|
11
|
+
* patching each font document's title, slug, weightName, and subfamily.
|
|
12
|
+
*
|
|
13
|
+
* @param {Object} params
|
|
14
|
+
* @param {Object} params.client - Sanity client
|
|
15
|
+
* @param {string} params.typefaceName
|
|
16
|
+
* @param {Object} params.slug - Typeface slug object
|
|
17
|
+
* @param {string[]} params.weightKeywordList
|
|
18
|
+
* @param {string[]} params.italicKeywordList
|
|
19
|
+
* @param {boolean} params.preserveShortenedNames
|
|
20
|
+
* @param {Function} params.setStatus
|
|
21
|
+
* @param {Function} params.setError
|
|
22
|
+
* @returns {Promise<Object>}
|
|
23
|
+
*/
|
|
24
|
+
export const renameFontDocuments = async ({
|
|
25
|
+
client,
|
|
26
|
+
typefaceName,
|
|
27
|
+
slug,
|
|
28
|
+
weightKeywordList,
|
|
29
|
+
italicKeywordList,
|
|
30
|
+
preserveShortenedNames = false,
|
|
31
|
+
setStatus,
|
|
32
|
+
setError,
|
|
33
|
+
}) => {
|
|
34
|
+
try {
|
|
35
|
+
if (!typefaceName) {
|
|
36
|
+
setStatus('Typeface needs a title');
|
|
37
|
+
setError(true);
|
|
38
|
+
console.error('Typeface needs title');
|
|
39
|
+
return { success: false, message: 'Typeface needs title' };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
setStatus('Fetching font documents...');
|
|
43
|
+
const slugCurrent = slug?.current || typefaceName;
|
|
44
|
+
|
|
45
|
+
const query = await client.fetch(
|
|
46
|
+
`*[_type == "typeface" && slug.current == $slugCurrent][0]{
|
|
47
|
+
"fonts": styles.fonts[]->{ _id, title, subfamily, fileInput, style, weight, _key }
|
|
48
|
+
}`,
|
|
49
|
+
{ slugCurrent }
|
|
50
|
+
);
|
|
51
|
+
const fontDocuments = query?.fonts || [];
|
|
52
|
+
|
|
53
|
+
if (fontDocuments.length === 0) {
|
|
54
|
+
setStatus('No font documents found for this typeface');
|
|
55
|
+
setError(true);
|
|
56
|
+
return { success: false, message: 'No font documents found for this typeface' };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
setStatus(`Found ${fontDocuments.length} font documents to process...`);
|
|
60
|
+
|
|
61
|
+
let updatedCount = 0;
|
|
62
|
+
let skippedCount = 0;
|
|
63
|
+
let errorCount = 0;
|
|
64
|
+
|
|
65
|
+
for (let i = 0; i < fontDocuments.length; i++) {
|
|
66
|
+
const fontDoc = fontDocuments[i];
|
|
67
|
+
setStatus(`Processing font ${i + 1}/${fontDocuments.length}: ${fontDoc.title}`);
|
|
68
|
+
|
|
69
|
+
if (!fontDoc.fileInput?.ttf?.asset?._ref && !fontDoc.fileInput?.otf?.asset?._ref) {
|
|
70
|
+
console.log(`Skipping ${fontDoc.title} — no TTF or OTF file found`);
|
|
71
|
+
skippedCount++;
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
let file;
|
|
77
|
+
let ttfAsset;
|
|
78
|
+
let otfAsset;
|
|
79
|
+
|
|
80
|
+
if (fontDoc.fileInput?.ttf?.asset?._ref) {
|
|
81
|
+
ttfAsset = await client.getDocument(fontDoc.fileInput.ttf.asset._ref);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (!ttfAsset?.url) {
|
|
85
|
+
if (fontDoc.fileInput?.otf?.asset?._ref) {
|
|
86
|
+
otfAsset = await client.getDocument(fontDoc.fileInput.otf.asset._ref);
|
|
87
|
+
}
|
|
88
|
+
if (!otfAsset?.url) {
|
|
89
|
+
console.log(`Skipping ${fontDoc.title} — no TTF or OTF URL`);
|
|
90
|
+
skippedCount++;
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
setStatus(`Fetching OTF file for ${fontDoc.title}...`);
|
|
94
|
+
const res = await fetch(otfAsset.url);
|
|
95
|
+
file = await res.blob();
|
|
96
|
+
} else {
|
|
97
|
+
setStatus(`Fetching TTF file for ${fontDoc.title}...`);
|
|
98
|
+
const res = await fetch(ttfAsset.url);
|
|
99
|
+
file = await res.blob();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const fontBuffer = await readFontFile(file);
|
|
103
|
+
const font = await parseFont(fontBuffer, `${fontDoc._id}.ttf`);
|
|
104
|
+
|
|
105
|
+
const { weightName, subfamilyName, fontTitle } = extractFontMetadata(
|
|
106
|
+
font,
|
|
107
|
+
typefaceName,
|
|
108
|
+
weightKeywordList,
|
|
109
|
+
italicKeywordList,
|
|
110
|
+
preserveShortenedNames,
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
const slugValue = fontTitle.toLowerCase().replace(/\s+/g, '-');
|
|
114
|
+
|
|
115
|
+
setStatus(`Updating font ${i + 1}/${fontDocuments.length}: ${fontTitle}`);
|
|
116
|
+
await client.patch(fontDoc._id)
|
|
117
|
+
.set({
|
|
118
|
+
title: fontTitle,
|
|
119
|
+
slug: { _type: 'slug', current: slugValue },
|
|
120
|
+
weightName: weightName,
|
|
121
|
+
subfamily: subfamilyName,
|
|
122
|
+
})
|
|
123
|
+
.commit();
|
|
124
|
+
|
|
125
|
+
updatedCount++;
|
|
126
|
+
} catch (err) {
|
|
127
|
+
console.error(`Error processing ${fontDoc.title}:`, err);
|
|
128
|
+
errorCount++;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const resultMessage = `Renamed ${updatedCount} of ${fontDocuments.length} font documents (${skippedCount} skipped, ${errorCount} errors)`;
|
|
133
|
+
setStatus(resultMessage);
|
|
134
|
+
|
|
135
|
+
return {
|
|
136
|
+
success: true,
|
|
137
|
+
message: resultMessage,
|
|
138
|
+
stats: { total: fontDocuments.length, updated: updatedCount, skipped: skippedCount, errors: errorCount },
|
|
139
|
+
};
|
|
140
|
+
} catch (err) {
|
|
141
|
+
console.error('Error renaming font documents:', err);
|
|
142
|
+
setError(true);
|
|
143
|
+
setStatus(`Error: ${err.message}`);
|
|
144
|
+
return { success: false, message: `Error: ${err.message}` };
|
|
145
|
+
}
|
|
146
|
+
};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// Standalone document resolution — determines if a font already exists in Sanity
|
|
2
|
+
|
|
3
|
+
import { RECOMMENDATION } from './planTypes';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Resolves whether a font document already exists in Sanity, returning match details
|
|
7
|
+
* and a recommendation for how to proceed.
|
|
8
|
+
*
|
|
9
|
+
* Resolution strategies (in priority order):
|
|
10
|
+
* 1. Exact _id match or draft _id match or slug.current match
|
|
11
|
+
* 2. Content match by typefaceName + weightName + style + subfamily + variableFont
|
|
12
|
+
*
|
|
13
|
+
* @param {object} font - { _id, typefaceName, weightName, style, subfamily, variableFont, title }
|
|
14
|
+
* @param {object} client - Sanity client (parameterized queries only)
|
|
15
|
+
* @returns {Promise<{ exact: object|null, candidates: object[], recommendation: string, lookupFailed: boolean }>}
|
|
16
|
+
*/
|
|
17
|
+
export const resolveExistingFont = async (font, client) => {
|
|
18
|
+
const result = {
|
|
19
|
+
exact: null,
|
|
20
|
+
candidates: [],
|
|
21
|
+
recommendation: RECOMMENDATION.CREATE,
|
|
22
|
+
lookupFailed: false,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
try {
|
|
26
|
+
// Strategy 1: ID / slug match
|
|
27
|
+
const idMatches = await client.fetch(
|
|
28
|
+
`*[_type == 'font' && (_id == $id || _id == $draftId || slug.current == $id)]{
|
|
29
|
+
_id, title, weight, style, weightName, typefaceName, subfamily, variableFont,
|
|
30
|
+
fileInput, description, metaData, metrics, opentypeFeatures, characterSet,
|
|
31
|
+
scriptFileInput, variableInstanceReferences
|
|
32
|
+
}`,
|
|
33
|
+
{ id: font._id, draftId: `drafts.${font._id}` }
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
if (idMatches.length > 0) {
|
|
37
|
+
result.exact = idMatches[0];
|
|
38
|
+
result.recommendation = RECOMMENDATION.USE_EXACT;
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Strategy 2: Content match (only when ID query returns nothing)
|
|
43
|
+
const subfamily = font.subfamily || '';
|
|
44
|
+
const contentMatches = await client.fetch(
|
|
45
|
+
`*[_type == 'font'
|
|
46
|
+
&& lower(typefaceName) == lower($typefaceName)
|
|
47
|
+
&& lower(weightName) == lower($weightName)
|
|
48
|
+
&& lower(style) == lower($style)
|
|
49
|
+
&& (variableFont == $variableFont || (!defined(variableFont) && $variableFont == false))
|
|
50
|
+
&& (
|
|
51
|
+
lower(coalesce(subfamily, '')) == lower($subfamily)
|
|
52
|
+
|| (lower(coalesce(subfamily, '')) in ['', 'regular'] && lower($subfamily) in ['', 'regular'])
|
|
53
|
+
)
|
|
54
|
+
]{
|
|
55
|
+
_id, title, weight, style, weightName, typefaceName, subfamily, variableFont,
|
|
56
|
+
fileInput, description, metaData, metrics, opentypeFeatures, characterSet,
|
|
57
|
+
scriptFileInput, variableInstanceReferences
|
|
58
|
+
}`,
|
|
59
|
+
{
|
|
60
|
+
typefaceName: font.typefaceName,
|
|
61
|
+
weightName: font.weightName || '',
|
|
62
|
+
style: font.style || 'Regular',
|
|
63
|
+
variableFont: font.variableFont || false,
|
|
64
|
+
subfamily: subfamily === '' ? 'regular' : subfamily,
|
|
65
|
+
}
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
if (contentMatches.length === 1) {
|
|
69
|
+
result.candidates = contentMatches;
|
|
70
|
+
result.recommendation = RECOMMENDATION.USE_CANDIDATE;
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (contentMatches.length > 1) {
|
|
75
|
+
result.candidates = contentMatches;
|
|
76
|
+
result.recommendation = RECOMMENDATION.AMBIGUOUS;
|
|
77
|
+
console.warn(`Ambiguous font match for "${font.title}" — ${contentMatches.length} candidates found:`,
|
|
78
|
+
contentMatches.map(c => c._id));
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
} catch (err) {
|
|
82
|
+
console.error('Error resolving existing font:', font._id, err.message);
|
|
83
|
+
result.lookupFailed = true;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return result;
|
|
87
|
+
};
|
|
@@ -1,65 +1,65 @@
|
|
|
1
|
-
// Converts arbitrary strings into valid Sanity document IDs (lowercase, hyphens, no special characters)
|
|
2
|
-
import slugify from 'slugify';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Sanitizes a string into a valid Sanity document ID.
|
|
6
|
-
*
|
|
7
|
-
* Sanity ID requirements:
|
|
8
|
-
* - Must start with a letter or underscore (not a number or hyphen)
|
|
9
|
-
* - Can only contain lowercase letters (a-z), numbers (0-9), hyphens (-), and underscores (_)
|
|
10
|
-
* - Must be between 1 and 128 characters
|
|
11
|
-
*
|
|
12
|
-
* @param {string} str - The raw string (e.g. font title or filename) to sanitize
|
|
13
|
-
* @returns {string} A valid Sanity document ID
|
|
14
|
-
*/
|
|
15
|
-
export function sanitizeForSanityId(str) {
|
|
16
|
-
if (!str || typeof str !== 'string') {
|
|
17
|
-
return 'font-' + Date.now();
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
let sanitized = str.toLowerCase().trim();
|
|
21
|
-
|
|
22
|
-
// Replace common symbols before slugify
|
|
23
|
-
sanitized = sanitized.replace(/\+/g, 'plus');
|
|
24
|
-
sanitized = sanitized.replace(/&/g, 'and');
|
|
25
|
-
sanitized = sanitized.replace(/@/g, 'at');
|
|
26
|
-
|
|
27
|
-
sanitized = slugify(sanitized, {
|
|
28
|
-
replacement: '-',
|
|
29
|
-
remove: /[^\w\s-]/g,
|
|
30
|
-
lower: true,
|
|
31
|
-
strict: true,
|
|
32
|
-
locale: 'en',
|
|
33
|
-
trim: true,
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
// Strip any characters that still aren't lowercase-alphanumeric, hyphens, or underscores
|
|
37
|
-
sanitized = sanitized.replace(/[^a-z0-9\-_]/g, '-');
|
|
38
|
-
|
|
39
|
-
// Collapse repeated hyphens and strip leading/trailing hyphens or underscores
|
|
40
|
-
sanitized = sanitized.replace(/-+/g, '-');
|
|
41
|
-
sanitized = sanitized.replace(/^[-_]+|[-_]+$/g, '');
|
|
42
|
-
|
|
43
|
-
// IDs must not start with a number or hyphen
|
|
44
|
-
if (sanitized && !/^[a-z_]/.test(sanitized)) {
|
|
45
|
-
sanitized = 'font_' + sanitized;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
if (!sanitized) {
|
|
49
|
-
sanitized = 'font_' + Date.now();
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// Sanity hard-caps IDs at 128 characters
|
|
53
|
-
if (sanitized.length > 128) {
|
|
54
|
-
const hash = Math.random().toString(36).substring(2, 8);
|
|
55
|
-
sanitized = sanitized.substring(0, 120) + '_' + hash;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// Paranoid final validation
|
|
59
|
-
if (!/^[a-z_][a-z0-9\-_]*$/.test(sanitized)) {
|
|
60
|
-
console.warn(`ID sanitization produced invalid result: "${sanitized}", using fallback`);
|
|
61
|
-
sanitized = 'font_' + Date.now();
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return sanitized;
|
|
65
|
-
}
|
|
1
|
+
// Converts arbitrary strings into valid Sanity document IDs (lowercase, hyphens, no special characters)
|
|
2
|
+
import slugify from 'slugify';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Sanitizes a string into a valid Sanity document ID.
|
|
6
|
+
*
|
|
7
|
+
* Sanity ID requirements:
|
|
8
|
+
* - Must start with a letter or underscore (not a number or hyphen)
|
|
9
|
+
* - Can only contain lowercase letters (a-z), numbers (0-9), hyphens (-), and underscores (_)
|
|
10
|
+
* - Must be between 1 and 128 characters
|
|
11
|
+
*
|
|
12
|
+
* @param {string} str - The raw string (e.g. font title or filename) to sanitize
|
|
13
|
+
* @returns {string} A valid Sanity document ID
|
|
14
|
+
*/
|
|
15
|
+
export function sanitizeForSanityId(str) {
|
|
16
|
+
if (!str || typeof str !== 'string') {
|
|
17
|
+
return 'font-' + Date.now();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let sanitized = str.toLowerCase().trim();
|
|
21
|
+
|
|
22
|
+
// Replace common symbols before slugify
|
|
23
|
+
sanitized = sanitized.replace(/\+/g, 'plus');
|
|
24
|
+
sanitized = sanitized.replace(/&/g, 'and');
|
|
25
|
+
sanitized = sanitized.replace(/@/g, 'at');
|
|
26
|
+
|
|
27
|
+
sanitized = slugify(sanitized, {
|
|
28
|
+
replacement: '-',
|
|
29
|
+
remove: /[^\w\s-]/g,
|
|
30
|
+
lower: true,
|
|
31
|
+
strict: true,
|
|
32
|
+
locale: 'en',
|
|
33
|
+
trim: true,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// Strip any characters that still aren't lowercase-alphanumeric, hyphens, or underscores
|
|
37
|
+
sanitized = sanitized.replace(/[^a-z0-9\-_]/g, '-');
|
|
38
|
+
|
|
39
|
+
// Collapse repeated hyphens and strip leading/trailing hyphens or underscores
|
|
40
|
+
sanitized = sanitized.replace(/-+/g, '-');
|
|
41
|
+
sanitized = sanitized.replace(/^[-_]+|[-_]+$/g, '');
|
|
42
|
+
|
|
43
|
+
// IDs must not start with a number or hyphen
|
|
44
|
+
if (sanitized && !/^[a-z_]/.test(sanitized)) {
|
|
45
|
+
sanitized = 'font_' + sanitized;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (!sanitized) {
|
|
49
|
+
sanitized = 'font_' + Date.now();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Sanity hard-caps IDs at 128 characters
|
|
53
|
+
if (sanitized.length > 128) {
|
|
54
|
+
const hash = Math.random().toString(36).substring(2, 8);
|
|
55
|
+
sanitized = sanitized.substring(0, 120) + '_' + hash;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Paranoid final validation
|
|
59
|
+
if (!/^[a-z_][a-z0-9\-_]*$/.test(sanitized)) {
|
|
60
|
+
console.warn(`ID sanitization produced invalid result: "${sanitized}", using fallback`);
|
|
61
|
+
sanitized = 'font_' + Date.now();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return sanitized;
|
|
65
|
+
}
|