📄 Huge intl feature

This commit is contained in:
RJ
2025-12-03 00:17:34 +02:00
parent 8b05aae5a8
commit 7e8b82f571
48 changed files with 955 additions and 138 deletions

View File

@@ -7,6 +7,10 @@ interface LinkNode extends Node {
children: Node[]
}
interface Options {
locale?: string
}
/**
* Detects internal blog post links:
* - Relative paths (no http/https)
@@ -24,11 +28,11 @@ function isInternalBlogLink(url: string): boolean {
/**
* Transforms internal .md links to blog routes:
* - tech/article.md → /blog/tech/article
* - article.md#section → /blog/article#section
* - nested/path/post.md?ref=foo → /blog/nested/path/post?ref=foo
* - tech/article.md → /[locale]/blog/tech/article
* - article.md#section → /[locale]/blog/article#section
* - nested/path/post.md?ref=foo → /[locale]/blog/nested/path/post?ref=foo
*/
function transformToBlogPath(url: string): string {
function transformToBlogPath(url: string, locale: string = 'en'): string {
// Split into path, hash, and query
const hashIndex = url.indexOf('#')
const queryIndex = url.indexOf('?')
@@ -50,17 +54,19 @@ function transformToBlogPath(url: string): string {
// Remove .md extension
const cleanPath = path.replace(/\.md$/, '')
// Build final URL
return `/blog/${cleanPath}${query}${hash}`
// Build final URL with locale prefix
return `/${locale}/blog/${cleanPath}${query}${hash}`
}
export function remarkInternalLinks() {
export function remarkInternalLinks(options: Options = {}) {
const locale = options.locale || 'en'
return (tree: Node) => {
visit(tree, 'link', (node: Node) => {
const linkNode = node as LinkNode
if (isInternalBlogLink(linkNode.url)) {
linkNode.url = transformToBlogPath(linkNode.url)
linkNode.url = transformToBlogPath(linkNode.url, locale)
}
})
}