@liiift-studio/sanity-font-manager 2.2.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 +263 -0
- package/dist/index.js +3699 -0
- package/dist/index.mjs +3622 -0
- package/package.json +77 -0
- package/src/components/BatchUploadFonts.jsx +639 -0
- package/src/components/FontScriptUploaderComponent.jsx +463 -0
- package/src/components/GenerateCollectionsPairsComponent.jsx +259 -0
- package/src/components/PriceInput.jsx +26 -0
- package/src/components/RegenerateSubfamiliesComponent.jsx +185 -0
- package/src/components/SingleUploaderTool.jsx +673 -0
- package/src/components/StatusDisplay.jsx +26 -0
- package/src/components/UpdateScriptsComponent.jsx +76 -0
- package/src/components/UploadButton.jsx +43 -0
- package/src/components/UploadScriptsComponent.jsx +537 -0
- package/src/hooks/useSanityClient.js +9 -0
- package/src/index.js +56 -0
- package/src/utils/generateCssFile.js +197 -0
- package/src/utils/generateFontData.js +145 -0
- package/src/utils/generateFontFile.js +38 -0
- package/src/utils/generateKeywords.js +185 -0
- package/src/utils/generateSubset.js +45 -0
- package/src/utils/getEmptyFontKit.js +99 -0
- package/src/utils/parseVariableFontInstances.js +211 -0
- package/src/utils/processFontFiles.js +477 -0
- package/src/utils/regenerateFontData.js +146 -0
- package/src/utils/sanitizeForSanityId.js +65 -0
- package/src/utils/updateFontPrices.js +94 -0
- package/src/utils/updateTypefaceDocument.js +160 -0
- package/src/utils/uploadFontFiles.js +316 -0
- package/src/utils/utils.js +16 -0
|
@@ -0,0 +1,477 @@
|
|
|
1
|
+
// Reads font files via FileReader, parses with fontkit, and builds the fontsObjects map — exports individual weight/style extraction helpers
|
|
2
|
+
|
|
3
|
+
import * as fontkit from 'fontkit';
|
|
4
|
+
import { nanoid } from 'nanoid';
|
|
5
|
+
import { expandAbbreviations, removeWeightNames, reverseSpellingLookup } from './generateKeywords';
|
|
6
|
+
import { sanitizeForSanityId } from './sanitizeForSanityId';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Reads a font file and returns its content as a Uint8Array.
|
|
10
|
+
* @param {File} file
|
|
11
|
+
* @returns {Promise<Uint8Array>}
|
|
12
|
+
*/
|
|
13
|
+
export const readFontFile = (file) => {
|
|
14
|
+
return new Promise((resolve, reject) => {
|
|
15
|
+
const reader = new FileReader();
|
|
16
|
+
reader.onload = (event) => { resolve(new Uint8Array(event.target.result)); };
|
|
17
|
+
reader.onerror = (error) => { reject(error); };
|
|
18
|
+
reader.readAsArrayBuffer(file);
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Processes an array of font files and extracts metadata for each.
|
|
24
|
+
* @param {File[]} files
|
|
25
|
+
* @param {string} title - Typeface title
|
|
26
|
+
* @param {string[]} weightKeywordList
|
|
27
|
+
* @param {string[]} italicKeywordList
|
|
28
|
+
* @param {Function} setStatus
|
|
29
|
+
* @param {boolean} preserveShortenedNames - Skip abbreviation expansion when true
|
|
30
|
+
* @param {boolean} preserveFileNames - Preserve original filename capitalization when true
|
|
31
|
+
* @returns {Promise<Object>}
|
|
32
|
+
*/
|
|
33
|
+
export const processFontFiles = async (
|
|
34
|
+
files,
|
|
35
|
+
title,
|
|
36
|
+
weightKeywordList,
|
|
37
|
+
italicKeywordList,
|
|
38
|
+
setStatus,
|
|
39
|
+
preserveShortenedNames = false,
|
|
40
|
+
preserveFileNames = false,
|
|
41
|
+
) => {
|
|
42
|
+
let failedFiles = [];
|
|
43
|
+
let subfamilies = {};
|
|
44
|
+
let fontsObjects = {};
|
|
45
|
+
let newPreferredStyle = { weight: -100, style: 'Italic', _ref: '' };
|
|
46
|
+
|
|
47
|
+
for (let i = 0; i < files.length; i++) {
|
|
48
|
+
const file = files[i];
|
|
49
|
+
const fontBuffer = await readFontFile(file);
|
|
50
|
+
const font = fontkit.create(fontBuffer);
|
|
51
|
+
|
|
52
|
+
console.log('File name: ', file.name);
|
|
53
|
+
|
|
54
|
+
if (file.name.endsWith('.woff2') || file.name.endsWith('.woff')) {
|
|
55
|
+
await handleWebfontMetadata(file, font, files);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const { weightName, subfamilyName, fontTitle, style, italicKW, variableFont } = extractFontMetadata(
|
|
59
|
+
font,
|
|
60
|
+
title,
|
|
61
|
+
weightKeywordList,
|
|
62
|
+
italicKeywordList,
|
|
63
|
+
preserveShortenedNames,
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
const id = sanitizeForSanityId(fontTitle);
|
|
67
|
+
|
|
68
|
+
let originalFilename = null;
|
|
69
|
+
if (preserveFileNames) {
|
|
70
|
+
originalFilename = file.name.replace(/\.(ttf|otf|woff2?|eot|svg)$/i, '');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
logFontInfo(id, fontTitle, font, file.name, subfamilyName, style, weightName, variableFont, italicKW);
|
|
74
|
+
|
|
75
|
+
subfamilies[id] = subfamilyName;
|
|
76
|
+
|
|
77
|
+
if (fontsObjects[id]) {
|
|
78
|
+
fontsObjects[id].files = [...fontsObjects[id].files, file];
|
|
79
|
+
if (preserveFileNames && originalFilename) {
|
|
80
|
+
fontsObjects[id].originalFilename = originalFilename;
|
|
81
|
+
}
|
|
82
|
+
} else {
|
|
83
|
+
fontsObjects[id] = createFontObject(
|
|
84
|
+
id,
|
|
85
|
+
fontTitle,
|
|
86
|
+
title,
|
|
87
|
+
font,
|
|
88
|
+
variableFont,
|
|
89
|
+
weightName,
|
|
90
|
+
subfamilyName,
|
|
91
|
+
file,
|
|
92
|
+
preserveFileNames ? originalFilename : null,
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
fontsObjects = sortFontObjects(fontsObjects);
|
|
98
|
+
const uniqueSubfamilies = [...new Set(Object.values(subfamilies))];
|
|
99
|
+
|
|
100
|
+
console.log('Subfamilies:', subfamilies);
|
|
101
|
+
console.log('Unique subfamilies:', uniqueSubfamilies, uniqueSubfamilies.length);
|
|
102
|
+
console.log('Font objects:', fontsObjects);
|
|
103
|
+
|
|
104
|
+
return { fontsObjects, subfamilies, uniqueSubfamilies, newPreferredStyle, failedFiles };
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Patches webfont name records from a matching TTF when woff/woff2 metadata is missing.
|
|
109
|
+
* @param {File} file
|
|
110
|
+
* @param {Object} font
|
|
111
|
+
* @param {File[]} files
|
|
112
|
+
*/
|
|
113
|
+
const handleWebfontMetadata = async (file, font, files) => {
|
|
114
|
+
if (
|
|
115
|
+
!font?.name?.records?.fullName ||
|
|
116
|
+
font?.name?.records?.fullName === '' ||
|
|
117
|
+
!/^[A-Z0-9]+$/.test(font?.name?.records?.fullName)
|
|
118
|
+
) {
|
|
119
|
+
const ttfFile = files.find(f => f.name === file.name.replace('.woff2', '.ttf').replace('.woff', '.ttf'));
|
|
120
|
+
if (ttfFile) {
|
|
121
|
+
const ttfFileBuffer = await readFontFile(ttfFile);
|
|
122
|
+
const ttfFileData = fontkit.create(ttfFileBuffer);
|
|
123
|
+
if (ttfFileData) font.name.records = ttfFileData?.name?.records;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Extracts and normalises metadata from a fontkit font object.
|
|
130
|
+
* @param {Object} font
|
|
131
|
+
* @param {string} title
|
|
132
|
+
* @param {string[]} weightKeywordList
|
|
133
|
+
* @param {string[]} italicKeywordList
|
|
134
|
+
* @param {boolean} preserveShortenedNames
|
|
135
|
+
* @returns {Object}
|
|
136
|
+
*/
|
|
137
|
+
export const extractFontMetadata = (font, title, weightKeywordList, italicKeywordList, preserveShortenedNames = false) => {
|
|
138
|
+
let weightName = extractWeightName(font, italicKeywordList);
|
|
139
|
+
if (!preserveShortenedNames) {
|
|
140
|
+
weightName = expandAbbreviations(weightName);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if ((weightName === '' || weightName.toLowerCase() === 'roman') && font?.name?.records?.fullName) {
|
|
144
|
+
weightName = extractWeightFromFullName(font, title);
|
|
145
|
+
if (!preserveShortenedNames) {
|
|
146
|
+
weightName = expandAbbreviations(weightName);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const variableFont = font?.variationAxes && Object.keys(font.variationAxes).length > 0;
|
|
151
|
+
|
|
152
|
+
let subfamilyName = font?.name?.records?.fullName?.en?.replace(title.trim(), '').trim() ||
|
|
153
|
+
font.subfamilyName.trim().replace(title.trim(), '').trim();
|
|
154
|
+
|
|
155
|
+
if (!preserveShortenedNames) {
|
|
156
|
+
subfamilyName = expandAbbreviations(subfamilyName);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
let fontTitle = font?.fullName.trim();
|
|
160
|
+
let style = (font?.italicAngle !== 0 || font?.fullName.toLowerCase().includes('italic')) ? 'Italic' : 'Regular';
|
|
161
|
+
|
|
162
|
+
const italicKW = processItalicKeywords(font, fontTitle, italicKeywordList);
|
|
163
|
+
|
|
164
|
+
subfamilyName = processSubfamilyName(subfamilyName, weightKeywordList, italicKW, preserveShortenedNames);
|
|
165
|
+
fontTitle = formatFontTitle(fontTitle, preserveShortenedNames);
|
|
166
|
+
|
|
167
|
+
subfamilyName = subfamilyName === '' ? 'Regular' : subfamilyName.replace(/\s+/g, ' ').trim();
|
|
168
|
+
|
|
169
|
+
if (subfamilyName !== '') {
|
|
170
|
+
weightName = weightName
|
|
171
|
+
.replace(`${subfamilyName} `, '')
|
|
172
|
+
.replace(` ${subfamilyName}`, '')
|
|
173
|
+
.trim();
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (variableFont) {
|
|
177
|
+
if (!fontTitle.toLowerCase().includes('vf')) {
|
|
178
|
+
fontTitle = fontTitle + ' VF';
|
|
179
|
+
}
|
|
180
|
+
subfamilyName = '';
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (!(variableFont && fontTitle.toLowerCase().includes('italic'))) {
|
|
184
|
+
fontTitle = addItalicToFontTitle(font, fontTitle, italicKW, style, preserveShortenedNames);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return { weightName, subfamilyName, fontTitle, style, italicKW, variableFont };
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Extracts the weight name from a font's preferred subfamily or subfamily record.
|
|
192
|
+
* Returns "Variable" for variable fonts.
|
|
193
|
+
* @param {Object} font
|
|
194
|
+
* @param {string[]} italicKW
|
|
195
|
+
* @returns {string}
|
|
196
|
+
*/
|
|
197
|
+
export const extractWeightName = (font, italicKW) => {
|
|
198
|
+
let weightName = font?.name?.records?.preferredSubfamily || font?.name?.records?.fontSubfamily;
|
|
199
|
+
|
|
200
|
+
if (typeof weightName === 'object') {
|
|
201
|
+
weightName = weightName?.en ||
|
|
202
|
+
(weightName.constructor === Object ? weightName[Object.keys(weightName)[0]] : null);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (font?.variationAxes && Object.keys(font.variationAxes).length > 0) {
|
|
206
|
+
return 'Variable';
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (italicKW) {
|
|
210
|
+
italicKW.forEach(keyword => {
|
|
211
|
+
const kwRegex = new RegExp(`\\b${keyword.trim()}\\b`, 'i');
|
|
212
|
+
if (kwRegex.test(weightName)) {
|
|
213
|
+
weightName = weightName.replace(kwRegex, '').trim();
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return weightName?.toString()
|
|
219
|
+
.replace('Italic', '')
|
|
220
|
+
.replace('It', '')
|
|
221
|
+
.replace('Slanted', '')
|
|
222
|
+
.replace('Slant', '')
|
|
223
|
+
.replace('Backslant', '')
|
|
224
|
+
.trim();
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Extracts a weight name from the font's full name record when subfamily is empty or "Roman".
|
|
229
|
+
* @param {Object} font
|
|
230
|
+
* @param {string} title
|
|
231
|
+
* @returns {string}
|
|
232
|
+
*/
|
|
233
|
+
export const extractWeightFromFullName = (font, title) => {
|
|
234
|
+
let weightName = font?.name?.records?.fullName;
|
|
235
|
+
weightName = weightName?.en
|
|
236
|
+
? weightName.en
|
|
237
|
+
: (weightName?.constructor === Object ? weightName[Object.keys(weightName)[0]] : weightName);
|
|
238
|
+
weightName = weightName?.replace(title + ' ', '').replace(title, '').trim();
|
|
239
|
+
weightName = weightName?.replace('Italic', '').replace('It', '').replace('Slanted', '').replace('Slant', '').trim();
|
|
240
|
+
return weightName;
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Strips weight and italic keywords from a subfamily name string.
|
|
245
|
+
* @param {string} subfamilyName
|
|
246
|
+
* @param {string[]} weightKeywordList
|
|
247
|
+
* @param {string[]} italicKeywordList
|
|
248
|
+
* @param {boolean} preserveShortenedNames
|
|
249
|
+
* @returns {string}
|
|
250
|
+
*/
|
|
251
|
+
export const processSubfamilyName = (subfamilyName, weightKeywordList, italicKeywordList, preserveShortenedNames = false) => {
|
|
252
|
+
weightKeywordList.forEach(keyword => {
|
|
253
|
+
const kwRegex = new RegExp(`\\b${keyword.trim()}\\b`, 'i');
|
|
254
|
+
if (kwRegex.test(subfamilyName)) {
|
|
255
|
+
subfamilyName = subfamilyName.replace(kwRegex, '').trim();
|
|
256
|
+
}
|
|
257
|
+
subfamilyName = removeWeightNames(subfamilyName) || subfamilyName;
|
|
258
|
+
if (!preserveShortenedNames) {
|
|
259
|
+
subfamilyName = expandAbbreviations(subfamilyName);
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
italicKeywordList.forEach(keyword => {
|
|
264
|
+
const kwRegex = new RegExp(`\\b${keyword.trim()}\\b`, 'i');
|
|
265
|
+
if (kwRegex.test(subfamilyName)) {
|
|
266
|
+
subfamilyName = subfamilyName.replace(kwRegex, '').trim();
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
return subfamilyName;
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Collects italic keywords present in a font's full name.
|
|
275
|
+
* @param {Object} font
|
|
276
|
+
* @param {string} fontTitle
|
|
277
|
+
* @param {string[]} italicKeywordList
|
|
278
|
+
* @returns {string[]}
|
|
279
|
+
*/
|
|
280
|
+
export const processItalicKeywords = (font, fontTitle, italicKeywordList) => {
|
|
281
|
+
let italicKW = [];
|
|
282
|
+
|
|
283
|
+
italicKeywordList.forEach(keyword => {
|
|
284
|
+
const kw = keyword.trim();
|
|
285
|
+
const kwRegex = new RegExp(`\\b${kw}\\b`, 'i');
|
|
286
|
+
if (kwRegex.test(fontTitle)) {
|
|
287
|
+
fontTitle = fontTitle.replace(kwRegex, '').trim();
|
|
288
|
+
italicKW.push(kw);
|
|
289
|
+
}
|
|
290
|
+
if (font?.fullName && typeof font.fullName === 'string' && font.fullName.toLowerCase().includes(kw.toLowerCase())) {
|
|
291
|
+
if (!italicKW.includes(kw)) italicKW.push(kw);
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
return italicKW;
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Normalises and title-cases a font title, optionally expanding abbreviations.
|
|
300
|
+
* @param {string} fontTitle
|
|
301
|
+
* @param {boolean} preserveShortenedNames
|
|
302
|
+
* @returns {string}
|
|
303
|
+
*/
|
|
304
|
+
export const formatFontTitle = (fontTitle, preserveShortenedNames = false) => {
|
|
305
|
+
const hasItalic = fontTitle.toLowerCase().includes('italic');
|
|
306
|
+
fontTitle = fontTitle.replace(/-/g, ' ');
|
|
307
|
+
|
|
308
|
+
return fontTitle.replace(/\s+/g, ' ').trim().split(' ').map(word => {
|
|
309
|
+
if (hasItalic && word.toLowerCase() === 'italic') return 'Italic';
|
|
310
|
+
let fullWord = word;
|
|
311
|
+
if (!preserveShortenedNames) {
|
|
312
|
+
fullWord = reverseSpellingLookup(word) || word;
|
|
313
|
+
}
|
|
314
|
+
return fullWord[0].toUpperCase() + fullWord.slice(1);
|
|
315
|
+
}).join(' ');
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Appends any italic keywords to the font title that aren't already present.
|
|
320
|
+
* @param {Object} font
|
|
321
|
+
* @param {string} fontTitle
|
|
322
|
+
* @param {string[]} italicKW
|
|
323
|
+
* @param {string} style
|
|
324
|
+
* @param {boolean} preserveShortenedNames
|
|
325
|
+
* @returns {string}
|
|
326
|
+
*/
|
|
327
|
+
export const addItalicToFontTitle = (font, fontTitle, italicKW, style, preserveShortenedNames = false) => {
|
|
328
|
+
const hasItalicAngle = font?.italicAngle !== 0;
|
|
329
|
+
const hasItalicInName = font?.fullName.toLowerCase().includes('italic');
|
|
330
|
+
|
|
331
|
+
if (italicKW.length > 0 || hasItalicAngle || hasItalicInName) {
|
|
332
|
+
italicKW = [...new Set(italicKW)];
|
|
333
|
+
|
|
334
|
+
if (italicKW.length === 0 && (hasItalicAngle || hasItalicInName)) {
|
|
335
|
+
italicKW = ['Italic'];
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
if (!preserveShortenedNames) {
|
|
339
|
+
italicKW = italicKW.map(item => reverseSpellingLookup(item) || item);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
italicKW = [...new Set(italicKW)];
|
|
343
|
+
|
|
344
|
+
if (italicKW.length > 1 && italicKW.includes('Italic')) {
|
|
345
|
+
italicKW = ['Italic'];
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const fontTitleLower = fontTitle.toLowerCase();
|
|
349
|
+
italicKW = italicKW.filter(keyword => {
|
|
350
|
+
const keywordLower = keyword.toLowerCase();
|
|
351
|
+
const kwRegex = new RegExp(`\\b${keywordLower}\\b`);
|
|
352
|
+
const isSubstring = fontTitleLower.split(' ').some(word =>
|
|
353
|
+
word.includes(keywordLower) || keywordLower.includes(word)
|
|
354
|
+
);
|
|
355
|
+
return !kwRegex.test(fontTitleLower) && !isSubstring;
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
if (italicKW.length > 0) {
|
|
359
|
+
fontTitle = fontTitle.trim() + ' ' + italicKW.join(' ');
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
return fontTitle;
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Builds a font object ready for staging and upload.
|
|
368
|
+
* originalFilename is stored temporarily and deleted before saving to Sanity.
|
|
369
|
+
* @param {string} id
|
|
370
|
+
* @param {string} fontTitle
|
|
371
|
+
* @param {string} title
|
|
372
|
+
* @param {Object} font
|
|
373
|
+
* @param {boolean} variableFont
|
|
374
|
+
* @param {string} weightName
|
|
375
|
+
* @param {string} subfamilyName
|
|
376
|
+
* @param {File} file
|
|
377
|
+
* @param {string|null} originalFilename
|
|
378
|
+
* @returns {Object}
|
|
379
|
+
*/
|
|
380
|
+
export const createFontObject = (id, fontTitle, title, font, variableFont, weightName, subfamilyName, file, originalFilename = null) => {
|
|
381
|
+
const fontObject = {
|
|
382
|
+
_key: nanoid(),
|
|
383
|
+
_id: id,
|
|
384
|
+
title: fontTitle,
|
|
385
|
+
slug: { _type: 'slug', current: id },
|
|
386
|
+
typefaceName: title,
|
|
387
|
+
style: (font?.italicAngle !== 0 || font?.fullName.toLowerCase().includes('italic')) ? 'Italic' : 'Regular',
|
|
388
|
+
variableFont: variableFont,
|
|
389
|
+
weightName: weightName,
|
|
390
|
+
subfamily: subfamilyName,
|
|
391
|
+
normalWeight: true,
|
|
392
|
+
weight: Number(determineWeight(font, weightName)),
|
|
393
|
+
fileInput: {},
|
|
394
|
+
files: [file],
|
|
395
|
+
fontKit: font,
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
if (originalFilename) {
|
|
399
|
+
fontObject.originalFilename = originalFilename;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
return fontObject;
|
|
403
|
+
};
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* Determines a numeric CSS weight value for a font.
|
|
407
|
+
* @param {Object} font
|
|
408
|
+
* @param {string} weightName
|
|
409
|
+
* @returns {number}
|
|
410
|
+
*/
|
|
411
|
+
export const determineWeight = (font, weightName) => {
|
|
412
|
+
if (font['OS/2']?.usWeightClass) {
|
|
413
|
+
return Number(font['OS/2'].usWeightClass);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
const wn = weightName?.toLowerCase() || '';
|
|
417
|
+
|
|
418
|
+
if (/hairline|extra thin|extrathin/.test(wn)) return 100;
|
|
419
|
+
if (/thin|extra light|extralight/.test(wn)) return 200;
|
|
420
|
+
if (/light|book/.test(wn)) return 300;
|
|
421
|
+
if (/regular|normal/.test(wn)) return 400;
|
|
422
|
+
if (/medium/.test(wn)) return 500;
|
|
423
|
+
if (/semi bold|semibold/.test(wn)) return 600;
|
|
424
|
+
if (/extra bold|extrabold/.test(wn)) return 800;
|
|
425
|
+
if (/bold/.test(wn)) return 700;
|
|
426
|
+
if (/black|ultra/.test(wn)) return 900;
|
|
427
|
+
|
|
428
|
+
return 400;
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Sorts a map of font objects by ascending weight, with Regular before Italic at equal weights.
|
|
433
|
+
* @param {Object} fontsObjects
|
|
434
|
+
* @returns {Object}
|
|
435
|
+
*/
|
|
436
|
+
export const sortFontObjects = (fontsObjects) => {
|
|
437
|
+
return Object.fromEntries(
|
|
438
|
+
Object.entries(fontsObjects).sort((a, b) => {
|
|
439
|
+
const weightA = Number(a[1].weight);
|
|
440
|
+
const weightB = Number(b[1].weight);
|
|
441
|
+
if (weightA === weightB) {
|
|
442
|
+
if (a[1].style === 'Regular' && b[1].style === 'Italic') return -1;
|
|
443
|
+
if (a[1].style === 'Italic' && b[1].style === 'Regular') return 1;
|
|
444
|
+
return 0;
|
|
445
|
+
}
|
|
446
|
+
return weightA - weightB;
|
|
447
|
+
})
|
|
448
|
+
);
|
|
449
|
+
};
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* Logs font metadata to the console for debugging.
|
|
453
|
+
* @param {string} id
|
|
454
|
+
* @param {string} fontTitle
|
|
455
|
+
* @param {Object} font
|
|
456
|
+
* @param {string} fileName
|
|
457
|
+
* @param {string} subfamilyName
|
|
458
|
+
* @param {string} style
|
|
459
|
+
* @param {string} weightName
|
|
460
|
+
* @param {boolean} variableFont
|
|
461
|
+
* @param {string[]} italicKW
|
|
462
|
+
*/
|
|
463
|
+
export const logFontInfo = (id, fontTitle, font, fileName, subfamilyName, style, weightName, variableFont, italicKW) => {
|
|
464
|
+
console.log('=== Font Info ====');
|
|
465
|
+
console.log('Font id: ', id);
|
|
466
|
+
console.log('Font title: ', fontTitle);
|
|
467
|
+
console.log('Fontkit fullName: ', font.fullName);
|
|
468
|
+
console.log('Fontkit family name: ', font.familyName);
|
|
469
|
+
console.log('File name: ', fileName);
|
|
470
|
+
console.log('Subfamily: ', subfamilyName);
|
|
471
|
+
console.log('Style: ', style);
|
|
472
|
+
console.log('Weight: ', weightName);
|
|
473
|
+
console.log('Variable: ', variableFont);
|
|
474
|
+
console.log('italicKW: ', italicKW);
|
|
475
|
+
console.log('Font italic angle: ', (font?.italicAngle !== 0 || font?.fullName.toLowerCase().includes('italic')) ? 'Italic' : 'Regular');
|
|
476
|
+
console.log('=======');
|
|
477
|
+
};
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// Renames font document IDs across a typeface when a typeface slug changes
|
|
2
|
+
|
|
3
|
+
import * as fontkit from 'fontkit';
|
|
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 = fontkit.create(fontBuffer);
|
|
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,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
|
+
}
|