Skip to content

Commit

Permalink
Merge pull request #25 from chengpeiquan/develop
Browse files Browse the repository at this point in the history
chore: typo
  • Loading branch information
chengpeiquan committed Dec 5, 2022
2 parents 5148393 + 7f8f169 commit 590c88e
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 159 deletions.
3 changes: 0 additions & 3 deletions .vitepress/theme/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { inBrowser } from 'vitepress'
import DefaultTheme from 'vitepress/theme'
import { changeLocales } from './plugins/locales'
import { setSymbolStyle, replaceSymbol } from './plugins/symbol'
import { siteIds, registerAnalytics, trackPageview } from './plugins/analytics'
import { isInvalidRoute, redirect } from './plugins/redirect'
import './styles/custom.css'
Expand All @@ -15,7 +14,6 @@ const theme: Theme = {
redirect()
}

setSymbolStyle()
siteIds.forEach((id) => registerAnalytics(id))

window.addEventListener('load', () => {
Expand All @@ -29,7 +27,6 @@ const theme: Theme = {

router.onAfterRouteChanged = (to) => {
changeLocales()
replaceSymbol()
siteIds.forEach((id) => trackPageview(id, to))
}
}
Expand Down
15 changes: 2 additions & 13 deletions .vitepress/theme/plugins/analytics.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { inBrowser } from 'vitepress'

/**
* 统计站点的 ID 列表
*/
export const siteIds = [
'd6895b6f22616e579e9e6d37936b8dca', // 主站
'025e7d9acbc7359afa71bdae5aa03f33', // 本站
'd6895b6f22616e579e9e6d37936b8dca', // Cropper
'8dca8e2532df48ea7f1b15c714588691', // Blog
]

/**
* 注册统计
*/
export function registerAnalytics(siteId: string) {
if (!inBrowser) return
if (document.querySelector(`#analytics-plugin-${siteId}`)) return
Expand All @@ -23,11 +17,6 @@ export function registerAnalytics(siteId: string) {
document.querySelector('head')?.appendChild(script)
}

/**
* 上报 PV 数据
* @param siteId - 站点 ID
* @param pageUrl - 页面 URL
*/
export function trackPageview(siteId: string, pageUrl: string) {
if (!inBrowser) return
if (!pageUrl || typeof pageUrl !== 'string') {
Expand Down
17 changes: 1 addition & 16 deletions .vitepress/theme/plugins/redirect.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,29 @@
import { inBrowser } from 'vitepress'

/**
* 重定向的 Map 表
*/
export const redirectMap = {
update: 'upgrade',
// update: 'upgrade',
}

/**
* 根据页面 URL 提取路由名称
*/
export function getRouteName() {
if (!inBrowser) return ''
const { pathname } = window.location
const routeName = pathname.slice(1).replace('.html', '')
return routeName
}

/**
* 判断是否无效路由
*/
export function isInvalidRoute() {
if (!inBrowser) return false
const routeName = getRouteName()
const keys = Object.keys(redirectMap)
return keys.includes(routeName)
}

/**
* 获取重定向目标
*/
export function redirectTarget() {
if (!inBrowser) return '/'
const routeName = getRouteName()
return `/${redirectMap[routeName]}.html` || '/'
}

/**
* 重定向
*/
export function redirect() {
if (!inBrowser) return
window.location.replace(redirectTarget())
Expand Down
127 changes: 0 additions & 127 deletions .vitepress/theme/plugins/symbol.ts

This file was deleted.

0 comments on commit 590c88e

Please sign in to comment.