@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/src/engine.mjs
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The engine: run rules and classify every violation against the resolved config.
|
|
3
|
+
*
|
|
4
|
+
* It is deliberately framework-agnostic. Source rules produce their violations by
|
|
5
|
+
* reading files; rendered rules interpret a per-slide DOM measurement supplied by
|
|
6
|
+
* the runner. Both feed the SAME classifier, so severity, overrides, and exceptions
|
|
7
|
+
* behave identically for either kind. A rule outcome is either an array of
|
|
8
|
+
* violations ([] = pass) or `{ skipped: reason }` (not applicable — reported apart
|
|
9
|
+
* from "ran and passed").
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** Is a rule active anywhere (base severity or any override lifts it above 'off')? */
|
|
13
|
+
export function isActive(config, ruleId) {
|
|
14
|
+
const base = config.base[ruleId] ?? 'off'
|
|
15
|
+
if (base !== 'off') return true
|
|
16
|
+
return config.overrides.some((ov) => ov.rules[ruleId] && ov.rules[ruleId] !== 'off')
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Classify one rule's outcome into { status, errors, warns, suppressed, expired }.
|
|
21
|
+
* status: 'pass' | 'warn' | 'error' | 'off' | 'skipped'
|
|
22
|
+
* suppressed: violations silenced by an `off` severity or a live exception
|
|
23
|
+
* expired: violations whose covering exception has lapsed (these fail)
|
|
24
|
+
*/
|
|
25
|
+
export function classify(config, rule, outcome) {
|
|
26
|
+
if (outcome && outcome.skipped) return { rule, status: 'skipped', reason: outcome.skipped, errors: [], warns: [], suppressed: [], expired: [] }
|
|
27
|
+
const violations = Array.isArray(outcome) ? outcome : []
|
|
28
|
+
const active = isActive(config, rule.id)
|
|
29
|
+
|
|
30
|
+
const errors = []
|
|
31
|
+
const warns = []
|
|
32
|
+
const suppressed = []
|
|
33
|
+
const expired = []
|
|
34
|
+
for (const v of violations) {
|
|
35
|
+
const ex = config.exceptionFor(rule.id, v.file)
|
|
36
|
+
if (ex) {
|
|
37
|
+
if (ex.expired) expired.push({ v, ex })
|
|
38
|
+
else suppressed.push({ v, ex })
|
|
39
|
+
continue
|
|
40
|
+
}
|
|
41
|
+
const sev = config.severityFor(rule.id, v.file)
|
|
42
|
+
if (sev === 'off') suppressed.push({ v, ex: null })
|
|
43
|
+
else if (sev === 'warn') warns.push(v)
|
|
44
|
+
else errors.push(v)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let status = 'pass'
|
|
48
|
+
if (errors.length || expired.length) status = 'error'
|
|
49
|
+
else if (warns.length) status = 'warn'
|
|
50
|
+
else if (!active && !suppressed.length) status = 'off'
|
|
51
|
+
return { rule, status, errors, warns, suppressed, expired }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Run the source rules that are active (or referenced by an exception) and classify them. */
|
|
55
|
+
export function runSourceRules(rules, config) {
|
|
56
|
+
return rules.map((rule) => {
|
|
57
|
+
const referenced = config.exceptions.some((e) => e.rule === rule.id)
|
|
58
|
+
if (!isActive(config, rule.id) && !referenced) {
|
|
59
|
+
return { rule, status: 'off', errors: [], warns: [], suppressed: [], expired: [] }
|
|
60
|
+
}
|
|
61
|
+
let outcome
|
|
62
|
+
try {
|
|
63
|
+
outcome = rule.check()
|
|
64
|
+
} catch (err) {
|
|
65
|
+
outcome = [{ file: undefined, message: `rule threw: ${err.message}` }]
|
|
66
|
+
}
|
|
67
|
+
return classify(config, rule, outcome)
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Classify rendered rules from measured slides. `measured` is
|
|
73
|
+
* [{ slide, file, metrics }]. A null `measured` means rendered checks did not run
|
|
74
|
+
* (no --rendered): every rendered rule is reported as skipped/not-applicable.
|
|
75
|
+
*/
|
|
76
|
+
export function runRenderedRules(rules, config, measured) {
|
|
77
|
+
return rules.map((rule) => {
|
|
78
|
+
if (measured === null) return { rule, status: 'skipped', reason: 'rendered checks not run (pass --rendered)', errors: [], warns: [], suppressed: [], expired: [] }
|
|
79
|
+
const referenced = config.exceptions.some((e) => e.rule === rule.id)
|
|
80
|
+
if (!isActive(config, rule.id) && !referenced) {
|
|
81
|
+
return { rule, status: 'off', errors: [], warns: [], suppressed: [], expired: [] }
|
|
82
|
+
}
|
|
83
|
+
// A rendered rule consumes either the generic DOM metrics (default) or the
|
|
84
|
+
// dedicated safe-area measurement.
|
|
85
|
+
const pick = rule.consumes === 'safeArea' ? (m) => m.safeArea : (m) => m.metrics
|
|
86
|
+
const violations = []
|
|
87
|
+
for (const m of measured) {
|
|
88
|
+
const data = pick(m)
|
|
89
|
+
if (!data) continue
|
|
90
|
+
for (const v of rule.evaluate(data, { slide: m.slide, file: m.file })) violations.push(v)
|
|
91
|
+
}
|
|
92
|
+
return classify(config, rule, violations)
|
|
93
|
+
})
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Roll results up into totals + a pass/fail verdict. */
|
|
97
|
+
export function summarize(results) {
|
|
98
|
+
let errorCount = 0
|
|
99
|
+
let warnCount = 0
|
|
100
|
+
let suppressedCount = 0
|
|
101
|
+
let expiredCount = 0
|
|
102
|
+
for (const r of results) {
|
|
103
|
+
errorCount += r.errors.length
|
|
104
|
+
warnCount += r.warns.length
|
|
105
|
+
suppressedCount += r.suppressed.length
|
|
106
|
+
expiredCount += r.expired.length
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
errorCount,
|
|
110
|
+
warnCount,
|
|
111
|
+
suppressedCount,
|
|
112
|
+
expiredCount,
|
|
113
|
+
failed: results.some((r) => r.status === 'error'),
|
|
114
|
+
}
|
|
115
|
+
}
|
package/src/glob.mjs
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A tiny, dependency-free glob matcher for `overrides`/`exceptions` file
|
|
3
|
+
* selectors. Supports the subset a deck config needs:
|
|
4
|
+
* ** any run of characters including `/`
|
|
5
|
+
* * any run of characters except `/`
|
|
6
|
+
* ? a single character except `/`
|
|
7
|
+
* Everything else is matched literally. Paths are compared repo-relative with
|
|
8
|
+
* forward slashes. A bare directory glob like `deck/chapter/99-legacy/` also
|
|
9
|
+
* matches everything beneath it, so authors can omit the trailing `**`.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** Compile one glob to a RegExp. Throws on an obviously malformed pattern. */
|
|
13
|
+
export function globToRegExp(glob) {
|
|
14
|
+
if (typeof glob !== 'string' || !glob.length) {
|
|
15
|
+
throw new Error(`Invalid file glob: ${JSON.stringify(glob)}`)
|
|
16
|
+
}
|
|
17
|
+
// A trailing "/" means "this directory and everything under it".
|
|
18
|
+
let g = glob.endsWith('/') ? `${glob}**` : glob
|
|
19
|
+
let re = '^'
|
|
20
|
+
for (let i = 0; i < g.length; i++) {
|
|
21
|
+
const c = g[i]
|
|
22
|
+
if (c === '*') {
|
|
23
|
+
if (g[i + 1] === '*') {
|
|
24
|
+
re += '.*'
|
|
25
|
+
i++
|
|
26
|
+
// consume a following slash so `**/x` also matches `x` at the root
|
|
27
|
+
if (g[i + 1] === '/') i++
|
|
28
|
+
} else {
|
|
29
|
+
re += '[^/]*'
|
|
30
|
+
}
|
|
31
|
+
} else if (c === '?') {
|
|
32
|
+
re += '[^/]'
|
|
33
|
+
} else if ('\\^$.|+()[]{}'.includes(c)) {
|
|
34
|
+
re += `\\${c}`
|
|
35
|
+
} else {
|
|
36
|
+
re += c
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
re += '$'
|
|
40
|
+
return new RegExp(re)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Does `relPath` (repo-relative, forward slashes) match any of the globs? */
|
|
44
|
+
export function matchesAny(relPath, globs) {
|
|
45
|
+
const p = String(relPath).split('\\').join('/')
|
|
46
|
+
return globs.some((g) => globToRegExp(g).test(p))
|
|
47
|
+
}
|
package/src/helpers.mjs
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared, Node-side helpers for the validator.
|
|
3
|
+
*
|
|
4
|
+
* The validator runs against the deck in the CURRENT WORKING DIRECTORY: in the
|
|
5
|
+
* template monorepo that is the repo root; in a scaffolded deck it is the deck
|
|
6
|
+
* root. Both have the same `deck/` layout (entry + one folder per chapter), so a
|
|
7
|
+
* cwd-relative root works in both. These are functions, not module-level
|
|
8
|
+
* constants, so tests can `process.chdir()` into a fixture between calls.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { readdirSync, existsSync } from 'node:fs'
|
|
12
|
+
import { join, relative } from 'node:path'
|
|
13
|
+
|
|
14
|
+
/** A file path made repo-relative with forward slashes (for display + glob match). */
|
|
15
|
+
export function toRel(file) {
|
|
16
|
+
return relative(repoRoot(), file).split('\\').join('/')
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** The deck root the validator operates on: the current working directory. */
|
|
20
|
+
export function repoRoot() {
|
|
21
|
+
return process.env.SLIDEV_VALIDATOR_ROOT || process.cwd()
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The single deck entry point. */
|
|
25
|
+
export function deckEntry() {
|
|
26
|
+
return join(repoRoot(), 'deck', 'slides.md')
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Each chapter is a folder deck/chapter/<chapter>/ with <chapter>.md + resources/. */
|
|
30
|
+
export function chapterDir() {
|
|
31
|
+
return join(repoRoot(), 'deck', 'chapter')
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The slide source files: the entry + one <chapter>.md per chapter folder. */
|
|
35
|
+
export function slideSourceFiles() {
|
|
36
|
+
const files = []
|
|
37
|
+
const entry = deckEntry()
|
|
38
|
+
if (existsSync(entry)) files.push(entry)
|
|
39
|
+
const dir = chapterDir()
|
|
40
|
+
if (existsSync(dir)) {
|
|
41
|
+
for (const ch of readdirSync(dir)) {
|
|
42
|
+
const md = join(dir, ch, `${ch}.md`)
|
|
43
|
+
if (existsSync(md)) files.push(md)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return files
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Every chapter's exported Excalidraw diagrams (committed sources). */
|
|
50
|
+
export function excalidrawSvgFiles() {
|
|
51
|
+
const out = []
|
|
52
|
+
const dir = chapterDir()
|
|
53
|
+
if (!existsSync(dir)) return out
|
|
54
|
+
for (const ch of readdirSync(dir)) {
|
|
55
|
+
const resDir = join(dir, ch, 'resources')
|
|
56
|
+
if (!existsSync(resDir)) continue
|
|
57
|
+
for (const f of readdirSync(resDir)) {
|
|
58
|
+
if (f.endsWith('.excalidraw.svg')) out.push(join(resDir, f))
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return out
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The Slidev canvas is a fixed 16:9 box. Anything rendering past it overflows. */
|
|
65
|
+
export const CANVAS = { width: 980, height: 552 }
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Sub-pixel slack: layout/scroll measurements jitter by ~1px. Real overflow
|
|
69
|
+
* (an extra bullet, a too-wide card) is tens of pixels, so this never masks it.
|
|
70
|
+
*/
|
|
71
|
+
export const TOLERANCE = 2
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Content must clear the bottom of the canvas by at least this many px. Fitting
|
|
75
|
+
* is not enough: content jammed against the floor reads as cut off. The static
|
|
76
|
+
* layouts keep a 24-32px bottom margin by design; 16px sits below that healthy
|
|
77
|
+
* floor but well above the few-px gap a too-tall slide leaves.
|
|
78
|
+
*/
|
|
79
|
+
export const MIN_BOTTOM_MARGIN = 16
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Parse a page selection like "4-10", "4,5,9", or a mix "4,5,9-11" into a sorted,
|
|
83
|
+
* de-duplicated list of 1-based slide numbers, clamped to the deck's slide count.
|
|
84
|
+
* An empty/undefined spec means "every slide". Throws on a malformed segment or a
|
|
85
|
+
* selection entirely outside 1..total, so a typo fails loudly.
|
|
86
|
+
*/
|
|
87
|
+
export function parsePages(spec, total) {
|
|
88
|
+
if (!spec || !spec.trim()) return Array.from({ length: total }, (_, i) => i + 1)
|
|
89
|
+
const out = new Set()
|
|
90
|
+
for (const raw of spec.split(',')) {
|
|
91
|
+
const seg = raw.trim()
|
|
92
|
+
if (!seg) continue
|
|
93
|
+
const range = seg.match(/^(\d+)\s*-\s*(\d+)$/)
|
|
94
|
+
if (range) {
|
|
95
|
+
let a = Number(range[1])
|
|
96
|
+
let b = Number(range[2])
|
|
97
|
+
if (a > b) [a, b] = [b, a]
|
|
98
|
+
for (let n = a; n <= b; n++) out.add(n)
|
|
99
|
+
} else if (/^\d+$/.test(seg)) {
|
|
100
|
+
out.add(Number(seg))
|
|
101
|
+
} else {
|
|
102
|
+
throw new Error(`Invalid page segment "${seg}". Use e.g. "4-10", "4,5,9", or "4,5,9-11".`)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
const pages = [...out].filter((n) => n >= 1 && n <= total).sort((a, b) => a - b)
|
|
106
|
+
if (!pages.length) throw new Error(`Page selection "${spec}" selected no slides in the deck's range 1-${total}.`)
|
|
107
|
+
return pages
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Turn the deck's title into a folder slug for screenshots. */
|
|
111
|
+
export function slugFromTitle(title) {
|
|
112
|
+
return String(title || '').toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '') || 'deck'
|
|
113
|
+
}
|
package/src/index.mjs
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Programmatic entry point for the Miragon Slidev validator.
|
|
3
|
+
*
|
|
4
|
+
* `validate()` resolves the deck's config, runs the source rules and (optionally)
|
|
5
|
+
* the rendered rules, classifies every violation by severity/override/exception,
|
|
6
|
+
* and returns { results, summary, meta } ready for report.mjs. The CLI
|
|
7
|
+
* (bin/index.mjs) is a thin wrapper over this.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { loadConfig } from './config.mjs'
|
|
11
|
+
import { runSourceRules, runRenderedRules, summarize } from './engine.mjs'
|
|
12
|
+
import { sourceRules, renderedRules, allRules } from './rules/index.mjs'
|
|
13
|
+
import { measureDeck } from './rendered-runner.mjs'
|
|
14
|
+
import { selfInfo, resolvedToolkitVersion, compat } from './versions.mjs'
|
|
15
|
+
|
|
16
|
+
export { loadConfig, ConfigError } from './config.mjs'
|
|
17
|
+
export { allRules, sourceRules, renderedRules, ruleById, knownRuleIds } from './rules/index.mjs'
|
|
18
|
+
export { formatText, toJson } from './report.mjs'
|
|
19
|
+
export { selfInfo, resolvedToolkitVersion, compat } from './versions.mjs'
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Run the validator against the deck in the current working directory.
|
|
23
|
+
* opts: { rendered?: boolean, pages?: string, port?: number, configPath?: string, log?: fn }
|
|
24
|
+
*/
|
|
25
|
+
export async function validate(opts = {}) {
|
|
26
|
+
const config = await loadConfig(opts.configPath)
|
|
27
|
+
|
|
28
|
+
const sourceResults = runSourceRules(sourceRules, config)
|
|
29
|
+
|
|
30
|
+
let renderedResults
|
|
31
|
+
let deckTitle
|
|
32
|
+
if (opts.rendered) {
|
|
33
|
+
const { measured, title } = await measureDeck({
|
|
34
|
+
port: opts.port ?? Number(process.env.VERIFY_PORT ?? 3030),
|
|
35
|
+
pages: opts.pages ?? process.env.VERIFY_PAGES,
|
|
36
|
+
log: opts.log,
|
|
37
|
+
})
|
|
38
|
+
deckTitle = title
|
|
39
|
+
renderedResults = runRenderedRules(renderedRules, config, measured)
|
|
40
|
+
} else {
|
|
41
|
+
renderedResults = runRenderedRules(renderedRules, config, null)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Order results by the registry (source first, then rendered) for a stable report.
|
|
45
|
+
const byId = new Map([...sourceResults, ...renderedResults].map((r) => [r.rule.id, r]))
|
|
46
|
+
const results = allRules.map((r) => byId.get(r.id)).filter(Boolean)
|
|
47
|
+
|
|
48
|
+
const summary = summarize(results)
|
|
49
|
+
const meta = {
|
|
50
|
+
validatorVersion: selfInfo().version,
|
|
51
|
+
toolkitVersion: resolvedToolkitVersion(),
|
|
52
|
+
configPath: config.configPath,
|
|
53
|
+
presetNames: config.presetNames,
|
|
54
|
+
rendered: !!opts.rendered,
|
|
55
|
+
deckTitle,
|
|
56
|
+
compat: compat(),
|
|
57
|
+
}
|
|
58
|
+
return { results, summary, meta, config }
|
|
59
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `recommended` preset: every rule at its documented default severity. This is
|
|
3
|
+
* the base a deck should `extends`. It is derived from the registry so a new rule
|
|
4
|
+
* joins the preset automatically — there is no second list to keep in sync.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { allRules } from '../rules/index.mjs'
|
|
8
|
+
|
|
9
|
+
const rules = {}
|
|
10
|
+
for (const r of allRules) rules[r.id] = r.meta.default
|
|
11
|
+
|
|
12
|
+
export default { name: '@miragon/slidev-validator/recommended', rules }
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `required` preset: only the non-negotiable brand invariants, all at `error`.
|
|
3
|
+
* These are the rules whose `meta.category` is 'required' — the same set the engine
|
|
4
|
+
* refuses to let a deck silently turn `off`/`warn` (see config.mjs). Derived from
|
|
5
|
+
* the registry, so marking a new rule `required` adds it here automatically.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { allRules } from '../rules/index.mjs'
|
|
9
|
+
|
|
10
|
+
const rules = {}
|
|
11
|
+
for (const r of allRules) if (r.meta.category === 'required') rules[r.id] = 'error'
|
|
12
|
+
|
|
13
|
+
export default { name: '@miragon/slidev-validator/required', rules }
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The rendered runner: boot (or reuse) a Slidev dev server for the deck, drive
|
|
3
|
+
* headless Chromium through every selected slide fully revealed, and take ONE DOM
|
|
4
|
+
* measurement per slide (measure.mjs). It returns the measurements; the engine
|
|
5
|
+
* applies the rendered rules. Driving Chromium directly (playwright-chromium's
|
|
6
|
+
* `chromium`) keeps this a plain CLI with no test-runner dependency.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { spawn } from 'node:child_process'
|
|
10
|
+
import { chromium } from 'playwright-chromium'
|
|
11
|
+
import { CANVAS, deckEntry, repoRoot, parsePages, slugFromTitle } from './helpers.mjs'
|
|
12
|
+
import { measureSlide } from './rules/rendered/measure.mjs'
|
|
13
|
+
import { measureSafeArea } from './rules/rendered/safe-area/measure.mjs'
|
|
14
|
+
import { loadSafeAreaConfig } from './rules/rendered/safe-area/config.mjs'
|
|
15
|
+
import { slideFileMap } from './slide-files.mjs'
|
|
16
|
+
|
|
17
|
+
/** Resolve true when the URL answers, false on timeout. */
|
|
18
|
+
async function waitForServer(url, timeoutMs) {
|
|
19
|
+
const deadline = Date.now() + timeoutMs
|
|
20
|
+
while (Date.now() < deadline) {
|
|
21
|
+
try {
|
|
22
|
+
const res = await fetch(url, { method: 'GET' })
|
|
23
|
+
if (res.ok || res.status === 200) return true
|
|
24
|
+
} catch {
|
|
25
|
+
// not up yet
|
|
26
|
+
}
|
|
27
|
+
await new Promise((r) => setTimeout(r, 400))
|
|
28
|
+
}
|
|
29
|
+
return false
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Boot `slidev` unless a server already answers at the URL. Returns a stop(). */
|
|
33
|
+
async function ensureServer(url, port, log) {
|
|
34
|
+
if (await waitForServer(url, 1500)) {
|
|
35
|
+
log(`Reusing dev server at ${url}`)
|
|
36
|
+
return async () => {}
|
|
37
|
+
}
|
|
38
|
+
log(`Starting Slidev at ${url} ...`)
|
|
39
|
+
const child = spawn('npx', ['slidev', deckEntry(), '--port', String(port)], {
|
|
40
|
+
cwd: repoRoot(),
|
|
41
|
+
stdio: 'ignore',
|
|
42
|
+
env: process.env,
|
|
43
|
+
})
|
|
44
|
+
const up = await waitForServer(url, 120_000)
|
|
45
|
+
if (!up) {
|
|
46
|
+
child.kill('SIGTERM')
|
|
47
|
+
throw new Error(`Slidev dev server did not come up at ${url} within 120s.`)
|
|
48
|
+
}
|
|
49
|
+
return async () => {
|
|
50
|
+
child.kill('SIGTERM')
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Navigate to slide `n`, fully reveal it (all clicks), settle fonts + transitions. */
|
|
55
|
+
async function gotoSlideRevealed(page, n) {
|
|
56
|
+
await page.evaluate((n) => window.__slidev__.nav.go(n), n)
|
|
57
|
+
await page.waitForFunction((n) => window.__slidev__.nav.currentPage === n, n)
|
|
58
|
+
await page.waitForFunction(() => !document.querySelector('[class*="leave-active"]'), null, { timeout: 3000 }).catch(() => {})
|
|
59
|
+
const clicks = await page.evaluate(() => window.__slidev__.nav.clicksTotal)
|
|
60
|
+
if (clicks > 0) await page.evaluate(({ n, c }) => window.__slidev__.nav.go(n, c), { n, c: clicks })
|
|
61
|
+
await page.evaluate(() => document.fonts?.ready)
|
|
62
|
+
await page.waitForTimeout(200)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Measure the deck. Returns { measured, title, slug, total, pages }. `measured` is
|
|
67
|
+
* [{ slide, file, metrics }] — one entry per selected slide (metrics null if a
|
|
68
|
+
* slide fails to render, which surfaces as an error downstream).
|
|
69
|
+
*/
|
|
70
|
+
export async function measureDeck({ port, pages, log = () => {} }) {
|
|
71
|
+
const url = `http://localhost:${port}`
|
|
72
|
+
const stop = await ensureServer(url, port, log)
|
|
73
|
+
const browser = await chromium.launch({ headless: true })
|
|
74
|
+
try {
|
|
75
|
+
const context = await browser.newContext({
|
|
76
|
+
viewport: { width: CANVAS.width + 10, height: CANVAS.height + 5 },
|
|
77
|
+
reducedMotion: 'reduce',
|
|
78
|
+
baseURL: url,
|
|
79
|
+
})
|
|
80
|
+
const page = await context.newPage()
|
|
81
|
+
await page.goto('/1', { waitUntil: 'networkidle' })
|
|
82
|
+
await page.waitForFunction(() => !!window.__slidev__?.nav)
|
|
83
|
+
await page.waitForTimeout(500)
|
|
84
|
+
|
|
85
|
+
const title = await page.evaluate(() => window.__slidev__.configs?.title ?? 'deck')
|
|
86
|
+
const total = await page.evaluate(() => window.__slidev__.nav.total)
|
|
87
|
+
const selected = parsePages(pages, total)
|
|
88
|
+
const fileMap = await slideFileMap()
|
|
89
|
+
|
|
90
|
+
// The toolkit-owned safe-area model (overlay selectors + canvas), loaded once.
|
|
91
|
+
const safeAreaConfig = loadSafeAreaConfig()
|
|
92
|
+
const overlaySel = safeAreaConfig ? safeAreaConfig.overlays.map((o) => ({ id: o.id, selector: o.selector, zIndex: o.zIndex })) : null
|
|
93
|
+
|
|
94
|
+
const measured = []
|
|
95
|
+
for (const n of selected) {
|
|
96
|
+
await gotoSlideRevealed(page, n)
|
|
97
|
+
const metrics = await page.evaluate(measureSlide, n)
|
|
98
|
+
const safeArea = overlaySel ? await page.evaluate(measureSafeArea, { n, overlaySel, canvas: safeAreaConfig.canvas }) : null
|
|
99
|
+
measured.push({ slide: n, file: fileMap.get(n) ?? 'deck/slides.md', metrics, safeArea })
|
|
100
|
+
}
|
|
101
|
+
return { measured, title, slug: slugFromTitle(title), total, pages: selected }
|
|
102
|
+
} finally {
|
|
103
|
+
await browser.close()
|
|
104
|
+
await stop()
|
|
105
|
+
}
|
|
106
|
+
}
|
package/src/report.mjs
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turn classified results into a human report (default) or JSON (`--format json`).
|
|
3
|
+
*
|
|
4
|
+
* The report deliberately separates three states the plan calls out: rules that
|
|
5
|
+
* RAN (pass/warn/error), rules DELIBERATELY DISABLED (off or a live exception, with
|
|
6
|
+
* where + why), and rules NOT RUN (skipped / not applicable). The header always
|
|
7
|
+
* records the validator version, the resolved toolkit version, and the config, so a
|
|
8
|
+
* report is self-describing and a CI log shows exactly what was enforced.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const GLYPH = { pass: '✓', warn: '!', error: '✗', off: '·', skipped: '–' }
|
|
12
|
+
|
|
13
|
+
function loc(v) {
|
|
14
|
+
const where = v.file ? (v.line ? `${v.file}:${v.line}` : v.file) : ''
|
|
15
|
+
const slide = v.slide ? `slide ${v.slide}` : ''
|
|
16
|
+
const head = [where, slide].filter(Boolean).join(' ')
|
|
17
|
+
return head ? `${head} ${v.message}` : v.message
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** The plain-text report. Returns the string; the CLI prints it. */
|
|
21
|
+
export function formatText({ results, summary, meta }) {
|
|
22
|
+
const out = []
|
|
23
|
+
out.push('Miragon Slidev validator')
|
|
24
|
+
if (meta.deckTitle) out.push(` deck "${meta.deckTitle}"`)
|
|
25
|
+
out.push(` validator ${meta.validatorVersion} toolkit ${meta.toolkitVersion ?? 'unresolved'} mode ${meta.rendered ? 'source + rendered' : 'source'}`)
|
|
26
|
+
out.push(` config ${meta.configPath ?? '(defaults: extends recommended)'}`)
|
|
27
|
+
if (meta.presetNames?.length) out.push(` extends ${meta.presetNames.join(', ')}`)
|
|
28
|
+
if (meta.compat && !meta.compat.ok) out.push(` ! toolkit ${meta.compat.installed} is outside the supported range ${meta.compat.range}`)
|
|
29
|
+
out.push('')
|
|
30
|
+
|
|
31
|
+
for (const r of results) {
|
|
32
|
+
const g = GLYPH[r.status] ?? '?'
|
|
33
|
+
const tag = r.rule.meta.category === 'required' ? ' [required]' : ''
|
|
34
|
+
out.push(`${g} ${r.rule.id}${tag} — ${r.rule.title}`)
|
|
35
|
+
for (const v of r.errors) out.push(` error ${loc(v)}`)
|
|
36
|
+
for (const { v, ex } of r.expired) out.push(` error ${loc(v)} (exception expired ${ex.expires})`)
|
|
37
|
+
for (const v of r.warns) out.push(` warn ${loc(v)}`)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Deliberately disabled + exceptions — always surfaced.
|
|
41
|
+
const disabled = results.filter((r) => r.status === 'off')
|
|
42
|
+
if (disabled.length) {
|
|
43
|
+
out.push('')
|
|
44
|
+
out.push('Disabled rules (off — will not fail the build):')
|
|
45
|
+
for (const r of disabled) out.push(` · ${r.rule.id}`)
|
|
46
|
+
}
|
|
47
|
+
const suppressed = results.flatMap((r) => r.suppressed.map((s) => ({ rule: r.rule, ...s })))
|
|
48
|
+
if (suppressed.length) {
|
|
49
|
+
out.push('')
|
|
50
|
+
out.push('Suppressed violations (exceptions / scoped off — reported, not failing):')
|
|
51
|
+
for (const s of suppressed) {
|
|
52
|
+
const why = s.ex ? `${s.ex.reason}${s.ex.ticket ? ` [${s.ex.ticket}]` : ''}${s.ex.expires ? ` (expires ${s.ex.expires})` : ''}` : 'scoped off'
|
|
53
|
+
out.push(` · ${s.rule.id}: ${loc(s.v)} — ${why}`)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const skipped = results.filter((r) => r.status === 'skipped')
|
|
57
|
+
if (skipped.length) {
|
|
58
|
+
out.push('')
|
|
59
|
+
out.push('Not run (not applicable):')
|
|
60
|
+
for (const r of skipped) out.push(` – ${r.rule.id}: ${r.reason}`)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
out.push('')
|
|
64
|
+
const verdict = summary.failed ? 'FAILED' : 'passed'
|
|
65
|
+
out.push(`${verdict}: ${summary.errorCount} error(s), ${summary.warnCount} warning(s), ${summary.suppressedCount} suppressed, ${summary.expiredCount} expired exception(s).`)
|
|
66
|
+
return out.join('\n')
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** The machine-readable report for CI. */
|
|
70
|
+
export function toJson({ results, summary, meta }) {
|
|
71
|
+
return {
|
|
72
|
+
meta,
|
|
73
|
+
summary,
|
|
74
|
+
rules: results.map((r) => ({
|
|
75
|
+
id: r.rule.id,
|
|
76
|
+
title: r.rule.title,
|
|
77
|
+
type: r.rule.type,
|
|
78
|
+
category: r.rule.meta.category,
|
|
79
|
+
status: r.status,
|
|
80
|
+
reason: r.reason,
|
|
81
|
+
errors: r.errors.map(loc),
|
|
82
|
+
warns: r.warns.map(loc),
|
|
83
|
+
expired: r.expired.map(({ v, ex }) => ({ where: loc(v), expires: ex.expires })),
|
|
84
|
+
suppressed: r.suppressed.map(({ v, ex }) => ({ where: loc(v), reason: ex?.reason ?? 'scoped off', ticket: ex?.ticket, expires: ex?.expires })),
|
|
85
|
+
})),
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The single registry of guardrails. Every rule has a stable `id` (the public API,
|
|
3
|
+
* used in config files), a `type` ('source' = static file check, no browser;
|
|
4
|
+
* 'rendered' = interprets the per-slide DOM measurement), and `meta.category`
|
|
5
|
+
* ('required' rules cannot be silently disabled) + `meta.default` severity.
|
|
6
|
+
*
|
|
7
|
+
* Add a rule: create its file, import it here, list it below. It becomes
|
|
8
|
+
* configurable, documented, and part of both presets automatically.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { sanctionedLayout } from './source/sanctioned-layout.mjs'
|
|
12
|
+
import { noRawHtml } from './source/no-raw-html.mjs'
|
|
13
|
+
import { noHtmlEntities } from './source/no-html-entities.mjs'
|
|
14
|
+
import { contentHeading } from './source/content-heading.mjs'
|
|
15
|
+
import { excalidrawCommittedLight } from './source/excalidraw-committed-light.mjs'
|
|
16
|
+
import { excalidrawBuiltTransparent } from './source/excalidraw-built-transparent.mjs'
|
|
17
|
+
|
|
18
|
+
import { elementOverflow } from './rendered/element-overflow.mjs'
|
|
19
|
+
import { noEmDash } from './rendered/no-em-dash.mjs'
|
|
20
|
+
import { noEmoji } from './rendered/no-emoji.mjs'
|
|
21
|
+
import { headingBlack } from './rendered/heading-black.mjs'
|
|
22
|
+
import { cardWhite } from './rendered/card-white.mjs'
|
|
23
|
+
import { noInlineFont } from './rendered/no-inline-font.mjs'
|
|
24
|
+
import { noRestyledBullets } from './rendered/no-restyled-bullets.mjs'
|
|
25
|
+
import { noNestedBullets } from './rendered/no-nested-bullets.mjs'
|
|
26
|
+
import { overlaySafeArea } from './rendered/overlay-safe-area.mjs'
|
|
27
|
+
|
|
28
|
+
/** Every rule, source then rendered, in report order. */
|
|
29
|
+
export const allRules = [
|
|
30
|
+
sanctionedLayout,
|
|
31
|
+
noRawHtml,
|
|
32
|
+
noHtmlEntities,
|
|
33
|
+
contentHeading,
|
|
34
|
+
excalidrawCommittedLight,
|
|
35
|
+
excalidrawBuiltTransparent,
|
|
36
|
+
elementOverflow,
|
|
37
|
+
noEmDash,
|
|
38
|
+
noEmoji,
|
|
39
|
+
headingBlack,
|
|
40
|
+
cardWhite,
|
|
41
|
+
noInlineFont,
|
|
42
|
+
noRestyledBullets,
|
|
43
|
+
noNestedBullets,
|
|
44
|
+
overlaySafeArea,
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
export const sourceRules = allRules.filter((r) => r.type === 'source')
|
|
48
|
+
export const renderedRules = allRules.filter((r) => r.type === 'rendered')
|
|
49
|
+
|
|
50
|
+
/** A rule looked up by its stable id, or undefined. */
|
|
51
|
+
export function ruleById(id) {
|
|
52
|
+
return allRules.find((r) => r.id === id)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The set of all known rule ids (for config validation). */
|
|
56
|
+
export function knownRuleIds() {
|
|
57
|
+
return new Set(allRules.map((r) => r.id))
|
|
58
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Cards stay white: no colored/gradient background, no colored left-accent border. */
|
|
2
|
+
export const cardWhite = {
|
|
3
|
+
id: 'card-white',
|
|
4
|
+
type: 'rendered',
|
|
5
|
+
title: 'cards stay white',
|
|
6
|
+
message: 'Cards must be white with the accent on the title text only',
|
|
7
|
+
meta: { category: 'required', default: 'error' },
|
|
8
|
+
evaluate(m, ctx) {
|
|
9
|
+
return m.badCards > 0
|
|
10
|
+
? [{ file: ctx.file, slide: ctx.slide, message: `${m.badCards} card(s) have a colored/gradient background or a colored left-accent border. Cards are always white with the accent on the title text only.` }]
|
|
11
|
+
: []
|
|
12
|
+
},
|
|
13
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { TOLERANCE, MIN_BOTTOM_MARGIN } from '../../helpers.mjs'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The slide must fit the 980x552 canvas AND clear the bottom by a healthy margin.
|
|
5
|
+
* Content jammed against the floor or past the right edge reads as cut off.
|
|
6
|
+
*/
|
|
7
|
+
export const elementOverflow = {
|
|
8
|
+
id: 'element-overflow',
|
|
9
|
+
type: 'rendered',
|
|
10
|
+
title: 'fits the canvas with a bottom margin',
|
|
11
|
+
message: 'Slide content must fit the 980x552 canvas with breathing room',
|
|
12
|
+
meta: { category: 'recommended', default: 'error' },
|
|
13
|
+
evaluate(m, ctx) {
|
|
14
|
+
const byX = m.right - m.cw
|
|
15
|
+
const bottomMargin = m.ch - m.contentBottom
|
|
16
|
+
const issues = []
|
|
17
|
+
if (bottomMargin < MIN_BOTTOM_MARGIN) {
|
|
18
|
+
issues.push(
|
|
19
|
+
bottomMargin < 0
|
|
20
|
+
? `content runs ${-bottomMargin}px past the bottom (reaches ${m.contentBottom}px, canvas is ${m.ch}px)`
|
|
21
|
+
: `only ${bottomMargin}px clear of the bottom (content reaches ${m.contentBottom}px; needs >= ${MIN_BOTTOM_MARGIN}px)`,
|
|
22
|
+
)
|
|
23
|
+
}
|
|
24
|
+
if (byX > TOLERANCE) issues.push(`${byX}px past the right edge (content reaches ${m.right}px, canvas is ${m.cw}px)`)
|
|
25
|
+
if (!issues.length) return []
|
|
26
|
+
return [{ file: ctx.file, slide: ctx.slide, message: `${issues.join(' and ')}. Reduce content (split the slide, fewer items, shrink the visual, or move detail to speaker notes).` }]
|
|
27
|
+
},
|
|
28
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Headings (h1/h2) must render black. Blue is only for kickers, accents, small labels. */
|
|
2
|
+
export const headingBlack = {
|
|
3
|
+
id: 'heading-black',
|
|
4
|
+
type: 'rendered',
|
|
5
|
+
title: 'headings are black, not blue',
|
|
6
|
+
message: 'Headings must render black, never blue',
|
|
7
|
+
meta: { category: 'required', default: 'error' },
|
|
8
|
+
evaluate(m, ctx) {
|
|
9
|
+
return m.blueHeads > 0
|
|
10
|
+
? [{ file: ctx.file, slide: ctx.slide, message: `${m.blueHeads} heading(s) render blue. Headings must be black; blue is only for kickers, accents, and small labels.` }]
|
|
11
|
+
: []
|
|
12
|
+
},
|
|
13
|
+
}
|