/
parser.js
350 lines (294 loc) · 9.17 KB
/
parser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/* eslint-disable jsdoc/require-jsdoc */
import TOML from '@ltd/j-toml';
import { get } from 'svelte/store';
import YAML from 'yaml';
import { allAssetPaths, getAssetKind } from '$lib/services/assets';
import { siteConfig } from '$lib/services/config';
import { allContentPaths } from '$lib/services/contents';
import { isObject } from '$lib/services/utils/misc';
import { escapeRegExp } from '$lib/services/utils/strings';
/**
* Parse a list of all files on the repository/filesystem to create entry and asset lists, with the
* relevant collection/file configuration added.
* @param {object[]} files Unfiltered file list.
* @returns {object} File list, including both entries and assets.
*/
export const createFileList = (files) => {
const entryFiles = [];
const assetFiles = [];
files.forEach((fileInfo) => {
const { path } = fileInfo;
const contentPathConfig = get(allContentPaths).find(
({ folder, file }) => path.startsWith(folder) || path === file,
);
const mediaPathConfig = get(allAssetPaths).findLast(({ internalPath }) =>
path.startsWith(internalPath),
);
if (contentPathConfig && path.match(/\.(?:json|markdown|md|toml|ya?ml)$/i)) {
entryFiles.push({
...fileInfo,
slug: path.match(/([^/]+?)(?:\.\w+)?$/)[1],
config: contentPathConfig,
});
}
if (mediaPathConfig) {
assetFiles.push({
...fileInfo,
config: mediaPathConfig,
});
}
});
return { entryFiles, assetFiles };
};
/**
*
* @param {object} config File’s collection configuration.
* @param {string} [config.format] Configured file format.
* @param {string} [config.extension] Configured File extension.
* @returns {string} Determined extension.
*/
export const getFileExtension = ({ format, extension }) => {
if (extension) {
return extension;
}
if (format === 'yml' || format === 'yaml') {
return 'yml';
}
if (format === 'toml') {
return 'toml';
}
if (format === 'json') {
return 'json';
}
return 'md';
};
/**
* Get the Frontmatter format’s delimiters.
* @param {string} format File format.
* @param {string} [delimiter] Configured delimiter.
* @returns {string[]} Start and end delimiters.
*/
const getFrontmatterDelimiters = (format, delimiter) => {
if (typeof delimiter === 'string' && delimiter.trim()) {
return [delimiter, delimiter];
}
if (Array.isArray(delimiter) && delimiter.length === 2) {
return delimiter;
}
if (format === 'json-frontmatter') {
return ['{', '}'];
}
if (format === 'toml-frontmatter') {
return ['+++', '+++'];
}
return ['---', '---'];
};
/**
* Parse raw content with given file details.
* @param {object} entry File entry.
* @param {string} entry.text Raw content.
* @param {string} entry.path File path.
* @param {object} entry.config File’s collection configuration.
* @param {string} [entry.config.file] File path for a file collection item.
* @param {string} [entry.config.extension] Configured file extension.
* @param {string} [entry.config.format] Configured file format.
* @param {string} [entry.config.frontmatterDelimiter] Configured Frontmatter delimiter.
* @returns {object} Parsed content.
*/
const parseEntryFile = ({
text,
path,
config: { file, extension, format, frontmatterDelimiter },
}) => {
format ||=
extension === 'md' || path.endsWith('.md')
? 'yaml-frontmatter'
: extension || file?.match(/\.([^.]+)$/)[1];
// Ignore files with unknown format
if (!format) {
return null;
}
try {
if (format.match(/^ya?ml$/) && path.match(/\.ya?ml$/)) {
return YAML.parse(text);
}
if (format === 'toml' && path.match(/\.toml$/)) {
return TOML.parse(text);
}
if (format === 'json' && path.match(/\.json$/)) {
return JSON.parse(text);
}
} catch {
return null;
}
if (format.match(/^(?:(?:yaml|toml|json)-)?frontmatter$/) && path.match(/\.(?:md|markdown)$/)) {
const [startDelimiter, endDelimiter] = getFrontmatterDelimiters(format, frontmatterDelimiter);
const [, head, body = ''] =
text.match(
new RegExp(
`^${escapeRegExp(startDelimiter)}\\n(.+?)\\n${escapeRegExp(endDelimiter)}(?:\\n(.+))?`,
'ms',
),
) || [];
// If the format is `frontmatter`, try to parse in different formats, starting with YAML
if (head && (format === 'frontmatter' || format === 'yaml-frontmatter')) {
try {
return { ...YAML.parse(head), body };
} catch {
//
}
}
if (head && (format === 'frontmatter' || format === 'toml-frontmatter')) {
try {
return { ...TOML.parse(head), body };
} catch {
//
}
}
if (head && (format === 'frontmatter' || format === 'json-frontmatter')) {
try {
return { ...JSON.parse(head), body };
} catch {
//
}
}
}
return null;
};
/**
* Parse raw content with given file details.
* @param {object} entry File entry.
* @param {object} entry.content Content object.
* @param {string} entry.path File path.
* @param {object} entry.config File’s collection configuration.
* @param {string} [entry.config.extension] Configured file extension.
* @param {string} [entry.config.format] Configured file format.
* @param {string} [entry.config.frontmatterDelimiter] Configured Frontmatter delimiter.
* @returns {string} Formatted string.
*/
export const formatEntryFile = ({
content,
path,
config: { extension, format, frontmatterDelimiter },
}) => {
content = JSON.parse(JSON.stringify(content));
format ||=
extension === 'md' || path.endsWith('.md')
? 'yaml-frontmatter'
: extension || path.match(/\.([^.]+)$/)[1];
const formatYAML = () => YAML.stringify(content, null, { lineWidth: 0 }).trim();
const formatTOML = () => TOML.stringify(content).trim();
const formatJSON = () => JSON.stringify(content, null, 2).trim();
try {
if (format.match(/^ya?ml$/)) {
return `${formatYAML()}\n`;
}
if (format === 'toml') {
return `${formatTOML()}\n`;
}
if (format === 'json') {
return `${formatJSON()}\n`;
}
} catch {
return '';
}
if (format.match(/^(?:(?:yaml|toml|json)-)?frontmatter$/)) {
const [startDelimiter, endDelimiter] = getFrontmatterDelimiters(format, frontmatterDelimiter);
const body = content.body ? `${content.body}\n` : '';
delete content.body;
try {
if (format === 'frontmatter' || format === 'yaml-frontmatter') {
return `${startDelimiter}\n${formatYAML()}\n${endDelimiter}\n${body}`;
}
if (format === 'toml-frontmatter') {
return `${startDelimiter}\n${formatTOML()}\n${endDelimiter}\n${body}`;
}
if (format === 'json-frontmatter') {
return `${startDelimiter}\n${formatJSON()}\n${endDelimiter}\n${body}`;
}
} catch {
//
}
}
return '';
};
/**
* Parse the given entry files to create a complete, serialized entry list.
* @param {object[]} entryFiles Entry file list.
* @returns {Entry[]} Entry list.
*/
export const parseEntryFiles = (entryFiles) => {
const { i18n: { locales, structure } = {} } = get(siteConfig);
const hasLocales = Array.isArray(locales);
const entries = [];
entryFiles.forEach((file) => {
const parsedFile = parseEntryFile(file);
if (!isObject(parsedFile)) {
return Promise.resolve();
}
const {
slug,
path,
sha,
meta = {},
config: { collectionName, fileName },
} = file;
const entry = { sha, slug, collectionName, fileName, ...meta };
if (!hasLocales) {
entry.locales = { default: { content: parsedFile, path, sha } };
}
if (hasLocales && (structure === 'single_file' || fileName)) {
entry.locales = Object.fromEntries(
locales
.filter((locale) => locale in parsedFile)
.map((locale) => [locale, { content: parsedFile[locale], path, sha }]),
);
}
if (hasLocales && (structure === 'multiple_folders' || structure === 'multiple_files')) {
const regex =
structure === 'multiple_folders'
? new RegExp(`^(${locales.join('|')})\\/`)
: new RegExp(`\\/(${locales.join('|')})\\.[a-zA-Z0-9]+$`);
const [, locale] = path.match(regex) || [];
const index = entries.findIndex((_entry) => _entry.slug === slug);
if (locale && parsedFile[locale]) {
if (index > -1) {
entries[index].locales[locale] = { content: parsedFile[locale], path, sha };
} else {
entry.locales = { [locale]: { content: parsedFile[locale], path, sha } };
}
}
}
entries.push(entry);
return Promise.resolve();
});
return entries;
};
/**
* Parse the given asset files to create a complete, serialized asset list.
* @param {object[]} assetFiles Asset file list.
* @returns {Asset[]} Asset list.
*/
export const parseAssetFiles = (assetFiles) =>
assetFiles.map((assetInfo) => {
const {
path,
name,
sha,
size,
text = null,
meta = {},
config: { collectionName, internalPath },
} = assetInfo;
return {
path,
name,
sha,
size,
kind: getAssetKind(name),
text,
collectionName,
folder: internalPath,
...meta,
};
});