feat(blog): authors page (#10216)

Co-authored-by: OzakIOne <OzakIOne@users.noreply.github.com>
Co-authored-by: sebastien <lorber.sebastien@gmail.com>
Co-authored-by: slorber <slorber@users.noreply.github.com>
This commit is contained in:
ozaki 2024-08-01 17:30:49 +02:00 committed by GitHub
parent 50f9fce29b
commit f356e29938
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
56 changed files with 1670 additions and 706 deletions

View file

@ -43,31 +43,28 @@ export async function getDataFilePath({
}
/**
* Looks up for a data file in the content paths, returns the object validated
* and normalized according to the `validate` callback.
* Looks up for a data file in the content paths
* Favors the localized content path over the base content path
* Currently supports Yaml and JSON data files
* It is the caller responsibility to validate and normalize the resulting data
*
* @returns `undefined` when file not found
* @throws Throws when validation fails, displaying a helpful context message.
* @throws Throws when data file can't be parsed
*/
export async function getDataFileData<T>(
params: DataFileParams & {
/** Used for the "The X file looks invalid" message. */
fileType: string;
},
validate: (content: unknown) => T,
): Promise<T | undefined> {
export async function readDataFile(params: DataFileParams): Promise<unknown> {
const filePath = await getDataFilePath(params);
if (!filePath) {
return undefined;
}
try {
const contentString = await fs.readFile(filePath, {encoding: 'utf8'});
const unsafeContent = Yaml.load(contentString);
// TODO we shouldn't validate here: it makes validation harder to test
return validate(unsafeContent);
return Yaml.load(contentString);
} catch (err) {
logger.error`The ${params.fileType} file at path=${filePath} looks invalid.`;
throw err;
const msg = logger.interpolate`The file at path=${path.relative(
process.cwd(),
filePath,
)} looks invalid (not Yaml nor JSON).`;
throw new Error(msg, {cause: err as Error});
}
}