unique title for each docs page

This commit is contained in:
vcoppe
2024-09-21 10:18:01 +02:00
parent f77793b7fe
commit 1a10ecc44b
4 changed files with 82 additions and 64 deletions

View File

@@ -3,16 +3,24 @@ import { languages } from '$lib/languages';
import { getURLForLanguage } from '$lib/utils';
export async function handle({ event, resolve }) {
let language = event.params.language ?? 'en';
const language = event.params.language ?? 'en';
const strings = await import(`./locales/${language}.json`);
let path = event.url.pathname;
let page = event.route.id?.replace('/[[language]]', '').split('/')[1] ?? 'home';
const path = event.url.pathname;
const page = event.route.id?.replace('/[[language]]', '').split('/')[1] ?? 'home';
let title = strings.metadata[`${page}_title`];
let description = strings.metadata[`description`];
const description = strings.metadata[`description`];
let htmlTag = `<html lang="${language}" translate="no">`;
if (page === 'help' && event.params.guide) {
const [guide, subguide] = event.params.guide.split('/');
const guideModule = subguide
? await import(`./lib/docs/${language}/${guide}/${subguide}.mdx`)
: await import(`./lib/docs/${language}/${guide}.mdx`);
title = `${title} | ${guideModule.metadata.title}`;
}
const htmlTag = `<html lang="${language}" translate="no">`;
let headTag = `<head>
<title>gpx.studio — ${title}</title>
@@ -37,7 +45,7 @@ export async function handle({ event, resolve }) {
`;
}
let stringsHTML = page === 'app' ? stringsToHTML(strings) : '';
const stringsHTML = page === 'app' ? stringsToHTML(strings) : '';
const response = await resolve(event, {
transformPageChunk: ({ html }) => html.replace('<html>', htmlTag).replace('<head>', headTag).replace('<body data-sveltekit-preload-data="hover">', `<body data-sveltekit-preload-data="hover"><div class="hidden">${stringsHTML}</div>`)

View File

@@ -1,63 +1,73 @@
<script lang="ts">
import '../app.pcss';
import { ModeWatcher } from 'mode-watcher';
import { isLoading, _, locale } from 'svelte-i18n';
import { page } from '$app/stores';
import Nav from '$lib/components/Nav.svelte';
import Footer from '$lib/components/Footer.svelte';
import { onMount } from 'svelte';
import { convertOldEmbeddingOptions } from '$lib/components/embedding/Embedding';
import { base } from '$app/paths';
import { languages } from '$lib/languages';
import { browser } from '$app/environment';
import { goto } from '$app/navigation';
import { getURLForLanguage } from '$lib/utils';
import '../app.pcss';
import { ModeWatcher } from 'mode-watcher';
import { isLoading, _, locale } from 'svelte-i18n';
import { page } from '$app/stores';
import Nav from '$lib/components/Nav.svelte';
import Footer from '$lib/components/Footer.svelte';
import { onMount } from 'svelte';
import { convertOldEmbeddingOptions } from '$lib/components/embedding/Embedding';
import { base } from '$app/paths';
import { languages } from '$lib/languages';
import { browser } from '$app/environment';
import { goto } from '$app/navigation';
import { getURLForLanguage } from '$lib/utils';
const appRoutes = ['/[[language]]/app', '/[[language]]/embed'];
const appRoutes = ['/[[language]]/app', '/[[language]]/embed'];
onMount(() => {
if ($page.url.searchParams.has('embed')) {
// convert old embedding options to new format and redirect to new embed page
let folders = $page.url.pathname.split('/');
let locale =
folders.indexOf('l') >= 0 ? folders[folders.indexOf('l') + 1] ?? 'en' : 'en';
window.location.href = `${getURLForLanguage(locale, '/embed')}?options=${encodeURIComponent(JSON.stringify(convertOldEmbeddingOptions($page.url.searchParams)))}`;
}
});
onMount(() => {
if ($page.url.searchParams.has('embed')) {
// convert old embedding options to new format and redirect to new embed page
let folders = $page.url.pathname.split('/');
let locale = folders.indexOf('l') >= 0 ? folders[folders.indexOf('l') + 1] ?? 'en' : 'en';
window.location.href = `${getURLForLanguage(locale, '/embed')}?options=${encodeURIComponent(JSON.stringify(convertOldEmbeddingOptions($page.url.searchParams)))}`;
}
});
$: if ($page.route.id.includes('[[language]]')) {
if ($page.params.language) {
let lang = $page.params.language.replace('/', '');
if ($locale !== lang) {
if (languages.hasOwnProperty(lang)) {
locale.set(lang);
} else if (browser) {
goto(`${base}/404`);
}
}
} else if ($locale !== 'en') {
locale.set('en');
}
}
$: if ($page.route.id?.includes('[[language]]')) {
if ($page.params.language) {
let lang = $page.params.language.replace('/', '');
if ($locale !== lang) {
if (languages.hasOwnProperty(lang)) {
locale.set(lang);
} else if (browser) {
goto(`${base}/404`);
}
}
} else if ($locale !== 'en') {
locale.set('en');
}
}
$: if (browser && !$isLoading && $_) {
document.title = `gpx.studio — ${$_(`metadata.${$page.route.id?.replace('/[[language]]', '').split('/')[1] ?? 'home'}_title`)}`;
}
$: showNavAndFooter = $page.route.id === null || !appRoutes.includes($page.route.id);
$: if (browser && !$isLoading && $locale) {
let title = `gpx.studio — ${$_(`metadata.${$page.route.id?.replace('/[[language]]', '').split('/')[1] ?? 'home'}_title`)}`;
if ($page.params.guide) {
const [guide, subguide] = $page.params.guide.split('/');
const promise = subguide
? import(`../lib/docs/${$locale ?? 'en'}/${guide}/${subguide}.mdx`)
: import(`../lib/docs/${$locale ?? 'en'}/${guide}.mdx`);
promise.then((module) => {
document.title = `${title} | ${module.metadata.title}`;
});
} else {
document.title = title;
}
}
$: showNavAndFooter = $page.route.id === null || !appRoutes.includes($page.route.id);
</script>
<ModeWatcher />
<div class="flex flex-col min-h-screen">
{#if !$isLoading}
{#if showNavAndFooter}
<Nav />
{/if}
<main class="grow flex flex-col">
<slot />
</main>
{#if showNavAndFooter}
<Footer />
{/if}
{/if}
{#if !$isLoading}
{#if showNavAndFooter}
<Nav />
{/if}
<main class="grow flex flex-col">
<slot />
</main>
{#if showNavAndFooter}
<Footer />
{/if}
{/if}
</div>

View File

@@ -2,9 +2,9 @@ import { guides } from '$lib/components/docs/docs.js';
function getModule(language: string | undefined, guide: string) {
language = language ?? 'en';
let subguide = guide.includes('/') ? guide.split('/').pop() : undefined;
if (subguide) {
guide = guide.replace(`/${subguide}`, '');
let subguide = undefined;
if (guide.includes('/')) {
[guide, subguide] = guide.split('/');
}
return subguide
? import(`./../../../lib/docs/${language}/${guide}/${subguide}.mdx`)

View File

@@ -2,9 +2,9 @@ import { getNextGuide, getPreviousGuide } from "$lib/components/docs/docs";
function getModule(language: string | undefined, guide: string) {
language = language ?? 'en';
let subguide = guide.includes('/') ? guide.split('/').pop() : undefined;
if (subguide) {
guide = guide.replace(`/${subguide}`, '');
let subguide = undefined;
if (guide.includes('/')) {
[guide, subguide] = guide.split('/');
}
return subguide
? import(`./../../../../lib/docs/${language}/${guide}/${subguide}.mdx`)