import { defineNuxtRouteMiddleware, useNuxtApp } from 'nuxt/app'; import { buildPronoun } from '#shared/buildPronoun.ts'; import type { Translator } from '#shared/translator.ts'; import { loadNounsData, loadPronounLibrary } from '~/src/data.ts'; import type { Config } from '~~/locale/config.ts'; const findPronoun = async (path: string, config: Config, translator: Translator) => { if (config.pronouns.prefix) { if (!path.startsWith(config.pronouns.prefix)) { return; } path = path.substring(config.pronouns.prefix.length); } const pronounLibrary = await loadPronounLibrary(config); const pronouns = pronounLibrary.pronouns; return buildPronoun(pronouns, path, config, translator) ?? undefined; }; const findNounConvention = async (path: string, config: Config) => { if (!config.nouns.enabled || !config.nouns.conventions?.enabled) { return undefined; } const nounsData = await loadNounsData(); return entriesWithKeys(nounsData.conventions ?? {}) .find((nounConvention) => nounConvention.key === path); }; export default defineNuxtRouteMiddleware(async (to) => { const { $translator: translator } = useNuxtApp(); const config = useConfig(); const path = decodeURIComponent(to.path.replaceAll(/^\/|\/$/g, '')); to.meta.pronoun = await findPronoun(path, config, translator); if (to.meta.pronoun !== undefined) { to.meta.headerCategory = 'pronouns'; return; } to.meta.nounConvention = await findNounConvention(path, config); if (to.meta.nounConvention !== undefined) { to.meta.headerCategory = 'nouns'; return; } return abortNavigation(); });