-
-
Notifications
You must be signed in to change notification settings - Fork 97
/
index.ts
107 lines (89 loc) · 2.34 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { MarpitOptions, MarpitRenderResult, Element } from '@marp-team/marpit'
import fs from 'fs'
import path from 'path'
import { promisify } from 'util'
import barePug from './bare/bare.pug'
import bareScss from './bare/bare.scss'
import bespokePug from './bespoke/bespoke.pug'
import bespokeScss from './bespoke/bespoke.scss'
const readFile = promisify(fs.readFile)
type RendererResult = MarpitRenderResult &
TemplateMeta & {
length: number
size: RenderedSize
}
interface TemplateCoreOption {
base?: string
lang: string
notifyWS?: string
renderer: (tplOpts: MarpitOptions) => RendererResult
}
export interface TemplateMeta {
description: string | undefined
image: string | undefined
title: string | undefined
url: string | undefined
}
interface RenderedSize {
height: number
width: number
}
export type TemplateOption = TemplateBareOption | TemplateBespokeOption
interface TemplateBareOption {}
interface TemplateBespokeOption {
osc?: boolean
progress?: boolean
}
export interface TemplateResult {
rendered: RendererResult
result: string
}
export type Template<T = TemplateOption> = (
locals: TemplateCoreOption & T
) => Promise<TemplateResult>
export const bare: Template<TemplateBareOption> = async opts => {
const rendered = opts.renderer({
container: [],
inlineSVG: true,
slideContainer: [],
})
return {
rendered,
result: barePug({
...opts,
...rendered,
bare: { css: bareScss },
watchJs: await watchJs(opts.notifyWS),
}),
}
}
export const bespoke: Template<TemplateBespokeOption> = async opts => {
const rendered = opts.renderer({
container: new Element('div', { id: 'p' }),
inlineSVG: true,
slideContainer: [],
})
return {
rendered,
result: bespokePug({
...opts,
...rendered,
bespoke: {
css: bespokeScss,
js: await libJs('bespoke.js'),
osc: opts.osc ?? true,
progress: opts.progress,
},
watchJs: await watchJs(opts.notifyWS),
}),
}
}
async function libJs(fn: string) {
return (await readFile(path.resolve(__dirname, fn))).toString()
}
async function watchJs(notifyWS?: string) {
if (notifyWS === undefined) return false
const watchJs = await libJs('watch.js')
return `window.__marpCliWatchWS=${JSON.stringify(notifyWS)};${watchJs}`
}
export default { bare, bespoke }