1- import { defineConfig , DefaultTheme } from 'vitepress'
1+ import { defineConfig } from 'vitepress'
2+ import type { DefaultTheme } from 'vitepress'
23import { getNestedSidebarItems } from './utils/sidebar'
34
45const 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 ( / \. m d $ / , '' )
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
636const 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