kawapress 0.2.2 → 0.4.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/package.json +4 -2
- package/src/api/config.ts +11 -1
- package/src/api/plugin.ts +11 -2
- package/src/client/components/KawaOutlet.vue +17 -12
- package/src/client/index.ts +2 -1
- package/src/client/platform.ts +30 -0
- package/src/compiler/code-snippet.ts +111 -0
- package/src/compiler/content-loader.ts +7 -3
- package/src/compiler/markdown-source.ts +240 -0
- package/src/compiler/markdown.ts +25 -12
- package/src/compiler/page-loader.ts +40 -12
- package/src/compiler/page-path.ts +93 -0
- package/src/compiler/source-region.ts +78 -0
- package/src/core/extension-points.ts +1 -1
- package/src/core/head.ts +1 -1
- package/src/core/heading-title.ts +9 -5
- package/src/core/outlet.ts +33 -11
- package/src/core/site.ts +12 -0
- package/src/generator/config.ts +50 -2
- package/src/generator/data/context.ts +4 -1
- package/src/generator/pages.ts +49 -10
- package/src/generator/vite/config.ts +2 -0
- package/src/generator/vite/plugins/dev-dead-links.ts +5 -4
- package/src/generator/vite/plugins/markdown.ts +5 -1
- package/src/generator/vite/plugins/pages.ts +2 -0
- package/src/generator.ts +4 -1
- package/src/index.ts +1 -0
- package/src/plugin/outlet-collector.ts +37 -22
- package/src/plugin/runtime-runner.ts +1 -1
|
@@ -1,28 +1,43 @@
|
|
|
1
|
+
import type { PageRewrites } from '../core/site'
|
|
1
2
|
import type { GeneratorPluginRunner } from '../plugin/generator-runner'
|
|
2
3
|
import type { LinkRegistry } from './link-registry'
|
|
3
4
|
import type { ParsedMarkdown } from './markdown'
|
|
4
|
-
import {
|
|
5
|
+
import { resolve } from 'node:path'
|
|
5
6
|
import { markdownPagePathToRoutePath } from '../core/markdown-route'
|
|
6
7
|
import { assertPageDataSerializable } from '../core/site'
|
|
7
8
|
import { createLinkRegistry } from './link-registry'
|
|
8
9
|
import { createMarkdownCompiler, parseMarkdown } from './markdown'
|
|
10
|
+
import { expandMarkdownSource } from './markdown-source'
|
|
11
|
+
import { markdownFileToSourcePath, resolvePagePathRewriter } from './page-path'
|
|
9
12
|
|
|
10
13
|
interface CachedPage {
|
|
11
14
|
source: string
|
|
12
|
-
parsed: Promise<
|
|
15
|
+
parsed: Promise<LoadedMarkdownPage>
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface LoadedMarkdownPage extends ParsedMarkdown {
|
|
19
|
+
/** Files the page reads through `<!--@include-->` and `<<<`, as absolute paths. */
|
|
20
|
+
dependencies: string[]
|
|
13
21
|
}
|
|
14
22
|
|
|
15
23
|
export interface MarkdownPageLoader {
|
|
16
24
|
sourceRoot: string
|
|
25
|
+
/** Glob patterns, relative to `sourceRoot`, of Markdown files that are not pages. */
|
|
26
|
+
srcExclude: readonly string[]
|
|
27
|
+
/** `rewrites` of the site config, which moves pages to other routes. */
|
|
28
|
+
rewrites: PageRewrites | undefined
|
|
17
29
|
/** Links and heading IDs of every page loaded so far. */
|
|
18
30
|
registry: LinkRegistry
|
|
19
|
-
|
|
31
|
+
/** Compiles the page whose file holds `source`, as written on disk. */
|
|
32
|
+
load: (source: string, file: string) => Promise<LoadedMarkdownPage>
|
|
20
33
|
}
|
|
21
34
|
|
|
22
35
|
export interface MarkdownPageLoaderOptions {
|
|
23
36
|
root: string
|
|
24
37
|
base: string
|
|
25
38
|
srcDir: string
|
|
39
|
+
srcExclude?: readonly string[]
|
|
40
|
+
rewrites?: PageRewrites | undefined
|
|
26
41
|
pluginRunner: GeneratorPluginRunner
|
|
27
42
|
}
|
|
28
43
|
|
|
@@ -36,11 +51,24 @@ export function createMarkdownPageLoader(
|
|
|
36
51
|
pluginRunner: options.pluginRunner,
|
|
37
52
|
})
|
|
38
53
|
const cache = new Map<string, CachedPage>()
|
|
54
|
+
const rewrite = resolvePagePathRewriter(options.rewrites)
|
|
39
55
|
|
|
40
56
|
return {
|
|
41
57
|
sourceRoot,
|
|
58
|
+
srcExclude: options.srcExclude ?? [],
|
|
59
|
+
rewrites: options.rewrites,
|
|
42
60
|
registry,
|
|
43
|
-
load(
|
|
61
|
+
load(rawSource, file) {
|
|
62
|
+
// Expanding first makes an edit to an included file a new page source,
|
|
63
|
+
// so the cache can never return a page built from an older include.
|
|
64
|
+
let expanded: ReturnType<typeof expandMarkdownSource>
|
|
65
|
+
try {
|
|
66
|
+
expanded = expandMarkdownSource(rawSource, { file, sourceRoot })
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
return Promise.reject(error)
|
|
70
|
+
}
|
|
71
|
+
const { source, dependencies, lineOrigins } = expanded
|
|
44
72
|
const cached = cache.get(file)
|
|
45
73
|
if (cached?.source === source) {
|
|
46
74
|
return cached.parsed
|
|
@@ -48,12 +76,16 @@ export function createMarkdownPageLoader(
|
|
|
48
76
|
|
|
49
77
|
const parsed = mdPromise.then(async (md) => {
|
|
50
78
|
const sourcePath = markdownFileToSourcePath(file, sourceRoot)
|
|
51
|
-
const
|
|
79
|
+
const pagePath = rewrite(sourcePath)
|
|
80
|
+
const routePath = markdownPagePathToRoutePath(pagePath)
|
|
81
|
+
// Relative links start from where the page is served, so a page moved
|
|
82
|
+
// by rewrites links as if its file were at the new path.
|
|
52
83
|
const result = await parseMarkdown(
|
|
53
84
|
md,
|
|
54
85
|
source,
|
|
55
86
|
routePath,
|
|
56
|
-
|
|
87
|
+
pagePath,
|
|
88
|
+
lineOrigins,
|
|
57
89
|
)
|
|
58
90
|
registry.set({
|
|
59
91
|
route: routePath,
|
|
@@ -68,18 +100,14 @@ export function createMarkdownPageLoader(
|
|
|
68
100
|
source,
|
|
69
101
|
file,
|
|
70
102
|
sourcePath,
|
|
103
|
+
pagePath,
|
|
71
104
|
routePath,
|
|
72
105
|
pageData: result.pageData,
|
|
73
106
|
})
|
|
74
|
-
return result
|
|
107
|
+
return { ...result, dependencies }
|
|
75
108
|
})
|
|
76
109
|
cache.set(file, { source, parsed })
|
|
77
110
|
return parsed
|
|
78
111
|
},
|
|
79
112
|
}
|
|
80
113
|
}
|
|
81
|
-
|
|
82
|
-
/** Path of a Markdown file relative to srcDir, for example `/guide/intro.md`. */
|
|
83
|
-
export function markdownFileToSourcePath(file: string, sourceRoot: string): string {
|
|
84
|
-
return `/${relative(sourceRoot, file).split(sep).join('/')}`
|
|
85
|
-
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { PageRewrites } from '../core/site'
|
|
2
|
+
import { relative, resolve, sep } from 'node:path'
|
|
3
|
+
import { compile, match } from 'path-to-regexp'
|
|
4
|
+
import { markdownPagePathToRoutePath } from '../core/markdown-route'
|
|
5
|
+
|
|
6
|
+
/** Turns the source path of a page into the page path its route comes from. */
|
|
7
|
+
export type PagePathRewriter = (sourcePath: string) => string
|
|
8
|
+
|
|
9
|
+
const KEEP: PagePathRewriter = sourcePath => sourcePath
|
|
10
|
+
const rewriters = new WeakMap<object, PagePathRewriter>()
|
|
11
|
+
|
|
12
|
+
/** Path of a Markdown file relative to srcDir, for example `/guide/intro.md`. */
|
|
13
|
+
export function markdownFileToSourcePath(file: string, sourceRoot: string): string {
|
|
14
|
+
return `/${relative(sourceRoot, file).split(sep).join('/')}`
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Page path of a Markdown file: its source path after `rewrites`, for example
|
|
19
|
+
* `/a/intro.md` for `packages/a/src/intro.md` and the rule
|
|
20
|
+
* `'packages/:pkg/src/:page*': ':pkg/:page*'`. Without rewrites it is the
|
|
21
|
+
* source path. The route of the page and its relative links both start here.
|
|
22
|
+
*/
|
|
23
|
+
export function markdownFileToPagePath(
|
|
24
|
+
file: string,
|
|
25
|
+
sourceRoot: string,
|
|
26
|
+
rewrites?: PageRewrites,
|
|
27
|
+
): string {
|
|
28
|
+
return resolvePagePathRewriter(rewrites)(markdownFileToSourcePath(file, resolve(sourceRoot)))
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Public route of a Markdown file, after `rewrites`. */
|
|
32
|
+
export function markdownFileToRoutePath(
|
|
33
|
+
file: string,
|
|
34
|
+
sourceRoot: string,
|
|
35
|
+
rewrites?: PageRewrites,
|
|
36
|
+
): string {
|
|
37
|
+
return markdownPagePathToRoutePath(markdownFileToPagePath(file, sourceRoot, rewrites))
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The rewriter of a `rewrites` value, compiled once per value.
|
|
42
|
+
*
|
|
43
|
+
* Paths are matched the way `rewrites` is written: relative to srcDir, without
|
|
44
|
+
* the leading slash. A function returns the new path, or nothing to keep the
|
|
45
|
+
* page where it is. Rules of an object are tried in order and the first match
|
|
46
|
+
* wins; a rule starting with `^` is a regular expression. Rules use the
|
|
47
|
+
* `path-to-regexp` 6 syntax, the same as VitePress.
|
|
48
|
+
*/
|
|
49
|
+
export function resolvePagePathRewriter(rewrites: PageRewrites | undefined): PagePathRewriter {
|
|
50
|
+
if (!rewrites) {
|
|
51
|
+
return KEEP
|
|
52
|
+
}
|
|
53
|
+
let rewriter = rewriters.get(rewrites)
|
|
54
|
+
if (!rewriter) {
|
|
55
|
+
rewriter = typeof rewrites === 'function'
|
|
56
|
+
? fromFunction(rewrites)
|
|
57
|
+
: fromRules(rewrites)
|
|
58
|
+
rewriters.set(rewrites, rewriter)
|
|
59
|
+
}
|
|
60
|
+
return rewriter
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function fromFunction(rewrite: (id: string) => string | undefined): PagePathRewriter {
|
|
64
|
+
return (sourcePath) => {
|
|
65
|
+
const destination = rewrite(sourcePath.slice(1))
|
|
66
|
+
return destination ? toPagePath(destination) : sourcePath
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function fromRules(rules: Record<string, string>): PagePathRewriter {
|
|
71
|
+
const compiled = Object.entries(rules).map(([from, to]) => ({
|
|
72
|
+
matches: match(from.startsWith('^') ? new RegExp(from) : from),
|
|
73
|
+
toPath: compile(`/${to}`, { validate: false }),
|
|
74
|
+
}))
|
|
75
|
+
if (compiled.length === 0) {
|
|
76
|
+
return KEEP
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return (sourcePath) => {
|
|
80
|
+
const id = sourcePath.slice(1)
|
|
81
|
+
for (const { matches, toPath } of compiled) {
|
|
82
|
+
const result = matches(id)
|
|
83
|
+
if (result) {
|
|
84
|
+
return toPagePath(toPath(result.params))
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return sourcePath
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function toPagePath(path: string): string {
|
|
92
|
+
return `/${path.replace(/^\/+/, '')}`
|
|
93
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Region markers that mark part of a file for `<<<` and `<!--@include-->`. A
|
|
3
|
+
* region starts with a `region <name>` comment and ends with the matching
|
|
4
|
+
* `endregion <name>` comment, written in the comment syntax of the language.
|
|
5
|
+
*/
|
|
6
|
+
const REGION_MARKERS: readonly RegExp[] = [
|
|
7
|
+
// JavaScript, TypeScript, Java, Go, Rust
|
|
8
|
+
/^\/\/ ?#?((?:end)?region) ([\w*-]+)$/,
|
|
9
|
+
// CSS, Less, SCSS
|
|
10
|
+
/^\/\* ?#((?:end)?region) ([\w*-]+) ?\*\/$/,
|
|
11
|
+
// C, C++
|
|
12
|
+
/^#pragma ((?:end)?region) ([\w*-]+)$/,
|
|
13
|
+
// HTML, Markdown, Vue templates
|
|
14
|
+
/^<!-- #?((?:end)?region) ([\w*-]+) -->$/,
|
|
15
|
+
// Visual Basic
|
|
16
|
+
/^#((?:End )?Region) ([\w*-]+)$/,
|
|
17
|
+
// Batch
|
|
18
|
+
/^::#((?:end)?region) ([\w*-]+)$/,
|
|
19
|
+
// Python, shell, YAML, PHP, C#, PowerShell
|
|
20
|
+
/^# ?((?:end)?region) ([\w*-]+)$/,
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
export interface SourceRegion {
|
|
24
|
+
/** Index of the first line inside the region. */
|
|
25
|
+
start: number
|
|
26
|
+
/** Index of the line holding the end marker. */
|
|
27
|
+
end: number
|
|
28
|
+
/** Marker syntax of this region, used to drop nested markers. */
|
|
29
|
+
marker: RegExp
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The first region called `name`, or `undefined` when the file has none. */
|
|
33
|
+
export function findSourceRegion(
|
|
34
|
+
lines: readonly string[],
|
|
35
|
+
name: string,
|
|
36
|
+
): SourceRegion | undefined {
|
|
37
|
+
let marker: RegExp | undefined
|
|
38
|
+
let start = -1
|
|
39
|
+
|
|
40
|
+
for (const [index, line] of lines.entries()) {
|
|
41
|
+
if (!marker) {
|
|
42
|
+
marker = REGION_MARKERS.find(candidate => isRegionMarker(line, candidate, name, false))
|
|
43
|
+
start = index + 1
|
|
44
|
+
}
|
|
45
|
+
else if (isRegionMarker(line, marker, name, true)) {
|
|
46
|
+
return { start, end: index, marker }
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Whether `line` is any region marker written in the syntax of `marker`. */
|
|
52
|
+
export function isAnyRegionMarker(line: string, marker: RegExp): boolean {
|
|
53
|
+
return marker.test(line.trim())
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function isRegionMarker(
|
|
57
|
+
line: string,
|
|
58
|
+
marker: RegExp,
|
|
59
|
+
name: string,
|
|
60
|
+
end: boolean,
|
|
61
|
+
): boolean {
|
|
62
|
+
const [, tag, markerName] = marker.exec(line.trim()) ?? []
|
|
63
|
+
if (!tag || markerName !== name) {
|
|
64
|
+
return false
|
|
65
|
+
}
|
|
66
|
+
return end ? /^end ?region$/i.test(tag) : /^region$/i.test(tag)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Removes the indentation every non-blank line shares. */
|
|
70
|
+
export function dedent(text: string): string {
|
|
71
|
+
const lines = text.split('\n')
|
|
72
|
+
const indent = Math.min(...lines
|
|
73
|
+
.filter(line => line.trim())
|
|
74
|
+
.map(line => line.length - line.trimStart().length))
|
|
75
|
+
return Number.isFinite(indent) && indent > 0
|
|
76
|
+
? lines.map(line => line.slice(indent)).join('\n')
|
|
77
|
+
: text
|
|
78
|
+
}
|
package/src/core/head.ts
CHANGED
|
@@ -156,7 +156,7 @@ function resolveHeadAttrs(
|
|
|
156
156
|
return attrs
|
|
157
157
|
}
|
|
158
158
|
|
|
159
|
-
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
159
|
+
export function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
160
160
|
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
161
161
|
return false
|
|
162
162
|
}
|
|
@@ -14,16 +14,20 @@ const FORMATTING_MARKUP_TYPES = new Set([
|
|
|
14
14
|
's_close',
|
|
15
15
|
'link_open',
|
|
16
16
|
'link_close',
|
|
17
|
+
// Inline HTML is markup around text, like `<kbd>Ctrl</kbd>`, or a component
|
|
18
|
+
// such as `<Badge text="beta" />` that only decorates the heading.
|
|
19
|
+
'html_inline',
|
|
17
20
|
])
|
|
18
21
|
|
|
19
22
|
/**
|
|
20
23
|
* The plain-text title of a heading: the text its author wrote on the heading
|
|
21
|
-
* line, without the Markdown formatting markup.
|
|
24
|
+
* line, without the Markdown formatting markup and without HTML tags.
|
|
22
25
|
*
|
|
23
|
-
* Formatting markup
|
|
24
|
-
*
|
|
25
|
-
* keeps
|
|
26
|
-
*
|
|
26
|
+
* Formatting markup and HTML tags disappear while the text between them stays,
|
|
27
|
+
* so `<kbd>Ctrl</kbd>` reads `Ctrl` and `<Badge text="beta" />` reads nothing.
|
|
28
|
+
* Inline code keeps its content without the backticks, and any other syntax
|
|
29
|
+
* keeps the markup it was written with, so inline math stays `$E=mc^2$` and an
|
|
30
|
+
* emoji shortcode stays `:smile:`. A token
|
|
27
31
|
* type this function has never seen keeps its `content` too, so a Markdown
|
|
28
32
|
* plugin can never make heading text disappear, and the title of a page never
|
|
29
33
|
* depends on which plugins a site installed.
|
package/src/core/outlet.ts
CHANGED
|
@@ -2,18 +2,26 @@ import type { Component } from 'vue'
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Every outlet a theme can render. The list is the public contract between
|
|
5
|
-
* plugins (which
|
|
5
|
+
* plugins (which contribute interface pieces) and themes (which draw the
|
|
6
|
+
* default content of each outlet).
|
|
7
|
+
*
|
|
8
|
+
* A name is a scope plus a role: `layout` is the page frame, `doc` is the page
|
|
9
|
+
* being read. `start` and `end` follow the reading direction.
|
|
6
10
|
*/
|
|
7
11
|
export const OUTLET_NAMES = [
|
|
8
|
-
'header
|
|
9
|
-
'
|
|
10
|
-
'
|
|
11
|
-
'
|
|
12
|
+
'layout.header',
|
|
13
|
+
'layout.header.start',
|
|
14
|
+
'layout.header.end',
|
|
15
|
+
'layout.sidebar',
|
|
16
|
+
'layout.footer',
|
|
17
|
+
'doc',
|
|
18
|
+
'doc.under-title',
|
|
19
|
+
'doc.toc',
|
|
12
20
|
] as const
|
|
13
21
|
|
|
14
22
|
export type OutletName = typeof OUTLET_NAMES[number]
|
|
15
23
|
|
|
16
|
-
/** What a plugin passes to `api.
|
|
24
|
+
/** What a plugin passes to `api.prependToOutlet()` or `api.appendToOutlet()`. */
|
|
17
25
|
export interface OutletItemInput {
|
|
18
26
|
id: string
|
|
19
27
|
component: Component
|
|
@@ -27,10 +35,14 @@ export interface OutletItem extends OutletItemInput {
|
|
|
27
35
|
owner: string
|
|
28
36
|
}
|
|
29
37
|
|
|
30
|
-
/**
|
|
31
|
-
export
|
|
32
|
-
|
|
33
|
-
|
|
38
|
+
/** Items of one outlet: `prepend` renders before the theme content, `append` after it. */
|
|
39
|
+
export interface OutletSlots {
|
|
40
|
+
readonly prepend: readonly OutletItem[]
|
|
41
|
+
readonly append: readonly OutletItem[]
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Outlet items grouped by outlet name. Each side is in plugin install order. */
|
|
45
|
+
export type OutletRegistry = Readonly<Record<OutletName, OutletSlots>>
|
|
34
46
|
|
|
35
47
|
export function isOutletName(value: unknown): value is OutletName {
|
|
36
48
|
return typeof value === 'string'
|
|
@@ -50,11 +62,21 @@ export function assertOutletName(
|
|
|
50
62
|
)
|
|
51
63
|
}
|
|
52
64
|
|
|
65
|
+
const EMPTY_SLOTS: OutletSlots = Object.freeze({ prepend: [], append: [] })
|
|
66
|
+
|
|
67
|
+
export function getOutletSlots(
|
|
68
|
+
registry: OutletRegistry,
|
|
69
|
+
outlet: OutletName,
|
|
70
|
+
): OutletSlots {
|
|
71
|
+
return registry[outlet] ?? EMPTY_SLOTS
|
|
72
|
+
}
|
|
73
|
+
|
|
53
74
|
export function getOutletItems(
|
|
54
75
|
registry: OutletRegistry,
|
|
55
76
|
outlet: OutletName,
|
|
56
77
|
): readonly OutletItem[] {
|
|
57
|
-
|
|
78
|
+
const { prepend, append } = getOutletSlots(registry, outlet)
|
|
79
|
+
return [...prepend, ...append]
|
|
58
80
|
}
|
|
59
81
|
|
|
60
82
|
/** One registered item that no page ever rendered. */
|
package/src/core/site.ts
CHANGED
|
@@ -14,10 +14,22 @@ export interface LocaleConfig {
|
|
|
14
14
|
themeConfig?: Partial<ThemeConfig>
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Moves pages to other routes: rules from a source path pattern to a page
|
|
19
|
+
* path, or a function from the source path to the page path. Paths are
|
|
20
|
+
* relative to srcDir and keep their `.md` extension.
|
|
21
|
+
*/
|
|
22
|
+
export type PageRewrites
|
|
23
|
+
= | Record<string, string>
|
|
24
|
+
| ((id: string) => string | undefined)
|
|
25
|
+
|
|
17
26
|
export interface SiteConfig {
|
|
18
27
|
title?: string
|
|
19
28
|
base?: string
|
|
20
29
|
srcDir?: string
|
|
30
|
+
/** Glob patterns, relative to `srcDir`, of Markdown files that are not pages. */
|
|
31
|
+
srcExclude?: string[]
|
|
32
|
+
rewrites?: PageRewrites
|
|
21
33
|
publicDir?: string
|
|
22
34
|
head?: HeadConfig[]
|
|
23
35
|
themeConfig?: Partial<ThemeConfig>
|
package/src/generator/config.ts
CHANGED
|
@@ -2,17 +2,21 @@ import type { KawapressConfig } from '../api/config'
|
|
|
2
2
|
import type { KawaPressPlugin } from '../api/plugin'
|
|
3
3
|
import type { DeadLinkIgnore, ResolvedDeadLinkConfig } from '../core/dead-links'
|
|
4
4
|
import type { HeadConfig } from '../core/head'
|
|
5
|
-
import type { LocaleConfig, SiteConfig } from '../core/site'
|
|
5
|
+
import type { LocaleConfig, PageRewrites, SiteConfig } from '../core/site'
|
|
6
6
|
import type { GeneratorPluginRunner } from '../plugin/generator-runner'
|
|
7
7
|
import { isAbsolute } from 'node:path'
|
|
8
8
|
import { normalizeBase } from '../core/base'
|
|
9
|
-
import { resolveHeadConfig } from '../core/head'
|
|
9
|
+
import { isPlainObject, resolveHeadConfig } from '../core/head'
|
|
10
10
|
import { createGeneratorPluginRunner } from '../plugin/generator-runner'
|
|
11
11
|
|
|
12
12
|
export interface ResolvedSiteConfig {
|
|
13
13
|
title: string
|
|
14
14
|
base: string
|
|
15
15
|
srcDir: string
|
|
16
|
+
/** Glob patterns, relative to `srcDir`, of Markdown files that are not pages. */
|
|
17
|
+
srcExclude: string[]
|
|
18
|
+
/** `rewrites` of the site config; leaves every page at its file path when unset. */
|
|
19
|
+
rewrites: PageRewrites | undefined
|
|
16
20
|
publicDir: string
|
|
17
21
|
head: HeadConfig[]
|
|
18
22
|
themeConfig?: object
|
|
@@ -39,6 +43,8 @@ export async function resolveSiteConfig(
|
|
|
39
43
|
title: userConfig.title,
|
|
40
44
|
base: userConfig.base,
|
|
41
45
|
srcDir: userConfig.srcDir,
|
|
46
|
+
srcExclude: userConfig.srcExclude,
|
|
47
|
+
rewrites: userConfig.rewrites,
|
|
42
48
|
publicDir: userConfig.publicDir,
|
|
43
49
|
head: userConfig.head,
|
|
44
50
|
themeConfig: userConfig.themeConfig,
|
|
@@ -51,6 +57,8 @@ export async function resolveSiteConfig(
|
|
|
51
57
|
throw new Error(`KawaPress: srcDir must be relative to the site root, got ${JSON.stringify(srcDir)}`)
|
|
52
58
|
}
|
|
53
59
|
|
|
60
|
+
const srcExclude = resolveSrcExclude(site.srcExclude)
|
|
61
|
+
const rewrites = resolveRewrites(site.rewrites)
|
|
54
62
|
const publicDir = normalizePublicDir(site.publicDir ?? 'public')
|
|
55
63
|
const head = resolveHeadConfig(site.head, {
|
|
56
64
|
owner: 'the site config',
|
|
@@ -63,6 +71,8 @@ export async function resolveSiteConfig(
|
|
|
63
71
|
title: site.title ?? 'KawaPress',
|
|
64
72
|
base: normalizeBase(site.base),
|
|
65
73
|
srcDir,
|
|
74
|
+
srcExclude,
|
|
75
|
+
rewrites,
|
|
66
76
|
publicDir,
|
|
67
77
|
head,
|
|
68
78
|
themeConfig: site.themeConfig,
|
|
@@ -125,6 +135,44 @@ function describeValue(value: unknown): string {
|
|
|
125
135
|
return Array.isArray(value) ? 'an array' : typeof value
|
|
126
136
|
}
|
|
127
137
|
|
|
138
|
+
function resolveRewrites(rewrites: unknown): PageRewrites | undefined {
|
|
139
|
+
if (rewrites === undefined || typeof rewrites === 'function') {
|
|
140
|
+
return rewrites as PageRewrites | undefined
|
|
141
|
+
}
|
|
142
|
+
if (!isPlainObject(rewrites)) {
|
|
143
|
+
throw new TypeError(
|
|
144
|
+
`KawaPress: rewrites must be an object of path rules or a function, got ${describeValue(rewrites)} in kawapress.config.ts.`,
|
|
145
|
+
)
|
|
146
|
+
}
|
|
147
|
+
for (const [from, to] of Object.entries(rewrites)) {
|
|
148
|
+
if (typeof to !== 'string' || !to) {
|
|
149
|
+
throw new TypeError(
|
|
150
|
+
`KawaPress: rewrites[${JSON.stringify(from)}] must be a page path, got ${describeValue(to)} in kawapress.config.ts.`,
|
|
151
|
+
)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return rewrites as Record<string, string>
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function resolveSrcExclude(srcExclude: unknown): string[] {
|
|
158
|
+
if (srcExclude === undefined) {
|
|
159
|
+
return []
|
|
160
|
+
}
|
|
161
|
+
if (!Array.isArray(srcExclude)) {
|
|
162
|
+
throw new TypeError(
|
|
163
|
+
`KawaPress: srcExclude must be an array of glob patterns, got ${describeValue(srcExclude)} in kawapress.config.ts.`,
|
|
164
|
+
)
|
|
165
|
+
}
|
|
166
|
+
return srcExclude.map((pattern, index) => {
|
|
167
|
+
if (typeof pattern !== 'string' || !pattern.trim() || isAbsolute(pattern)) {
|
|
168
|
+
throw new Error(
|
|
169
|
+
`KawaPress: srcExclude[${index}] must be a glob pattern relative to srcDir, got ${JSON.stringify(pattern)} in kawapress.config.ts.`,
|
|
170
|
+
)
|
|
171
|
+
}
|
|
172
|
+
return pattern
|
|
173
|
+
})
|
|
174
|
+
}
|
|
175
|
+
|
|
128
176
|
function normalizePublicDir(publicDir: string): string {
|
|
129
177
|
if (!publicDir.trim() || isAbsolute(publicDir)) {
|
|
130
178
|
throw new Error(
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { DataLoaderConfig } from '../../api/data'
|
|
2
|
-
import type { SiteData } from '../../core/site'
|
|
2
|
+
import type { PageRewrites, SiteData } from '../../core/site'
|
|
3
3
|
import type { GeneratorPluginRunner } from '../../plugin/generator-runner'
|
|
4
4
|
import type { ResolvedSiteConfig } from '../config'
|
|
5
5
|
import { resolve } from 'node:path'
|
|
@@ -8,6 +8,8 @@ const DATA_LOADER_CONTEXT = Symbol.for('kawapress.data-loader-context')
|
|
|
8
8
|
|
|
9
9
|
export interface InternalDataLoaderContext extends DataLoaderConfig {
|
|
10
10
|
readonly pluginRunner: GeneratorPluginRunner
|
|
11
|
+
/** `rewrites` of the site config, which gives content entries their public URL. */
|
|
12
|
+
readonly rewrites: PageRewrites | undefined
|
|
11
13
|
}
|
|
12
14
|
|
|
13
15
|
export function createDataLoaderContext(
|
|
@@ -32,6 +34,7 @@ export function createDataLoaderContext(
|
|
|
32
34
|
publicDir: resolve(srcDir, config.publicDir),
|
|
33
35
|
site,
|
|
34
36
|
pluginRunner: config.pluginRunner,
|
|
37
|
+
rewrites: config.rewrites,
|
|
35
38
|
}
|
|
36
39
|
}
|
|
37
40
|
|
package/src/generator/pages.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { Dirent } from 'node:fs'
|
|
2
|
+
import type { PageRewrites } from '../core/site'
|
|
2
3
|
import { readdir } from 'node:fs/promises'
|
|
3
4
|
import { isAbsolute, join, relative, resolve, sep } from 'node:path'
|
|
4
|
-
import
|
|
5
|
+
import picomatch from 'picomatch'
|
|
6
|
+
import { markdownFileToSourcePath, resolvePagePathRewriter } from '../compiler/page-path'
|
|
5
7
|
import { markdownPagePathToRoutePath } from '../core/markdown-route'
|
|
6
8
|
|
|
7
9
|
/** Directories that never hold pages: build output and installed packages. */
|
|
@@ -13,6 +15,11 @@ export interface MarkdownPage {
|
|
|
13
15
|
file: string
|
|
14
16
|
/** Path relative to the source root, for example `/guide/intro.md`. */
|
|
15
17
|
sourcePath: string
|
|
18
|
+
/**
|
|
19
|
+
* Source path after `rewrites`, for example `/intro.md`; the same as
|
|
20
|
+
* `sourcePath` when no rule moves the page.
|
|
21
|
+
*/
|
|
22
|
+
pagePath: string
|
|
16
23
|
/** Public route path of the page, for example `/guide/intro`. */
|
|
17
24
|
routePath: string
|
|
18
25
|
}
|
|
@@ -27,6 +34,13 @@ export interface MarkdownPageScope {
|
|
|
27
34
|
* the site has no public directory.
|
|
28
35
|
*/
|
|
29
36
|
publicDir?: string | undefined
|
|
37
|
+
/**
|
|
38
|
+
* Glob patterns, relative to `sourceRoot`, of Markdown files that are not
|
|
39
|
+
* pages, that is the resolved `srcExclude`. Leave it out to keep every file.
|
|
40
|
+
*/
|
|
41
|
+
exclude?: readonly string[] | undefined
|
|
42
|
+
/** `rewrites` of the site config, which moves pages to other routes. */
|
|
43
|
+
rewrites?: PageRewrites | undefined
|
|
30
44
|
}
|
|
31
45
|
|
|
32
46
|
/**
|
|
@@ -38,27 +52,52 @@ export async function listMarkdownPages(
|
|
|
38
52
|
scope: MarkdownPageScope,
|
|
39
53
|
): Promise<MarkdownPage[]> {
|
|
40
54
|
const sourceRoot = resolve(scope.sourceRoot)
|
|
55
|
+
const isExcluded = createExcludeMatcher(sourceRoot, scope.exclude)
|
|
56
|
+
const rewrite = resolvePagePathRewriter(scope.rewrites)
|
|
41
57
|
const files = await findMarkdownFiles(sourceRoot, resolvePublicDirectory(scope))
|
|
42
|
-
|
|
58
|
+
const pages = files.filter(file => !isExcluded(file)).map((file) => {
|
|
43
59
|
const sourcePath = markdownFileToSourcePath(file, sourceRoot)
|
|
44
|
-
|
|
60
|
+
const pagePath = rewrite(sourcePath)
|
|
61
|
+
return { file, sourcePath, pagePath, routePath: markdownPagePathToRoutePath(pagePath) }
|
|
45
62
|
})
|
|
63
|
+
assertUniqueRoutes(pages)
|
|
64
|
+
return pages
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Two files on one route would make one of them unreachable, so it is an error. */
|
|
68
|
+
function assertUniqueRoutes(pages: readonly MarkdownPage[]): void {
|
|
69
|
+
const owners = new Map<string, string>()
|
|
70
|
+
for (const page of pages) {
|
|
71
|
+
const owner = owners.get(page.routePath)
|
|
72
|
+
if (owner) {
|
|
73
|
+
throw new Error(
|
|
74
|
+
`KawaPress: ${JSON.stringify(owner)} and ${JSON.stringify(page.sourcePath)} both become the route ${JSON.stringify(page.routePath)}. Check rewrites in kawapress.config.ts.`,
|
|
75
|
+
)
|
|
76
|
+
}
|
|
77
|
+
owners.set(page.routePath, page.sourcePath)
|
|
78
|
+
}
|
|
46
79
|
}
|
|
47
80
|
|
|
48
81
|
/** Whether `file` is a page of this scope, without reading any directory. */
|
|
49
82
|
export function isMarkdownPage(file: string, scope: MarkdownPageScope): boolean {
|
|
50
|
-
|
|
83
|
+
const sourceRoot = resolve(scope.sourceRoot)
|
|
84
|
+
return isMarkdownPageFile(file, sourceRoot, resolvePublicDirectory(scope))
|
|
85
|
+
&& !createExcludeMatcher(sourceRoot, scope.exclude)(file)
|
|
51
86
|
}
|
|
52
87
|
|
|
53
88
|
/**
|
|
54
|
-
*
|
|
55
|
-
*
|
|
89
|
+
* Whether a file matches one of the exclude patterns. Patterns use the glob
|
|
90
|
+
* syntax of `picomatch` and are matched against the path below `sourceRoot`.
|
|
56
91
|
*/
|
|
57
|
-
|
|
58
|
-
file: string,
|
|
92
|
+
function createExcludeMatcher(
|
|
59
93
|
sourceRoot: string,
|
|
60
|
-
|
|
61
|
-
|
|
94
|
+
patterns: readonly string[] | undefined,
|
|
95
|
+
): (file: string) => boolean {
|
|
96
|
+
if (!patterns?.length) {
|
|
97
|
+
return () => false
|
|
98
|
+
}
|
|
99
|
+
const matches = picomatch([...patterns], { dot: true })
|
|
100
|
+
return file => matches(relative(sourceRoot, file).split(sep).join('/'))
|
|
62
101
|
}
|
|
63
102
|
|
|
64
103
|
async function findMarkdownFiles(
|