@miragon/slidev-validator 0.1.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 +167 -0
- package/bin/index.mjs +178 -0
- package/package.json +50 -0
- package/src/config.mjs +186 -0
- package/src/engine.mjs +115 -0
- package/src/glob.mjs +47 -0
- package/src/helpers.mjs +113 -0
- package/src/index.mjs +59 -0
- package/src/presets/recommended.mjs +12 -0
- package/src/presets/required.mjs +13 -0
- package/src/rendered-runner.mjs +106 -0
- package/src/report.mjs +87 -0
- package/src/rules/index.mjs +58 -0
- package/src/rules/rendered/card-white.mjs +13 -0
- package/src/rules/rendered/element-overflow.mjs +28 -0
- package/src/rules/rendered/heading-black.mjs +13 -0
- package/src/rules/rendered/measure.mjs +76 -0
- package/src/rules/rendered/no-em-dash.mjs +11 -0
- package/src/rules/rendered/no-emoji.mjs +11 -0
- package/src/rules/rendered/no-inline-font.mjs +13 -0
- package/src/rules/rendered/no-nested-bullets.mjs +13 -0
- package/src/rules/rendered/no-restyled-bullets.mjs +13 -0
- package/src/rules/rendered/overlay-safe-area.mjs +43 -0
- package/src/rules/rendered/safe-area/config.mjs +38 -0
- package/src/rules/rendered/safe-area/measure.mjs +93 -0
- package/src/rules/rendered/safe-area/model.mjs +179 -0
- package/src/rules/source/content-heading.mjs +93 -0
- package/src/rules/source/excalidraw-built-transparent.mjs +43 -0
- package/src/rules/source/excalidraw-committed-light.mjs +35 -0
- package/src/rules/source/no-html-entities.mjs +28 -0
- package/src/rules/source/no-raw-html.mjs +48 -0
- package/src/rules/source/sanctioned-layout.mjs +78 -0
- package/src/slide-files.mjs +25 -0
- package/src/versions.mjs +81 -0
package/README.md
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
# @miragon/slidev-validator
|
|
2
|
+
|
|
3
|
+
The design-system linter for Miragon Slidev decks. It replaces the old
|
|
4
|
+
statically-copied `verify/` folder with a **versioned, configurable** package, so
|
|
5
|
+
central guardrail improvements reach existing decks over a controlled `npm update`
|
|
6
|
+
instead of a frozen copy — and each deck can tune or scope rules without forking.
|
|
7
|
+
|
|
8
|
+
It runs two kinds of checks:
|
|
9
|
+
|
|
10
|
+
- **Source rules** — static checks over the deck's Markdown and `.excalidraw.svg`
|
|
11
|
+
sources. Fast, no browser; this is what CI runs.
|
|
12
|
+
- **Rendered rules** — measured on the live, fully-revealed slide in headless
|
|
13
|
+
Chromium (booted against a Slidev dev server). Catches overflow, colour and
|
|
14
|
+
typography issues the source can't see.
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
Added automatically to decks scaffolded with `npm create @miragon/slidev-deck`.
|
|
19
|
+
To adopt it in an existing deck, see [MIGRATION.md](../../MIGRATION.md).
|
|
20
|
+
|
|
21
|
+
```jsonc
|
|
22
|
+
// package.json
|
|
23
|
+
{
|
|
24
|
+
"devDependencies": { "@miragon/slidev-validator": "0.1.0" },
|
|
25
|
+
"scripts": {
|
|
26
|
+
"verify": "slidev-validator --rendered",
|
|
27
|
+
"verify:source": "slidev-validator"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
It declares a peer dependency on `@miragon/slidev-toolkit` (`>=1.15.0 <2`) — the
|
|
33
|
+
theme whose layouts and brand invariants the rules enforce.
|
|
34
|
+
|
|
35
|
+
## CLI
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
slidev-validator # source rules only (fast, CI-safe)
|
|
39
|
+
slidev-validator --rendered # also boot Slidev + Chromium and run rendered rules
|
|
40
|
+
slidev-validator --format json # machine-readable report
|
|
41
|
+
slidev-validator --pages "4-6" # limit the rendered run to some slides
|
|
42
|
+
slidev-validator --port 3030 # dev-server port for the rendered run
|
|
43
|
+
slidev-validator --config <path> # use a specific config file
|
|
44
|
+
slidev-validator --max-warnings 0 # fail if there are any warnings
|
|
45
|
+
slidev-validator init # write a starter slidev-validator.config.mjs
|
|
46
|
+
slidev-validator migrate # adopt the validator in an existing deck
|
|
47
|
+
slidev-validator --version # print validator + resolved toolkit versions
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Exit code is non-zero on any **error**-severity violation, an **expired exception**,
|
|
51
|
+
a **config error**, or warnings above `--max-warnings`.
|
|
52
|
+
|
|
53
|
+
## Configuration
|
|
54
|
+
|
|
55
|
+
**The config file is optional.** With no `slidev-validator.config.mjs` present, the
|
|
56
|
+
validator behaves as if you had written `extends: ['@miragon/slidev-validator/recommended']`
|
|
57
|
+
— every rule runs at its default severity (the report notes `config (defaults:
|
|
58
|
+
extends recommended)`). Add a config file only to tune, scope, or waive rules.
|
|
59
|
+
|
|
60
|
+
`slidev-validator.config.mjs` in the deck root (checked in, versionable), shaped
|
|
61
|
+
like ESLint / tsconfig:
|
|
62
|
+
|
|
63
|
+
```js
|
|
64
|
+
export default {
|
|
65
|
+
extends: ['@miragon/slidev-validator/recommended'],
|
|
66
|
+
rules: {
|
|
67
|
+
'no-nested-bullets': 'warn',
|
|
68
|
+
'slide-number-overlap': 'off',
|
|
69
|
+
},
|
|
70
|
+
overrides: [
|
|
71
|
+
{ files: ['deck/chapter/99-legacy/**'], rules: { 'element-overflow': 'off' } },
|
|
72
|
+
],
|
|
73
|
+
exceptions: [
|
|
74
|
+
{ rule: 'card-white', files: ['deck/chapter/07-*/**'], reason: 'Customer logo tile', ticket: 'ABC-123', expires: '2026-12-31' },
|
|
75
|
+
],
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
- **Severities:** `off` | `warn` | `error`. `warn` reports but does not fail the
|
|
80
|
+
build; `error` fails.
|
|
81
|
+
- **`extends`** merges preset severity maps (in order) before your `rules` block.
|
|
82
|
+
- **`overrides`** apply severities to files matching a glob (`**`, `*`, `?`;
|
|
83
|
+
later match wins).
|
|
84
|
+
- **`exceptions`** deliberately suppress a rule's violations (optionally scoped to
|
|
85
|
+
files), always with a `reason`, and optional `ticket` / `expires`. Every
|
|
86
|
+
exception is listed in the report and in CI — a suppression is never silent. An
|
|
87
|
+
expired exception becomes an error.
|
|
88
|
+
|
|
89
|
+
### Required vs recommended, and the disable policy
|
|
90
|
+
|
|
91
|
+
Each rule has a category. Rules marked **required** encode non-negotiable brand
|
|
92
|
+
invariants and **cannot be set below `error`** via `rules`/`overrides` — the only
|
|
93
|
+
sanctioned way to suppress one is an `exceptions` entry (which is reported). This
|
|
94
|
+
prevents a deck from silently turning off a core guardrail. Recommended rules can
|
|
95
|
+
be freely set to `warn` or `off`.
|
|
96
|
+
|
|
97
|
+
Invalid rule ids, invalid severities, malformed globs, and malformed/expired
|
|
98
|
+
exceptions all fail loudly with a clear config error.
|
|
99
|
+
|
|
100
|
+
### The report distinguishes three states
|
|
101
|
+
|
|
102
|
+
- **Ran** — pass / warn / error.
|
|
103
|
+
- **Deliberately disabled** — `off`, or suppressed by a live exception (listed with
|
|
104
|
+
where + why).
|
|
105
|
+
- **Not run** — not applicable (rendered rules without `--rendered`; the built
|
|
106
|
+
Excalidraw check without a prior `npm run build`).
|
|
107
|
+
|
|
108
|
+
The header records the validator version, the resolved toolkit version, and the
|
|
109
|
+
config, so a report — and a CI log — is self-describing.
|
|
110
|
+
|
|
111
|
+
## Rules
|
|
112
|
+
|
|
113
|
+
| id | type | category | default |
|
|
114
|
+
|---|---|---|---|
|
|
115
|
+
| `sanctioned-layout` | source | required | error |
|
|
116
|
+
| `no-raw-html` | source | required | error |
|
|
117
|
+
| `no-html-entities` | source | required | error |
|
|
118
|
+
| `content-heading` | source | required | error |
|
|
119
|
+
| `excalidraw-committed-light` | source | recommended | error |
|
|
120
|
+
| `excalidraw-built-transparent` | source | recommended | warn |
|
|
121
|
+
| `element-overflow` | rendered | recommended | error |
|
|
122
|
+
| `no-em-dash` | rendered | required | error |
|
|
123
|
+
| `no-emoji` | rendered | required | error |
|
|
124
|
+
| `heading-black` | rendered | required | error |
|
|
125
|
+
| `card-white` | rendered | required | error |
|
|
126
|
+
| `no-inline-font` | rendered | recommended | error |
|
|
127
|
+
| `no-restyled-bullets` | rendered | recommended | error |
|
|
128
|
+
| `no-nested-bullets` | rendered | recommended | warn |
|
|
129
|
+
| `overlay-safe-area` | rendered | required | error |
|
|
130
|
+
|
|
131
|
+
`content-heading` has a per-slide opt-out (`allowMultilineHeading: true` in the
|
|
132
|
+
slide frontmatter). `overlay-safe-area` reads the toolkit-owned safe-area model and
|
|
133
|
+
cannot be switched off by a deck; its only escape hatch is a per-slide
|
|
134
|
+
`safeAreaExceptions` frontmatter entry (with a reason), which is reported.
|
|
135
|
+
|
|
136
|
+
Ids are the **stable public API** — config files reference them, so they change
|
|
137
|
+
only in a major version.
|
|
138
|
+
|
|
139
|
+
## Compatibility & versioning
|
|
140
|
+
|
|
141
|
+
The validator resolves the installed toolkit version and warns when it falls
|
|
142
|
+
outside the supported peer range; the report records both versions.
|
|
143
|
+
|
|
144
|
+
SemVer policy for rules:
|
|
145
|
+
|
|
146
|
+
- Adding a rule with default `warn` → **minor**.
|
|
147
|
+
- Promoting a rule to `error`, or adding a new `required` rule → **major**
|
|
148
|
+
(it can newly fail a previously-green deck).
|
|
149
|
+
|
|
150
|
+
Because the validator is an exact-pinned dependency, a rollback is just
|
|
151
|
+
`npm install @miragon/slidev-validator@<previous>`; the lockfile keeps CI
|
|
152
|
+
reproducible.
|
|
153
|
+
|
|
154
|
+
## Architecture
|
|
155
|
+
|
|
156
|
+
- `src/rules/` — one rule per file (`source/` + `rendered/`), listed in
|
|
157
|
+
`src/rules/index.mjs`. Source rules `check()` files; rendered rules `evaluate()`
|
|
158
|
+
the single per-slide DOM measurement (`rendered/measure.mjs`).
|
|
159
|
+
- `src/config.mjs` — loads + validates config, resolves severities, enforces the
|
|
160
|
+
required-rule policy.
|
|
161
|
+
- `src/engine.mjs` — classifies each violation (error / warn / suppressed /
|
|
162
|
+
expired / off / skipped).
|
|
163
|
+
- `src/rendered-runner.mjs` — boots/reuses Slidev, drives Chromium, measures.
|
|
164
|
+
- `bin/index.mjs` — the CLI.
|
|
165
|
+
|
|
166
|
+
Add a rule: create its file, list it in `src/rules/index.mjs`, document it here.
|
|
167
|
+
It becomes configurable and joins both presets automatically.
|
package/bin/index.mjs
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* The `slidev-validator` CLI — a thin wrapper over src/index.mjs `validate()`.
|
|
4
|
+
*
|
|
5
|
+
* slidev-validator run source rules (fast, no browser)
|
|
6
|
+
* slidev-validator --rendered also boot Slidev + Chromium and run rendered rules
|
|
7
|
+
* slidev-validator init write a starter slidev-validator.config.mjs
|
|
8
|
+
* slidev-validator migrate adopt the validator in an existing deck (config + scripts)
|
|
9
|
+
*
|
|
10
|
+
* Flags: --pages "4-6", --port 3030, --format json, --config <path>,
|
|
11
|
+
* --max-warnings <n>, -v/--version, -h/--help.
|
|
12
|
+
*
|
|
13
|
+
* Exit code is non-zero on any error-severity violation, an expired exception, a
|
|
14
|
+
* config error, or more warnings than --max-warnings (default: unlimited).
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { existsSync, readFileSync, writeFileSync } from 'node:fs'
|
|
18
|
+
import { join } from 'node:path'
|
|
19
|
+
import { parseArgs } from 'node:util'
|
|
20
|
+
import { validate, ConfigError, selfInfo, resolvedToolkitVersion } from '../src/index.mjs'
|
|
21
|
+
import { formatText, toJson } from '../src/report.mjs'
|
|
22
|
+
|
|
23
|
+
const STARTER_CONFIG = `// Miragon Slidev validator config. See @miragon/slidev-validator.
|
|
24
|
+
// Severities: 'off' | 'warn' | 'error'. Rules marked [required] cannot be lowered
|
|
25
|
+
// below 'error' here — suppress them deliberately via an 'exceptions' entry instead.
|
|
26
|
+
export default {
|
|
27
|
+
extends: ['@miragon/slidev-validator/recommended'],
|
|
28
|
+
rules: {
|
|
29
|
+
// 'no-nested-bullets': 'warn',
|
|
30
|
+
// 'slide-number-overlap': 'off',
|
|
31
|
+
},
|
|
32
|
+
overrides: [
|
|
33
|
+
// { files: ['deck/chapter/99-legacy/**'], rules: { 'element-overflow': 'off' } },
|
|
34
|
+
],
|
|
35
|
+
exceptions: [
|
|
36
|
+
// { rule: 'card-white', files: ['deck/chapter/07-*/**'], reason: 'Customer logo tile', ticket: 'ABC-123', expires: '2026-12-31' },
|
|
37
|
+
],
|
|
38
|
+
}
|
|
39
|
+
`
|
|
40
|
+
|
|
41
|
+
const USAGE = `Usage: slidev-validator [command] [options]
|
|
42
|
+
|
|
43
|
+
Commands:
|
|
44
|
+
(default) run the validator over ./deck (source rules)
|
|
45
|
+
init write a starter slidev-validator.config.mjs
|
|
46
|
+
migrate adopt the validator in an existing deck (config + scripts)
|
|
47
|
+
|
|
48
|
+
Options:
|
|
49
|
+
--rendered also run the rendered (browser) checks
|
|
50
|
+
--pages "4-6" limit the rendered run to some slides ("2,5,9" or "4-6")
|
|
51
|
+
--port <n> dev-server port for the rendered run (default 3030)
|
|
52
|
+
--format json machine-readable report
|
|
53
|
+
--config <path> use a specific config file
|
|
54
|
+
--max-warnings <n> fail if warnings exceed <n> (default: unlimited)
|
|
55
|
+
-v, --version print versions
|
|
56
|
+
-h, --help show this help`
|
|
57
|
+
|
|
58
|
+
function parse(argv) {
|
|
59
|
+
const { values, positionals } = parseArgs({
|
|
60
|
+
args: argv,
|
|
61
|
+
allowPositionals: true,
|
|
62
|
+
options: {
|
|
63
|
+
rendered: { type: 'boolean' },
|
|
64
|
+
pages: { type: 'string' },
|
|
65
|
+
port: { type: 'string' },
|
|
66
|
+
format: { type: 'string' },
|
|
67
|
+
config: { type: 'string' },
|
|
68
|
+
'max-warnings': { type: 'string' },
|
|
69
|
+
version: { type: 'boolean', short: 'v' },
|
|
70
|
+
help: { type: 'boolean', short: 'h' },
|
|
71
|
+
},
|
|
72
|
+
})
|
|
73
|
+
return { values, command: positionals[0] }
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function cmdInit() {
|
|
77
|
+
const target = join(process.cwd(), 'slidev-validator.config.mjs')
|
|
78
|
+
if (existsSync(target)) {
|
|
79
|
+
console.log('slidev-validator.config.mjs already exists — leaving it untouched.')
|
|
80
|
+
return 0
|
|
81
|
+
}
|
|
82
|
+
writeFileSync(target, STARTER_CONFIG)
|
|
83
|
+
console.log('Wrote slidev-validator.config.mjs (extends @miragon/slidev-validator/recommended).')
|
|
84
|
+
return 0
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Adopt the validator in an existing (statically-copied) deck: config + scripts. */
|
|
88
|
+
function cmdMigrate() {
|
|
89
|
+
cmdInit()
|
|
90
|
+
const pkgPath = join(process.cwd(), 'package.json')
|
|
91
|
+
if (!existsSync(pkgPath)) {
|
|
92
|
+
console.log('No package.json here — skipping script migration.')
|
|
93
|
+
return 0
|
|
94
|
+
}
|
|
95
|
+
const pkg = JSON.parse(readFileSync(pkgPath, 'utf8'))
|
|
96
|
+
const changes = []
|
|
97
|
+
pkg.scripts ??= {}
|
|
98
|
+
const want = { verify: 'slidev-validator --rendered', 'verify:source': 'slidev-validator', 'verify:ci': 'slidev-validator' }
|
|
99
|
+
for (const [k, v] of Object.entries(want)) {
|
|
100
|
+
if (pkg.scripts[k] !== v && (pkg.scripts[k]?.includes('playwright') || pkg.scripts[k]?.includes('verify/') || pkg.scripts[k] === undefined)) {
|
|
101
|
+
pkg.scripts[k] = v
|
|
102
|
+
changes.push(` scripts.${k} → ${v}`)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
for (const dep of ['@playwright/test', 'playwright-chromium']) {
|
|
106
|
+
if (pkg.devDependencies?.[dep]) {
|
|
107
|
+
delete pkg.devDependencies[dep]
|
|
108
|
+
changes.push(` removed devDependency ${dep} (now transitive via @miragon/slidev-validator)`)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (!pkg.devDependencies?.['@miragon/slidev-validator']) {
|
|
112
|
+
changes.push(' NOTE: add "@miragon/slidev-validator" to devDependencies (pin an exact version) and run npm install')
|
|
113
|
+
}
|
|
114
|
+
if (changes.length) {
|
|
115
|
+
writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
|
|
116
|
+
console.log('Updated package.json:')
|
|
117
|
+
console.log(changes.join('\n'))
|
|
118
|
+
}
|
|
119
|
+
if (existsSync(join(process.cwd(), 'verify'))) {
|
|
120
|
+
console.log('You can now delete the old, statically-copied verify/ folder — the validator supersedes it.')
|
|
121
|
+
}
|
|
122
|
+
return 0
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async function cmdRun(values) {
|
|
126
|
+
try {
|
|
127
|
+
const { results, summary, meta } = await validate({
|
|
128
|
+
rendered: !!values.rendered,
|
|
129
|
+
pages: values.pages,
|
|
130
|
+
port: values.port ? Number(values.port) : undefined,
|
|
131
|
+
configPath: values.config,
|
|
132
|
+
log: (m) => console.error(m),
|
|
133
|
+
})
|
|
134
|
+
if (values.format === 'json') {
|
|
135
|
+
console.log(JSON.stringify(toJson({ results, summary, meta }), null, 2))
|
|
136
|
+
} else {
|
|
137
|
+
console.log(formatText({ results, summary, meta }))
|
|
138
|
+
}
|
|
139
|
+
const maxWarnings = values['max-warnings'] !== undefined ? Number(values['max-warnings']) : Infinity
|
|
140
|
+
const warnFail = summary.warnCount > maxWarnings
|
|
141
|
+
if (warnFail) console.error(`\n${summary.warnCount} warning(s) exceed --max-warnings ${maxWarnings}.`)
|
|
142
|
+
return summary.failed || warnFail ? 1 : 0
|
|
143
|
+
} catch (err) {
|
|
144
|
+
if (err instanceof ConfigError) {
|
|
145
|
+
console.error(`Config error: ${err.message}`)
|
|
146
|
+
return 2
|
|
147
|
+
}
|
|
148
|
+
throw err
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async function main() {
|
|
153
|
+
const { values, command } = parse(process.argv.slice(2))
|
|
154
|
+
if (values.help) {
|
|
155
|
+
console.log(USAGE)
|
|
156
|
+
return 0
|
|
157
|
+
}
|
|
158
|
+
if (values.version) {
|
|
159
|
+
console.log(`@miragon/slidev-validator ${selfInfo().version}`)
|
|
160
|
+
console.log(`@miragon/slidev-toolkit ${resolvedToolkitVersion() ?? 'unresolved'}`)
|
|
161
|
+
return 0
|
|
162
|
+
}
|
|
163
|
+
if (command === 'init') return cmdInit()
|
|
164
|
+
if (command === 'migrate') return cmdMigrate()
|
|
165
|
+
if (command && command !== 'run') {
|
|
166
|
+
console.error(`Unknown command: ${command}\n`)
|
|
167
|
+
console.log(USAGE)
|
|
168
|
+
return 2
|
|
169
|
+
}
|
|
170
|
+
return cmdRun(values)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
main()
|
|
174
|
+
.then((code) => process.exit(code))
|
|
175
|
+
.catch((err) => {
|
|
176
|
+
console.error(err instanceof Error ? err.stack || err.message : err)
|
|
177
|
+
process.exit(1)
|
|
178
|
+
})
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@miragon/slidev-validator",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "The Miragon Slidev deck validator: a configurable design-system linter (stable rule IDs, off/warn/error severities, extends/overrides/exceptions) shipped as a versioned CLI so central guardrail improvements reach decks over npm instead of a frozen copy.",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"slidev",
|
|
8
|
+
"miragon",
|
|
9
|
+
"validator",
|
|
10
|
+
"linter",
|
|
11
|
+
"design-system"
|
|
12
|
+
],
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/Miragon/slidev-deck-template.git",
|
|
17
|
+
"directory": "packages/validator"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "https://github.com/Miragon/slidev-deck-template/tree/main/packages/validator#readme",
|
|
20
|
+
"bugs": "https://github.com/Miragon/slidev-deck-template/issues",
|
|
21
|
+
"bin": {
|
|
22
|
+
"slidev-validator": "bin/index.mjs"
|
|
23
|
+
},
|
|
24
|
+
"exports": {
|
|
25
|
+
".": "./src/index.mjs",
|
|
26
|
+
"./recommended": "./src/presets/recommended.mjs",
|
|
27
|
+
"./required": "./src/presets/required.mjs"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"test": "node --test"
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=20"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@slidev/parser": "52.18.0",
|
|
37
|
+
"playwright-chromium": "1.62.1"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"@miragon/slidev-toolkit": ">=1.15.0 <2"
|
|
41
|
+
},
|
|
42
|
+
"files": [
|
|
43
|
+
"bin",
|
|
44
|
+
"src"
|
|
45
|
+
],
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public",
|
|
48
|
+
"provenance": true
|
|
49
|
+
}
|
|
50
|
+
}
|
package/src/config.mjs
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The configuration model — ESLint/tsconfig-shaped.
|
|
3
|
+
*
|
|
4
|
+
* export default {
|
|
5
|
+
* extends: ['@miragon/slidev-validator/recommended'],
|
|
6
|
+
* rules: { 'no-em-dash': 'error', 'slide-number-overlap': 'off' },
|
|
7
|
+
* overrides: [{ files: ['deck/chapter/99-legacy/**'], rules: { 'element-overflow': 'off' } }],
|
|
8
|
+
* exceptions: [{ rule: 'card-white', files: ['deck/chapter/07-*\/**'], reason: '…', ticket: 'ABC-1', expires: '2026-12-31' }],
|
|
9
|
+
* }
|
|
10
|
+
*
|
|
11
|
+
* Severities: 'off' | 'warn' | 'error'. Rules marked `required` in the registry
|
|
12
|
+
* cannot be set below 'error' here — the only sanctioned way to suppress them is an
|
|
13
|
+
* `exceptions` entry (with a reason), which is always surfaced in the report and CI.
|
|
14
|
+
* Invalid rule ids, severities, globs, and expired/ malformed exceptions fail loudly
|
|
15
|
+
* with a ConfigError.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { existsSync } from 'node:fs'
|
|
19
|
+
import { join, resolve, dirname } from 'node:path'
|
|
20
|
+
import { pathToFileURL } from 'node:url'
|
|
21
|
+
import { knownRuleIds, ruleById } from './rules/index.mjs'
|
|
22
|
+
import { globToRegExp, matchesAny } from './glob.mjs'
|
|
23
|
+
import { repoRoot } from './helpers.mjs'
|
|
24
|
+
|
|
25
|
+
export class ConfigError extends Error {}
|
|
26
|
+
|
|
27
|
+
const SEVERITIES = new Set(['off', 'warn', 'error'])
|
|
28
|
+
const CONFIG_NAMES = ['slidev-validator.config.mjs', 'slidev-validator.config.js']
|
|
29
|
+
|
|
30
|
+
/** Locate the deck's config file (explicit path wins), or null if none exists. */
|
|
31
|
+
export function findConfigFile(explicit) {
|
|
32
|
+
if (explicit) {
|
|
33
|
+
const p = resolve(explicit)
|
|
34
|
+
if (!existsSync(p)) throw new ConfigError(`Config file not found: ${explicit}`)
|
|
35
|
+
return p
|
|
36
|
+
}
|
|
37
|
+
for (const name of CONFIG_NAMES) {
|
|
38
|
+
const p = join(repoRoot(), name)
|
|
39
|
+
if (existsSync(p)) return p
|
|
40
|
+
}
|
|
41
|
+
return null
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** A preset/extends module resolved to its `{ rules }` map. */
|
|
45
|
+
async function importExtends(spec, configDir) {
|
|
46
|
+
if (spec === '@miragon/slidev-validator/recommended') return (await import('./presets/recommended.mjs')).default
|
|
47
|
+
if (spec === '@miragon/slidev-validator/required') return (await import('./presets/required.mjs')).default
|
|
48
|
+
const url = spec.startsWith('.') ? pathToFileURL(resolve(configDir, spec)).href : spec
|
|
49
|
+
let mod
|
|
50
|
+
try {
|
|
51
|
+
mod = await import(url)
|
|
52
|
+
} catch (err) {
|
|
53
|
+
throw new ConfigError(`Could not resolve extends "${spec}": ${err.message}`)
|
|
54
|
+
}
|
|
55
|
+
const preset = mod.default ?? mod
|
|
56
|
+
if (!preset || typeof preset.rules !== 'object') {
|
|
57
|
+
throw new ConfigError(`extends "${spec}" does not export a { rules } object`)
|
|
58
|
+
}
|
|
59
|
+
return preset
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function assertSeverity(sev, where) {
|
|
63
|
+
if (!SEVERITIES.has(sev)) {
|
|
64
|
+
throw new ConfigError(`Invalid severity ${JSON.stringify(sev)} for ${where}. Use 'off', 'warn', or 'error'.`)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function assertKnownRule(id, where) {
|
|
69
|
+
if (!knownRuleIds().has(id)) {
|
|
70
|
+
throw new ConfigError(`Unknown rule id "${id}" in ${where}. It is not a known validator rule.`)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function assertGlobs(globs, where) {
|
|
75
|
+
if (!Array.isArray(globs) || !globs.length) throw new ConfigError(`${where} needs a non-empty "files" array of globs.`)
|
|
76
|
+
for (const g of globs) {
|
|
77
|
+
if (typeof g !== 'string') throw new ConfigError(`${where} has a non-string glob: ${JSON.stringify(g)}`)
|
|
78
|
+
try {
|
|
79
|
+
globToRegExp(g)
|
|
80
|
+
} catch (err) {
|
|
81
|
+
throw new ConfigError(`${where} has an invalid glob "${g}": ${err.message}`)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Load and fully validate the deck's config. Returns a resolved object plus a
|
|
88
|
+
* `severityFor(ruleId, relFile)` resolver and the list of exceptions. Applies the
|
|
89
|
+
* default (extends recommended) when the deck ships no config file.
|
|
90
|
+
*/
|
|
91
|
+
export async function loadConfig(explicitPath) {
|
|
92
|
+
const configPath = findConfigFile(explicitPath)
|
|
93
|
+
let raw
|
|
94
|
+
if (configPath) {
|
|
95
|
+
const mod = await import(pathToFileURL(configPath).href)
|
|
96
|
+
raw = mod.default ?? mod
|
|
97
|
+
if (typeof raw !== 'object' || raw === null) throw new ConfigError(`${configPath} must default-export a config object.`)
|
|
98
|
+
} else {
|
|
99
|
+
raw = { extends: ['@miragon/slidev-validator/recommended'] }
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const configDir = configPath ? dirname(configPath) : repoRoot()
|
|
103
|
+
const extendsList = raw.extends ?? []
|
|
104
|
+
if (!Array.isArray(extendsList)) throw new ConfigError('"extends" must be an array of preset specifiers.')
|
|
105
|
+
|
|
106
|
+
// 1. Base severities from extends (in order), then the top-level rules block.
|
|
107
|
+
const base = {}
|
|
108
|
+
const presetNames = []
|
|
109
|
+
for (const spec of extendsList) {
|
|
110
|
+
const preset = await importExtends(spec, configDir)
|
|
111
|
+
presetNames.push(preset.name ?? spec)
|
|
112
|
+
for (const [id, sev] of Object.entries(preset.rules)) base[id] = sev
|
|
113
|
+
}
|
|
114
|
+
const userRules = raw.rules ?? {}
|
|
115
|
+
if (typeof userRules !== 'object') throw new ConfigError('"rules" must be an object of ruleId → severity.')
|
|
116
|
+
for (const [id, sev] of Object.entries(userRules)) {
|
|
117
|
+
assertKnownRule(id, '"rules"')
|
|
118
|
+
assertSeverity(sev, `rule "${id}"`)
|
|
119
|
+
base[id] = sev
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// 2. Overrides — validated, kept in order (later match wins).
|
|
123
|
+
const overrides = []
|
|
124
|
+
const rawOverrides = raw.overrides ?? []
|
|
125
|
+
if (!Array.isArray(rawOverrides)) throw new ConfigError('"overrides" must be an array.')
|
|
126
|
+
rawOverrides.forEach((ov, i) => {
|
|
127
|
+
assertGlobs(ov.files, `overrides[${i}]`)
|
|
128
|
+
const rules = ov.rules ?? {}
|
|
129
|
+
for (const [id, sev] of Object.entries(rules)) {
|
|
130
|
+
assertKnownRule(id, `overrides[${i}].rules`)
|
|
131
|
+
assertSeverity(sev, `overrides[${i}].rules["${id}"]`)
|
|
132
|
+
}
|
|
133
|
+
overrides.push({ globs: ov.files, rules })
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
// 3. Exceptions — validated; each carries a reason and is reported.
|
|
137
|
+
const exceptions = []
|
|
138
|
+
const rawExceptions = raw.exceptions ?? []
|
|
139
|
+
if (!Array.isArray(rawExceptions)) throw new ConfigError('"exceptions" must be an array.')
|
|
140
|
+
rawExceptions.forEach((ex, i) => {
|
|
141
|
+
assertKnownRule(ex.rule, `exceptions[${i}].rule`)
|
|
142
|
+
if (!ex.reason || typeof ex.reason !== 'string') throw new ConfigError(`exceptions[${i}] for "${ex.rule}" needs a non-empty "reason".`)
|
|
143
|
+
if (ex.files !== undefined) assertGlobs(ex.files, `exceptions[${i}]`)
|
|
144
|
+
let expired = false
|
|
145
|
+
if (ex.expires !== undefined) {
|
|
146
|
+
const t = Date.parse(ex.expires)
|
|
147
|
+
if (Number.isNaN(t)) throw new ConfigError(`exceptions[${i}].expires "${ex.expires}" is not a valid date (use YYYY-MM-DD).`)
|
|
148
|
+
expired = t < Date.now()
|
|
149
|
+
}
|
|
150
|
+
exceptions.push({ rule: ex.rule, globs: ex.files ?? null, reason: ex.reason, ticket: ex.ticket, expires: ex.expires, expired })
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
// 4. Required-rule guard: a required rule may not be dropped below 'error' via
|
|
154
|
+
// rules/overrides. The sanctioned escape is an exceptions entry (reported).
|
|
155
|
+
const requiredIds = [...knownRuleIds()].filter((id) => ruleById(id).meta.category === 'required')
|
|
156
|
+
for (const id of requiredIds) {
|
|
157
|
+
if (base[id] !== undefined && base[id] !== 'error') {
|
|
158
|
+
throw new ConfigError(`Rule "${id}" is REQUIRED and cannot be set to "${base[id]}". To suppress it deliberately, add an { rule: "${id}", reason: … } entry to "exceptions" (which is reported in CI) instead of lowering its severity.`)
|
|
159
|
+
}
|
|
160
|
+
for (const ov of overrides) {
|
|
161
|
+
if (ov.rules[id] !== undefined && ov.rules[id] !== 'error') {
|
|
162
|
+
throw new ConfigError(`Rule "${id}" is REQUIRED and cannot be lowered to "${ov.rules[id]}" in an override. Use an "exceptions" entry (reported in CI) instead.`)
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Effective severity of a rule for a given repo-relative file. */
|
|
168
|
+
function severityFor(ruleId, relFile) {
|
|
169
|
+
let sev = base[ruleId] ?? 'off'
|
|
170
|
+
if (relFile) {
|
|
171
|
+
for (const ov of overrides) {
|
|
172
|
+
if (ov.rules[ruleId] !== undefined && matchesAny(relFile, ov.globs)) sev = ov.rules[ruleId]
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return sev
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** The matching exception for a (rule, file), or null. A fileless exception is global. */
|
|
179
|
+
function exceptionFor(ruleId, relFile) {
|
|
180
|
+
return (
|
|
181
|
+
exceptions.find((ex) => ex.rule === ruleId && (ex.globs === null || (relFile && matchesAny(relFile, ex.globs)))) ?? null
|
|
182
|
+
)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return { configPath, presetNames, base, overrides, exceptions, severityFor, exceptionFor, raw }
|
|
186
|
+
}
|