import type { ISeoVariable } from '../../service/seo/seo.interface'; const longestFirst = (a: string, b: string): number => b.length - a.length; /** * The kind of page a Titles & meta template is for, as the server keys its * variable sets: home, search, not_found, author, date, post:{type}, * archive:{type}, term:{taxonomy}. Empty for a field of no template. */ export const templateContext = ( key: string, postTypes: string[], taxonomies: string[] ): string => { const postType = [...postTypes] .sort(longestFirst) .find(slug => key.startsWith(`pt_${slug}_`)); if (postType) { return key.startsWith(`pt_${postType}_archive_`) ? `archive:${postType}` : `post:${postType}`; } const taxonomy = [...taxonomies] .sort(longestFirst) .find(slug => key.startsWith(`tax_${slug}_`)); if (taxonomy) { return `term:${taxonomy}`; } const prefixes: Array<[string, string]> = [ ['home_', 'home'], ['author_', 'author'], ['date_archive_', 'date'], ['search_', 'search'], ['not_found_', 'not_found'], ]; return prefixes.find(([prefix]) => key.startsWith(prefix))?.[1] ?? ''; }; /** * The variables of one template: the ones its kind of page can use, each * with what it prints there. Every variable when the server sent no set. */ export const contextVariables = ( variables: ISeoVariable[], set: Record | undefined ): ISeoVariable[] => set ? variables .filter(variable => variable.name in set) .map(variable => ({ ...variable, example: set[variable.name] })) : variables;