Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: i18n with sitemap #2708

Merged
merged 1 commit into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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