-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
theme.ts
165 lines (130 loc) · 4.05 KB
/
theme.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* eslint-disable @typescript-eslint/no-namespace */
import fs from 'fs'
import path from 'path'
import { Marpit } from '@marp-team/marpit'
import { isDynamicPattern } from 'globby'
import { warn } from './cli'
import { isError } from './error'
import { File } from './file'
export class Theme {
readonly filename: string
readonly overrideName?: string
name?: string
private readBuffer?: Buffer
private constructor(filename: string, opts: Theme.Options) {
this.filename = filename
this.overrideName =
opts.overrideName === true ? this.genUniqName() : opts.overrideName
this.name = this.overrideName
}
get buffer() {
return this.readBuffer!
}
get css() {
const buf = this.buffer.toString()
return this.overrideName ? `${buf}\n/* @theme ${this.overrideName} */` : buf
}
async load() {
this.readBuffer = await fs.promises.readFile(this.filename)
}
private genUniqName() {
const uniq = () => Math.random().toString(36).slice(2)
return `${uniq()}${uniq()}${uniq()}${uniq()}`
}
static async initialize(filename: string, opts: Theme.Options = {}) {
const instance = new Theme(filename, opts)
await instance.load()
return instance
}
}
export class ThemeSet {
readonly fn: string[]
readonly fnForWatch: string[]
/** The key-value pair from file path to instance */
readonly themes = new Map<string, Theme>()
onThemeUpdated: (path: string) => void = () => {
// No ops
}
private observedMarkdowns = new Map<string, string | undefined>()
private constructor(opts: { fn: string[]; fnForWatch: string[] }) {
this.fn = opts.fn
this.fnForWatch = opts.fnForWatch
}
async findPath() {
return await ThemeSet.findPath(this.fn)
}
async load(fn: string) {
const filename = path.resolve(fn)
let theme = this.themes.get(filename)
if (theme) {
await theme.load()
} else {
theme = await Theme.initialize(filename)
this.add(theme)
}
if (theme.name !== undefined) this.notify(theme.name)
}
observe(markdownPath: string, theme: string | undefined) {
this.observedMarkdowns.set(markdownPath, theme)
}
registerTo(engine: Marpit) {
for (const theme of this.themes.values()) {
try {
const engineTheme = engine.themeSet.add(theme.css)
theme.name = engineTheme.name
} catch (e: unknown) {
const fn = path.relative(process.cwd(), theme.filename)
const errorMsg = isError(e) ? ` (${e.message})` : ''
warn(`Cannot register theme CSS: ${fn}${errorMsg}`)
}
}
}
unobserve(markdownPath: string) {
this.observedMarkdowns.delete(markdownPath)
}
private add(theme: Theme) {
this.themes.set(theme.filename, theme)
}
private notify(theme: string) {
this.observedMarkdowns.forEach((v, path) => {
if (v === theme) this.onThemeUpdated(path)
})
}
static async initialize(baseFn: string[], themes: Theme[] = []) {
const fn = [...baseFn, ...themes.map((t) => t.filename)]
const found = await ThemeSet.findPath(fn)
const fnForWatch = new Set<string>(found.map((f) => path.resolve(f)))
for (const f of fn) {
// globby's hasMagic (backed by fast-glob) always recognizes "\\" (Windows path separator) as the escape character.
if (!isDynamicPattern(f.split(path.sep).join('/'))) {
try {
const stat: fs.Stats = await fs.promises.lstat(f)
if (stat.isFile() || stat.isDirectory() || stat.isSymbolicLink())
fnForWatch.add(path.resolve(f))
} catch (e: unknown) {
// No ops
}
}
}
const instance = new ThemeSet({ fn, fnForWatch: [...fnForWatch] })
for (const f of found) await instance.load(f)
themes.forEach((t) => instance.add(t))
return instance
}
private static async findPath(fn: string[]): Promise<string[]> {
return File.findPath(
{
expandDirectories: {
extensions: [],
files: ['*.css'],
},
},
...fn
)
}
}
export namespace Theme {
export interface Options {
overrideName?: true | string
}
}