57 lines
1.5 KiB
TypeScript
57 lines
1.5 KiB
TypeScript
/**
|
|
* Generate URLs for objects in the format:
|
|
* https://zapwall.fr/<object_type_name>/<id_hash>_<index>_<version>
|
|
*
|
|
* - object_type_name: author, series, publication, review
|
|
* - id_hash: SHA-256 hash of the object
|
|
* - index: number of objects of the same type with the same hash (0 by default)
|
|
* - version: version number (0 by default)
|
|
*/
|
|
|
|
export type ObjectTypeName = 'author' | 'series' | 'publication' | 'review'
|
|
|
|
/**
|
|
* Generate URL for an object
|
|
*/
|
|
export function generateObjectUrl(
|
|
objectType: ObjectTypeName,
|
|
idHash: string,
|
|
index: number = 0,
|
|
version: number = 0
|
|
): string {
|
|
return `https://zapwall.fr/${objectType}/${idHash}_${index}_${version}`
|
|
}
|
|
|
|
/**
|
|
* Parse URL to extract object information
|
|
*/
|
|
export function parseObjectUrl(url: string): {
|
|
objectType: ObjectTypeName | null
|
|
idHash: string | null
|
|
index: number | null
|
|
version: number | null
|
|
} {
|
|
const match = url.match(/https?:\/\/zapwall\.fr\/(author|series|publication|review)\/([a-f0-9]+)_(\d+)_(\d+)/i)
|
|
if (!match || !match[1] || !match[2] || !match[3] || !match[4]) {
|
|
return { objectType: null, idHash: null, index: null, version: null }
|
|
}
|
|
|
|
return {
|
|
objectType: match[1] as ObjectTypeName,
|
|
idHash: match[2],
|
|
index: parseInt(match[3], 10),
|
|
version: parseInt(match[4], 10),
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the object type name from tag type
|
|
*/
|
|
export function getObjectTypeName(tagType: 'author' | 'series' | 'publication' | 'quote'): ObjectTypeName {
|
|
// Map 'quote' to 'review' for URLs
|
|
if (tagType === 'quote') {
|
|
return 'review'
|
|
}
|
|
return tagType
|
|
}
|