PronounsPage/app/middleware/languageItemFromPath.ts
Valentyne Stigloher 10180aa6a3 (refactor) use #shared alias instead of ~~/shared
the #shared alias used by Nuxt cannot be easily disabled and to prevent breackage with jiti, we make use of it
2025-08-17 18:56:02 +02:00

52 lines
1.7 KiB
TypeScript

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();
});