Skip to content

Commit

Permalink
feat: i18n with sitemap (#2708)
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd committed Jul 30, 2023
1 parent 2be6858 commit 7778187
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
5 changes: 4 additions & 1 deletion docs/.vitepress/config.ts
Expand Up @@ -13,7 +13,10 @@ export default defineConfig({
cleanUrls: true,

sitemap: {
hostname: 'https://vitepress.dev'
hostname: 'https://vitepress.dev',
transformItems(items) {
return items.filter((item) => !item.url.includes('migration'))
}
},

head: [
Expand Down
56 changes: 46 additions & 10 deletions src/node/build/generateSitemap.ts
Expand Up @@ -14,19 +14,55 @@ import { task } from '../utils/task'
export async function generateSitemap(siteConfig: SiteConfig) {
if (!siteConfig.sitemap?.hostname) return

const getLastmod = async (url: string) => {
if (!siteConfig.lastUpdated) return undefined

let path = url.replace(/(^|\/)$/, '$1index')
path = path.replace(/(\.html)?$/, '.md')
path = siteConfig.rewrites.inv[path] || path

return (await getGitTimestamp(path)) || undefined
}

await task('generating sitemap', async () => {
let items: SitemapItem[] = await Promise.all(
siteConfig.pages.map(async (page) => {
//
let url = siteConfig.rewrites.map[page] || page
url = url.replace(/(^|\/)index\.md$/, '$1')
url = url.replace(/\.md$/, siteConfig.cleanUrls ? '' : '.html')

const lastmod = siteConfig.lastUpdated && (await getGitTimestamp(page))
return lastmod ? { url, lastmod } : { url }
const locales = siteConfig.userConfig.locales || {}
const filteredLocales = Object.keys(locales).filter(
(locale) => locales[locale].lang && locale !== 'root'
)
const defaultLang =
locales?.root?.lang || siteConfig.userConfig.lang || 'en-US'

const pages = siteConfig.pages.map(
(page) => siteConfig.rewrites.map[page] || page
)

const groupedPages: Record<string, { lang: string; url: string }[]> = {}
pages.forEach((page) => {
const locale = page.split('/')[0]
const lang = locales[locale]?.lang || defaultLang

let url = page.replace(/(^|\/)index\.md$/, '$1')
url = url.replace(/\.md$/, siteConfig.cleanUrls ? '' : '.html')
if (filteredLocales.includes(locale)) page = page.slice(locale.length + 1)

if (!groupedPages[page]) groupedPages[page] = []
groupedPages[page].push({ url, lang })
})

const _items = await Promise.all(
Object.values(groupedPages).map(async (pages) => {
if (pages.length < 2)
return { url: pages[0].url, lastmod: await getLastmod(pages[0].url) }

return await Promise.all(
pages.map(async ({ url }) => {
return { url, lastmod: await getLastmod(url), links: pages }
})
)
})
)
items = items.sort((a, b) => a.url.localeCompare(b.url))

let items: SitemapItem[] = _items.flat()
items = (await siteConfig.sitemap?.transformItems?.(items)) || items

const sitemapStream = new SitemapStream(siteConfig.sitemap)
Expand Down
2 changes: 2 additions & 0 deletions src/node/utils/getGitTimestamp.ts
@@ -1,4 +1,5 @@
import { spawn } from 'cross-spawn'
import fs from 'fs-extra'
import { basename, dirname } from 'path'

const cache = new Map<string, number>()
Expand All @@ -9,6 +10,7 @@ export function getGitTimestamp(file: string) {

return new Promise<number>((resolve, reject) => {
const cwd = dirname(file)
if (!fs.existsSync(cwd)) return resolve(0)
const fileName = basename(file)
const child = spawn('git', ['log', '-1', '--pretty="%ai"', fileName], {
cwd
Expand Down

0 comments on commit 7778187

Please sign in to comment.