@miragon/slidev-validator 0.2.0 → 0.3.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 CHANGED
@@ -161,6 +161,11 @@ reproducible.
161
161
  - `src/engine.mjs` — classifies each violation (error / warn / suppressed /
162
162
  expired / off / skipped).
163
163
  - `src/rendered-runner.mjs` — boots/reuses Slidev, drives Chromium, measures.
164
+ - `src/helpers.mjs` — file discovery. The source-file set is derived from Slidev's
165
+ own FS loader (following every `src:` import to any depth), not a fixed folder
166
+ shape, so a flat `deck/chapter/<chapter>/` deck and a nested
167
+ `deck/content/<topic>/<chapter>/slides.md` deck are both scanned in full. Excalidraw
168
+ discovery recurses all of `deck/` for `*.excalidraw.svg`.
164
169
  - `bin/index.mjs` — the CLI.
165
170
 
166
171
  Add a rule: create its file, list it in `src/rules/index.mjs`, document it here.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@miragon/slidev-validator",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
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
6
  "keywords": [
package/src/engine.mjs CHANGED
@@ -51,8 +51,13 @@ export function classify(config, rule, outcome) {
51
51
  return { rule, status, errors, warns, suppressed, expired }
52
52
  }
53
53
 
54
- /** Run the source rules that are active (or referenced by an exception) and classify them. */
55
- export function runSourceRules(rules, config) {
54
+ /**
55
+ * Run the source rules that are active (or referenced by an exception) and classify
56
+ * them. `context` carries the pre-resolved file lists (`sourceFiles`,
57
+ * `excalidrawFiles`) so discovery — which is async, via Slidev's loader — happens
58
+ * ONCE up front and each rule's sync `check()` just consumes the resolved set.
59
+ */
60
+ export function runSourceRules(rules, config, context = {}) {
56
61
  return rules.map((rule) => {
57
62
  const referenced = config.exceptions.some((e) => e.rule === rule.id)
58
63
  if (!isActive(config, rule.id) && !referenced) {
@@ -60,7 +65,7 @@ export function runSourceRules(rules, config) {
60
65
  }
61
66
  let outcome
62
67
  try {
63
- outcome = rule.check()
68
+ outcome = rule.check(context)
64
69
  } catch (err) {
65
70
  outcome = [{ file: undefined, message: `rule threw: ${err.message}` }]
66
71
  }
package/src/helpers.mjs CHANGED
@@ -3,13 +3,18 @@
3
3
  *
4
4
  * The validator runs against the deck in the CURRENT WORKING DIRECTORY: in the
5
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.
6
+ * root. Both keep their slides under `deck/`, entered at `deck/slides.md`, but the
7
+ * on-disk shape below that is the DECK'S choice: the reference template is flat
8
+ * (one folder per chapter), other decks nest arbitrarily (e.g.
9
+ * `deck/content/<topic>/<chapter>/slides.md`). So source discovery derives the file
10
+ * set from what Slidev actually loads (following every `src:` import), never from a
11
+ * fixed folder shape. These are functions, not module-level constants, so tests can
12
+ * `process.chdir()` / point `SLIDEV_VALIDATOR_ROOT` at a fixture between calls.
9
13
  */
10
14
 
11
- import { readdirSync, existsSync } from 'node:fs'
15
+ import { readdirSync, existsSync, statSync } from 'node:fs'
12
16
  import { join, relative } from 'node:path'
17
+ import { load } from '@slidev/parser/fs'
13
18
 
14
19
  /** A file path made repo-relative with forward slashes (for display + glob match). */
15
20
  export function toRel(file) {
@@ -26,38 +31,80 @@ export function deckEntry() {
26
31
  return join(repoRoot(), 'deck', 'slides.md')
27
32
  }
28
33
 
29
- /** Each chapter is a folder deck/chapter/<chapter>/ with <chapter>.md + resources/. */
34
+ /** The deck root everything under `deck/` is discovered from. */
35
+ export function deckDir() {
36
+ return join(repoRoot(), 'deck')
37
+ }
38
+
39
+ /**
40
+ * The flat reference convention: one folder per chapter, deck/chapter/<chapter>/
41
+ * with <chapter>.md + resources/. Only used as a FALLBACK when the Slidev loader
42
+ * yields nothing (a malformed entry), so a broken deck still gets scanned instead
43
+ * of silently passing on an empty file set.
44
+ */
30
45
  export function chapterDir() {
31
- return join(repoRoot(), 'deck', 'chapter')
46
+ return join(deckDir(), 'chapter')
32
47
  }
33
48
 
34
- /** The slide source files: the entry + one <chapter>.md per chapter folder. */
35
- export function slideSourceFiles() {
36
- const files = []
49
+ /**
50
+ * The slide source files the deck ACTUALLY renders: the distinct set of
51
+ * `source.filepath` values from Slidev's own FS loader (the same loader
52
+ * `slideFileMap()` uses), which follows every `src:` import to any depth, plus the
53
+ * entry itself. This is structure-agnostic: a flat deck and a deeply nested one
54
+ * both resolve to their full, real file set. Async because the loader is.
55
+ *
56
+ * Fallback: if the loader yields nothing (throws, or an entry with no resolvable
57
+ * slides), scan the flat deck/chapter/<chapter>/<chapter>.md convention so a broken
58
+ * deck surfaces something to check rather than an accidental green on zero files.
59
+ */
60
+ export async function slideSourceFiles() {
61
+ const files = new Set()
37
62
  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)
63
+ if (existsSync(entry)) files.add(entry)
64
+ try {
65
+ const data = await load(repoRoot(), entry)
66
+ for (const s of data.slides) {
67
+ const fp = s.source?.filepath
68
+ if (fp) files.add(fp)
69
+ }
70
+ } catch {
71
+ // loader failed — fall through to the flat-convention fallback below
72
+ }
73
+ if (files.size <= (existsSync(entry) ? 1 : 0)) {
74
+ const dir = chapterDir()
75
+ if (existsSync(dir)) {
76
+ for (const ch of readdirSync(dir)) {
77
+ const md = join(dir, ch, `${ch}.md`)
78
+ if (existsSync(md)) files.add(md)
79
+ }
44
80
  }
45
81
  }
46
- return files
82
+ return [...files]
47
83
  }
48
84
 
49
- /** Every chapter's exported Excalidraw diagrams (committed sources). */
85
+ /**
86
+ * Every committed Excalidraw diagram source in the deck. Recurses all of `deck/`
87
+ * for `*.excalidraw.svg` (under any `resources/` folder, at any nesting depth), so
88
+ * diagrams in a nested deck are found the same as in the flat reference deck.
89
+ */
50
90
  export function excalidrawSvgFiles() {
51
91
  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))
92
+ const walk = (d) => {
93
+ if (!existsSync(d)) return
94
+ for (const n of readdirSync(d)) {
95
+ if (n === 'node_modules' || n === '.git') continue
96
+ const p = join(d, n)
97
+ let st
98
+ try {
99
+ st = statSync(p)
100
+ } catch {
101
+ continue
102
+ }
103
+ if (st.isDirectory()) walk(p)
104
+ else if (n.endsWith('.excalidraw.svg')) out.push(p)
59
105
  }
60
106
  }
107
+ walk(deckDir())
61
108
  return out
62
109
  }
63
110
 
package/src/index.mjs CHANGED
@@ -11,6 +11,7 @@ import { loadConfig } from './config.mjs'
11
11
  import { runSourceRules, runRenderedRules, summarize } from './engine.mjs'
12
12
  import { sourceRules, renderedRules, allRules } from './rules/index.mjs'
13
13
  import { measureDeck } from './rendered-runner.mjs'
14
+ import { slideSourceFiles, excalidrawSvgFiles } from './helpers.mjs'
14
15
  import { selfInfo, resolvedToolkitVersion, compat } from './versions.mjs'
15
16
 
16
17
  export { loadConfig, ConfigError } from './config.mjs'
@@ -25,7 +26,13 @@ export { selfInfo, resolvedToolkitVersion, compat } from './versions.mjs'
25
26
  export async function validate(opts = {}) {
26
27
  const config = await loadConfig(opts.configPath)
27
28
 
28
- const sourceResults = runSourceRules(sourceRules, config)
29
+ // Resolve the deck's real file set ONCE (async, via Slidev's loader), then hand
30
+ // it to every source rule — structure-agnostic discovery, no fixed folder shape.
31
+ const sourceContext = {
32
+ sourceFiles: await slideSourceFiles(),
33
+ excalidrawFiles: excalidrawSvgFiles(),
34
+ }
35
+ const sourceResults = runSourceRules(sourceRules, config, sourceContext)
29
36
 
30
37
  let renderedResults
31
38
  let deckTitle
@@ -1,6 +1,6 @@
1
1
  import { readFileSync } from 'node:fs'
2
2
  import { parseSync } from '@slidev/parser'
3
- import { slideSourceFiles, toRel } from '../../helpers.mjs'
3
+ import { toRel } from '../../helpers.mjs'
4
4
 
5
5
  /**
6
6
  * Content headings (frontmatter `title` → `<h2 class="*-title">`) must stay on one
@@ -77,9 +77,9 @@ export const contentHeading = {
77
77
  title: 'content headings stay single-line',
78
78
  message: 'Content headings must be single-line: no explicit break, and within the layout character budget',
79
79
  meta: { category: 'required', default: 'error' },
80
- check() {
80
+ check({ sourceFiles = [] } = {}) {
81
81
  const offenders = []
82
- for (const file of slideSourceFiles()) {
82
+ for (const file of sourceFiles) {
83
83
  const rel = toRel(file)
84
84
  for (const s of slideHeadings(file)) {
85
85
  if (s.src) continue // import stub — heading lives in the imported file
@@ -1,5 +1,5 @@
1
1
  import { readFileSync } from 'node:fs'
2
- import { excalidrawSvgFiles, toRel } from '../../helpers.mjs'
2
+ import { toRel } from '../../helpers.mjs'
3
3
 
4
4
  /**
5
5
  * The COMMITTED .excalidraw.svg is the editable source. It must render light in
@@ -16,9 +16,9 @@ export const excalidrawCommittedLight = {
16
16
  title: 'committed excalidraw diagrams are light (no dark filter, light background)',
17
17
  message: 'Committed Excalidraw diagrams must be light (no dark filter, light background)',
18
18
  meta: { category: 'recommended', default: 'error' },
19
- check() {
19
+ check({ excalidrawFiles = [] } = {}) {
20
20
  const offenders = []
21
- for (const file of excalidrawSvgFiles()) {
21
+ for (const file of excalidrawFiles) {
22
22
  const svg = readFileSync(file, 'utf8')
23
23
  const rel = toRel(file)
24
24
  const tag = (svg.match(/<svg\b[^>]*>/) || [''])[0]
@@ -1,5 +1,5 @@
1
1
  import { readFileSync } from 'node:fs'
2
- import { slideSourceFiles, toRel } from '../../helpers.mjs'
2
+ import { toRel } from '../../helpers.mjs'
3
3
 
4
4
  /**
5
5
  * Authors must write the literal character, never an HTML escape: an entity like
@@ -14,9 +14,9 @@ export const noHtmlEntities = {
14
14
  title: 'no HTML entities in slide source',
15
15
  message: 'HTML entities must not appear in slide source — write the literal character instead',
16
16
  meta: { category: 'required', default: 'error' },
17
- check() {
17
+ check({ sourceFiles = [] } = {}) {
18
18
  const offenders = []
19
- for (const file of slideSourceFiles()) {
19
+ for (const file of sourceFiles) {
20
20
  const rel = toRel(file)
21
21
  readFileSync(file, 'utf8').split('\n').forEach((line, i) => {
22
22
  const m = line.match(ENTITY)
@@ -1,5 +1,5 @@
1
1
  import { readFileSync } from 'node:fs'
2
- import { slideSourceFiles, toRel } from '../../helpers.mjs'
2
+ import { toRel } from '../../helpers.mjs'
3
3
 
4
4
  /**
5
5
  * Consistency guardrail: a slide is markdown + Miragon/Slidev components, never raw
@@ -25,9 +25,9 @@ export const noRawHtml = {
25
25
  title: 'no raw HTML in slide source — markdown and components only',
26
26
  message: 'Slides must use markdown and components only — replace raw HTML with a component or plain markdown',
27
27
  meta: { category: 'required', default: 'error' },
28
- check() {
28
+ check({ sourceFiles = [] } = {}) {
29
29
  const offenders = []
30
- for (const file of slideSourceFiles()) {
30
+ for (const file of sourceFiles) {
31
31
  const rel = toRel(file)
32
32
  let src = readFileSync(file, 'utf8')
33
33
  src = blank(src, /```[\s\S]*?```/g) // fenced code blocks
@@ -3,7 +3,7 @@ import { createRequire } from 'node:module'
3
3
  import { pathToFileURL } from 'node:url'
4
4
  import { dirname, join } from 'node:path'
5
5
  import { parseSync } from '@slidev/parser'
6
- import { repoRoot, slideSourceFiles, toRel } from '../../helpers.mjs'
6
+ import { repoRoot, toRel } from '../../helpers.mjs'
7
7
 
8
8
  /**
9
9
  * The theme's layout archetypes live one-.vue-file-per-layout under the toolkit's
@@ -61,11 +61,11 @@ export const sanctionedLayout = {
61
61
  title: 'every slide declares a sanctioned layout',
62
62
  message: 'Every slide must declare a sanctioned layout archetype',
63
63
  meta: { category: 'required', default: 'error' },
64
- check() {
64
+ check({ sourceFiles = [] } = {}) {
65
65
  const allowed = sanctionedLayouts()
66
66
  const list = [...allowed].sort().join(', ')
67
67
  const offenders = []
68
- for (const file of slideSourceFiles()) {
68
+ for (const file of sourceFiles) {
69
69
  const rel = toRel(file)
70
70
  for (const s of slideFrontmatters(file)) {
71
71
  if (s.src) continue // import stub — the layout lives in the imported file