44 */
55
66import type { BlogPost , HreflangLink , SupportedLang } from '@models/blog' ;
7-
8- const SITE_URL = 'https://jjuanrivvera.com' ;
9-
10- // Language code mapping for hreflang (ISO 639-1 + ISO 3166-1)
11- const LANG_CODES : Record < SupportedLang , string > = {
12- en : 'en-US' ,
13- es : 'es-ES' ,
14- pt : 'pt-BR' ,
15- } ;
7+ import { SITE_CONFIG , LOCALE_MAP } from '@config/site' ;
168
179/**
1810 * Build blog post URL for a specific language
11+ * @param slug - The post slug (with or without language prefix)
12+ * @param lang - The target language code
13+ * @returns Full URL to the blog post
14+ * @example
15+ * buildPostUrl('my-post', 'en') // => 'https://jjuanrivvera.com/blog/my-post'
16+ * buildPostUrl('my-post', 'es') // => 'https://jjuanrivvera.com/es/blog/my-post'
1917 */
2018function buildPostUrl ( slug : string , lang : SupportedLang ) : string {
2119 // Remove language prefix from slug if present (safe regex)
2220 const cleanSlug = slug . replace ( / ^ ( e n | e s | p t ) \/ / , '' ) ;
2321
2422 // English posts don't have language prefix
2523 if ( lang === 'en' ) {
26- return `${ SITE_URL } /blog/${ cleanSlug } ` ;
24+ return `${ SITE_CONFIG . url } /blog/${ cleanSlug } ` ;
2725 }
2826
29- return `${ SITE_URL } /${ lang } /blog/${ cleanSlug } ` ;
27+ return `${ SITE_CONFIG . url } /${ lang } /blog/${ cleanSlug } ` ;
3028}
3129
3230/**
@@ -43,7 +41,7 @@ export function generateHreflangLinks(
4341
4442 // Add current post's language
4543 links . push ( {
46- lang : LANG_CODES [ currentPost . data . lang ] ,
44+ lang : LOCALE_MAP [ currentPost . data . lang ] ,
4745 url : buildPostUrl ( currentPost . slug , currentPost . data . lang ) ,
4846 } ) ;
4947
@@ -58,7 +56,7 @@ export function generateHreflangLinks(
5856
5957 translations . forEach ( ( translation ) => {
6058 links . push ( {
61- lang : LANG_CODES [ translation . data . lang ] ,
59+ lang : LOCALE_MAP [ translation . data . lang ] ,
6260 url : buildPostUrl ( translation . slug , translation . data . lang ) ,
6361 } ) ;
6462 } ) ;
@@ -87,6 +85,12 @@ export function generateHreflangLinks(
8785
8886/**
8987 * Get translation of a post in a specific language
88+ * @param currentPost - The current blog post
89+ * @param targetLang - The target language to find translation for
90+ * @param allPosts - All blog posts to search within
91+ * @returns The translated post or null if not found
92+ * @example
93+ * const spanishPost = getTranslation(englishPost, 'es', allPosts);
9094 */
9195export function getTranslation (
9296 currentPost : BlogPost ,
@@ -107,6 +111,12 @@ export function getTranslation(
107111
108112/**
109113 * Get all available translations for a post
114+ * @param currentPost - The current blog post
115+ * @param allPosts - All blog posts to search within
116+ * @returns Map of language codes to their corresponding posts
117+ * @example
118+ * const translations = getAllTranslations(post, allPosts);
119+ * const spanishVersion = translations.get('es');
110120 */
111121export function getAllTranslations (
112122 currentPost : BlogPost ,
@@ -136,24 +146,36 @@ export function getAllTranslations(
136146
137147/**
138148 * Build blog listing URL for a specific language
149+ * @param lang - The target language code
150+ * @param page - The page number (defaults to 1)
151+ * @returns Full URL to the blog listing page
152+ * @example
153+ * buildBlogListingUrl('en', 1) // => 'https://jjuanrivvera.com/blog'
154+ * buildBlogListingUrl('es', 2) // => 'https://jjuanrivvera.com/es/blog/2'
139155 */
140156export function buildBlogListingUrl (
141157 lang : SupportedLang ,
142158 page : number = 1
143159) : string {
144160 const basePath = lang === 'en' ? '/blog' : `/${ lang } /blog` ;
145161 return page === 1
146- ? `${ SITE_URL } ${ basePath } `
147- : `${ SITE_URL } ${ basePath } /${ page } ` ;
162+ ? `${ SITE_CONFIG . url } ${ basePath } `
163+ : `${ SITE_CONFIG . url } ${ basePath } /${ page } ` ;
148164}
149165
150166/**
151167 * Build tag page URL for a specific language
168+ * @param tag - The tag name (will be normalized to lowercase with hyphens)
169+ * @param lang - The target language code
170+ * @returns Full URL to the tag archive page
171+ * @example
172+ * buildTagUrl('TypeScript', 'en') // => 'https://jjuanrivvera.com/blog/tag/typescript'
173+ * buildTagUrl('Web Dev', 'es') // => 'https://jjuanrivvera.com/es/blog/tag/web-dev'
152174 */
153175export function buildTagUrl ( tag : string , lang : SupportedLang ) : string {
154176 const normalizedTag = tag . toLowerCase ( ) . replace ( / \s + / g, '-' ) ;
155177 const basePath = lang === 'en' ? '/blog/tag' : `/${ lang } /blog/tag` ;
156- return `${ SITE_URL } ${ basePath } /${ normalizedTag } ` ;
178+ return `${ SITE_CONFIG . url } ${ basePath } /${ normalizedTag } ` ;
157179}
158180
159181/**
0 commit comments