Skip to content

Commit 532a88f

Browse files
committed
✨ Make doc links relative to language
1 parent 59a7503 commit 532a88f

7 files changed

Lines changed: 122 additions & 10 deletions

File tree

‎src/docs/welcome/ru.md‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
Russian
2+
3+
<div class="header">
4+
<div>
5+
<h1>Welcome to</h1>
6+
<h1>Animated Java!</h1>
7+
</div>
8+
<img src="/images/armor_stand_wave.webp" alt="An Armor Stand waving at the user" />
9+
</div>
10+
11+
This documentation will help **you** 🫵 take your Data Pack project's polish to the
12+
next level with Animated Java!
13+
14+
<br>
15+
16+
:::warning[Before you get started]
17+
It is highly recommended to be familiar with how [Data Packs](https://minecraft.wiki/w/Data_pack) and [Resource Packs](https://minecraft.wiki/w/Resource_pack) work in Minecraft: Java Edition.
18+
:::
19+
20+
<style>
21+
.header {
22+
display: flex;
23+
flex-direction: row;
24+
align-items: center;
25+
justify-content: space-between;
26+
gap: 16px;
27+
}
28+
29+
.header div {
30+
display: flex;
31+
flex-direction: column;
32+
}
33+
34+
h1 {
35+
margin: 0;
36+
}
37+
38+
img {
39+
border: unset;
40+
outline: unset;
41+
box-shadow: unset;
42+
background: unset;
43+
}
44+
45+
@media (width < 950px) {
46+
.header {
47+
flex-direction: column-reverse;
48+
text-align: center;
49+
}
50+
51+
img {
52+
width: 150px;
53+
height: 150px;
54+
margin-top: 8px;
55+
}
56+
}
57+
</style>

‎src/lib/docs/docs.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { resolve } from '$app/paths'
12
import type { Component } from 'svelte'
23

34
const MODULES = import.meta.glob('/src/docs/**/*.md')
@@ -13,3 +14,8 @@ export function getDocModule(lang = 'en', path: string) {
1314

1415
return mod as () => Promise<{ default: Component; metadata: Record<string, any> }>
1516
}
17+
18+
export function docHref(path: string, lang: string): string {
19+
const prefix = lang === DEFAULT_LANGUAGE ? '' : `/${lang}`
20+
return resolve(`${prefix}/${path}`)
21+
}

‎src/lib/docs/rehypeDocLinks.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { Root } from 'hast'
2+
import type { Plugin } from 'unified'
3+
import { visit } from 'unist-util-visit'
4+
5+
const rehypeDocLinks: Plugin<[], Root> = () => tree => {
6+
visit(tree, 'element', node => {
7+
if (node.tagName !== 'a') return
8+
const href = node.properties?.href as string | undefined
9+
if (!href || href.startsWith('http') || href.startsWith('#')) return
10+
11+
// Mark as a doc-internal link; store the path for runtime resolution
12+
node.properties['data-doc-link'] = href
13+
})
14+
}
15+
16+
export default rehypeDocLinks

‎src/routes/[[lang]]/docs/+layout.svelte‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<script lang="ts">
22
import { browser } from '$app/environment'
3-
import { afterNavigate } from '$app/navigation'
3+
import { afterNavigate, goto } from '$app/navigation'
4+
import { resolve } from '$app/paths'
45
import Footer from '$lib/components/footer.svelte'
56
import MinecraftIcon from '$lib/components/minecraftIcon.svelte'
6-
import { onDestroy, onMount, tick } from 'svelte'
7+
import { DEFAULT_LANGUAGE, docHref } from '$lib/docs/docs'
8+
import { onDestroy, onMount, setContext, tick } from 'svelte'
79
810
interface TocHeading {
911
id: string
@@ -17,6 +19,23 @@
1719
const defaultDescription = 'Effortlessly craft complex animations for Minecraft: Java Edition'
1820
const socialImage = `${siteUrl}/images/animated_java_icon.svg`
1921
22+
const lang = $derived(data.params.lang ?? DEFAULT_LANGUAGE)
23+
$effect(() => {
24+
setContext('lang', lang)
25+
console.log(`Current language set to: ${lang}`)
26+
})
27+
28+
function handleClick(event: MouseEvent): void {
29+
const target = (event.target as HTMLElement).closest('a')
30+
if (!target) return
31+
const path = target.getAttribute('data-doc-link')
32+
if (!path) return
33+
34+
event.preventDefault()
35+
const prefix = lang === DEFAULT_LANGUAGE ? '' : `/${lang}`
36+
void goto(resolve(`${prefix}/docs/${path}`))
37+
}
38+
2039
const pageTitle = $derived(
2140
data.currentDoc?.title ? `${data.currentDoc.title} | ${docsTitle}` : docsTitle
2241
)
@@ -201,6 +220,8 @@
201220
<meta name="twitter:image" content={socialImage} />
202221
</svelte:head>
203222

223+
<svelte:window onclick={handleClick} />
224+
204225
<div class="docs-shell">
205226
<header class="docs-header minecraft-box">
206227
<a href="/" class="brand-link">
@@ -265,7 +286,7 @@
265286
<a
266287
class={`section-link ${data.currentPath === section.to ? 'active' : ''}`}
267288
onclick={closeMobileSidebar}
268-
href={section.to}
289+
href={docHref(section.to, lang)}
269290
>{section.title}
270291

271292
{#if data.currentPath === section.to}
@@ -283,7 +304,7 @@
283304
<a
284305
class:active={data.currentPath === item.to}
285306
onclick={closeMobileSidebar}
286-
href={item.to}
307+
href={docHref(item.to, lang)}
287308
>{item.title}
288309

289310
{#if data.currentPath === item.to}
@@ -307,15 +328,17 @@
307328
{#if data.previous ?? data.next}
308329
<nav class="docs-pagination" aria-label="Previous and next page links">
309330
{#if data.previous}
310-
<a class="minecraft-button" href={data.previous.to}
331+
<a class="minecraft-button" href={docHref(data.previous.to, lang)}
311332
>&larr; {data.previous.title}</a
312333
>
313334
{:else}
314335
<div></div>
315336
{/if}
316337

317338
{#if data.next}
318-
<a class="minecraft-button" href={data.next.to}>{data.next.title} &rarr;</a>
339+
<a class="minecraft-button" href={docHref(data.next.to, lang)}
340+
>{data.next.title} &rarr;</a
341+
>
319342
{/if}
320343
</nav>
321344
{/if}

‎src/routes/[[lang]]/docs/+layout.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { DEFAULT_LANGUAGE } from '$lib/docs/docs'
12
import { docsNavLinks, docsSidebar, flattenDocLinks, type DocLeaf } from '$lib/docs/navigation'
3+
import type { LayoutLoad } from '../$types'
24

35
const FLATTENED_DOCS = flattenDocLinks(docsSidebar)
46

@@ -8,8 +10,11 @@ function normalizedPath(pathname: string): string {
810
return pathname
911
}
1012

11-
export function load({ url }: { url: URL }) {
12-
const currentPath = normalizedPath(url.pathname)
13+
export const load: LayoutLoad = ({ params, url }) => {
14+
const currentPath = normalizedPath(url.pathname).replace(
15+
'/' + (params.lang ?? DEFAULT_LANGUAGE),
16+
''
17+
)
1318
const currentIndex = FLATTENED_DOCS.findIndex(doc => doc.to === currentPath)
1419
const currentDoc = currentIndex >= 0 ? FLATTENED_DOCS[currentIndex] : null
1520

@@ -21,12 +26,15 @@ export function load({ url }: { url: URL }) {
2126
next = currentIndex < FLATTENED_DOCS.length - 1 ? FLATTENED_DOCS[currentIndex + 1] : null
2227
}
2328

29+
console.log('Current path:', currentPath)
30+
2431
return {
2532
docsNavLinks,
2633
docsSidebar,
2734
currentPath,
2835
currentDoc,
2936
previous,
3037
next,
38+
params,
3139
}
3240
}

‎svelte.config.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { resolve } from 'path'
55
import rehypeSlug from 'rehype-slug'
66
import remarkDirective from 'remark-directive'
77
import remarkGfm from 'remark-gfm'
8+
import rehypeDocLinks from './src/lib/docs/rehypeDocLinks.ts'
89
import remarkAdmonitions from './src/lib/docs/remark-admonitions.ts'
910
import './src/lib/prismjs/mcfunction.ts'
1011

@@ -17,7 +18,7 @@ const config = {
1718
vitePreprocess(),
1819
mdsvex({
1920
extensions: ['.md'],
20-
rehypePlugins: [rehypeSlug, remarkAdmonitions],
21+
rehypePlugins: [rehypeSlug, remarkAdmonitions, rehypeDocLinks],
2122
remarkPlugins: [remarkGfm, remarkDirective],
2223
highlight: {
2324
highlighter: (code, lang) => {

‎tsconfig.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"sourceMap": true,
1212
"esModuleInterop": true,
1313
"forceConsistentCasingInFileNames": true,
14-
"skipLibCheck": true
14+
"skipLibCheck": true,
15+
"allowImportingTsExtensions": true
1516
}
1617
}

0 commit comments

Comments
 (0)