mirror of
https://gitlab.com/PronounsPage/PronounsPage.git
synced 2025-08-03 11:07:00 -04:00
52 lines
1.7 KiB
TypeScript
52 lines
1.7 KiB
TypeScript
import { defineNuxtRouteMiddleware, useNuxtApp } from 'nuxt/app';
|
|
|
|
import type { Config } from '~/locale/config.ts';
|
|
import { buildPronoun } from '~/src/buildPronoun.ts';
|
|
import { loadNounsData, loadPronounLibrary } from '~/src/data.ts';
|
|
import type { Translator } from '~/src/translator.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 withKey(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();
|
|
});
|