Skip to content

Commit 9ea07d3

Browse files
committed
feat: add redirect to new doc
1 parent e71008a commit 9ea07d3

2 files changed

Lines changed: 41 additions & 9 deletions

File tree

README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
11
# PicGo-Core-Doc
22

3-
[![PicGo Convention](https://img.shields.io/badge/picgo-convention-blue.svg?style=flat-square)](https://github.com/PicGo/bump-version)
4-
5-
PicGo-Core 的官方文档 -> https://picgo.github.io/PicGo-Core-Doc/
6-
7-
## 贡献指南
8-
9-
非常欢迎你为 PicGo-Core-Doc 做出贡献!发起 Pull Request 之前,请确保阅读了[贡献指南](/CONTRIBUTING.md)
3+
Archived repository for PicGo-Core documentation. The newest documentation can be found at [https://docs.picgo.app/core/](https://docs.picgo.app/core/).

docs/.vitepress/config.mts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,37 @@
1-
import { defineConfig, DefaultTheme } from 'vitepress'
1+
import { defineConfig } from 'vitepress'
2+
import type { DefaultTheme } from 'vitepress'
23
import { getNestedSidebarItems } from './utils/sidebar'
34

45
const EDIT_LINK_PATTERN = 'https://github.com/PicGo/PicGo-Core-Doc/edit/master/docs/:path'
6+
const NEW_SITE_ORIGIN = 'https://docs.picgo.app'
7+
const NEW_SECTION_BASE = '/core/'
8+
const LEGACY_BASE = '/PicGo-Core-Doc/'
9+
10+
const normalizeRelativePath = (relativePath: string): { localePrefix: string; path: string } => {
11+
const withoutExt = relativePath.replace(/\.md$/, '')
12+
const isZh = withoutExt.startsWith('zh/')
13+
const localePrefix = isZh ? '/zh' : ''
14+
const localizedPath = isZh ? withoutExt.slice(3) : withoutExt
15+
16+
if (!localizedPath || localizedPath === 'index') {
17+
return { localePrefix, path: '' }
18+
}
19+
20+
if (localizedPath.endsWith('/index')) {
21+
return { localePrefix, path: `${localizedPath.slice(0, -'/index'.length)}/` }
22+
}
23+
24+
return { localePrefix, path: localizedPath }
25+
}
26+
27+
const buildCanonicalUrl = (relativePath: string): string => {
28+
const { localePrefix, path } = normalizeRelativePath(relativePath)
29+
const base = `${NEW_SITE_ORIGIN}${localePrefix}${NEW_SECTION_BASE}`
30+
const normalizedPath = path.replace(/^\/+/, '')
31+
return `${base}${normalizedPath}`
32+
}
33+
34+
const buildRedirectScript = (): string => `\n(function() {\n var newHost = '${NEW_SITE_ORIGIN}';\n var sectionBase = '${NEW_SECTION_BASE}';\n var oldBase = '${LEGACY_BASE}';\n var path = window.location.pathname || '/';\n\n if (path.startsWith(oldBase)) {\n path = path.slice(oldBase.length);\n } else {\n path = path.replace(/^\\/+/, '');\n }\n\n var isZh = path.startsWith('zh/');\n if (isZh) {\n path = path.slice(3);\n }\n\n if (path === '' || path === 'index.html') {\n path = '';\n } else if (path.endsWith('/index.html')) {\n path = path.slice(0, -'/index.html'.length) + '/';\n } else if (path.endsWith('.html')) {\n path = path.slice(0, -'.html'.length);\n }\n\n if (path.startsWith('/')) {\n path = path.slice(1);\n }\n\n var newUrl = newHost + (isZh ? '/zh' : '') + sectionBase + path + window.location.search + window.location.hash;\n if (newUrl !== window.location.href) {\n window.location.replace(newUrl);\n }\n})();\n`
535

636
const communityNavItems = [
737
{
@@ -158,6 +188,14 @@ export default defineConfig({
158188
}
159189
]
160190
],
191+
transformHead: ({ pageData }) => {
192+
const canonicalUrl = buildCanonicalUrl(pageData.relativePath)
193+
return [
194+
['link', { rel: 'canonical', href: canonicalUrl }],
195+
['meta', { 'http-equiv': 'refresh', content: `0;url=${canonicalUrl}` }],
196+
['script', {}, buildRedirectScript()]
197+
]
198+
},
161199
sitemap: {
162200
hostname: 'https://picgo.github.io/PicGo-Core-Doc/'
163201
},
@@ -242,4 +280,4 @@ export default defineConfig({
242280
themeConfig: zhThemeConfig
243281
}
244282
}
245-
})
283+
})

0 commit comments

Comments
 (0)