PronounsPage/app/composables/useSpelling.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

91 lines
3.1 KiB
TypeScript

import futurus from 'avris-futurus';
import { useCookie } from 'nuxt/app';
import zhConverter from 'zh_cn_zh_tw';
import { hbsLatinToCyrillic } from '../../locale/hbs/script.ts';
import useConfig from './useConfig.ts';
import { longtimeCookieSetting } from '#shared/cookieSettings.ts';
import { escapeHtml } from '#shared/helpers.ts';
import { useMainStore } from '~/store/index.ts';
declare global {
interface Window {
toShavian?(text: string): string;
}
}
export default () => {
const config = useConfig();
const store = useMainStore();
const spellingCookie = useCookie('spelling', longtimeCookieSetting);
return {
handleSpelling(str: string): string {
if (config.locale === 'zh' && store.spelling === 'simplified') {
return zhConverter.convertToSimplifiedChinese(str);
}
if (config.locale === 'pl' && store.spelling === 'futurysci') {
return futurus.futuriseText(str);
}
if (config.locale === 'pl' && store.spelling === 'caron') {
// https://www.reddit.com/r/linguisticshumor/comments/1lqrhro/seriously_why_dont_we_have_%C4%8D_%C5%99_%C5%A1_and_%C5%BE_instead_of/
return str
.replaceAll('cz', 'č').replaceAll('rz', 'ř').replaceAll('sz', 'š').replaceAll('ż', 'ž')
.replaceAll('Cz', 'Č').replaceAll('Rz', 'Ř').replaceAll('sz', 'Š').replaceAll('ż', 'Ž')
;
}
if (config.locale === 'en' && store.spelling === 'shavian') {
if (typeof window === 'undefined' || !window.toShavian) {
this.setSpelling(''); // dependencies not loaded, disable
return str;
}
const shavian = str.replace(/(<[^>]*>)|([^<]+)/g, (match, htmlTag, text) => {
if (htmlTag) {
return htmlTag;
}
return window.toShavian!(text);
});
return `<span class="shavian">${shavian}</span>`;
}
if (config.locale === 'tok' && store.spelling === 'sitelen') {
return `<span class="sitelen">${str}</span>`;
}
if (config.locale === 'hbs' && store.spelling === 'cyrillic') {
return hbsLatinToCyrillic(str);
}
return str;
},
convertName(name: string): string {
if (config.locale === 'tok') {
const m = name.match(/^jan (.+?) \(((?:[mnptkswlj]?[iueoa][mn]? ?)+)\)$/i);
if (!m) {
return escapeHtml(name);
}
if (store.spelling === 'sitelen') {
return `jan <span class="cartouche">${escapeHtml(m[2])}</span>`;
}
return `jan ${escapeHtml(m[1])}`;
}
return escapeHtml(name);
},
setSpelling(spelling: string): void {
store.setSpelling(spelling);
spellingCookie.value = store.spelling;
},
};
};