@5cover/lectern 0.0.0-stage → 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 +250 -2
- package/dist/Deck-C1ecJqVD.d.ts +110 -0
- package/dist/cli/index.js +768 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/index.d.ts +144 -0
- package/dist/index.js +566 -0
- package/dist/index.js.map +1 -0
- package/dist/jsx-runtime.d.ts +1 -0
- package/dist/jsx-runtime.js +9 -0
- package/dist/jsx-runtime.js.map +1 -0
- package/dist/profiles/engie/index.d.ts +163 -0
- package/dist/profiles/engie/index.js +393 -0
- package/dist/profiles/engie/index.js.map +1 -0
- package/package.json +91 -3
- package/src/cli/build.ts +79 -0
- package/src/cli/dev.ts +58 -0
- package/src/cli/index.ts +126 -0
- package/src/cli/paths.ts +81 -0
- package/src/cli/pdf.ts +70 -0
- package/src/cli/render.ts +40 -0
- package/src/cli/rules.ts +31 -0
- package/src/cli/serve.ts +112 -0
- package/src/client/entry.ts +170 -0
- package/src/client/graph.tsx +207 -0
- package/src/components/Deck.tsx +19 -0
- package/src/components/InfographDiscovery.tsx +51 -0
- package/src/components/layout.tsx +145 -0
- package/src/components/mechanics.tsx +74 -0
- package/src/components/util.ts +12 -0
- package/src/index.ts +37 -0
- package/src/infographDiscovery.ts +188 -0
- package/src/infographDiscoveryView.tsx +202 -0
- package/src/jsx-runtime.ts +4 -0
- package/src/profiles/engie/assets/engie-logo-dark.svg +1 -0
- package/src/profiles/engie/assets/engie-logo-white.svg +6 -0
- package/src/profiles/engie/assets.d.ts +4 -0
- package/src/profiles/engie/atoms.tsx +203 -0
- package/src/profiles/engie/engie.css +916 -0
- package/src/profiles/engie/index.ts +44 -0
- package/src/profiles/engie/profile.ts +132 -0
- package/src/profiles/engie/slides.tsx +230 -0
- package/src/profiles/index.ts +30 -0
- package/src/profiles/types.ts +49 -0
- package/src/render/renderDeck.tsx +74 -0
- package/src/render/template.ts +68 -0
- package/src/types.ts +53 -0
- package/tsconfig.authoring.json +8 -0
- package/vite-env.d.ts +11 -0
package/src/cli/dev.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import http from 'node:http'
|
|
2
|
+
import { createServer } from 'vite'
|
|
3
|
+
import { htmlShell } from '../render/template'
|
|
4
|
+
import { clientEntrySource, presentAlias } from './paths'
|
|
5
|
+
import { fsAllow, renderDeckViaVite } from './render'
|
|
6
|
+
|
|
7
|
+
export interface DevOptions {
|
|
8
|
+
deckPath: string
|
|
9
|
+
port: number
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export async function dev({ deckPath, port }: DevOptions): Promise<void> {
|
|
13
|
+
const server = await createServer({
|
|
14
|
+
configFile: false,
|
|
15
|
+
root: process.cwd(),
|
|
16
|
+
logLevel: 'warn',
|
|
17
|
+
appType: 'custom',
|
|
18
|
+
esbuild: { jsx: 'automatic', jsxImportSource: '@5cover/lectern' },
|
|
19
|
+
resolve: { alias: presentAlias() },
|
|
20
|
+
server: { middlewareMode: true, fs: { allow: fsAllow(deckPath) } },
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
async function renderPage(url: string): Promise<string> {
|
|
24
|
+
const { meta, profile, slidesHtml } = await renderDeckViaVite(server, deckPath)
|
|
25
|
+
const clientTag = `<script type="module">${clientEntrySource(profile)}</script>`
|
|
26
|
+
const html = htmlShell({ meta, slidesHtml, bodyScripts: clientTag, headTags: '' })
|
|
27
|
+
return server.transformIndexHtml(url, html)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const httpServer = http.createServer((req, res) => {
|
|
31
|
+
server.middlewares(req, res, async () => {
|
|
32
|
+
try {
|
|
33
|
+
const html = await renderPage(req.url ?? '/')
|
|
34
|
+
res.statusCode = 200
|
|
35
|
+
res.setHeader('content-type', 'text/html; charset=utf-8')
|
|
36
|
+
res.end(html)
|
|
37
|
+
} catch (err) {
|
|
38
|
+
server.ssrFixStacktrace(err as Error)
|
|
39
|
+
res.statusCode = 500
|
|
40
|
+
res.setHeader('content-type', 'text/plain; charset=utf-8')
|
|
41
|
+
res.end(`lectern dev error:\n\n${(err as Error).stack ?? String(err)}`)
|
|
42
|
+
}
|
|
43
|
+
})
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
// reveal owns the DOM, so any source change triggers a full page reload.
|
|
47
|
+
server.watcher.on('change', () => {
|
|
48
|
+
server.moduleGraph.invalidateAll()
|
|
49
|
+
server.ws.send({ type: 'full-reload' })
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
await new Promise<void>(res => httpServer.listen(port, res))
|
|
53
|
+
const url = `http://localhost:${port}/`
|
|
54
|
+
// eslint-disable-next-line no-console
|
|
55
|
+
console.log(
|
|
56
|
+
`\n lectern dev server running\n ➜ ${url}\n Editing ${deckPath} reloads automatically. Ctrl+C to stop.\n`
|
|
57
|
+
)
|
|
58
|
+
}
|
package/src/cli/index.ts
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { existsSync } from 'node:fs'
|
|
3
|
+
import { resolve } from 'node:path'
|
|
4
|
+
import { parseArgs } from 'node:util'
|
|
5
|
+
|
|
6
|
+
const HELP = `lectern - reveal.js decks authored as TSX, styled with ENGIE Fluid.
|
|
7
|
+
|
|
8
|
+
Usage:
|
|
9
|
+
lectern dev [deck.tsx] [--port 4321]
|
|
10
|
+
lectern build [deck.tsx] [--out dist] [--no-single-file]
|
|
11
|
+
lectern serve [deck.tsx] [--port 4321] [--host 127.0.0.1]
|
|
12
|
+
lectern pdf [deck.tsx] [--out deck.pdf]
|
|
13
|
+
lectern rules [profile | deck.tsx] (default profile: engie)
|
|
14
|
+
|
|
15
|
+
Arguments:
|
|
16
|
+
deck.tsx Path to the deck entry (default: ./deck.tsx)
|
|
17
|
+
profile Built-in profile name to describe (default: engie);
|
|
18
|
+
pass a deck path to describe the profile it uses
|
|
19
|
+
|
|
20
|
+
Options:
|
|
21
|
+
--port, -p Dev or production server port (default 4321)
|
|
22
|
+
--host Production server host (default 127.0.0.1)
|
|
23
|
+
--out, -o Output dir (build) or file (pdf)
|
|
24
|
+
--no-single-file Emit separate assets instead of one inlined index.html
|
|
25
|
+
--help, -h Show this help
|
|
26
|
+
`
|
|
27
|
+
|
|
28
|
+
function resolveDeck(input: string | undefined): string {
|
|
29
|
+
const candidate = resolve(process.cwd(), input ?? 'deck.tsx')
|
|
30
|
+
if (!existsSync(candidate)) {
|
|
31
|
+
console.error(`Deck not found: ${candidate}`)
|
|
32
|
+
process.exit(1)
|
|
33
|
+
}
|
|
34
|
+
return candidate
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function main() {
|
|
38
|
+
const argv = process.argv.slice(2)
|
|
39
|
+
const command = argv[0]
|
|
40
|
+
|
|
41
|
+
if (!command || command === '--help' || command === '-h' || command === 'help') {
|
|
42
|
+
console.log(HELP)
|
|
43
|
+
return
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const { values, positionals } = parseArgs({
|
|
47
|
+
args: argv.slice(1),
|
|
48
|
+
allowPositionals: true,
|
|
49
|
+
options: {
|
|
50
|
+
port: { type: 'string', short: 'p' },
|
|
51
|
+
host: { type: 'string' },
|
|
52
|
+
out: { type: 'string', short: 'o' },
|
|
53
|
+
'single-file': { type: 'boolean', default: true },
|
|
54
|
+
help: { type: 'boolean', short: 'h' },
|
|
55
|
+
},
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
if (values.help) {
|
|
59
|
+
console.log(HELP)
|
|
60
|
+
return
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// `rules` takes a built-in profile name or a deck path; a deck is rendered to find its profile.
|
|
64
|
+
if (command === 'rules') {
|
|
65
|
+
const { rules } = await import('./rules')
|
|
66
|
+
const target = positionals[0]
|
|
67
|
+
if (target && existsSync(resolve(process.cwd(), target))) {
|
|
68
|
+
const { renderStatic } = await import('./build')
|
|
69
|
+
rules((await renderStatic(resolveDeck(target))).profile)
|
|
70
|
+
} else {
|
|
71
|
+
const { resolveProfile } = await import('../profiles')
|
|
72
|
+
try {
|
|
73
|
+
rules(resolveProfile(target))
|
|
74
|
+
} catch (err) {
|
|
75
|
+
console.error((err as Error).message)
|
|
76
|
+
process.exit(1)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const deckPath = resolveDeck(positionals[0])
|
|
83
|
+
|
|
84
|
+
switch (command) {
|
|
85
|
+
case 'dev': {
|
|
86
|
+
const { dev } = await import('./dev')
|
|
87
|
+
await dev({ deckPath, port: values.port ? Number(values.port) : 4321 })
|
|
88
|
+
break
|
|
89
|
+
}
|
|
90
|
+
case 'build': {
|
|
91
|
+
const { build } = await import('./build')
|
|
92
|
+
const outDir = values.out ?? 'dist'
|
|
93
|
+
const file = await build({ deckPath, outDir, singleFile: values['single-file'] })
|
|
94
|
+
console.log(`✔ Built ${file}`)
|
|
95
|
+
break
|
|
96
|
+
}
|
|
97
|
+
case 'serve': {
|
|
98
|
+
const { serve } = await import('./serve')
|
|
99
|
+
const { url } = await serve({
|
|
100
|
+
deckPath,
|
|
101
|
+
port: values.port ? Number(values.port) : 4321,
|
|
102
|
+
host: values.host ?? '127.0.0.1',
|
|
103
|
+
})
|
|
104
|
+
console.log(
|
|
105
|
+
`\n lectern production server running\n ➜ ${url}\n Built from ${deckPath}. Source changes require a restart. Ctrl+C to stop.\n`
|
|
106
|
+
)
|
|
107
|
+
break
|
|
108
|
+
}
|
|
109
|
+
case 'pdf': {
|
|
110
|
+
const { pdf } = await import('./pdf')
|
|
111
|
+
const out = values.out ?? 'deck.pdf'
|
|
112
|
+
const file = await pdf({ deckPath, out })
|
|
113
|
+
console.log(`✔ Exported ${file}`)
|
|
114
|
+
break
|
|
115
|
+
}
|
|
116
|
+
default:
|
|
117
|
+
console.error(`Unknown command: ${command}\n`)
|
|
118
|
+
console.log(HELP)
|
|
119
|
+
process.exit(1)
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
main().catch(err => {
|
|
124
|
+
console.error(err)
|
|
125
|
+
process.exit(1)
|
|
126
|
+
})
|
package/src/cli/paths.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs'
|
|
2
|
+
import { isAbsolute, join, resolve } from 'node:path'
|
|
3
|
+
import { fileURLToPath } from 'node:url'
|
|
4
|
+
import type { Profile } from '../profiles/types'
|
|
5
|
+
|
|
6
|
+
/** Convert any OS path to a POSIX-style path (safe in import specifiers / Vite ids). */
|
|
7
|
+
export function posix(p: string): string {
|
|
8
|
+
return p.replace(/\\/g, '/')
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Package root: two levels up from src/cli or dist/cli. */
|
|
12
|
+
export const PACKAGE_ROOT = resolve(import.meta.dirname, '..', '..')
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Prefer running from source (`src/`), the client entry imports raw CSS which only Vite should process. Fall back to `dist/` for an installed package.
|
|
16
|
+
*/
|
|
17
|
+
function pickBase(): string {
|
|
18
|
+
const srcClient = join(PACKAGE_ROOT, 'src', 'client', 'entry.ts')
|
|
19
|
+
if (existsSync(srcClient)) return join(PACKAGE_ROOT, 'src')
|
|
20
|
+
return join(PACKAGE_ROOT, 'dist')
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const BASE = pickBase()
|
|
24
|
+
|
|
25
|
+
function firstExisting(...candidates: string[]): string {
|
|
26
|
+
for (const c of candidates) if (existsSync(c)) return c
|
|
27
|
+
return candidates[0]
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Absolute path to the browser client entry (Vite processes it). */
|
|
31
|
+
export const CLIENT_ENTRY = firstExisting(join(BASE, 'client', 'entry.ts'), join(BASE, 'client', 'entry.js'))
|
|
32
|
+
|
|
33
|
+
/** Absolute path to the SSR renderDeck module. */
|
|
34
|
+
export const RENDER_MODULE = firstExisting(
|
|
35
|
+
join(BASE, 'render', 'renderDeck.tsx'),
|
|
36
|
+
join(BASE, 'render', 'renderDeck.ts'),
|
|
37
|
+
join(BASE, 'render', 'renderDeck.js'),
|
|
38
|
+
join(PACKAGE_ROOT, 'dist', 'index.js')
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
/** Public package entry, used for the `lectern` alias during in-repo runs. */
|
|
42
|
+
const INDEX_MODULE = firstExisting(join(BASE, 'index.ts'), join(PACKAGE_ROOT, 'dist', 'index.js'))
|
|
43
|
+
const JSX_MODULE = firstExisting(join(BASE, 'jsx-runtime.ts'), join(PACKAGE_ROOT, 'dist', 'jsx-runtime.js'))
|
|
44
|
+
const ENGIE_MODULE = firstExisting(
|
|
45
|
+
join(BASE, 'profiles', 'engie', 'index.ts'),
|
|
46
|
+
join(PACKAGE_ROOT, 'dist', 'profiles', 'engie', 'index.js')
|
|
47
|
+
)
|
|
48
|
+
const ENGIE_STYLESHEET = join(PACKAGE_ROOT, 'src', 'profiles', 'engie', 'engie.css')
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Source of the browser entry module: lectern's client runtime, then the
|
|
52
|
+
* profile's stylesheet, so profile rules come last in the cascade.
|
|
53
|
+
*/
|
|
54
|
+
export function clientEntrySource(profile: Profile): string {
|
|
55
|
+
const imports = [posix(CLIENT_ENTRY)]
|
|
56
|
+
const { stylesheet } = profile
|
|
57
|
+
if (stylesheet) {
|
|
58
|
+
const spec = String(stylesheet)
|
|
59
|
+
imports.push(spec.startsWith('file:') ? posix(fileURLToPath(spec)) : isAbsolute(spec) ? posix(spec) : spec)
|
|
60
|
+
}
|
|
61
|
+
return imports.map(i => `import ${JSON.stringify(i)};`).join('\n')
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* When running inside the `lectern` repo, decks import the bare `lectern` specifier, which node_modules can't resolve (this *is* that package).
|
|
66
|
+
* Alias it to the local source. Harmless for installed usage (the real package resolves first, this alias only kicks in when it points at src).
|
|
67
|
+
*/
|
|
68
|
+
export function presentAlias(): Array<{ find: RegExp; replacement: string }> {
|
|
69
|
+
return [
|
|
70
|
+
{ find: /^@5cover\/lectern$/, replacement: INDEX_MODULE },
|
|
71
|
+
{ find: /^@5cover\/lectern\/jsx-runtime$/, replacement: JSX_MODULE },
|
|
72
|
+
{ find: /^@5cover\/lectern\/jsx-dev-runtime$/, replacement: JSX_MODULE },
|
|
73
|
+
{ find: /^@5cover\/lectern\/engie$/, replacement: ENGIE_MODULE },
|
|
74
|
+
{ find: /^@5cover\/lectern\/engie\.css$/, replacement: ENGIE_STYLESHEET },
|
|
75
|
+
{ find: /^lectern\/jsx-runtime$/, replacement: JSX_MODULE },
|
|
76
|
+
{ find: /^lectern\/jsx-dev-runtime$/, replacement: JSX_MODULE },
|
|
77
|
+
{ find: /^lectern\/engie$/, replacement: ENGIE_MODULE },
|
|
78
|
+
{ find: /^lectern\/engie\.css$/, replacement: ENGIE_STYLESHEET },
|
|
79
|
+
{ find: /^lectern$/, replacement: INDEX_MODULE },
|
|
80
|
+
]
|
|
81
|
+
}
|
package/src/cli/pdf.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { mkdtempSync, rmSync } from 'node:fs'
|
|
2
|
+
import { tmpdir } from 'node:os'
|
|
3
|
+
import { join, resolve } from 'node:path'
|
|
4
|
+
import { pathToFileURL } from 'node:url'
|
|
5
|
+
import { build } from './build'
|
|
6
|
+
|
|
7
|
+
export interface PdfOptions {
|
|
8
|
+
deckPath: string
|
|
9
|
+
out: string
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Export the deck to PDF: build a static bundle, then drive reveal.js's
|
|
14
|
+
* `?print-pdf` mode with headless Chromium (Playwright) and print to PDF.
|
|
15
|
+
*/
|
|
16
|
+
export async function pdf({ deckPath, out }: PdfOptions): Promise<string> {
|
|
17
|
+
const workDir = mkdtempSync(join(tmpdir(), 'lectern-pdf-'))
|
|
18
|
+
const indexHtml = await build({ deckPath, outDir: workDir, singleFile: true })
|
|
19
|
+
|
|
20
|
+
let chromium: typeof import('playwright').chromium
|
|
21
|
+
try {
|
|
22
|
+
;({ chromium } = await import('playwright'))
|
|
23
|
+
} catch {
|
|
24
|
+
rmSync(workDir, { recursive: true, force: true })
|
|
25
|
+
throw new Error(
|
|
26
|
+
'PDF export needs Playwright. Install it with:\n pnpm add -D playwright\n npx playwright install chromium'
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let browser
|
|
31
|
+
try {
|
|
32
|
+
browser = await chromium.launch()
|
|
33
|
+
} catch (err) {
|
|
34
|
+
rmSync(workDir, { recursive: true, force: true })
|
|
35
|
+
throw new Error(
|
|
36
|
+
`Could not launch Chromium. Install the browser once with:\n npx playwright install chromium\n\nOriginal error: ${(err as Error).message}`
|
|
37
|
+
)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const outAbs = resolve(process.cwd(), out)
|
|
41
|
+
try {
|
|
42
|
+
const page = await browser.newPage()
|
|
43
|
+
const url = `${pathToFileURL(indexHtml).href}?print-pdf`
|
|
44
|
+
await page.goto(url, { waitUntil: 'load' })
|
|
45
|
+
|
|
46
|
+
// Wait until reveal is ready, its per-slide print pages (.pdf-page) have
|
|
47
|
+
// been laid out, and every mermaid diagram has finished rendering.
|
|
48
|
+
await page.waitForFunction(
|
|
49
|
+
() => {
|
|
50
|
+
const w = window as unknown as { Reveal?: { isReady?: () => boolean } }
|
|
51
|
+
const revealReady = !!w.Reveal?.isReady?.()
|
|
52
|
+
const pages = document.querySelectorAll('.pdf-page').length
|
|
53
|
+
const pendingMermaid = document.querySelectorAll('.mermaid:not([data-processed])').length
|
|
54
|
+
return revealReady && pages > 0 && pendingMermaid === 0
|
|
55
|
+
},
|
|
56
|
+
{ timeout: 45_000 }
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
await page.pdf({
|
|
60
|
+
path: outAbs,
|
|
61
|
+
printBackground: true,
|
|
62
|
+
preferCSSPageSize: true,
|
|
63
|
+
})
|
|
64
|
+
} finally {
|
|
65
|
+
await browser.close()
|
|
66
|
+
rmSync(workDir, { recursive: true, force: true })
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return outAbs
|
|
70
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
|
|
2
|
+
import { tmpdir } from 'node:os'
|
|
3
|
+
import { dirname, join } from 'node:path'
|
|
4
|
+
import type { ViteDevServer } from 'vite'
|
|
5
|
+
import type { Profile } from '../profiles/types'
|
|
6
|
+
import type { ResolvedDeckMeta } from '../types'
|
|
7
|
+
import { CLIENT_ENTRY, PACKAGE_ROOT, RENDER_MODULE, posix } from './paths'
|
|
8
|
+
|
|
9
|
+
export interface RenderedDeck {
|
|
10
|
+
meta: ResolvedDeckMeta
|
|
11
|
+
profile: Profile
|
|
12
|
+
slidesHtml: string
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Render the deck INSIDE the Vite SSR graph so the deck's VNodes and
|
|
17
|
+
* `renderDeck` share one Preact instance (avoids dual-instance Fragment bugs).
|
|
18
|
+
* We do this by loading a tiny generated harness module through ssrLoadModule.
|
|
19
|
+
*/
|
|
20
|
+
export async function renderDeckViaVite(server: ViteDevServer, deckPath: string): Promise<RenderedDeck> {
|
|
21
|
+
const harnessDir = mkdtempSync(join(tmpdir(), 'lectern-harness-'))
|
|
22
|
+
const harnessPath = join(harnessDir, 'harness.tsx')
|
|
23
|
+
const source = [
|
|
24
|
+
`import deckModule from ${JSON.stringify(posix(deckPath))};`,
|
|
25
|
+
`import { renderDeck } from ${JSON.stringify(posix(RENDER_MODULE))};`,
|
|
26
|
+
`export function run() { return renderDeck(deckModule); }`,
|
|
27
|
+
].join('\n')
|
|
28
|
+
writeFileSync(harnessPath, source, 'utf8')
|
|
29
|
+
try {
|
|
30
|
+
const mod = await server.ssrLoadModule(posix(harnessPath))
|
|
31
|
+
return (mod.run as () => RenderedDeck)()
|
|
32
|
+
} finally {
|
|
33
|
+
rmSync(harnessDir, { recursive: true, force: true })
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Vite fs.allow list covering the package, deck and temp locations. */
|
|
38
|
+
export function fsAllow(deckPath: string): string[] {
|
|
39
|
+
return [PACKAGE_ROOT, dirname(deckPath), dirname(CLIENT_ENTRY), tmpdir(), process.cwd()]
|
|
40
|
+
}
|
package/src/cli/rules.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Profile, RuleSeverity } from '../profiles/types'
|
|
2
|
+
|
|
3
|
+
const SEVERITY_LABEL: Record<RuleSeverity, string> = {
|
|
4
|
+
must: 'MUST',
|
|
5
|
+
should: 'SHOULD',
|
|
6
|
+
advisory: 'ADVISORY',
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const useColor = process.stdout.isTTY
|
|
10
|
+
const bold = (s: string) => (useColor ? `\x1b[1m${s}\x1b[0m` : s)
|
|
11
|
+
const dim = (s: string) => (useColor ? `\x1b[2m${s}\x1b[0m` : s)
|
|
12
|
+
const blue = (s: string) => (useColor ? `\x1b[38;5;39m${s}\x1b[0m` : s)
|
|
13
|
+
|
|
14
|
+
/** Print a profile's natural-language rulebook. */
|
|
15
|
+
export function rules(profile: Profile): void {
|
|
16
|
+
console.log()
|
|
17
|
+
console.log(` ${bold(profile.label)} ${dim(`(${profile.name})`)} — presentation rules`)
|
|
18
|
+
console.log(` ${dim(profile.description)}`)
|
|
19
|
+
|
|
20
|
+
for (const group of profile.rules) {
|
|
21
|
+
console.log()
|
|
22
|
+
console.log(` ${blue(`▸ ${group.title}`)}`)
|
|
23
|
+
for (const r of group.rules) {
|
|
24
|
+
const tag = `[${SEVERITY_LABEL[r.severity]}]`.padEnd(11)
|
|
25
|
+
console.log(` ${dim(tag)} ${r.statement}`)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
console.log()
|
|
29
|
+
console.log(dim(` Guidelines are descriptive; linting is not implemented yet.`))
|
|
30
|
+
console.log()
|
|
31
|
+
}
|
package/src/cli/serve.ts
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import http, { type Server } from 'node:http'
|
|
2
|
+
import { mkdtempSync, readFileSync, rmSync } from 'node:fs'
|
|
3
|
+
import { tmpdir } from 'node:os'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
import { build } from './build'
|
|
6
|
+
|
|
7
|
+
export interface ServeOptions {
|
|
8
|
+
deckPath: string
|
|
9
|
+
port: number
|
|
10
|
+
host: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface ServeHandle {
|
|
14
|
+
url: string
|
|
15
|
+
close(): Promise<void>
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Build a deck into a temporary, single-file production bundle and host it
|
|
20
|
+
* locally. Unlike `dev`, this never starts Vite's development server or
|
|
21
|
+
* watches source files.
|
|
22
|
+
*/
|
|
23
|
+
export async function serve({ deckPath, port, host }: ServeOptions): Promise<ServeHandle> {
|
|
24
|
+
const outDir = mkdtempSync(join(tmpdir(), 'lectern-serve-'))
|
|
25
|
+
let document: Buffer
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
const indexHtml = await build({ deckPath, outDir, singleFile: true })
|
|
29
|
+
document = readFileSync(indexHtml)
|
|
30
|
+
} catch (err) {
|
|
31
|
+
rmSync(outDir, { recursive: true, force: true })
|
|
32
|
+
throw err
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const server = http.createServer((req, res) => {
|
|
36
|
+
const path = new URL(req.url ?? '/', `http://${req.headers.host ?? host}`).pathname
|
|
37
|
+
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
|
38
|
+
res.statusCode = 405
|
|
39
|
+
res.setHeader('allow', 'GET, HEAD')
|
|
40
|
+
res.end()
|
|
41
|
+
return
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (path !== '/' && path !== '/index.html') {
|
|
45
|
+
res.statusCode = 404
|
|
46
|
+
res.end('Not found')
|
|
47
|
+
return
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
res.statusCode = 200
|
|
51
|
+
res.setHeader('content-type', 'text/html; charset=utf-8')
|
|
52
|
+
res.setHeader('content-length', document.length)
|
|
53
|
+
if (req.method === 'HEAD') {
|
|
54
|
+
res.end()
|
|
55
|
+
return
|
|
56
|
+
}
|
|
57
|
+
res.end(document)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
let closed = false
|
|
61
|
+
const cleanup = () => rmSync(outDir, { recursive: true, force: true })
|
|
62
|
+
const close = () =>
|
|
63
|
+
new Promise<void>((resolve, reject) => {
|
|
64
|
+
if (closed) {
|
|
65
|
+
resolve()
|
|
66
|
+
return
|
|
67
|
+
}
|
|
68
|
+
closed = true
|
|
69
|
+
process.removeListener('SIGINT', shutdown)
|
|
70
|
+
process.removeListener('SIGTERM', shutdown)
|
|
71
|
+
server.close(err => (err ? reject(err) : resolve()))
|
|
72
|
+
})
|
|
73
|
+
const shutdown = () => {
|
|
74
|
+
void close()
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
server.once('close', cleanup)
|
|
78
|
+
process.once('SIGINT', shutdown)
|
|
79
|
+
process.once('SIGTERM', shutdown)
|
|
80
|
+
|
|
81
|
+
try {
|
|
82
|
+
await listen(server, port, host)
|
|
83
|
+
} catch (err) {
|
|
84
|
+
process.removeListener('SIGINT', shutdown)
|
|
85
|
+
process.removeListener('SIGTERM', shutdown)
|
|
86
|
+
server.removeListener('close', cleanup)
|
|
87
|
+
cleanup()
|
|
88
|
+
throw err
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const displayHost = host.includes(':') && !host.startsWith('[') ? `[${host}]` : host
|
|
92
|
+
const address = server.address()
|
|
93
|
+
const actualPort = typeof address === 'object' && address ? address.port : port
|
|
94
|
+
return { url: `http://${displayHost}:${actualPort}/`, close }
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function listen(server: Server, port: number, host: string): Promise<void> {
|
|
98
|
+
return new Promise((resolve, reject) => {
|
|
99
|
+
const onError = (err: Error) => {
|
|
100
|
+
server.off('listening', onListening)
|
|
101
|
+
reject(err)
|
|
102
|
+
}
|
|
103
|
+
const onListening = () => {
|
|
104
|
+
server.off('error', onError)
|
|
105
|
+
resolve()
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
server.once('error', onError)
|
|
109
|
+
server.once('listening', onListening)
|
|
110
|
+
server.listen(port, host)
|
|
111
|
+
})
|
|
112
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Browser runtime for a `lectern` deck. Bundled by Vite (dev + build).
|
|
3
|
+
* Imports every core asset so the built output is fully self-contained:
|
|
4
|
+
* reveal.js core + plugins and Mermaid. The active profile's stylesheet is
|
|
5
|
+
* imported after this module by the CLI. Reads deck config from the
|
|
6
|
+
* `#lectern-config` JSON script tag.
|
|
7
|
+
*/
|
|
8
|
+
import mermaid from 'mermaid'
|
|
9
|
+
import Reveal from 'reveal.js'
|
|
10
|
+
import RevealHighlight from 'reveal.js/plugin/highlight'
|
|
11
|
+
import RevealNotes from 'reveal.js/plugin/notes'
|
|
12
|
+
|
|
13
|
+
import 'reveal.js/plugin/highlight/monokai.css'
|
|
14
|
+
import 'reveal.js/reveal.css'
|
|
15
|
+
|
|
16
|
+
interface PresentConfig {
|
|
17
|
+
profile?: string
|
|
18
|
+
transition?: string
|
|
19
|
+
reveal?: Record<string, unknown>
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function readConfig(): PresentConfig {
|
|
23
|
+
const el = document.getElementById('lectern-config')
|
|
24
|
+
if (!el?.textContent) return {}
|
|
25
|
+
try {
|
|
26
|
+
return JSON.parse(el.textContent) as PresentConfig
|
|
27
|
+
} catch {
|
|
28
|
+
return {}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Profile tokens live on the `.reveal.profile-<name>` element, not :root.
|
|
33
|
+
function cssVar(name: string, fallback: string): string {
|
|
34
|
+
const root = document.querySelector('.reveal') ?? document.documentElement
|
|
35
|
+
const v = getComputedStyle(root).getPropertyValue(name).trim()
|
|
36
|
+
return v || fallback
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function main() {
|
|
40
|
+
const config = readConfig()
|
|
41
|
+
|
|
42
|
+
// Ensure the active profile class is lectern (the renderer sets it server-side).
|
|
43
|
+
const revealEl = document.querySelector('.reveal')
|
|
44
|
+
if (config.profile && revealEl && !revealEl.classList.contains(`profile-${config.profile}`)) {
|
|
45
|
+
revealEl.classList.add(`profile-${config.profile}`)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Theme Mermaid to match the active profile before rendering.
|
|
49
|
+
const brand = cssVar('--lectern-brand', '#007bc5')
|
|
50
|
+
const brandStrong = cssVar('--lectern-brand-strong', '#004573')
|
|
51
|
+
const ink = cssVar('--lectern-ink', '#232d35')
|
|
52
|
+
const line = cssVar('--lectern-line', '#d6dde2')
|
|
53
|
+
const bgSoft = cssVar('--lectern-bg-soft', '#f6f8f9')
|
|
54
|
+
// The profile's *resolved* font stack (from Fluid) — we don't invent a font.
|
|
55
|
+
// Mermaid pins labels to this value in its generated stylesheet, so measured
|
|
56
|
+
// and rendered widths agree.
|
|
57
|
+
const font = getComputedStyle(revealEl ?? document.documentElement).fontFamily || 'sans-serif'
|
|
58
|
+
|
|
59
|
+
mermaid.initialize({
|
|
60
|
+
startOnLoad: false,
|
|
61
|
+
securityLevel: 'loose',
|
|
62
|
+
theme: 'base',
|
|
63
|
+
fontFamily: font,
|
|
64
|
+
flowchart: { htmlLabels: false, useMaxWidth: false, padding: 10, nodeSpacing: 50, rankSpacing: 55 },
|
|
65
|
+
themeVariables: {
|
|
66
|
+
fontFamily: font,
|
|
67
|
+
primaryColor: bgSoft,
|
|
68
|
+
primaryBorderColor: brand,
|
|
69
|
+
primaryTextColor: ink,
|
|
70
|
+
lineColor: brandStrong,
|
|
71
|
+
secondaryColor: '#ffffff',
|
|
72
|
+
tertiaryColor: bgSoft,
|
|
73
|
+
tertiaryBorderColor: line,
|
|
74
|
+
clusterBkg: '#ffffff',
|
|
75
|
+
clusterBorder: line,
|
|
76
|
+
},
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
const deck = new Reveal({
|
|
80
|
+
width: 1280,
|
|
81
|
+
height: 720,
|
|
82
|
+
// Let the presentation fill any viewport. A deck may still explicitly
|
|
83
|
+
// cap either value through its `reveal` configuration.
|
|
84
|
+
minScale: 0,
|
|
85
|
+
maxScale: Infinity,
|
|
86
|
+
hash: true,
|
|
87
|
+
// In PDF export, keep one page per slide (show all fragments) instead of one page per fragment step: better for a handout.
|
|
88
|
+
pdfSeparateFragments: false,
|
|
89
|
+
transition: (config.transition as 'slide') ?? 'slide',
|
|
90
|
+
plugins: [RevealHighlight, RevealNotes],
|
|
91
|
+
...(config.reveal ?? {}),
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
await deck.initialize()
|
|
95
|
+
|
|
96
|
+
// Reveal marks its viewport during initialization; tag it with the profile so
|
|
97
|
+
// profile CSS can style the area around the scaled canvas.
|
|
98
|
+
const viewport = revealEl?.closest('.reveal-viewport')
|
|
99
|
+
if (config.profile && viewport) viewport.classList.add(`profile-${config.profile}`)
|
|
100
|
+
|
|
101
|
+
// Mermaid must render while its slide is VISIBLE: a diagram rendered inside a `display:none` slide collapses to zero size. So we render lazily: all at once for PDF (every slide is shown), otherwise per-slide on navigation.
|
|
102
|
+
async function renderMermaid(scope: ParentNode | null) {
|
|
103
|
+
const nodes = Array.from((scope ?? document).querySelectorAll<HTMLElement>('.mermaid:not([data-processed])'))
|
|
104
|
+
if (!nodes.length) return
|
|
105
|
+
// Mermaid measures SVG labels in screen coordinates. Reveal scales the
|
|
106
|
+
// `.slides` canvas with a CSS transform, which would make Mermaid bake
|
|
107
|
+
// that scale into node dimensions and then scale them a second time.
|
|
108
|
+
// Render in the unscaled 1280×720 coordinate system, then restore
|
|
109
|
+
// Reveal's transform for display.
|
|
110
|
+
const slides = document.querySelector<HTMLElement>('.reveal .slides')
|
|
111
|
+
const transform = slides?.style.transform
|
|
112
|
+
if (slides && transform?.includes('scale(')) {
|
|
113
|
+
slides.style.transform = transform.replace(/scale\([^)]*\)/, 'scale(1)')
|
|
114
|
+
}
|
|
115
|
+
try {
|
|
116
|
+
await mermaid.run({ nodes })
|
|
117
|
+
} catch (err) {
|
|
118
|
+
console.error('[lectern] mermaid render error', err)
|
|
119
|
+
} finally {
|
|
120
|
+
if (slides && transform !== undefined) {
|
|
121
|
+
slides.style.transform = transform
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
// Mermaid appends a shared tooltip to <body>. Its default absolute
|
|
125
|
+
// position extends the document by a few pixels, which becomes a
|
|
126
|
+
// trailing blank PDF page in print mode. Keep it available for hover
|
|
127
|
+
// interactions without letting it participate in document height.
|
|
128
|
+
document.querySelectorAll<HTMLElement>('body > .mermaidTooltip').forEach(tooltip => {
|
|
129
|
+
tooltip.style.position = 'fixed'
|
|
130
|
+
})
|
|
131
|
+
deck.layout()
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const isPrint = /print-pdf/.test(location.search)
|
|
135
|
+
if (isPrint) {
|
|
136
|
+
const formatPdfSlideNumbers = () => {
|
|
137
|
+
const pages = Array.from(document.querySelectorAll<HTMLElement>('.pdf-page'))
|
|
138
|
+
pages.forEach((page, index) => {
|
|
139
|
+
const number = page.querySelector<HTMLElement>('.slide-number-pdf')
|
|
140
|
+
if (number) number.textContent = `${index + 1} / ${pages.length}`
|
|
141
|
+
})
|
|
142
|
+
}
|
|
143
|
+
deck.on('pdf-ready', formatPdfSlideNumbers)
|
|
144
|
+
await renderMermaid(document)
|
|
145
|
+
// Reveal's PDF renderer ignores the `c/t` slide-number format and
|
|
146
|
+
// injects a bare sequential number into each generated `.pdf-page`.
|
|
147
|
+
// Its asynchronous `pdf-ready` event is the point at which the total
|
|
148
|
+
// number of exported pages is known.
|
|
149
|
+
formatPdfSlideNumbers()
|
|
150
|
+
} else {
|
|
151
|
+
await renderMermaid(deck.getCurrentSlide())
|
|
152
|
+
deck.on('slidechanged', event => {
|
|
153
|
+
void renderMermaid((event as unknown as { currentSlide: HTMLElement }).currentSlide)
|
|
154
|
+
})
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Opt-in infograph discovery. Dynamic import keeps Cytoscape out of decks
|
|
158
|
+
// that do not embed the component; in PDF mode it renders the seed graph
|
|
159
|
+
// without an interactive surface.
|
|
160
|
+
if (document.querySelector('[data-lectern-infograph-discovery]')) {
|
|
161
|
+
const { initInfographDiscovery } = await import('./graph')
|
|
162
|
+
const infographReady = initInfographDiscovery(deck as unknown as Parameters<typeof initInfographDiscovery>[0])
|
|
163
|
+
if (isPrint) await infographReady
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Expose for tooling (PDF export waits on this).
|
|
167
|
+
;(window as unknown as { Reveal: unknown }).Reveal = deck
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
void main()
|