@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
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The single browser-side measurement pass, run once per fully-revealed slide.
|
|
3
|
+
* Every rendered rule interprets the plain object this returns, so the DOM is
|
|
4
|
+
* walked ONE time per slide no matter how many rendered rules are enabled.
|
|
5
|
+
*
|
|
6
|
+
* This function is serialized and executed inside the page by Playwright, so it
|
|
7
|
+
* must be fully self-contained (no imports, no closure over module scope).
|
|
8
|
+
*/
|
|
9
|
+
export function measureSlide(n) {
|
|
10
|
+
/**
|
|
11
|
+
* `:not(.disable-view-transition)` selects the live presentation slide and
|
|
12
|
+
* skips the overview miniatures the Agenda renders.
|
|
13
|
+
*/
|
|
14
|
+
const root = document.querySelector(`.slidev-page-${n}:not(.disable-view-transition)`)
|
|
15
|
+
if (!root) return null
|
|
16
|
+
const layout = root.querySelector('.slidev-layout') || root
|
|
17
|
+
const all = [...layout.querySelectorAll('*')]
|
|
18
|
+
const rgb = (s) => {
|
|
19
|
+
const m = s.match(/rgba?\(([^)]+)\)/)
|
|
20
|
+
if (!m) return null
|
|
21
|
+
const p = m[1].split(',').map((x) => parseFloat(x))
|
|
22
|
+
return { r: p[0], g: p[1], b: p[2], a: p[3] === undefined ? 1 : p[3] }
|
|
23
|
+
}
|
|
24
|
+
const isBlue = (c) => !!c && c.b > 150 && c.b > c.r + 40 && c.b > c.g + 40
|
|
25
|
+
|
|
26
|
+
// --- Fit: horizontal overflow + how close content sits to the bottom ---
|
|
27
|
+
const box = layout.getBoundingClientRect()
|
|
28
|
+
const cw = layout.clientWidth || 980
|
|
29
|
+
const ch = layout.clientHeight || 552
|
|
30
|
+
const scale = box.width / cw
|
|
31
|
+
let right = cw
|
|
32
|
+
let contentBottom = 0
|
|
33
|
+
all.forEach((el) => {
|
|
34
|
+
const cr = el.getBoundingClientRect()
|
|
35
|
+
if (!cr.width && !cr.height) return
|
|
36
|
+
if (getComputedStyle(el).position === 'fixed') return
|
|
37
|
+
right = Math.max(right, (cr.right - box.left) / scale)
|
|
38
|
+
if (cr.height / scale / ch >= 0.92) return
|
|
39
|
+
contentBottom = Math.max(contentBottom, (cr.bottom - box.top) / scale)
|
|
40
|
+
})
|
|
41
|
+
right = Math.round(Math.max(right, layout.scrollWidth))
|
|
42
|
+
contentBottom = Math.round(contentBottom)
|
|
43
|
+
|
|
44
|
+
// --- Text rules ---
|
|
45
|
+
const text = layout.innerText || ''
|
|
46
|
+
const emDash = text.includes('—')
|
|
47
|
+
const pictographic = '\\p{Extended' + '_Pictographic}'
|
|
48
|
+
const emoji = new RegExp(pictographic, 'u').test(text)
|
|
49
|
+
|
|
50
|
+
// --- Headings must be black, never blue ---
|
|
51
|
+
const blueHeads = [...layout.querySelectorAll('h1, h2')].filter((h) => isBlue(rgb(getComputedStyle(h).color))).length
|
|
52
|
+
|
|
53
|
+
// --- Cards stay white: no colored/gradient background, no colored left-accent border ---
|
|
54
|
+
const badCards = [...layout.querySelectorAll('[class*="rounded-xl"]')].filter((el) => {
|
|
55
|
+
const s = getComputedStyle(el)
|
|
56
|
+
if (s.backgroundImage && s.backgroundImage !== 'none') return true
|
|
57
|
+
const bg = rgb(s.backgroundColor)
|
|
58
|
+
if (bg && bg.a > 0.01 && !(bg.r >= 250 && bg.g >= 250 && bg.b >= 250)) return true
|
|
59
|
+
const lw = parseFloat(s.borderLeftWidth) || 0
|
|
60
|
+
const tw = parseFloat(s.borderTopWidth) || 0
|
|
61
|
+
if (lw >= 3 && lw > tw + 1) {
|
|
62
|
+
const lc = rgb(s.borderLeftColor)
|
|
63
|
+
if (lc && !(Math.abs(lc.r - lc.g) < 24 && Math.abs(lc.g - lc.b) < 24)) return true
|
|
64
|
+
}
|
|
65
|
+
return false
|
|
66
|
+
}).length
|
|
67
|
+
|
|
68
|
+
// --- Layouts own typography: no inline font-family in author slide content ---
|
|
69
|
+
const inlineFonts = all.filter((el) => el.style && el.style.fontFamily && !el.closest('svg, pre, code, button')).length
|
|
70
|
+
|
|
71
|
+
// --- Bullets: layout owns the marker; no per-slide list-style override; max 1 nesting level ---
|
|
72
|
+
const listOverrides = [...layout.querySelectorAll('ul, li')].filter((el) => el.style && (el.style.listStyle || el.style.listStyleType)).length
|
|
73
|
+
const nestedLists = layout.querySelectorAll('ul ul, ul ol, ol ul, ol ol').length
|
|
74
|
+
|
|
75
|
+
return { cw, ch, right, contentBottom, emDash, emoji, blueHeads, badCards, inlineFonts, listOverrides, nestedLists }
|
|
76
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** No em-dashes in rendered slide content. Use commas, periods, parentheses, or colons. */
|
|
2
|
+
export const noEmDash = {
|
|
3
|
+
id: 'no-em-dash',
|
|
4
|
+
type: 'rendered',
|
|
5
|
+
title: 'no em-dashes',
|
|
6
|
+
message: 'Slide content must not contain an em-dash (—)',
|
|
7
|
+
meta: { category: 'required', default: 'error' },
|
|
8
|
+
evaluate(m, ctx) {
|
|
9
|
+
return m.emDash ? [{ file: ctx.file, slide: ctx.slide, message: 'contains an em-dash (—). Use commas, periods, parentheses, or colons.' }] : []
|
|
10
|
+
},
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** No emoji icons in rendered slide content. Use inline SVG or Iconify (i-*) classes. */
|
|
2
|
+
export const noEmoji = {
|
|
3
|
+
id: 'no-emoji',
|
|
4
|
+
type: 'rendered',
|
|
5
|
+
title: 'no emoji icons',
|
|
6
|
+
message: 'Slide content must not contain emoji',
|
|
7
|
+
meta: { category: 'required', default: 'error' },
|
|
8
|
+
evaluate(m, ctx) {
|
|
9
|
+
return m.emoji ? [{ file: ctx.file, slide: ctx.slide, message: 'contains an emoji. Use inline SVG or Iconify (i-*) icon classes instead.' }] : []
|
|
10
|
+
},
|
|
11
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Layouts own typography: no inline font-family in author slide content. */
|
|
2
|
+
export const noInlineFont = {
|
|
3
|
+
id: 'no-inline-font',
|
|
4
|
+
type: 'rendered',
|
|
5
|
+
title: 'typography not overridden',
|
|
6
|
+
message: 'Slide content must not set font-family inline',
|
|
7
|
+
meta: { category: 'recommended', default: 'error' },
|
|
8
|
+
evaluate(m, ctx) {
|
|
9
|
+
return m.inlineFonts > 0
|
|
10
|
+
? [{ file: ctx.file, slide: ctx.slide, message: `${m.inlineFonts} element(s) set font-family inline. Let the layout own typography.` }]
|
|
11
|
+
: []
|
|
12
|
+
},
|
|
13
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Keep bullet lists to one level. Split the slide instead of sub-bullets. */
|
|
2
|
+
export const noNestedBullets = {
|
|
3
|
+
id: 'no-nested-bullets',
|
|
4
|
+
type: 'rendered',
|
|
5
|
+
title: 'no nested bullets',
|
|
6
|
+
message: 'Bullet lists must stay at one level',
|
|
7
|
+
meta: { category: 'recommended', default: 'warn' },
|
|
8
|
+
evaluate(m, ctx) {
|
|
9
|
+
return m.nestedLists > 0
|
|
10
|
+
? [{ file: ctx.file, slide: ctx.slide, message: `${m.nestedLists} nested list(s). Keep to one level; split the slide instead of sub-bullets.` }]
|
|
11
|
+
: []
|
|
12
|
+
},
|
|
13
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Bullets: the layout owns the marker; no per-slide inline list-style override. */
|
|
2
|
+
export const noRestyledBullets = {
|
|
3
|
+
id: 'no-restyled-bullets',
|
|
4
|
+
type: 'rendered',
|
|
5
|
+
title: 'bullets not restyled',
|
|
6
|
+
message: 'Slide content must not override bullet styling inline',
|
|
7
|
+
meta: { category: 'recommended', default: 'error' },
|
|
8
|
+
evaluate(m, ctx) {
|
|
9
|
+
return m.listOverrides > 0
|
|
10
|
+
? [{ file: ctx.file, slide: ctx.slide, message: `${m.listOverrides} list element(s) override bullet styling inline. Use plain <ul>/<li>; the layout provides the marker.` }]
|
|
11
|
+
: []
|
|
12
|
+
},
|
|
13
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { evaluateSlide } from './safe-area/model.mjs'
|
|
2
|
+
import { loadSafeAreaConfig } from './safe-area/config.mjs'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Author content must clear the global template overlays (page/chapter display,
|
|
6
|
+
* progress bar) — not just fit the canvas. This rule consumes the dedicated
|
|
7
|
+
* safe-area measurement (`consumes: 'safeArea'`) and the toolkit-owned safe-area
|
|
8
|
+
* model. The model is read from the TOOLKIT, so a deck cannot switch the protection
|
|
9
|
+
* off; the only escape hatch is a per-slide, per-overlay `safeAreaExceptions`
|
|
10
|
+
* frontmatter entry (a reason is required), which downgrades a collision to an
|
|
11
|
+
* allowed exception — reported, never failing.
|
|
12
|
+
*/
|
|
13
|
+
export const overlaySafeArea = {
|
|
14
|
+
id: 'overlay-safe-area',
|
|
15
|
+
type: 'rendered',
|
|
16
|
+
consumes: 'safeArea',
|
|
17
|
+
title: 'content clears the global overlays',
|
|
18
|
+
message: 'Author content must clear the global template overlays (page display, progress bar)',
|
|
19
|
+
meta: { category: 'required', default: 'error' },
|
|
20
|
+
evaluate(data, ctx) {
|
|
21
|
+
if (!data) return []
|
|
22
|
+
const config = loadSafeAreaConfig()
|
|
23
|
+
if (!config) return []
|
|
24
|
+
|
|
25
|
+
const rectMap = new Map(data.overlays.map((o) => [o.id, o.rect]))
|
|
26
|
+
const instances = config.overlays.map((descriptor) => ({ descriptor, rect: rectMap.get(descriptor.id) ?? null }))
|
|
27
|
+
const all = evaluateSlide(instances, data.content, { exceptions: data.exceptions, canvas: config.canvas })
|
|
28
|
+
|
|
29
|
+
const out = []
|
|
30
|
+
for (const o of config.overlays) {
|
|
31
|
+
const real = all.filter((v) => v.overlayId === o.id && !v.excepted)
|
|
32
|
+
if (!real.length) continue
|
|
33
|
+
const worst = [...real].sort((a, b) => b.intrusionPx - a.intrusionPx)[0]
|
|
34
|
+
const more = real.length > 1 ? ` (+${real.length - 1} more)` : ''
|
|
35
|
+
out.push({
|
|
36
|
+
file: ctx.file,
|
|
37
|
+
slide: ctx.slide,
|
|
38
|
+
message: `does not clear the ${o.label}: [${worst.code}] ${worst.content} ${worst.detail}${more} Move or shrink the element to clear the reserved zone.`,
|
|
39
|
+
})
|
|
40
|
+
}
|
|
41
|
+
return out
|
|
42
|
+
},
|
|
43
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Loads the central safe-area model from the toolkit (global/safe-areas.json).
|
|
3
|
+
*
|
|
4
|
+
* Resolution mirrors sanctioned-layout: prefer the INSTALLED
|
|
5
|
+
* `@miragon/slidev-toolkit` (a scaffolded deck consumes the toolkit purely from
|
|
6
|
+
* npm), and fall back to the in-repo source so the template monorepo verifies
|
|
7
|
+
* itself. Reading the config from the TOOLKIT — never from the deck — is what stops
|
|
8
|
+
* a presentation from quietly switching the global protection off: a deck can add
|
|
9
|
+
* per-slide exceptions, but it cannot edit this file.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { readFileSync, existsSync } from 'node:fs'
|
|
13
|
+
import { createRequire } from 'node:module'
|
|
14
|
+
import { pathToFileURL } from 'node:url'
|
|
15
|
+
import { dirname, join } from 'node:path'
|
|
16
|
+
import { repoRoot } from '../../../helpers.mjs'
|
|
17
|
+
|
|
18
|
+
function configPath() {
|
|
19
|
+
try {
|
|
20
|
+
const from = pathToFileURL(join(repoRoot(), 'package.json'))
|
|
21
|
+
const pkg = createRequire(from).resolve('@miragon/slidev-toolkit/package.json')
|
|
22
|
+
return join(dirname(pkg), 'global', 'safe-areas.json')
|
|
23
|
+
} catch {
|
|
24
|
+
return join(repoRoot(), 'packages', 'toolkit', 'global', 'safe-areas.json')
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
let cached = null
|
|
29
|
+
|
|
30
|
+
/** Returns { canvas, overlays } or null when the toolkit ships no config. */
|
|
31
|
+
export function loadSafeAreaConfig() {
|
|
32
|
+
if (cached) return cached
|
|
33
|
+
const path = configPath()
|
|
34
|
+
if (!existsSync(path)) return null
|
|
35
|
+
const raw = JSON.parse(readFileSync(path, 'utf8'))
|
|
36
|
+
cached = { canvas: raw.canvas, overlays: raw.overlays }
|
|
37
|
+
return cached
|
|
38
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser-side safe-area measurement for one slide. Serialized into the page by
|
|
3
|
+
* Playwright, so it must be fully self-contained (no imports, no module closure).
|
|
4
|
+
*
|
|
5
|
+
* Measures the global overlays (page display, progress bar) and the author content
|
|
6
|
+
* near a canvas edge, all in CANVAS pixels. The transformed `.slidev-slide-content`
|
|
7
|
+
* is the containing block for the fixed overlays AND the ancestor of the layout
|
|
8
|
+
* content, so measuring both against it puts them in one comparable space. Returns
|
|
9
|
+
* { overlays, content, exceptions, frame } or null when the slide is not rendered.
|
|
10
|
+
*/
|
|
11
|
+
export function measureSafeArea({ n, overlaySel, canvas }) {
|
|
12
|
+
const root = document.querySelector(`.slidev-page-${n}:not(.disable-view-transition)`)
|
|
13
|
+
if (!root) return null
|
|
14
|
+
const frame = root.closest('.slidev-slide-content') || root
|
|
15
|
+
const box = frame.getBoundingClientRect()
|
|
16
|
+
const cw = frame.clientWidth || canvas.width
|
|
17
|
+
const scale = box.width / cw || 1
|
|
18
|
+
const toCanvas = (r) => ({
|
|
19
|
+
left: (r.left - box.left) / scale,
|
|
20
|
+
top: (r.top - box.top) / scale,
|
|
21
|
+
right: (r.right - box.left) / scale,
|
|
22
|
+
bottom: (r.bottom - box.top) / scale,
|
|
23
|
+
})
|
|
24
|
+
const rgb = (s) => {
|
|
25
|
+
const m = s.match(/rgba?\(([^)]+)\)/)
|
|
26
|
+
if (!m) return null
|
|
27
|
+
const p = m[1].split(',').map((x) => parseFloat(x))
|
|
28
|
+
return { r: p[0], g: p[1], b: p[2], a: p[3] === undefined ? 1 : p[3] }
|
|
29
|
+
}
|
|
30
|
+
const w = (r) => r.right - r.left
|
|
31
|
+
const h = (r) => r.bottom - r.top
|
|
32
|
+
|
|
33
|
+
// --- Global overlays (measured live; null when not rendered on this slide) ---
|
|
34
|
+
const overlays = overlaySel.map((o) => {
|
|
35
|
+
const el = document.querySelector(o.selector)
|
|
36
|
+
if (!el) return { id: o.id, rect: null }
|
|
37
|
+
const cs = getComputedStyle(el)
|
|
38
|
+
if (cs.display === 'none' || cs.visibility === 'hidden') return { id: o.id, rect: null }
|
|
39
|
+
const r = el.getBoundingClientRect()
|
|
40
|
+
if (!r.width || !r.height) return { id: o.id, rect: null }
|
|
41
|
+
return { id: o.id, rect: toCanvas(r) }
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
// --- Author content near an edge ---
|
|
45
|
+
const layout = root.querySelector('.slidev-layout') || root
|
|
46
|
+
const REPLACED = new Set(['img', 'svg', 'canvas', 'video', 'image'])
|
|
47
|
+
const paints = (el) => {
|
|
48
|
+
const tag = el.tagName.toLowerCase()
|
|
49
|
+
if (REPLACED.has(tag)) return true
|
|
50
|
+
const cs = getComputedStyle(el)
|
|
51
|
+
const bg = rgb(cs.backgroundColor)
|
|
52
|
+
if (bg && bg.a > 0.05) return true
|
|
53
|
+
if (cs.backgroundImage && cs.backgroundImage !== 'none') return true
|
|
54
|
+
const border = ['Top', 'Right', 'Bottom', 'Left'].some((s) => {
|
|
55
|
+
const bw = parseFloat(cs['border' + s + 'Width']) || 0
|
|
56
|
+
const c = rgb(cs['border' + s + 'Color'])
|
|
57
|
+
return bw > 0 && c && c.a > 0.05
|
|
58
|
+
})
|
|
59
|
+
if (border) return true
|
|
60
|
+
return el.children.length === 0 && (el.textContent || '').trim().length > 0
|
|
61
|
+
}
|
|
62
|
+
const opaqueBg = (el) => {
|
|
63
|
+
const tag = el.tagName.toLowerCase()
|
|
64
|
+
if (REPLACED.has(tag)) return true
|
|
65
|
+
const bg = rgb(getComputedStyle(el).backgroundColor)
|
|
66
|
+
return !!bg && bg.a > 0.5
|
|
67
|
+
}
|
|
68
|
+
const content = []
|
|
69
|
+
for (const el of [...layout.querySelectorAll('*')]) {
|
|
70
|
+
const r = el.getBoundingClientRect()
|
|
71
|
+
if (!r.width || !r.height) continue
|
|
72
|
+
const cs = getComputedStyle(el)
|
|
73
|
+
if (cs.visibility === 'hidden' || cs.display === 'none' || parseFloat(cs.opacity) < 0.05) continue
|
|
74
|
+
if (cs.position === 'fixed') continue // the overlays themselves / other chrome
|
|
75
|
+
const c = toCanvas(r)
|
|
76
|
+
const nearBottom = c.bottom > canvas.height - 80
|
|
77
|
+
const nearTop = c.top < 80
|
|
78
|
+
if (!nearBottom && !nearTop) continue
|
|
79
|
+
if (h(c) / canvas.height >= 0.92 && w(c) / canvas.width >= 0.92) continue // full-canvas shell
|
|
80
|
+
if (!paints(el)) continue
|
|
81
|
+
const z = cs.zIndex
|
|
82
|
+
content.push({
|
|
83
|
+
label: `${el.tagName.toLowerCase()}${el.className && typeof el.className === 'string' ? '.' + el.className.trim().split(/\s+/)[0] : ''}`,
|
|
84
|
+
rect: c,
|
|
85
|
+
zIndex: z === 'auto' || z === '' ? null : Number(z),
|
|
86
|
+
opaque: opaqueBg(el),
|
|
87
|
+
})
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const fm = window.__slidev__.nav.slides?.[n - 1]?.meta?.slide?.frontmatter ?? {}
|
|
91
|
+
const exceptions = Array.isArray(fm.safeAreaExceptions) ? fm.safeAreaExceptions : []
|
|
92
|
+
return { overlays, content, exceptions, frame: { cw, scale: +scale.toFixed(4) } }
|
|
93
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Safe-area geometry evaluator — the pure core of the "no content collides with a
|
|
3
|
+
* global template overlay" guardrail (ported from the original verify/safe-area.ts).
|
|
4
|
+
*
|
|
5
|
+
* The template injects global chrome onto (almost) every slide: the page/chapter
|
|
6
|
+
* display bottom-left and the progress bar top. Both are `position: fixed`,
|
|
7
|
+
* `pointer-events: none` overlays that paint ON TOP of slide content. The generic
|
|
8
|
+
* rendered checks deliberately skip fixed elements and only enforce a uniform 16px
|
|
9
|
+
* bottom band, so content can sit close to the bottom, pass, and still render
|
|
10
|
+
* through the page display. This module closes that gap.
|
|
11
|
+
*
|
|
12
|
+
* It is framework-agnostic: it takes already-measured rectangles (the caller reads
|
|
13
|
+
* real getBoundingClientRect()s in the browser, converted to CANVAS pixels — origin
|
|
14
|
+
* top-left of the 980x552 canvas) plus the central config, and returns violations.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const CANVAS = { width: 980, height: 552 }
|
|
18
|
+
|
|
19
|
+
export function width(r) {
|
|
20
|
+
return Math.max(0, r.right - r.left)
|
|
21
|
+
}
|
|
22
|
+
export function height(r) {
|
|
23
|
+
return Math.max(0, r.bottom - r.top)
|
|
24
|
+
}
|
|
25
|
+
export function isEmpty(r) {
|
|
26
|
+
return width(r) <= 0 || height(r) <= 0
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Intersection of two rectangles (may be empty; check with isEmpty). */
|
|
30
|
+
export function intersect(a, b) {
|
|
31
|
+
return {
|
|
32
|
+
left: Math.max(a.left, b.left),
|
|
33
|
+
top: Math.max(a.top, b.top),
|
|
34
|
+
right: Math.min(a.right, b.right),
|
|
35
|
+
bottom: Math.min(a.bottom, b.bottom),
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** True when the two rectangles share any area beyond `tolerance` px on each axis. */
|
|
40
|
+
export function overlaps(a, b, tolerance = 0) {
|
|
41
|
+
const i = intersect(a, b)
|
|
42
|
+
return width(i) > tolerance && height(i) > tolerance
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Build the reserved zone for an overlay: the painted rect grown by its margins on
|
|
47
|
+
* the interior sides and extended to the canvas edges it is anchored to.
|
|
48
|
+
*/
|
|
49
|
+
export function reservedZone(o, rect, canvas = CANVAS) {
|
|
50
|
+
const { anchor: a, margins: m } = o
|
|
51
|
+
return {
|
|
52
|
+
left: a.horizontal === 'left' || a.horizontal === 'full' ? 0 : rect.left - m.left,
|
|
53
|
+
right: a.horizontal === 'right' || a.horizontal === 'full' ? canvas.width : rect.right + m.right,
|
|
54
|
+
top: a.vertical === 'top' || a.vertical === 'full' ? 0 : rect.top - m.top,
|
|
55
|
+
bottom: a.vertical === 'bottom' || a.vertical === 'full' ? canvas.height : rect.bottom + m.bottom,
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The interior vertical safe line and the margin code it enforces, or null. */
|
|
60
|
+
function verticalGuard(o, rect) {
|
|
61
|
+
if (o.anchor.vertical === 'bottom') return { line: rect.top - o.margins.top, code: 'insufficient-bottom-margin' }
|
|
62
|
+
if (o.anchor.vertical === 'top') return { line: rect.bottom + o.margins.bottom, code: 'insufficient-top-margin' }
|
|
63
|
+
return null
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The interior horizontal safe line and the margin code it enforces, or null. */
|
|
67
|
+
function horizontalGuard(o, rect) {
|
|
68
|
+
if (o.anchor.horizontal === 'left') return { line: rect.right + o.margins.right, code: 'insufficient-left-margin' }
|
|
69
|
+
if (o.anchor.horizontal === 'right') return { line: rect.left - o.margins.left, code: 'insufficient-right-margin' }
|
|
70
|
+
return null
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const round = (n) => Math.round(n)
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Evaluate every content box against one overlay instance. Returns one violation
|
|
77
|
+
* per offending element (empty = clean). A null overlay rect (hidden on this
|
|
78
|
+
* layout) reserves nothing; a null content rect yields a `missing-geometry`
|
|
79
|
+
* violation so incomplete measurements fail loud, not silent.
|
|
80
|
+
*/
|
|
81
|
+
export function evaluateOverlay(instance, content, opts = {}) {
|
|
82
|
+
const { descriptor: o, rect: overlay } = instance
|
|
83
|
+
if (!overlay) return []
|
|
84
|
+
const canvas = opts.canvas ?? CANVAS
|
|
85
|
+
const tol = opts.tolerance ?? 1
|
|
86
|
+
const exception = (opts.exceptions ?? []).find((e) => e.overlay === o.id)
|
|
87
|
+
const zone = reservedZone(o, overlay, canvas)
|
|
88
|
+
const vGuard = verticalGuard(o, overlay)
|
|
89
|
+
const hGuard = horizontalGuard(o, overlay)
|
|
90
|
+
|
|
91
|
+
const out = []
|
|
92
|
+
for (const c of content) {
|
|
93
|
+
if (!c.rect) {
|
|
94
|
+
out.push({
|
|
95
|
+
code: 'missing-geometry',
|
|
96
|
+
overlayId: o.id,
|
|
97
|
+
overlayLabel: o.label,
|
|
98
|
+
content: c.label,
|
|
99
|
+
intrusionPx: 0,
|
|
100
|
+
detail: 'element reported no bounding box; geometry could not be verified.',
|
|
101
|
+
excepted: !!exception,
|
|
102
|
+
reason: exception?.reason,
|
|
103
|
+
})
|
|
104
|
+
continue
|
|
105
|
+
}
|
|
106
|
+
const inZone = intersect(c.rect, zone)
|
|
107
|
+
if (isEmpty(inZone) || width(inZone) <= tol || height(inZone) <= tol) continue
|
|
108
|
+
|
|
109
|
+
const painted = intersect(c.rect, overlay)
|
|
110
|
+
let code
|
|
111
|
+
let intrusion
|
|
112
|
+
let detail
|
|
113
|
+
|
|
114
|
+
if (!isEmpty(painted) && width(painted) > tol && height(painted) > tol) {
|
|
115
|
+
const coversOpaque = c.zIndex != null && c.zIndex > o.zIndex && c.opaque
|
|
116
|
+
code = coversOpaque ? 'hidden-by-overlay' : 'overlap'
|
|
117
|
+
intrusion = round(Math.min(width(painted), height(painted)))
|
|
118
|
+
detail = coversOpaque
|
|
119
|
+
? `element paints an opaque background at z-index ${c.zIndex} over the overlay (z-index ${o.zIndex}), hiding it across ${round(width(painted))}x${round(height(painted))}px.`
|
|
120
|
+
: `element's box enters the overlay's painted area by ${round(width(painted))}x${round(height(painted))}px, so the two render on top of each other.`
|
|
121
|
+
} else {
|
|
122
|
+
const penV = vGuard ? intrusionPastLine(c.rect, vGuard) : -Infinity
|
|
123
|
+
const penH = hGuard ? intrusionPastLine(c.rect, hGuard) : -Infinity
|
|
124
|
+
const vViol = penV > tol
|
|
125
|
+
const hViol = penH > tol
|
|
126
|
+
const overlapsX = c.rect.right > overlay.left + tol && c.rect.left < overlay.right - tol
|
|
127
|
+
let axis
|
|
128
|
+
if (vViol && hViol) axis = overlapsX ? 'v' : 'h'
|
|
129
|
+
else if (vViol) axis = 'v'
|
|
130
|
+
else axis = 'h'
|
|
131
|
+
|
|
132
|
+
if (axis === 'v' && vGuard) {
|
|
133
|
+
code = vGuard.code
|
|
134
|
+
intrusion = round(penV)
|
|
135
|
+
} else if (hGuard) {
|
|
136
|
+
code = hGuard.code
|
|
137
|
+
intrusion = round(penH)
|
|
138
|
+
} else {
|
|
139
|
+
code = 'overlap'
|
|
140
|
+
intrusion = round(Math.min(width(inZone), height(inZone)))
|
|
141
|
+
}
|
|
142
|
+
detail = clearanceDetail(c.rect, o, overlay, canvas, intrusion)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
out.push({ code, overlayId: o.id, overlayLabel: o.label, content: c.label, intrusionPx: intrusion, detail, excepted: !!exception, reason: exception?.reason })
|
|
146
|
+
}
|
|
147
|
+
return out
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** How far (px) a content rect reaches past an interior safe line (>0 = violating). */
|
|
151
|
+
function intrusionPastLine(c, guard) {
|
|
152
|
+
switch (guard.code) {
|
|
153
|
+
case 'insufficient-bottom-margin':
|
|
154
|
+
return c.bottom - guard.line
|
|
155
|
+
case 'insufficient-top-margin':
|
|
156
|
+
return guard.line - c.top
|
|
157
|
+
case 'insufficient-left-margin':
|
|
158
|
+
return guard.line - c.left
|
|
159
|
+
case 'insufficient-right-margin':
|
|
160
|
+
return c.right - guard.line
|
|
161
|
+
default:
|
|
162
|
+
return -Infinity
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** A human, actionable sentence for a buffer intrusion. */
|
|
167
|
+
function clearanceDetail(c, o, overlay, canvas, intrusion) {
|
|
168
|
+
const clauses = []
|
|
169
|
+
if (o.anchor.vertical === 'bottom') clauses.push(`>= ${round(canvas.height - overlay.top + o.margins.top)}px from the bottom (now ${round(canvas.height - c.bottom)}px)`)
|
|
170
|
+
if (o.anchor.vertical === 'top') clauses.push(`>= ${round(overlay.bottom + o.margins.bottom)}px from the top (now ${round(c.top)}px)`)
|
|
171
|
+
if (o.anchor.horizontal === 'left') clauses.push(`>= ${round(overlay.right + o.margins.right)}px from the left (now ${round(c.left)}px)`)
|
|
172
|
+
if (o.anchor.horizontal === 'right') clauses.push(`>= ${round(canvas.width - overlay.left + o.margins.left)}px from the right (now ${round(canvas.width - c.right)}px)`)
|
|
173
|
+
return `element reaches ${intrusion}px into the ${o.label}'s reserved zone. Keep it ${clauses.join(' and ')}.`
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Evaluate every overlay on a slide and flatten the violations. */
|
|
177
|
+
export function evaluateSlide(overlays, content, opts = {}) {
|
|
178
|
+
return overlays.flatMap((o) => evaluateOverlay(o, content, opts))
|
|
179
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs'
|
|
2
|
+
import { parseSync } from '@slidev/parser'
|
|
3
|
+
import { slideSourceFiles, toRel } from '../../helpers.mjs'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Content headings (frontmatter `title` → `<h2 class="*-title">`) must stay on one
|
|
7
|
+
* line; a wrap shrinks the content area below. Statement headings (`hero`/`section`/
|
|
8
|
+
* `subsection`/`cover`, markdown `#` → `<h1>`) may wrap and are not checked.
|
|
9
|
+
*
|
|
10
|
+
* Two checks: an explicit break (`<br>`/newline/trailing `\`) is always flagged; the
|
|
11
|
+
* char budget is a heuristic ("WWW" and "iii" differ in width).
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Max heading chars per layout, derived from each layout's container width + font
|
|
16
|
+
* size (~0.5em advance) with headroom over the longest real heading (38).
|
|
17
|
+
* `content-image`'s title sits in one narrow grid column.
|
|
18
|
+
*/
|
|
19
|
+
const HEADING_LIMITS = {
|
|
20
|
+
content: 50,
|
|
21
|
+
compare: 50,
|
|
22
|
+
'content-image': 28,
|
|
23
|
+
showcase: 56,
|
|
24
|
+
goodbad: 56,
|
|
25
|
+
excalidraw: 56,
|
|
26
|
+
bpmn: 56,
|
|
27
|
+
dmn: 56,
|
|
28
|
+
mermaid: 56,
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Off-by-default per-slide opt-out from both checks. */
|
|
32
|
+
const OPT_OUT_KEY = 'allowMultilineHeading'
|
|
33
|
+
|
|
34
|
+
/** Visible length: code points (emoji/umlauts = 1), spaces counted, trimmed. */
|
|
35
|
+
function visibleLength(title) {
|
|
36
|
+
return [...title.trim()].length
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** `<br>`/`<br/>`, an embedded newline, or a trailing backslash continuation. */
|
|
40
|
+
function hasExplicitBreak(title) {
|
|
41
|
+
return /<br\s*\/?>/i.test(title) || /[\r\n]/.test(title) || /\\\s*$/.test(title)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Pure per-heading check (fixture-testable). Returns reasons; [] = ok. */
|
|
45
|
+
export function scanHeading(title, layout, optOut = false) {
|
|
46
|
+
if (optOut) return []
|
|
47
|
+
if (!layout || !(layout in HEADING_LIMITS)) return []
|
|
48
|
+
if (typeof title !== 'string' || !title.trim()) return []
|
|
49
|
+
|
|
50
|
+
const reasons = []
|
|
51
|
+
if (hasExplicitBreak(title)) {
|
|
52
|
+
reasons.push('content heading contains an explicit line break; write it as a single-line heading')
|
|
53
|
+
}
|
|
54
|
+
const limit = HEADING_LIMITS[layout]
|
|
55
|
+
const len = visibleLength(title)
|
|
56
|
+
if (len > limit) {
|
|
57
|
+
reasons.push(`content heading is ${len} characters; the ${layout} layout allows up to ${limit}. Shorten it or use a shorter heading`)
|
|
58
|
+
}
|
|
59
|
+
return reasons
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Per-slide frontmatter via Slidev's parser; `src:` import stubs surface with `src` set. */
|
|
63
|
+
function slideHeadings(file) {
|
|
64
|
+
const parsed = parseSync(readFileSync(file, 'utf8'), file)
|
|
65
|
+
return parsed.slides.map((s) => ({
|
|
66
|
+
title: s.frontmatter?.title,
|
|
67
|
+
layout: s.frontmatter?.layout,
|
|
68
|
+
src: s.frontmatter?.src,
|
|
69
|
+
optOut: s.frontmatter?.[OPT_OUT_KEY] === true,
|
|
70
|
+
start: (s.start ?? 0) + 1,
|
|
71
|
+
}))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export const contentHeading = {
|
|
75
|
+
id: 'content-heading',
|
|
76
|
+
type: 'source',
|
|
77
|
+
title: 'content headings stay single-line',
|
|
78
|
+
message: 'Content headings must be single-line: no explicit break, and within the layout character budget',
|
|
79
|
+
meta: { category: 'required', default: 'error' },
|
|
80
|
+
check() {
|
|
81
|
+
const offenders = []
|
|
82
|
+
for (const file of slideSourceFiles()) {
|
|
83
|
+
const rel = toRel(file)
|
|
84
|
+
for (const s of slideHeadings(file)) {
|
|
85
|
+
if (s.src) continue // import stub — heading lives in the imported file
|
|
86
|
+
for (const reason of scanHeading(s.title, s.layout, s.optOut)) {
|
|
87
|
+
offenders.push({ file: rel, line: s.start, message: `${reason}: "${s.title}"` })
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return offenders
|
|
92
|
+
},
|
|
93
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { readFileSync, readdirSync, statSync, existsSync } from 'node:fs'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
import { repoRoot, toRel } from '../../helpers.mjs'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The chapter-resources plugin (deck/vite.config.ts) strips the baked background at
|
|
7
|
+
* build time so the diagram is transparent on the slide and inside white cards. We
|
|
8
|
+
* validate the BUILT output. Requires a prior `npm run build`; if dist/ is absent
|
|
9
|
+
* the check is reported as NOT APPLICABLE (skipped) rather than passing silently,
|
|
10
|
+
* so the report distinguishes "ran and passed" from "never ran".
|
|
11
|
+
*/
|
|
12
|
+
export const excalidrawBuiltTransparent = {
|
|
13
|
+
id: 'excalidraw-built-transparent',
|
|
14
|
+
type: 'source',
|
|
15
|
+
title: 'built excalidraw diagrams are transparent',
|
|
16
|
+
message: 'Built Excalidraw diagrams must be transparent',
|
|
17
|
+
meta: { category: 'recommended', default: 'warn' },
|
|
18
|
+
check() {
|
|
19
|
+
const distRoot = join(repoRoot(), 'dist')
|
|
20
|
+
const files = []
|
|
21
|
+
const walk = (d) => {
|
|
22
|
+
if (!existsSync(d)) return
|
|
23
|
+
for (const n of readdirSync(d)) {
|
|
24
|
+
const p = join(d, n)
|
|
25
|
+
if (statSync(p).isDirectory()) walk(p)
|
|
26
|
+
else if (n.endsWith('.excalidraw.svg')) files.push(p)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
walk(distRoot)
|
|
30
|
+
if (!files.length) {
|
|
31
|
+
return { skipped: 'no dist/ output — run `npm run build` to validate built diagrams' }
|
|
32
|
+
}
|
|
33
|
+
const offenders = []
|
|
34
|
+
for (const file of files) {
|
|
35
|
+
const svg = readFileSync(file, 'utf8')
|
|
36
|
+
const rel = toRel(file)
|
|
37
|
+
if (/<rect x="0" y="0" width="[0-9.]+" height="[0-9.]+" fill="#[0-9a-fA-F]{6}"><\/rect>/.test(svg)) {
|
|
38
|
+
offenders.push({ file: rel, message: 'still has a baked background rect (plugin must strip it for the slide)' })
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return offenders
|
|
42
|
+
},
|
|
43
|
+
}
|