valaxy 1.0.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/client/components/ValaxyCopyright.vue +2 -1
- package/client/components/ValaxyCover.vue +92 -0
- package/client/components/ValaxyFootnoteTooltip.vue +6 -14
- package/client/components/ValaxySvgExport.vue +71 -0
- package/client/components/footnote/FootnotePopover.vue +146 -0
- package/client/components/footnote/LegacyFootnoteTooltip.vue +25 -0
- package/client/composables/collections.ts +13 -9
- package/client/composables/decrypt.ts +8 -1
- package/client/composables/outline/anchor.ts +20 -8
- package/client/composables/post/filter.ts +38 -0
- package/client/composables/post/index.ts +2 -34
- package/client/index.d.ts +0 -1
- package/client/layouts/README.md +1 -1
- package/client/locales/en.yml +2 -0
- package/client/locales/zh-CN.yml +2 -0
- package/client/main.ts +35 -20
- package/client/modules/valaxy.ts +14 -5
- package/client/shims.d.ts +27 -0
- package/client/utils/index.ts +1 -0
- package/client/utils/layouts.ts +359 -0
- package/client/utils/path.ts +30 -0
- package/client/utils/svg.ts +104 -0
- package/dist/node/cli/index.mjs +4 -11
- package/dist/node/index.d.mts +71 -46
- package/dist/node/index.mjs +4 -11
- package/dist/shared/{valaxy.CjaAzLmd.d.mts → valaxy.BGgY-Hwl.d.mts} +28 -3
- package/dist/shared/{valaxy.DstXQBtK.mjs → valaxy.CRodiDWz.mjs} +402 -1811
- package/dist/types/index.d.mts +2 -2
- package/package.json +9 -15
- package/types/config.ts +12 -1
- package/types/frontmatter/page.ts +9 -2
- package/types/vue-router.d.ts +8 -0
- package/vendor/README.md +8 -8
- package/vendor/sources.json +0 -31
- package/vendor/i18n/LICENSE +0 -20
- package/vendor/i18n/index.d.mts +0 -7
- package/vendor/i18n/index.mjs +0 -1389
- package/vendor/i18n/types.d.ts +0 -48
- package/vendor/layouts/LICENSE +0 -21
- package/vendor/layouts/client.d.ts +0 -11
- package/vendor/layouts/index.d.mts +0 -86
- package/vendor/layouts/index.mjs +0 -235
- package/vendor/layouts/layoutName-DVkP7Qpo.mjs +0 -82
- package/vendor/layouts/runtime.d.mts +0 -72
- package/vendor/layouts/runtime.mjs +0 -284
package/client/main.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { dataSymbol, initValaxyConfig, valaxyConfigSymbol } from 'valaxy'
|
|
|
4
4
|
import { setupLayouts } from 'virtual:generated-layouts'
|
|
5
5
|
import { createSSRApp, createApp as vueCreateApp } from 'vue'
|
|
6
6
|
import { createMemoryHistory, createRouter, createWebHistory } from 'vue-router'
|
|
7
|
-
import { routes as autoRoutes } from 'vue-router/auto-routes'
|
|
7
|
+
import { routes as autoRoutes, handleHotUpdate } from 'vue-router/auto-routes'
|
|
8
8
|
|
|
9
9
|
// import App from '/@valaxyjs/App.vue'
|
|
10
10
|
import App from './App.vue'
|
|
@@ -19,25 +19,12 @@ import '#valaxy/styles'
|
|
|
19
19
|
|
|
20
20
|
const valaxyConfig = initValaxyConfig()
|
|
21
21
|
|
|
22
|
-
const routes = [...autoRoutes]
|
|
23
|
-
|
|
24
|
-
const { redirectRoutes, useVueRouter } = valaxyConfig.value.runtimeConfig.redirects
|
|
25
|
-
if (useVueRouter)
|
|
26
|
-
routes.push(...redirectRoutes)
|
|
27
|
-
|
|
28
|
-
// fix chinese path
|
|
29
|
-
routes.forEach((i) => {
|
|
30
|
-
i?.children?.forEach((j: { path: string }) => {
|
|
31
|
-
j.path = encodeURI(j.path)
|
|
32
|
-
})
|
|
33
|
-
})
|
|
34
|
-
|
|
35
22
|
/**
|
|
36
23
|
* Flatten nested routes so that `setupLayouts` can wrap every leaf route.
|
|
37
24
|
*
|
|
38
25
|
* `vue-router/vite` (file-based routing) generates nested route trees for subdirectories
|
|
39
26
|
* (e.g. `pages/zh/guide/foo.md` → `{ path: '/zh', children: [{ path: 'guide', children: … }] }`).
|
|
40
|
-
* `
|
|
27
|
+
* `setupLayouts` (`virtual:generated-layouts`) only wraps **top-level** routes,
|
|
41
28
|
* so nested children would miss the layout wrapper entirely.
|
|
42
29
|
*
|
|
43
30
|
* This function extracts every leaf (component-bearing or redirect) route into a flat
|
|
@@ -81,11 +68,29 @@ function filterDraft(routes: any[]) {
|
|
|
81
68
|
})
|
|
82
69
|
}
|
|
83
70
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
)
|
|
71
|
+
/**
|
|
72
|
+
* Turn the routes generated by `vue-router/vite` into the ones valaxy registers.
|
|
73
|
+
* Also used on HMR, when `vue-router/auto-routes` hands over freshly generated routes.
|
|
74
|
+
*/
|
|
75
|
+
function resolveRoutes(generatedRoutes: any[]) {
|
|
76
|
+
const routes = [...generatedRoutes]
|
|
77
|
+
|
|
78
|
+
const { redirectRoutes, useVueRouter } = valaxyConfig.value.runtimeConfig.redirects
|
|
79
|
+
if (useVueRouter)
|
|
80
|
+
routes.push(...redirectRoutes)
|
|
81
|
+
|
|
82
|
+
// Non-ASCII file names (e.g. `pages/posts/中文.md`) need no extra handling here:
|
|
83
|
+
// `vue-router/vite` already percent-encodes static path segments, and encoding
|
|
84
|
+
// them again makes the route unreachable from its real URL.
|
|
85
|
+
|
|
86
|
+
// not filter hide for ssg
|
|
87
|
+
return setupLayouts(import.meta.env.DEV
|
|
88
|
+
? flattenRoutes(routes)
|
|
89
|
+
: filterDraft(flattenRoutes(routes)),
|
|
90
|
+
)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export const routesWithLayout = resolveRoutes(autoRoutes)
|
|
89
94
|
|
|
90
95
|
if (import.meta.env.DEV)
|
|
91
96
|
setupValaxyDevTools()
|
|
@@ -117,6 +122,16 @@ export function createValaxyApp(options: CreateValaxyAppOptions = {}): ValaxySSG
|
|
|
117
122
|
},
|
|
118
123
|
})
|
|
119
124
|
|
|
125
|
+
// Without a registered router, `vue-router/auto-routes` reloads the page on every page edit.
|
|
126
|
+
if (import.meta.hot) {
|
|
127
|
+
handleHotUpdate(router, (newRoutes) => {
|
|
128
|
+
// vue-router has just added the raw generated routes, replace them with resolved ones
|
|
129
|
+
router.clearRoutes()
|
|
130
|
+
for (const route of resolveRoutes(newRoutes))
|
|
131
|
+
router.addRoute(route)
|
|
132
|
+
})
|
|
133
|
+
}
|
|
134
|
+
|
|
120
135
|
const head = createHead ? createHead() : undefined
|
|
121
136
|
|
|
122
137
|
app.use(router)
|
package/client/modules/valaxy.ts
CHANGED
|
@@ -12,10 +12,10 @@ import type { ComputedRef } from 'vue'
|
|
|
12
12
|
import type { Router } from 'vue-router'
|
|
13
13
|
import type { PageDataPayload } from '../../types'
|
|
14
14
|
import type { ValaxySSGContext } from '../setups'
|
|
15
|
-
import { ensureSuffix } from '@antfu/utils'
|
|
16
15
|
import { useStorage } from '@vueuse/core'
|
|
17
16
|
import { nextTick, watch } from 'vue'
|
|
18
17
|
import { createI18n } from 'vue-i18n'
|
|
18
|
+
import { isSamePagePath } from '../utils/path'
|
|
19
19
|
|
|
20
20
|
// @ts-expect-error virtual
|
|
21
21
|
import valaxyMessages from '/@valaxyjs/locales'
|
|
@@ -37,10 +37,7 @@ import valaxyMessages from '/@valaxyjs/locales'
|
|
|
37
37
|
// import en from '../../../../../demo/yun/locales/en.yml'
|
|
38
38
|
|
|
39
39
|
function shouldHotReload(payload: PageDataPayload): boolean {
|
|
40
|
-
|
|
41
|
-
const locationPath = location.pathname
|
|
42
|
-
.replace(/(?:(^|\/)index)?\.html$/, '')
|
|
43
|
-
return ensureSuffix('/', encodeURI(payloadPath)) === ensureSuffix('/', encodeURI(locationPath))
|
|
40
|
+
return isSamePagePath(payload.path, location.pathname, import.meta.env.BASE_URL)
|
|
44
41
|
}
|
|
45
42
|
|
|
46
43
|
// init i18n, by valaxy config
|
|
@@ -89,13 +86,25 @@ export async function install({ app, router }: ValaxySSGContext, config: Compute
|
|
|
89
86
|
function handleHMR(router: Router): void {
|
|
90
87
|
// update route.data on HMR updates of active page
|
|
91
88
|
if (import.meta.hot) {
|
|
89
|
+
let lastPayload: PageDataPayload | undefined
|
|
90
|
+
|
|
92
91
|
import.meta.hot.on('valaxy:pageData', (payload: PageDataPayload) => {
|
|
93
92
|
if (shouldHotReload(payload)) {
|
|
93
|
+
lastPayload = payload
|
|
94
94
|
window.$pageData = payload.pageData
|
|
95
95
|
|
|
96
96
|
// console.log(payload.pageData.headers)
|
|
97
97
|
Object.assign(router.currentRoute.value.meta, payload.pageData)
|
|
98
98
|
}
|
|
99
99
|
})
|
|
100
|
+
|
|
101
|
+
// The routes HMR in `main.ts` re-navigates to the current page with a fresh
|
|
102
|
+
// `meta`, which may happen after the payload above was applied.
|
|
103
|
+
router.afterEach((to) => {
|
|
104
|
+
if (lastPayload && shouldHotReload(lastPayload))
|
|
105
|
+
Object.assign(to.meta, lastPayload.pageData)
|
|
106
|
+
else
|
|
107
|
+
lastPayload = undefined
|
|
108
|
+
})
|
|
100
109
|
}
|
|
101
110
|
}
|
package/client/shims.d.ts
CHANGED
|
@@ -1,4 +1,31 @@
|
|
|
1
1
|
// vite hmr data
|
|
2
|
+
declare module 'virtual:valaxy-cover-components' {
|
|
3
|
+
import type { Component } from 'vue'
|
|
4
|
+
|
|
5
|
+
const components: ReadonlyMap<string, Component>
|
|
6
|
+
export default components
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// Generated by `node/plugins/layouts.ts`, runtime in `client/utils/layouts.ts`
|
|
10
|
+
declare module 'virtual:generated-layouts' {
|
|
11
|
+
import type { Component, ComputedRef } from 'vue'
|
|
12
|
+
import type { Router, RouteRecordNormalized, RouteRecordRaw } from 'vue-router'
|
|
13
|
+
|
|
14
|
+
type LayoutName = string | false
|
|
15
|
+
type LayoutProps = Record<string, unknown>
|
|
16
|
+
|
|
17
|
+
/** Layout components by normalized name; lazy entries are loader functions. */
|
|
18
|
+
export const layouts: Record<string, Component | (() => Promise<{ default: Component } | Component>)>
|
|
19
|
+
export function createGetRoutes(router: Router, withLayout: true): RouteRecordNormalized[]
|
|
20
|
+
export function createGetRoutes(router: Router, withLayout?: false): () => RouteRecordNormalized[]
|
|
21
|
+
/** Wrap routes in the layout resolved from `meta.layout` (or the default layout). */
|
|
22
|
+
export function setupLayouts(routes: readonly RouteRecordRaw[]): RouteRecordRaw[]
|
|
23
|
+
/** Change the current page's layout until the router navigates to another path. */
|
|
24
|
+
export function setPageLayout(name: LayoutName | null | undefined, props?: LayoutProps): void
|
|
25
|
+
/** Reactive name of the current route's layout, `false` when it renders without one. */
|
|
26
|
+
export function useLayout(): ComputedRef<LayoutName>
|
|
27
|
+
}
|
|
28
|
+
|
|
2
29
|
declare module '/@valaxyjs/config' {
|
|
3
30
|
const config: string
|
|
4
31
|
export default config
|
package/client/utils/index.ts
CHANGED
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime for `virtual:generated-layouts` (generated by `node/plugins/layouts.ts`).
|
|
3
|
+
*
|
|
4
|
+
* Ported from vite-plugin-vue-layouts-next@3.2.0 (MIT, https://github.com/loicduong/vite-plugin-vue-layouts-next)
|
|
5
|
+
* with unchanged behavior: `meta.layout` (name, `false` or `{ name, props }`), `meta.layoutProps`,
|
|
6
|
+
* the default/fallback layout, lazy-layout preloading, `setPageLayout` and `useLayout`.
|
|
7
|
+
*/
|
|
8
|
+
import type { Component, ComputedRef } from 'vue'
|
|
9
|
+
import type { RouteLocationNormalized, RouteLocationNormalizedLoaded, Router, RouteRecordNormalized, RouteRecordRaw } from 'vue-router'
|
|
10
|
+
import { computed, defineAsyncComponent, defineComponent, h, hasInjectionContext, inject, shallowRef } from 'vue'
|
|
11
|
+
import { matchedRouteKey, routerKey, RouterView, START_LOCATION, useRoute, useRouter } from 'vue-router'
|
|
12
|
+
|
|
13
|
+
export type LayoutName = string | false
|
|
14
|
+
export type LayoutProps = Record<string, unknown>
|
|
15
|
+
|
|
16
|
+
/** Object form of `meta.layout`. */
|
|
17
|
+
export interface LayoutOptions {
|
|
18
|
+
/** Layout name, `false` for no layout, or omitted for the default layout. */
|
|
19
|
+
name?: LayoutName
|
|
20
|
+
props?: LayoutProps
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type LayoutMeta = LayoutName | LayoutOptions
|
|
24
|
+
|
|
25
|
+
const LAZY = Symbol.for('valaxy:layouts:lazy')
|
|
26
|
+
|
|
27
|
+
export type LazyLayout = (() => Promise<{ default: Component } | Component>) & { [LAZY]: true }
|
|
28
|
+
export type LayoutMap = Record<string, Component | LazyLayout>
|
|
29
|
+
|
|
30
|
+
interface ResolvedLayout {
|
|
31
|
+
name: LayoutName
|
|
32
|
+
props?: LayoutProps
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
type Meta = Record<string, any> | undefined
|
|
36
|
+
|
|
37
|
+
function readLayoutMeta(meta: Meta): { name: LayoutName | undefined, props?: LayoutProps } {
|
|
38
|
+
const layout = meta?.layout
|
|
39
|
+
if (layout !== null && typeof layout === 'object') {
|
|
40
|
+
return {
|
|
41
|
+
name: layout.name,
|
|
42
|
+
props: layout.props ?? meta?.layoutProps,
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
name: layout,
|
|
47
|
+
props: meta?.layoutProps,
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function declaresLayout(meta: Meta) {
|
|
52
|
+
const layout = meta?.layout
|
|
53
|
+
if (layout !== null && typeof layout === 'object')
|
|
54
|
+
return layout.name !== false
|
|
55
|
+
return !!layout
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Without `withLayout`, the returned getter hides the generated layout routes. */
|
|
59
|
+
export function createGetRoutes(router: Router, withLayout: true): RouteRecordNormalized[]
|
|
60
|
+
export function createGetRoutes(router: Router, withLayout?: false): () => RouteRecordNormalized[]
|
|
61
|
+
export function createGetRoutes(router: Router, withLayout = false) {
|
|
62
|
+
const routes = router.getRoutes()
|
|
63
|
+
if (withLayout)
|
|
64
|
+
return routes
|
|
65
|
+
return () => routes.filter(route => !route.meta.isLayout)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function hasChildWithLayout(route: RouteRecordRaw): boolean {
|
|
69
|
+
if (!route.children || route.children.length === 0)
|
|
70
|
+
return false
|
|
71
|
+
return route.children.some((child) => {
|
|
72
|
+
if (declaresLayout(child.meta))
|
|
73
|
+
return true
|
|
74
|
+
if (child.meta?.isLayout)
|
|
75
|
+
return true
|
|
76
|
+
return hasChildWithLayout(child)
|
|
77
|
+
})
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface SetupLayoutsOptions {
|
|
81
|
+
inheritDefaultLayout: boolean
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function createSetupLayouts(wrapper: Component, options: SetupLayoutsOptions) {
|
|
85
|
+
const { inheritDefaultLayout } = options
|
|
86
|
+
|
|
87
|
+
function wrap(route: RouteRecordRaw, keepRootPath: boolean): RouteRecordRaw {
|
|
88
|
+
return {
|
|
89
|
+
path: route.path,
|
|
90
|
+
component: wrapper,
|
|
91
|
+
children: keepRootPath && route.path === '/' ? [route] : [{ ...route, path: '' } as RouteRecordRaw],
|
|
92
|
+
meta: { isLayout: true },
|
|
93
|
+
} as RouteRecordRaw
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function deepSetupLayout(routes: RouteRecordRaw[], top = true): RouteRecordRaw[] {
|
|
97
|
+
return routes.map((route) => {
|
|
98
|
+
const childHasLayout = top && !inheritDefaultLayout && (route.children?.length ?? 0) > 0
|
|
99
|
+
? hasChildWithLayout(route)
|
|
100
|
+
: false
|
|
101
|
+
if (route.children && route.children.length > 0)
|
|
102
|
+
route.children = deepSetupLayout(route.children, false)
|
|
103
|
+
if (top) {
|
|
104
|
+
if (!route.component && route.children?.find(r => (r.path === '' || r.path === '/') && r.meta?.isLayout))
|
|
105
|
+
return route
|
|
106
|
+
if (readLayoutMeta(route.meta).name !== false) {
|
|
107
|
+
if (inheritDefaultLayout || !childHasLayout)
|
|
108
|
+
return wrap(route, true)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (declaresLayout(route.meta))
|
|
112
|
+
return wrap(route, false)
|
|
113
|
+
return route
|
|
114
|
+
})
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return (routes: readonly RouteRecordRaw[]) => deepSetupLayout(routes as RouteRecordRaw[])
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* The virtual module wraps its async imports with this, so a lazy entry is always
|
|
122
|
+
* identified explicitly rather than guessed from its shape - unlike Vue Router's route
|
|
123
|
+
* components, a bare functional component is a perfectly valid, non-lazy layout here.
|
|
124
|
+
*/
|
|
125
|
+
export function lazyLayout(loader: () => Promise<{ default: Component } | Component>): LazyLayout {
|
|
126
|
+
return Object.assign(loader, { [LAZY]: true as const })
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function isLazyLayout(entry: unknown): entry is LazyLayout {
|
|
130
|
+
return typeof entry === 'function' && (entry as any)[LAZY] === true
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const PREFIX = '[valaxy:layouts]'
|
|
134
|
+
|
|
135
|
+
/** In-place override set by `setPageLayout`; cleared on navigation to another path. */
|
|
136
|
+
const override = shallowRef<ResolvedLayout | null>(null)
|
|
137
|
+
const guardedRouters = new WeakSet<Router>()
|
|
138
|
+
/** `defaultLayout` of the last created wrapper; shared with `useLayout()`. */
|
|
139
|
+
let resolvedDefaultLayout = 'default'
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Change the layout of the current page without navigating, optionally passing props
|
|
143
|
+
* to the layout component. The override lasts until the router navigates to a different `path`.
|
|
144
|
+
*/
|
|
145
|
+
export function setPageLayout(name: LayoutName | null | undefined, props?: LayoutProps) {
|
|
146
|
+
override.value = name == null ? null : { name, props }
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Reactive name of the layout resolved for the current route.
|
|
151
|
+
* `false` when no wrapper is rendered around it (a static `layout: false` route, or
|
|
152
|
+
* one deliberately left unwrapped because `inheritDefaultLayout` is `false`).
|
|
153
|
+
*/
|
|
154
|
+
export function useLayout(): ComputedRef<LayoutName> {
|
|
155
|
+
const route = useRoute()
|
|
156
|
+
return computed(() => {
|
|
157
|
+
const own = innermostLayoutRecord(route)
|
|
158
|
+
if (!own)
|
|
159
|
+
return false
|
|
160
|
+
return resolveLayoutFor(route, own, override.value).name
|
|
161
|
+
})
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function innermostLayoutRecord(route: RouteLocationNormalizedLoaded) {
|
|
165
|
+
const { matched } = route
|
|
166
|
+
for (let i = matched.length - 1; i >= 0; i--) {
|
|
167
|
+
if (matched[i].meta.isLayout)
|
|
168
|
+
return matched[i]
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Layout (name and props) the wrapper whose generated record is `own` renders for `route`.
|
|
174
|
+
*
|
|
175
|
+
* The static layout comes from the wrapped record's own meta (`||` fallback to the
|
|
176
|
+
* default), so nested trees keep one layout per level. Dynamic inputs - the
|
|
177
|
+
* `setPageLayout` override and a guard assignment to the merged `route.meta.layout` /
|
|
178
|
+
* `route.meta.layoutProps` - apply only to the innermost wrapper. A dynamic layout
|
|
179
|
+
* brings its own props and never inherits the page's static ones.
|
|
180
|
+
*/
|
|
181
|
+
function resolveLayoutFor(route: RouteLocationNormalized, own: RouteRecordNormalized, overrideValue: ResolvedLayout | null): ResolvedLayout {
|
|
182
|
+
const { matched } = route
|
|
183
|
+
const idx = matched.indexOf(own)
|
|
184
|
+
const page = readLayoutMeta(matched[idx + 1]?.meta)
|
|
185
|
+
const staticLayout: ResolvedLayout = {
|
|
186
|
+
name: page.name || resolvedDefaultLayout,
|
|
187
|
+
props: page.props,
|
|
188
|
+
}
|
|
189
|
+
if (matched.slice(idx + 2).some(r => r.meta.isLayout))
|
|
190
|
+
return staticLayout
|
|
191
|
+
if (overrideValue)
|
|
192
|
+
return overrideValue
|
|
193
|
+
|
|
194
|
+
const meta = route.meta as Record<string, any>
|
|
195
|
+
const staticMerged = matched.reduce<unknown>((m, r) => (r.meta as Meta)?.layout ?? m, undefined)
|
|
196
|
+
const staticMergedProps = matched.reduce<unknown>((m, r) => (r.meta as Meta)?.layoutProps ?? m, undefined)
|
|
197
|
+
const guardProps = meta.layoutProps !== staticMergedProps ? meta.layoutProps : undefined
|
|
198
|
+
if (meta.layout != null && meta.layout !== staticMerged) {
|
|
199
|
+
const guard = readLayoutMeta({ layout: meta.layout, layoutProps: guardProps })
|
|
200
|
+
return {
|
|
201
|
+
name: guard.name ?? resolvedDefaultLayout,
|
|
202
|
+
props: guard.props,
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return {
|
|
206
|
+
name: staticLayout.name,
|
|
207
|
+
props: guardProps ?? staticLayout.props,
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function unwrapModule(mod: any) {
|
|
212
|
+
return mod && typeof mod === 'object' && 'default' in mod ? mod.default : mod
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** Options-API navigation guards that never run on a layout component (see `checkRouteGuards`). */
|
|
216
|
+
const ROUTE_GUARD_NAMES = ['beforeRouteEnter', 'beforeRouteUpdate', 'beforeRouteLeave'] as const
|
|
217
|
+
|
|
218
|
+
/** `fallbackLayout` is rendered when the requested layout is not in `layouts`; if it is missing too, the page renders without a layout. */
|
|
219
|
+
export function createLayoutWrapper(layouts: LayoutMap, defaultLayout: string, fallbackLayout = defaultLayout): Component {
|
|
220
|
+
resolvedDefaultLayout = defaultLayout
|
|
221
|
+
/** Lazy layouts already loaded (by the `beforeResolve` preload or an async render). */
|
|
222
|
+
const resolved = new Map<string, Component>()
|
|
223
|
+
/** In-flight loads, so a layout's factory runs at most once. */
|
|
224
|
+
const pending = new Map<string, Promise<Component>>()
|
|
225
|
+
/** `defineAsyncComponent` per name, for renders that happen before the preload. */
|
|
226
|
+
const asyncCache = new Map<string, Component>()
|
|
227
|
+
const warned = new Set<string>()
|
|
228
|
+
let fallbackWarned = false
|
|
229
|
+
const guardWarned = new Set<string>()
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Layouts are rendered by `LayoutWrapper`, not matched as route components, so Vue
|
|
233
|
+
* Router never calls their Options-API route guards; the Composition-API
|
|
234
|
+
* `onBeforeRouteUpdate`/`onBeforeRouteLeave` still work since they subscribe directly
|
|
235
|
+
* to the router. Skipped for a `defineAsyncComponent` wrapper that hasn't resolved
|
|
236
|
+
* yet - the caller passes the real component once loaded.
|
|
237
|
+
*/
|
|
238
|
+
function checkRouteGuards(name: string, comp: any) {
|
|
239
|
+
if (guardWarned.has(name) || !comp || (typeof comp !== 'object' && typeof comp !== 'function'))
|
|
240
|
+
return
|
|
241
|
+
const target = 'default' in comp ? comp.default : comp
|
|
242
|
+
if (!target)
|
|
243
|
+
return
|
|
244
|
+
const opts = target.__vccOpts ?? target
|
|
245
|
+
const guards = ROUTE_GUARD_NAMES.filter(g => opts?.[g])
|
|
246
|
+
if (guards.length > 0) {
|
|
247
|
+
guardWarned.add(name)
|
|
248
|
+
console.warn(`${PREFIX} Layout "${name}" declares ${guards.join(', ')}; layouts are not route components, so these guards will not run. Use onBeforeRouteUpdate/onBeforeRouteLeave or a router guard instead.`)
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function load(name: string, loader: LazyLayout) {
|
|
253
|
+
let promise = pending.get(name)
|
|
254
|
+
if (!promise) {
|
|
255
|
+
promise = Promise.resolve(loader()).then((mod) => {
|
|
256
|
+
const component = unwrapModule(mod)
|
|
257
|
+
resolved.set(name, component)
|
|
258
|
+
return component
|
|
259
|
+
}, (error) => {
|
|
260
|
+
pending.delete(name)
|
|
261
|
+
throw error
|
|
262
|
+
})
|
|
263
|
+
pending.set(name, promise)
|
|
264
|
+
}
|
|
265
|
+
return promise
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function resolveComponent(name: string): Component | undefined {
|
|
269
|
+
const loaded = resolved.get(name)
|
|
270
|
+
if (loaded) {
|
|
271
|
+
checkRouteGuards(name, loaded)
|
|
272
|
+
return loaded
|
|
273
|
+
}
|
|
274
|
+
const entry = layouts[name]
|
|
275
|
+
if (!entry)
|
|
276
|
+
return undefined
|
|
277
|
+
if (isLazyLayout(entry)) {
|
|
278
|
+
let created = asyncCache.get(name)
|
|
279
|
+
if (!created) {
|
|
280
|
+
created = defineAsyncComponent(() => load(name, entry))
|
|
281
|
+
asyncCache.set(name, created)
|
|
282
|
+
}
|
|
283
|
+
return created
|
|
284
|
+
}
|
|
285
|
+
checkRouteGuards(name, entry)
|
|
286
|
+
return entry
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function resolveLayout(name: string) {
|
|
290
|
+
const found = resolveComponent(name)
|
|
291
|
+
if (found)
|
|
292
|
+
return found
|
|
293
|
+
if (!warned.has(name)) {
|
|
294
|
+
warned.add(name)
|
|
295
|
+
console.warn(`${PREFIX} Layout "${name}" not found, falling back to "${fallbackLayout}"`)
|
|
296
|
+
}
|
|
297
|
+
const fallback = resolveComponent(fallbackLayout)
|
|
298
|
+
if (!fallback && !fallbackWarned) {
|
|
299
|
+
fallbackWarned = true
|
|
300
|
+
console.warn(`${PREFIX} Fallback layout "${fallbackLayout}" not found, rendering without a layout`)
|
|
301
|
+
}
|
|
302
|
+
return fallback
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const preload = async (to: RouteLocationNormalized, from: RouteLocationNormalized) => {
|
|
306
|
+
const keepOverride = from === START_LOCATION || to.path === from.path
|
|
307
|
+
for (const rec of to.matched) {
|
|
308
|
+
if (!rec.meta.isLayout)
|
|
309
|
+
continue
|
|
310
|
+
const { name } = resolveLayoutFor(to, rec, keepOverride ? override.value : null)
|
|
311
|
+
if (name === false)
|
|
312
|
+
continue
|
|
313
|
+
const key = layouts[name] ? name : fallbackLayout
|
|
314
|
+
const entry = layouts[key]
|
|
315
|
+
if (isLazyLayout(entry) && !resolved.has(key))
|
|
316
|
+
await load(key, entry)
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function installGuards(router: Router) {
|
|
321
|
+
if (guardedRouters.has(router))
|
|
322
|
+
return
|
|
323
|
+
guardedRouters.add(router)
|
|
324
|
+
router.beforeResolve(preload)
|
|
325
|
+
router.afterEach((to, from, failure) => {
|
|
326
|
+
if (failure)
|
|
327
|
+
return
|
|
328
|
+
if (from !== START_LOCATION && to.path !== from.path)
|
|
329
|
+
override.value = null
|
|
330
|
+
})
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
return defineComponent({
|
|
334
|
+
name: 'LayoutWrapper',
|
|
335
|
+
beforeRouteEnter(to, from) {
|
|
336
|
+
if (hasInjectionContext()) {
|
|
337
|
+
const router = inject(routerKey, null)
|
|
338
|
+
if (router)
|
|
339
|
+
installGuards(router)
|
|
340
|
+
}
|
|
341
|
+
return preload(to, from)
|
|
342
|
+
},
|
|
343
|
+
setup() {
|
|
344
|
+
const route = useRoute()
|
|
345
|
+
const own = inject(matchedRouteKey)!
|
|
346
|
+
installGuards(useRouter())
|
|
347
|
+
const layout = computed(() => resolveLayoutFor(route, own.value!, override.value))
|
|
348
|
+
return () => {
|
|
349
|
+
const { name, props } = layout.value
|
|
350
|
+
if (name === false)
|
|
351
|
+
return h(RouterView)
|
|
352
|
+
const LayoutComponent = resolveLayout(name)
|
|
353
|
+
return LayoutComponent
|
|
354
|
+
? h(LayoutComponent, props ? { ...props } : null, { default: () => h(RouterView) })
|
|
355
|
+
: h(RouterView)
|
|
356
|
+
}
|
|
357
|
+
},
|
|
358
|
+
})
|
|
359
|
+
}
|
package/client/utils/path.ts
CHANGED
|
@@ -7,6 +7,36 @@ function joinPath(base: string, path: string): string {
|
|
|
7
7
|
return `${base}${path}`.replace(/\/+/g, '/')
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
+
/**
|
|
11
|
+
* Decode a percent-encoded URL path (`/posts/%E4%B8%AD` → `/posts/中`).
|
|
12
|
+
*
|
|
13
|
+
* Route records and `route.path` are percent-encoded, while file names, config
|
|
14
|
+
* links and collection keys are written raw. Decode before comparing them.
|
|
15
|
+
* Malformed sequences return the path unchanged.
|
|
16
|
+
*/
|
|
17
|
+
export function decodePath(path: string): string {
|
|
18
|
+
try {
|
|
19
|
+
return decodeURI(path)
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return path
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Check whether a page file path (`/posts/中文.md`, `/guide/index.md`) is the page
|
|
28
|
+
* shown at a browser `pathname` (`/base/posts/%E4%B8%AD%E6%96%87`).
|
|
29
|
+
*/
|
|
30
|
+
export function isSamePagePath(filePath: string, pathname: string, base = '/'): boolean {
|
|
31
|
+
const pagePath = filePath.replace(/(?:(^|\/)index)?\.md$/, '$1')
|
|
32
|
+
let locationPath = decodePath(pathname).replace(/(?:(^|\/)index)?\.html$/, '$1')
|
|
33
|
+
if (base !== '/' && locationPath.startsWith(base))
|
|
34
|
+
locationPath = locationPath.slice(base.length - 1)
|
|
35
|
+
|
|
36
|
+
const withSlash = (path: string) => path.endsWith('/') ? path : `${path}/`
|
|
37
|
+
return withSlash(pagePath) === withSlash(locationPath)
|
|
38
|
+
}
|
|
39
|
+
|
|
10
40
|
/**
|
|
11
41
|
* Append Vite's resolved base to an internal root-absolute URL.
|
|
12
42
|
* External and relative URLs are returned unchanged.
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
export interface SvgToPngOptions {
|
|
2
|
+
/** Output pixels. Defaults to a 1200 × 630 social preview. */
|
|
3
|
+
width?: number
|
|
4
|
+
height?: number
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
// Freeze SVG presentation and the current CSS animation frame without copying
|
|
8
|
+
// page layout (width, height, position) into the standalone image.
|
|
9
|
+
const presentationProperties = [
|
|
10
|
+
'color',
|
|
11
|
+
'display',
|
|
12
|
+
'visibility',
|
|
13
|
+
'opacity',
|
|
14
|
+
'fill',
|
|
15
|
+
'fill-opacity',
|
|
16
|
+
'fill-rule',
|
|
17
|
+
'stroke',
|
|
18
|
+
'stroke-width',
|
|
19
|
+
'stroke-opacity',
|
|
20
|
+
'stroke-linecap',
|
|
21
|
+
'stroke-linejoin',
|
|
22
|
+
'stroke-dasharray',
|
|
23
|
+
'stroke-dashoffset',
|
|
24
|
+
'stroke-miterlimit',
|
|
25
|
+
'stop-color',
|
|
26
|
+
'stop-opacity',
|
|
27
|
+
'flood-color',
|
|
28
|
+
'flood-opacity',
|
|
29
|
+
'font-family',
|
|
30
|
+
'font-size',
|
|
31
|
+
'font-style',
|
|
32
|
+
'font-weight',
|
|
33
|
+
'letter-spacing',
|
|
34
|
+
'text-anchor',
|
|
35
|
+
'dominant-baseline',
|
|
36
|
+
'paint-order',
|
|
37
|
+
'vector-effect',
|
|
38
|
+
'transform',
|
|
39
|
+
'transform-origin',
|
|
40
|
+
'transform-box',
|
|
41
|
+
'clip-path',
|
|
42
|
+
'mask',
|
|
43
|
+
'filter',
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
/** Export a mounted, self-contained SVG's current frame using the browser canvas. */
|
|
47
|
+
export async function svgToPng(svg: SVGSVGElement, options: SvgToPngOptions = {}): Promise<Blob> {
|
|
48
|
+
const { width = 1200, height = 630 } = options
|
|
49
|
+
if (![width, height].every(value => Number.isInteger(value) && value > 0 && value <= 8192))
|
|
50
|
+
throw new Error('SVG export dimensions must be integers between 1 and 8192.')
|
|
51
|
+
if (!svg.isConnected)
|
|
52
|
+
throw new Error('Mount the SVG before exporting so its styles can be resolved.')
|
|
53
|
+
if (svg.querySelector('foreignObject, script, animate, animateMotion, animateTransform, set'))
|
|
54
|
+
throw new Error('SVG export supports SVG graphics and CSS animation, without HTML, scripts or SMIL animation.')
|
|
55
|
+
for (const element of svg.querySelectorAll('[href], [xlink\\:href]')) {
|
|
56
|
+
const href = element.getAttribute('href') || element.getAttributeNS('http://www.w3.org/1999/xlink', 'href') || ''
|
|
57
|
+
if (href && !href.startsWith('#') && !href.startsWith('data:image/'))
|
|
58
|
+
throw new Error('Embed SVG image resources as data URLs before exporting.')
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
await svg.ownerDocument.fonts?.ready
|
|
62
|
+
const clone = svg.cloneNode(true) as SVGSVGElement
|
|
63
|
+
const sources = [svg, ...svg.querySelectorAll<SVGElement>('*')]
|
|
64
|
+
const targets = [clone, ...clone.querySelectorAll<SVGElement>('*')]
|
|
65
|
+
sources.forEach((source, index) => {
|
|
66
|
+
const target = targets[index]
|
|
67
|
+
const style = getComputedStyle(source)
|
|
68
|
+
for (const property of presentationProperties) {
|
|
69
|
+
// Browsers may resolve url(#gradient) against the page. Keep local SVG
|
|
70
|
+
// definitions local after moving the snapshot to a blob URL.
|
|
71
|
+
const value = style.getPropertyValue(property).replace(/url\(["']?[^#)"']*#([^#)"']+)["']?\)/g, 'url(#$1)')
|
|
72
|
+
if (value)
|
|
73
|
+
target.style.setProperty(property, value)
|
|
74
|
+
}
|
|
75
|
+
target.style.setProperty('animation', 'none')
|
|
76
|
+
target.style.setProperty('transition', 'none')
|
|
77
|
+
})
|
|
78
|
+
clone.setAttribute('xmlns', 'http://www.w3.org/2000/svg')
|
|
79
|
+
clone.setAttribute('width', String(width))
|
|
80
|
+
clone.setAttribute('height', String(height))
|
|
81
|
+
clone.style.width = `${width}px`
|
|
82
|
+
clone.style.height = `${height}px`
|
|
83
|
+
|
|
84
|
+
const source = new Blob([new XMLSerializer().serializeToString(clone)], { type: 'image/svg+xml;charset=utf-8' })
|
|
85
|
+
const url = URL.createObjectURL(source)
|
|
86
|
+
try {
|
|
87
|
+
const image = new Image()
|
|
88
|
+
image.src = url
|
|
89
|
+
await image.decode()
|
|
90
|
+
const canvas = document.createElement('canvas')
|
|
91
|
+
canvas.width = width
|
|
92
|
+
canvas.height = height
|
|
93
|
+
const context = canvas.getContext('2d')
|
|
94
|
+
if (!context)
|
|
95
|
+
throw new Error('This browser does not support canvas image export.')
|
|
96
|
+
context.drawImage(image, 0, 0, width, height)
|
|
97
|
+
return await new Promise<Blob>((resolve, reject) => {
|
|
98
|
+
canvas.toBlob(blob => blob ? resolve(blob) : reject(new Error('Could not encode the SVG as PNG.')), 'image/png')
|
|
99
|
+
})
|
|
100
|
+
}
|
|
101
|
+
finally {
|
|
102
|
+
URL.revokeObjectURL(url)
|
|
103
|
+
}
|
|
104
|
+
}
|