kawapress 0.0.1 → 0.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/README.md +17 -5
- package/README.zh-CN.md +17 -5
- package/package.json +7 -3
- package/src/api/config.ts +12 -16
- package/src/api/plugin.ts +20 -3
- package/src/cli.ts +28 -2
- package/src/client/app.ts +36 -6
- package/src/client/components/KawaOutlet.vue +36 -0
- package/src/client/components/Root.vue +7 -0
- package/src/client/composables.ts +11 -10
- package/src/client/entries/entry-client.ts +8 -3
- package/src/client/entries/entry-server.ts +36 -6
- package/src/client/index.ts +8 -2
- package/src/client/outlet.ts +85 -0
- package/src/client/page-data.ts +23 -16
- package/src/client/page-table.ts +138 -0
- package/src/client/router/RouterLink.ts +34 -0
- package/src/client/router/RouterView.ts +25 -0
- package/src/client/router/app-router.ts +84 -0
- package/src/client/router/history.ts +53 -0
- package/src/client/router/injection.ts +29 -0
- package/src/client/router/link-interceptor.ts +67 -0
- package/src/client/router/reload.ts +34 -0
- package/src/client/router/route.ts +82 -0
- package/src/client/router/router.ts +160 -0
- package/src/client/testing/virtual-modules.ts +19 -0
- package/src/client/types/global-components.d.ts +1 -0
- package/src/client/types/virtual-modules.d.ts +3 -5
- package/src/compiler/frontmatter-compat.ts +12 -0
- package/src/compiler/link-registry.ts +29 -0
- package/src/compiler/markdown.ts +223 -21
- package/src/compiler/page-components.ts +144 -0
- package/src/compiler/page-loader.ts +15 -2
- package/src/compiler/page-summary.ts +96 -0
- package/src/core/dead-links.ts +234 -0
- package/src/core/extension-points.ts +64 -0
- package/src/core/head.ts +207 -0
- package/src/core/heading-title.ts +78 -0
- package/src/core/hydration-plan.ts +79 -0
- package/src/core/locale.ts +28 -18
- package/src/core/not-found.ts +22 -0
- package/src/core/outlet.ts +110 -0
- package/src/core/page-chunk.ts +50 -0
- package/src/core/page-manifest.ts +53 -0
- package/src/core/router.ts +46 -0
- package/src/core/site.ts +19 -19
- package/src/core/tokens.ts +59 -0
- package/src/generator/config.ts +93 -13
- package/src/generator/data/context.ts +1 -0
- package/src/generator/git.ts +79 -0
- package/src/generator/pages.ts +126 -0
- package/src/generator/runtime-dependencies.ts +280 -0
- package/src/generator/vite/app-chunk.ts +65 -0
- package/src/generator/vite/config.ts +98 -10
- package/src/generator/vite/page-inputs.ts +22 -0
- package/src/generator/vite/plugins/dev-dead-links.ts +72 -0
- package/src/generator/vite/plugins/pages.ts +162 -114
- package/src/generator.ts +13 -0
- package/src/index.ts +30 -2
- package/src/plugin/execution.ts +17 -13
- package/src/plugin/generator-runner.ts +8 -3
- package/src/plugin/outlet-collector.ts +69 -0
- package/src/plugin/runtime-runner.ts +7 -1
- package/src/server/build-node-env.ts +48 -0
- package/src/server/build.ts +50 -4
- package/src/server/check-dead-links.ts +88 -0
- package/src/server/page-assets.ts +138 -0
- package/src/server/prerender.ts +134 -27
- package/src/server/preview-routing.ts +64 -0
- package/src/server/preview.ts +78 -11
- package/src/server/request-handler.ts +36 -12
- package/src/server/template.ts +31 -0
- package/src/client/router.ts +0 -81
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import type { Component, ShallowRef } from 'vue'
|
|
2
|
+
import type { PageTable } from '../../core/page-manifest'
|
|
3
|
+
import type { Route, RouteGuard, RouteHook, Router } from '../../core/router'
|
|
4
|
+
import type { RouteHistory } from './history'
|
|
5
|
+
import { shallowRef } from 'vue'
|
|
6
|
+
import { loadPage } from '../page-data'
|
|
7
|
+
import { resolveRoute } from './route'
|
|
8
|
+
|
|
9
|
+
/** How a committed navigation reaches the browser history. */
|
|
10
|
+
type HistoryWrite = 'push' | 'replace' | 'none'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The router object a KawaPress app provides. `page` is internal: `RouterView`
|
|
14
|
+
* renders it, and it is the only piece of state that is not part of the route.
|
|
15
|
+
*/
|
|
16
|
+
export interface AppRouterInstance extends Router {
|
|
17
|
+
/** Component of the current route; `null` for the not-found page. */
|
|
18
|
+
readonly page: Readonly<ShallowRef<Component | null>>
|
|
19
|
+
/**
|
|
20
|
+
* Resolves `location`, loads its page and commits it without writing a
|
|
21
|
+
* history entry. Used for the route the served HTML was rendered with and for
|
|
22
|
+
* the URL adopted after hydration. A page that cannot be loaded is an error:
|
|
23
|
+
* the first render has to match the served HTML, so there is nothing to fall
|
|
24
|
+
* back to.
|
|
25
|
+
*/
|
|
26
|
+
enter: (location: string) => Promise<void>
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface CreateRouterOptions {
|
|
30
|
+
pages: PageTable
|
|
31
|
+
history: RouteHistory
|
|
32
|
+
/**
|
|
33
|
+
* Called when the page module of a navigation cannot be loaded, typically a
|
|
34
|
+
* stale page table after a redeploy. The navigation is then abandoned and the
|
|
35
|
+
* current page stays. Without this hook the error is rethrown, which is what a
|
|
36
|
+
* server render wants.
|
|
37
|
+
*/
|
|
38
|
+
onPageLoadError?: (error: unknown, to: Route) => void
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Create the router of one app.
|
|
43
|
+
*
|
|
44
|
+
* A documentation site only looks a path up in the page table, so a navigation
|
|
45
|
+
* is: run the guards, load the page module, commit the route, write the history,
|
|
46
|
+
* run the post hooks. Back and forward arrive through the history listener and
|
|
47
|
+
* never write a history entry themselves.
|
|
48
|
+
*/
|
|
49
|
+
export function createRouter({
|
|
50
|
+
pages,
|
|
51
|
+
history,
|
|
52
|
+
onPageLoadError,
|
|
53
|
+
}: CreateRouterOptions): AppRouterInstance {
|
|
54
|
+
const currentRoute = shallowRef<Route>(resolveRoute(history.location(), pages))
|
|
55
|
+
const page = shallowRef<Component | null>(null)
|
|
56
|
+
const guards: RouteGuard[] = []
|
|
57
|
+
const hooks: RouteHook[] = []
|
|
58
|
+
/** Counter that makes the latest navigation win over slower earlier ones. */
|
|
59
|
+
let revision = 0
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* `recover` decides what a page module that cannot be loaded means: a reader
|
|
63
|
+
* who is already on a page gets a document reload (`onPageLoadError`), the
|
|
64
|
+
* first render of a document throws.
|
|
65
|
+
*/
|
|
66
|
+
async function navigate(
|
|
67
|
+
location: string,
|
|
68
|
+
write: HistoryWrite,
|
|
69
|
+
recover: 'reload' | 'throw',
|
|
70
|
+
): Promise<void> {
|
|
71
|
+
const from = currentRoute.value
|
|
72
|
+
const to = resolveRoute(location, pages)
|
|
73
|
+
const started = ++revision
|
|
74
|
+
|
|
75
|
+
if (!await runGuards(to, from, started)) {
|
|
76
|
+
return
|
|
77
|
+
}
|
|
78
|
+
// A guard may be asynchronous; a newer navigation may have started since.
|
|
79
|
+
if (started !== revision) {
|
|
80
|
+
return
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
let component = page.value
|
|
84
|
+
// Same path means a query- or hash-only change: do not load the page again.
|
|
85
|
+
if (to.path !== from.path || component === null) {
|
|
86
|
+
let loaded: Component | null
|
|
87
|
+
try {
|
|
88
|
+
loaded = await loadPage(to.path, pages)
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
if (started !== revision) {
|
|
92
|
+
return
|
|
93
|
+
}
|
|
94
|
+
// A page the table does not have is a not-found page, not a failure; only
|
|
95
|
+
// a chunk that could not be fetched lands here.
|
|
96
|
+
if (recover === 'reload' && onPageLoadError) {
|
|
97
|
+
onPageLoadError(error, to)
|
|
98
|
+
return
|
|
99
|
+
}
|
|
100
|
+
throw error
|
|
101
|
+
}
|
|
102
|
+
component = loaded
|
|
103
|
+
if (started !== revision) {
|
|
104
|
+
return
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (write !== 'none') {
|
|
109
|
+
history[write](to.fullPath)
|
|
110
|
+
}
|
|
111
|
+
currentRoute.value = to
|
|
112
|
+
page.value = component
|
|
113
|
+
|
|
114
|
+
for (const hook of [...hooks]) {
|
|
115
|
+
hook(to, from)
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async function runGuards(to: Route, from: Route, started: number): Promise<boolean> {
|
|
120
|
+
for (const guard of [...guards]) {
|
|
121
|
+
const result = await guard(to, from)
|
|
122
|
+
if (started !== revision || result === false) {
|
|
123
|
+
return false
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return true
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
history.listen((location) => {
|
|
130
|
+
void navigate(location, 'none', 'reload').catch(reportNavigationError)
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
return {
|
|
134
|
+
currentRoute,
|
|
135
|
+
page,
|
|
136
|
+
enter: location => navigate(location, 'none', 'throw'),
|
|
137
|
+
push: location => navigate(location, 'push', 'reload'),
|
|
138
|
+
replace: location => navigate(location, 'replace', 'reload'),
|
|
139
|
+
beforeEach(guard) {
|
|
140
|
+
guards.push(guard)
|
|
141
|
+
return () => remove(guards, guard)
|
|
142
|
+
},
|
|
143
|
+
afterEach(hook) {
|
|
144
|
+
hooks.push(hook)
|
|
145
|
+
return () => remove(hooks, hook)
|
|
146
|
+
},
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function remove<T>(list: T[], item: T): void {
|
|
151
|
+
const index = list.indexOf(item)
|
|
152
|
+
if (index !== -1) {
|
|
153
|
+
list.splice(index, 1)
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** A navigation nobody awaits (a history event, a click) must not fail silently. */
|
|
158
|
+
function reportNavigationError(error: unknown): void {
|
|
159
|
+
console.error('[KawaPress] Navigation failed:', error)
|
|
160
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stand-in for the virtual modules of a site, so unit tests can import a module
|
|
3
|
+
* that reads them. `vitest.config.ts` aliases every `virtual:kawapress-*` id at
|
|
4
|
+
* this file; a test that needs other values mocks the module it imports.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export const site = {
|
|
8
|
+
title: 'Test site',
|
|
9
|
+
base: '/',
|
|
10
|
+
head: [],
|
|
11
|
+
locales: { root: { label: 'English', lang: 'en', link: '/' } },
|
|
12
|
+
themeConfig: {},
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const pages = {}
|
|
16
|
+
|
|
17
|
+
export const pageModules = {}
|
|
18
|
+
|
|
19
|
+
export const runtimePlugins = []
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
/// <reference types="vite/client" />
|
|
2
2
|
|
|
3
3
|
declare module 'virtual:kawapress-pages' {
|
|
4
|
-
export const pages:
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}>>
|
|
8
|
-
export const pageData: Record<string, import('../../core/site').PageData>
|
|
4
|
+
export const pages: import('../../core/page-manifest').PageTable
|
|
5
|
+
/** Route path → module id of the page, read by prerendering to find its chunk. */
|
|
6
|
+
export const pageModules: Record<string, string>
|
|
9
7
|
}
|
|
10
8
|
|
|
11
9
|
declare module 'virtual:kawapress-site' {
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { MarkdownExit } from 'markdown-exit'
|
|
2
|
+
import { normalizeFrontmatterSource } from '../core/frontmatter'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Lets authors write frontmatter as a leading JSON object, the way VitePress
|
|
6
|
+
* does. Every Markdown parser KawaPress builds for a page has to install this
|
|
7
|
+
* before the frontmatter plugin reads the source.
|
|
8
|
+
*/
|
|
9
|
+
export function installJsonFrontmatterCompatibility(md: MarkdownExit): void {
|
|
10
|
+
const parse = md.parse.bind(md)
|
|
11
|
+
md.parse = (source, env = {}) => parse(normalizeFrontmatterSource(source), env)
|
|
12
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { PageLinks } from '../core/dead-links'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Per-page link records collected while Markdown pages are compiled. The page
|
|
5
|
+
* loader owns one registry per site, so a build or a dev session shares the same
|
|
6
|
+
* view of the site.
|
|
7
|
+
*/
|
|
8
|
+
export interface LinkRegistry {
|
|
9
|
+
/** Store a page, replacing any earlier record of the same route. */
|
|
10
|
+
set: (page: PageLinks) => void
|
|
11
|
+
get: (route: string) => PageLinks | undefined
|
|
12
|
+
/** Every recorded page, in the order the site loaded them. */
|
|
13
|
+
pages: () => PageLinks[]
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function createLinkRegistry(): LinkRegistry {
|
|
17
|
+
const pages = new Map<string, PageLinks>()
|
|
18
|
+
return {
|
|
19
|
+
set(page) {
|
|
20
|
+
pages.set(page.route, page)
|
|
21
|
+
},
|
|
22
|
+
get(route) {
|
|
23
|
+
return pages.get(route)
|
|
24
|
+
},
|
|
25
|
+
pages() {
|
|
26
|
+
return [...pages.values()]
|
|
27
|
+
},
|
|
28
|
+
}
|
|
29
|
+
}
|
package/src/compiler/markdown.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import type { MarkdownSfcBlocks } from '@mdit-vue/plugin-sfc'
|
|
2
|
-
import type { MarkdownExit } from 'markdown-exit'
|
|
3
|
-
import type {
|
|
2
|
+
import type { MarkdownExit, Token } from 'markdown-exit'
|
|
3
|
+
import type { LinkRecord } from '../core/dead-links'
|
|
4
|
+
import type { PageData } from '../core/extension-points'
|
|
5
|
+
import type { PageHeader } from '../core/site'
|
|
4
6
|
import type { GeneratorPluginRunner } from '../plugin/generator-runner'
|
|
7
|
+
import type { PageComponent, PageComponentEnv } from './page-components'
|
|
5
8
|
import { componentPlugin } from '@mdit-vue/plugin-component'
|
|
6
9
|
import { frontmatterPlugin } from '@mdit-vue/plugin-frontmatter'
|
|
7
10
|
import { headersPlugin } from '@mdit-vue/plugin-headers'
|
|
@@ -10,18 +13,42 @@ import { attrs as attrsPlugin } from '@mdit/plugin-attrs'
|
|
|
10
13
|
import { createMarkdownExit } from 'markdown-exit'
|
|
11
14
|
import anchorPlugin from 'markdown-it-anchor'
|
|
12
15
|
import { withBase } from '../core/base'
|
|
13
|
-
import {
|
|
16
|
+
import { resolveHeadConfig } from '../core/head'
|
|
17
|
+
import { resolveHeadingTitle } from '../core/heading-title'
|
|
14
18
|
import { stringifyJsonForScript } from '../core/json'
|
|
15
19
|
import { markdownPagePathToRoutePath } from '../core/markdown-route'
|
|
16
20
|
import { resolvePageMetadata } from '../core/page-metadata'
|
|
21
|
+
import { installJsonFrontmatterCompatibility } from './frontmatter-compat'
|
|
17
22
|
import { useMarkdownItPlugin } from './markdown-it-compat'
|
|
23
|
+
import {
|
|
24
|
+
injectPageComponentImports,
|
|
25
|
+
isScriptSetupBlock,
|
|
26
|
+
readScriptLang,
|
|
27
|
+
} from './page-components'
|
|
18
28
|
|
|
19
|
-
|
|
29
|
+
/** Class of the permalink that KawaPress renders inside every heading. */
|
|
30
|
+
const HEADER_ANCHOR_CLASS = 'header-anchor'
|
|
31
|
+
|
|
32
|
+
interface MarkdownEnv extends PageComponentEnv {
|
|
20
33
|
path: string
|
|
21
34
|
sourcePath?: string
|
|
22
35
|
frontmatter?: Record<string, unknown>
|
|
23
36
|
headers?: PageHeader[]
|
|
24
37
|
sfcBlocks?: MarkdownSfcBlocks
|
|
38
|
+
/** Set by the LLMS plugin when it compiles Markdown for plain-text output. */
|
|
39
|
+
ssgMarkdown?: boolean
|
|
40
|
+
/** Set once the `doc.header` outlet has been placed after the first H1. */
|
|
41
|
+
docHeaderOutletPlaced?: boolean
|
|
42
|
+
/** Page body after frontmatter removal, set by the frontmatter plugin. */
|
|
43
|
+
content?: string
|
|
44
|
+
/** Internal links of the page, with line numbers relative to the source file. */
|
|
45
|
+
links?: LinkRecord[]
|
|
46
|
+
/** Anchor IDs of every heading on the page, from h1 to h6. */
|
|
47
|
+
headings?: string[]
|
|
48
|
+
/** 1-based line of each inline link token, filled while parsing. */
|
|
49
|
+
linkLines?: Map<Token, number>
|
|
50
|
+
/** Components the page declared with `addPageComponent()`. */
|
|
51
|
+
pageComponents?: PageComponent[]
|
|
25
52
|
}
|
|
26
53
|
|
|
27
54
|
export interface CompiledPage {
|
|
@@ -48,7 +75,7 @@ export async function createMarkdownCompiler(options: MarkdownCompilerOptions =
|
|
|
48
75
|
useMarkdownItPlugin(md, anchorPlugin, {
|
|
49
76
|
level: [1, 2, 3, 4, 5, 6],
|
|
50
77
|
permalink: anchorPlugin.permalink.linkInsideHeader({
|
|
51
|
-
class:
|
|
78
|
+
class: HEADER_ANCHOR_CLASS,
|
|
52
79
|
placement: 'after',
|
|
53
80
|
space: true,
|
|
54
81
|
symbol: '#',
|
|
@@ -56,15 +83,66 @@ export async function createMarkdownCompiler(options: MarkdownCompilerOptions =
|
|
|
56
83
|
})
|
|
57
84
|
// include h1: page title inference reads it; outline rendering filters to h2+ itself
|
|
58
85
|
useMarkdownItPlugin(md, headersPlugin, { level: [1, 2, 3] })
|
|
86
|
+
installHeadingTitles(md)
|
|
87
|
+
installPageLinkCollector(md)
|
|
59
88
|
installLinkRenderer(md, options.base ?? '/')
|
|
89
|
+
installDocHeaderOutlet(md)
|
|
60
90
|
|
|
61
91
|
await options.pluginRunner?.runMarkdown(md)
|
|
62
92
|
return md
|
|
63
93
|
}
|
|
64
94
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
95
|
+
/**
|
|
96
|
+
* Replaces the title `@mdit-vue/plugin-headers` extracted with the plain text
|
|
97
|
+
* the author wrote. The plugin only sees what the inline children of a heading
|
|
98
|
+
* left behind, so the title of a heading with a formula, or with a syntax only
|
|
99
|
+
* a Markdown plugin understands, would differ from the same heading read by the
|
|
100
|
+
* page summary reader and by the search index. Both lists are joined by the
|
|
101
|
+
* heading ID, which the plugin reads from `markdown-it-anchor` either way, so
|
|
102
|
+
* slugs never change here.
|
|
103
|
+
*/
|
|
104
|
+
function installHeadingTitles(md: MarkdownExit): void {
|
|
105
|
+
const render = md.renderer.render.bind(md.renderer)
|
|
106
|
+
|
|
107
|
+
md.renderer.render = (tokens, options, env) => {
|
|
108
|
+
// Runs after the headers plugin wrapped the same method, so the headers of
|
|
109
|
+
// this page already exist when their titles are replaced.
|
|
110
|
+
const html = render(tokens, options, env)
|
|
111
|
+
const headers = (env as MarkdownEnv).headers
|
|
112
|
+
|
|
113
|
+
if (headers?.length) {
|
|
114
|
+
replaceHeaderTitles(headers, collectHeadingTitles(tokens))
|
|
115
|
+
}
|
|
116
|
+
return html
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Plain-text title of every heading of the page, keyed by heading ID. */
|
|
121
|
+
function collectHeadingTitles(tokens: readonly Token[]): Map<string, string> {
|
|
122
|
+
const titles = new Map<string, string>()
|
|
123
|
+
|
|
124
|
+
for (const [index, token] of tokens.entries()) {
|
|
125
|
+
if (token.type !== 'heading_open') {
|
|
126
|
+
continue
|
|
127
|
+
}
|
|
128
|
+
const id = token.attrGet('id')
|
|
129
|
+
const inline = tokens[index + 1]
|
|
130
|
+
if (id && inline?.type === 'inline') {
|
|
131
|
+
titles.set(id, resolveHeadingTitle(inline))
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return titles
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function replaceHeaderTitles(headers: PageHeader[], titles: Map<string, string>): void {
|
|
139
|
+
for (const header of headers) {
|
|
140
|
+
const title = titles.get(header.slug)
|
|
141
|
+
if (title !== undefined) {
|
|
142
|
+
header.title = title
|
|
143
|
+
}
|
|
144
|
+
replaceHeaderTitles(header.children, titles)
|
|
145
|
+
}
|
|
68
146
|
}
|
|
69
147
|
|
|
70
148
|
function installAsyncSfcRenderCompatibility(md: MarkdownExit): void {
|
|
@@ -91,8 +169,76 @@ function installAsyncSfcRenderCompatibility(md: MarkdownExit): void {
|
|
|
91
169
|
}
|
|
92
170
|
}
|
|
93
171
|
|
|
172
|
+
const DOC_HEADER_OUTLET = '<KawaOutlet name="doc.header" />'
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Renders the `doc.header` outlet right after the first H1 of every page, so
|
|
176
|
+
* plugins can place interface pieces inside the Markdown body without the
|
|
177
|
+
* theme reaching into compiled content. Pages without an H1 get no outlet.
|
|
178
|
+
*/
|
|
179
|
+
function installDocHeaderOutlet(md: MarkdownExit): void {
|
|
180
|
+
const original = md.renderer.rules.heading_close
|
|
181
|
+
|
|
182
|
+
md.renderer.rules.heading_close = (tokens, index, options, env, renderer) => {
|
|
183
|
+
const rendered = original
|
|
184
|
+
? original(tokens, index, options, env, renderer)
|
|
185
|
+
: renderer.renderToken(tokens, index, options)
|
|
186
|
+
const renderEnv = env as MarkdownEnv
|
|
187
|
+
|
|
188
|
+
if (
|
|
189
|
+
tokens[index].tag !== 'h1'
|
|
190
|
+
|| typeof env !== 'object'
|
|
191
|
+
|| env === null
|
|
192
|
+
|| renderEnv.ssgMarkdown
|
|
193
|
+
|| renderEnv.docHeaderOutletPlaced
|
|
194
|
+
) {
|
|
195
|
+
return rendered
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
renderEnv.docHeaderOutletPlaced = true
|
|
199
|
+
return `${rendered}\n${DOC_HEADER_OUTLET}\n`
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
94
203
|
const EXTERNAL_LINK_RE = /^(?:[a-z][a-z\d+.-]*:|\/\/)/i
|
|
95
204
|
|
|
205
|
+
/**
|
|
206
|
+
* Collect what the dead link checker needs: the line of every inline link and
|
|
207
|
+
* the final anchor ID of every heading. Runs after `markdown-it-anchor`, so
|
|
208
|
+
* `heading_open` tokens already carry the generated slug or a custom `{#id}`.
|
|
209
|
+
*/
|
|
210
|
+
function installPageLinkCollector(md: MarkdownExit): void {
|
|
211
|
+
md.core.ruler.push('kawapress:page-links', (state) => {
|
|
212
|
+
const env = state.env as MarkdownEnv
|
|
213
|
+
const linkLines = new Map<Token, number>()
|
|
214
|
+
const headings: string[] = []
|
|
215
|
+
let blockLine = 1
|
|
216
|
+
|
|
217
|
+
for (const token of state.tokens) {
|
|
218
|
+
if (token.map) {
|
|
219
|
+
blockLine = token.map[0] + 1
|
|
220
|
+
}
|
|
221
|
+
if (token.type === 'heading_open') {
|
|
222
|
+
const id = token.attrGet('id')
|
|
223
|
+
if (id) {
|
|
224
|
+
headings.push(id)
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
if (token.type !== 'inline') {
|
|
228
|
+
continue
|
|
229
|
+
}
|
|
230
|
+
for (const child of token.children ?? []) {
|
|
231
|
+
if (child.type === 'link_open') {
|
|
232
|
+
linkLines.set(child, blockLine)
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
env.linkLines = linkLines
|
|
238
|
+
env.headings = headings
|
|
239
|
+
})
|
|
240
|
+
}
|
|
241
|
+
|
|
96
242
|
function installLinkRenderer(md: MarkdownExit, base: string): void {
|
|
97
243
|
const defaultRender = md.renderer.rules.link_open
|
|
98
244
|
md.renderer.rules.link_open = (tokens, index, options, env, renderer) => {
|
|
@@ -108,13 +254,17 @@ function installLinkRenderer(md: MarkdownExit, base: string): void {
|
|
|
108
254
|
appendRel(token, 'noreferrer')
|
|
109
255
|
}
|
|
110
256
|
}
|
|
111
|
-
else if (href
|
|
112
|
-
|
|
257
|
+
else if (href) {
|
|
258
|
+
const link = resolveInternalLink(
|
|
113
259
|
href,
|
|
114
260
|
markdownEnv,
|
|
115
261
|
base,
|
|
116
262
|
token.attrIndex('download') < 0,
|
|
117
|
-
)
|
|
263
|
+
)
|
|
264
|
+
token.attrSet('href', link.href)
|
|
265
|
+
if (!isHeaderAnchor(token)) {
|
|
266
|
+
recordLink(markdownEnv, token, href, link.target)
|
|
267
|
+
}
|
|
118
268
|
}
|
|
119
269
|
|
|
120
270
|
return defaultRender
|
|
@@ -129,12 +279,23 @@ function appendRel(token: { attrGet: (name: string) => null | string, attrSet: (
|
|
|
129
279
|
token.attrSet('rel', [...rel].join(' '))
|
|
130
280
|
}
|
|
131
281
|
|
|
132
|
-
|
|
282
|
+
interface ResolvedInternalLink {
|
|
283
|
+
/** Value written to the page, including the site base. */
|
|
284
|
+
href: string
|
|
285
|
+
/** Public route or public file path without the site base, including search and hash. */
|
|
286
|
+
target: string
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function resolveInternalLink(
|
|
133
290
|
href: string,
|
|
134
291
|
env: MarkdownEnv,
|
|
135
292
|
base: string,
|
|
136
293
|
normalizePage: boolean,
|
|
137
|
-
):
|
|
294
|
+
): ResolvedInternalLink {
|
|
295
|
+
if (href.startsWith('#')) {
|
|
296
|
+
return { href, target: href }
|
|
297
|
+
}
|
|
298
|
+
|
|
138
299
|
const sourcePath = env.sourcePath ?? inferMarkdownSourcePath(env.path)
|
|
139
300
|
const encodedSourcePath = sourcePath
|
|
140
301
|
.split('/')
|
|
@@ -144,7 +305,28 @@ function normalizeInternalLink(
|
|
|
144
305
|
const pathname = decodeURI(normalizePage
|
|
145
306
|
? markdownPagePathToRoutePath(resolved.pathname)
|
|
146
307
|
: resolved.pathname)
|
|
147
|
-
|
|
308
|
+
const target = `${pathname}${resolved.search}${resolved.hash}`
|
|
309
|
+
return { href: withBase(target, base), target }
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function recordLink(
|
|
313
|
+
env: MarkdownEnv,
|
|
314
|
+
token: Token,
|
|
315
|
+
href: string,
|
|
316
|
+
target: string,
|
|
317
|
+
): void {
|
|
318
|
+
const links = (env.links ??= [])
|
|
319
|
+
links.push({
|
|
320
|
+
href,
|
|
321
|
+
target,
|
|
322
|
+
line: env.linkLines?.get(token) ?? 1,
|
|
323
|
+
})
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/** The permalink inside every heading is generated by KawaPress, not authored. */
|
|
327
|
+
function isHeaderAnchor(token: Token): boolean {
|
|
328
|
+
const classes = token.attrGet('class')?.split(/\s+/) ?? []
|
|
329
|
+
return classes.includes(HEADER_ANCHOR_CLASS)
|
|
148
330
|
}
|
|
149
331
|
|
|
150
332
|
function inferMarkdownSourcePath(routePath: string): string {
|
|
@@ -163,21 +345,44 @@ export async function parseMarkdown(
|
|
|
163
345
|
path: string,
|
|
164
346
|
sourcePath?: string,
|
|
165
347
|
): Promise<ParsedMarkdown> {
|
|
166
|
-
const env: MarkdownEnv = { path, sourcePath }
|
|
348
|
+
const env: MarkdownEnv = { path, sourcePath, links: [], headings: [] }
|
|
167
349
|
const html = await md.renderAsync(src, env)
|
|
168
350
|
|
|
351
|
+
env.links = shiftLinksToSourceLines(src, env)
|
|
169
352
|
const frontmatter = env.frontmatter ?? {}
|
|
170
353
|
const headers = env.headers ?? []
|
|
171
354
|
const metadata = resolvePageMetadata(frontmatter, headers)
|
|
355
|
+
const head = resolveHeadConfig(frontmatter.head, {
|
|
356
|
+
owner: `the frontmatter of route ${JSON.stringify(path)}`,
|
|
357
|
+
path: 'head',
|
|
358
|
+
})
|
|
172
359
|
const pageData: PageData = {
|
|
173
360
|
path,
|
|
174
361
|
...metadata,
|
|
362
|
+
...(head.length === 0 ? {} : { head }),
|
|
175
363
|
frontmatter,
|
|
176
364
|
headers,
|
|
177
365
|
}
|
|
178
366
|
return { html, env, pageData }
|
|
179
367
|
}
|
|
180
368
|
|
|
369
|
+
/**
|
|
370
|
+
* Token line numbers start at the page body, so frontmatter lines are added back
|
|
371
|
+
* to report the line the author sees in the Markdown file.
|
|
372
|
+
*/
|
|
373
|
+
function shiftLinksToSourceLines(src: string, env: MarkdownEnv): LinkRecord[] {
|
|
374
|
+
const offset = countRemovedLines(src, env.content)
|
|
375
|
+
return (env.links ?? []).map(link => ({ ...link, line: link.line + offset }))
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
function countRemovedLines(source: string, body: string | undefined): number {
|
|
379
|
+
if (!body || !source.endsWith(body)) {
|
|
380
|
+
return 0
|
|
381
|
+
}
|
|
382
|
+
const removed = source.slice(0, source.length - body.length)
|
|
383
|
+
return (removed.match(/\n/g) ?? []).length
|
|
384
|
+
}
|
|
385
|
+
|
|
181
386
|
export async function compileMarkdownToVue(
|
|
182
387
|
md: MarkdownExit,
|
|
183
388
|
src: string,
|
|
@@ -188,8 +393,6 @@ export async function compileMarkdownToVue(
|
|
|
188
393
|
return { code: assembleVueSfc(html, env, pageData), pageData }
|
|
189
394
|
}
|
|
190
395
|
|
|
191
|
-
const scriptSetupRE = /<\s*script[^>]*\ssetup\b/
|
|
192
|
-
const scriptLangRE = /\slang\s*=\s*["']([^"']+)["']/
|
|
193
396
|
const scriptEndRE = /<\/script>/
|
|
194
397
|
|
|
195
398
|
export function assembleVueSfc(html: string, env: MarkdownEnv, pageData: PageData): string {
|
|
@@ -203,20 +406,19 @@ export function assembleVueSfc(html: string, env: MarkdownEnv, pageData: PageDat
|
|
|
203
406
|
const pageDataCode = `export const __pageData = ${serializedPageData}`
|
|
204
407
|
|
|
205
408
|
const scripts = env.sfcBlocks?.scripts.map(block => block.content) ?? []
|
|
206
|
-
const plainIndex = scripts.findIndex(content => !
|
|
409
|
+
const plainIndex = scripts.findIndex(content => !isScriptSetupBlock(content))
|
|
207
410
|
if (plainIndex >= 0) {
|
|
208
411
|
scripts[plainIndex] = scripts[plainIndex].replace(scriptEndRE, `${pageDataCode}\n</script>`)
|
|
209
412
|
}
|
|
210
413
|
else {
|
|
211
|
-
const
|
|
212
|
-
const lang = setupScript?.match(scriptLangRE)?.[1]
|
|
414
|
+
const lang = scripts.map(readScriptLang).find(value => value !== undefined)
|
|
213
415
|
const langAttribute = lang ? ` lang=${JSON.stringify(lang)}` : ''
|
|
214
416
|
scripts.unshift(`<script${langAttribute}>\n${pageDataCode}\nexport default { name: ${JSON.stringify(pageData.path)} }\n</script>`)
|
|
215
417
|
}
|
|
216
418
|
|
|
217
419
|
return [
|
|
218
420
|
`<template>${templateHtml}</template>`,
|
|
219
|
-
...scripts,
|
|
421
|
+
...injectPageComponentImports(scripts, env.pageComponents ?? []),
|
|
220
422
|
...(env.sfcBlocks?.styles.map(block => block.content) ?? []),
|
|
221
423
|
...(env.sfcBlocks?.customBlocks.map(block => block.content) ?? []),
|
|
222
424
|
].join('\n')
|