From ecd708109817ed1f8c0d915b01ad873804dfd4fc Mon Sep 17 00:00:00 2001 From: mrrpnya Date: Sun, 16 Feb 2025 20:47:14 -0800 Subject: [PATCH] ... --- nuxt.config.ts | 13 +++-- pages/article/[lang]/[category]/[id].vue | 40 ++++++++++------ utils/page_updater/update_pagelist.ts | 60 +++++++++++++++++++++--- 3 files changed, 86 insertions(+), 27 deletions(-) diff --git a/nuxt.config.ts b/nuxt.config.ts index 900ec38..6480dd0 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -19,7 +19,7 @@ for (const [lang, langData] of Object.entries(blog_list.languages)) { // Remove the file extension (e.g. "page.md" becomes "page") const postSlug = canonicalId.replace(/\.md$/, ''); // Build the localized route (e.g. "/en/article/page") - blog_nitro_routes.push(`/${lang}/article/${postSlug}`); + blog_nitro_routes.push(`/${lang}/article/${lang}/${categoryName.toLowerCase()}/${postSlug}`); } } } @@ -37,9 +37,9 @@ export default defineNuxtConfig({ }, }, routeRules: { - "/article/:category:/:id": { - redirect: "/article/:category:/:id/index.html", - }, + "/article/:lang/:category:/:id": { + redirect: "/article/:lang/:category:/:id/index.html", + } }, app: { pageTransition: { @@ -54,7 +54,10 @@ export default defineNuxtConfig({ "@nuxtjs/i18n", ], i18n: { + strategy: 'prefix_and_default', + defaultLocale: 'en', vueI18n: './i18n.config.ts', + baseUrl: process.env.SITE_BASE_URL, locales: [ { code: 'en', @@ -62,7 +65,7 @@ export default defineNuxtConfig({ }, { code: 'tp', - name: 'Toki Pona' + name: 'toki pona' } ] }, diff --git a/pages/article/[lang]/[category]/[id].vue b/pages/article/[lang]/[category]/[id].vue index fde046f..b87aa6a 100644 --- a/pages/article/[lang]/[category]/[id].vue +++ b/pages/article/[lang]/[category]/[id].vue @@ -31,6 +31,9 @@ const background: Ref = ref(''); const next: Ref = ref(''); const previous: Ref = ref(''); +import page_list_Data from "~/assets/meta/post_list.json" +const page_list = page.PageList.fromJSON(JSON.stringify(await page_list_Data)) + function tagsToString(tags: String[]): string { var tagString = ''; for (let i = 0; i < tags.length; i++) { @@ -62,6 +65,7 @@ watch(route, async () => { }, { immediate: true }); +import * as page from "~/utils/page_updater/update_pagelist" async function fetchArticle(url: string, region?: string): Promise { if (!url) { @@ -70,19 +74,30 @@ async function fetchArticle(url: string, region?: string): Promise { } try { // Trim the .md extension - var trimmedUrl = url.replace(/\.md$/, ''); + const trimmedUrl = url.replace(/^\//, '').replace(/\.md$/, ''); console.log('[id].vue - Fetching article: ' + trimmedUrl); // Define the available languages in the order of preference (falling back on the next if one fails) const languages = [region ? region : locale.value, 'en', 'fr', 'de']; // Add all available languages in order of preference - console.log(languages) + console.log(languages); let dataFound = false; let data: any = null; + // Check the presence of the article in each language using page utility for (let lang of languages) { - console.log(`Querying ${lang}${trimmedUrl}`) + // Check if the page exists in the specified language + const pageExists = page_list.pageExistsInLang(lang, trimmedUrl); + console.log(`Checking if article exists for language ${lang}: ${pageExists}`); + + if (!pageExists) { + console.warn(`Article does not exist for language ${lang}`); + continue; // Skip this language and try the next one + } + + // If the page exists, fetch its content + console.log(`Querying /${lang}/${trimmedUrl}`); const { data: languageData, error } = await useAsyncData(`${lang}/${trimmedUrl}`, () => - queryContent(`${lang}${trimmedUrl}`).findOne() + queryContent(`/${lang}/${trimmedUrl}`).findOne() ); if (error.value) { @@ -138,13 +153,8 @@ const data = await fetchArticle(url.value); updateMetadata(data); console.log('Prefetching article'); -onMounted(async () => { - console.log('Fetching article :3'); - await fetchArticle(url.value); -}); const temp_url = route.query.post as string; -await fetchArticle(temp_url); const fullTitle = data.title + ' | ' + siteConfig.siteTitle; @@ -211,14 +221,14 @@ useSeoMeta({
-
+
@@ -230,12 +240,12 @@ useSeoMeta({
- Previous +
- Next +
diff --git a/utils/page_updater/update_pagelist.ts b/utils/page_updater/update_pagelist.ts index 2887eed..099cdb2 100644 --- a/utils/page_updater/update_pagelist.ts +++ b/utils/page_updater/update_pagelist.ts @@ -1,4 +1,3 @@ - import * as pages from "./pages.ts"; import { format } from "date-fns"; @@ -196,6 +195,51 @@ export class PageList { pages: data.pages, })); } + + /** + * Check if a page exists in any language. + * @param canonicalId - The canonical ID of the page (language prefix removed) + * @returns True if the page exists in any language, otherwise false. + */ + pageExists(canonicalId: string): boolean { + for (const lang of Object.keys(this.languages)) { + const categories = this.languages[lang].categories; + for (const categoryName of Object.keys(categories)) { + const page = categories[categoryName].posts.find( + (p) => PageList.getCanonicalId(p.id) === canonicalId, + ); + if (page) { + return true; // Page found + } + } + } + return false; // Page not found in any language + } + + /** + * Check if a page exists in a specific language. + * @param lang - The language code (e.g., "en") + * @param canonicalId - The canonical ID of the page (language prefix removed) + * @returns True if the page exists in the specified language, otherwise false. + */ + pageExistsInLang(lang: string, canonicalId: string): boolean { + const categories = this.languages[lang]?.categories; + if (!categories) { + return false; // Language not found + } + + // Check each category for the page with the given canonical ID + for (const categoryName of Object.keys(categories)) { + const page = categories[categoryName].posts.find( + p => p.id == canonicalId + ); + if (page) { + return true; // Page found in this language + } + } + + return false; // Page not found in the specified language + } } /** @@ -204,7 +248,9 @@ export class PageList { * @param pagesInfo - An object where keys are paths (e.g., "content/[lang]/...") and values are page data. * @returns A PageCategory object containing an array of Page objects. */ -export function generatePageCategory(pagesInfo: Record): PageCategory { +export function generatePageCategory( + pagesInfo: Record, +): PageCategory { const pageList: Page[] = []; for (const [filePath, page] of Object.entries(pagesInfo)) { @@ -236,7 +282,7 @@ export function generatePageCategory(pagesInfo: Record): PageCatego title: "", description: "", tags: [], - show: true + show: true, }; } @@ -259,7 +305,7 @@ export const postDirectories: pages.PageLocation[] = [ tags: ["site"], map: "site", root: "", // Not used in the new structure - show: false + show: false, }, { title: "Collections", @@ -268,7 +314,7 @@ export const postDirectories: pages.PageLocation[] = [ tags: ["collection"], map: "collections", root: "", - show: true + show: true, }, { title: "Guides", @@ -276,6 +322,6 @@ export const postDirectories: pages.PageLocation[] = [ tags: ["guide"], map: "guides", root: "", - show: true + show: true, }, -]; \ No newline at end of file +];