-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
option.ts
147 lines (129 loc) · 3.79 KB
/
option.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
import { tmpdir } from 'os'
import path from 'path'
import { MarpOptions } from '@marp-team/marp-core'
import { Options } from 'markdown-it'
import { nanoid } from 'nanoid'
import { TextDocument, Uri, workspace } from 'vscode'
import themes, { ThemeType } from './themes'
import {
marpConfiguration,
mathTypesettingConfiguration,
unlink,
writeFile,
} from './utils'
export interface WorkFile {
path: string
cleanup: () => Promise<void>
}
let cachedPreviewOption: MarpOptions | undefined
const breaks = (inheritedValue: boolean): boolean => {
switch (marpConfiguration().get<string>('breaks')) {
case 'off':
return false
case 'inherit':
return inheritedValue
default:
return true
}
}
const enableHtml = () =>
marpConfiguration().get<boolean>('enableHtml') && workspace.isTrusted
const math = () => {
const conf = mathTypesettingConfiguration()
return conf === 'off' ? false : conf
}
const pdfOutlines = () => {
switch (marpConfiguration().get<string>('pdf.outlines')) {
case 'pages':
return { pages: true, headings: false }
case 'headings':
return { pages: false, headings: true }
case 'both':
return { pages: true, headings: true }
default:
return false
}
}
export const marpCoreOptionForPreview = (
baseOption: Options & MarpOptions
): MarpOptions => {
if (!cachedPreviewOption) {
cachedPreviewOption = {
container: { tag: 'div', id: '__marp-vscode' },
slideContainer: { tag: 'div', 'data-marp-vscode-slide-wrapper': '' },
html: enableHtml() || undefined,
inlineSVG: {
backdropSelector: false,
},
markdown: {
breaks: breaks(!!baseOption.breaks),
typographer: baseOption.typographer,
},
math: math(),
minifyCSS: false,
script: false,
}
}
return cachedPreviewOption
}
export const marpCoreOptionForCLI = async (
{ uri }: TextDocument,
{
allowLocalFiles = true,
pdfNotes,
}: { allowLocalFiles?: boolean; pdfNotes?: boolean } = {}
) => {
const confMdPreview = workspace.getConfiguration('markdown.preview', uri)
const baseOpts = {
allowLocalFiles,
pdfNotes,
pdfOutlines: pdfOutlines(),
html: enableHtml() || undefined,
options: {
markdown: {
breaks: breaks(!!confMdPreview.get<boolean>('breaks')),
typographer: confMdPreview.get<boolean>('typographer'),
},
math: math(),
},
themeSet: [] as string[],
vscode: {} as Record<string, any>,
}
const workspaceFolder = workspace.getWorkspaceFolder(uri)
const parentFolder = uri.scheme === 'file' && path.dirname(uri.fsPath)
const baseFolder = (() => {
if (workspaceFolder) return workspaceFolder.uri
if (parentFolder) return Uri.parse(`file:${parentFolder}`, true)
return undefined
})()
const themeFiles: WorkFile[] = (
await Promise.all(
themes.loadStyles(baseFolder).map((promise) =>
promise.then(
async (theme) => {
if (theme.type === ThemeType.File) {
return { path: theme.path, cleanup: () => Promise.resolve() }
}
if (
theme.type === ThemeType.Remote ||
theme.type === ThemeType.VirtualFS
) {
const cssName = `.marp-vscode-cli-theme-${nanoid()}.css`
const tmp = path.join(tmpdir(), cssName)
const tmpUri = Uri.file(tmp)
await writeFile(tmpUri, theme.css)
return { path: tmp, cleanup: () => unlink(tmpUri) }
}
},
(e) => console.error(e)
)
)
)
).filter((w): w is WorkFile => !!w)
baseOpts.themeSet = themeFiles.map((w) => w.path)
baseOpts.vscode.themeFiles = themeFiles
return baseOpts
}
export const clearMarpCoreOptionCache = () => {
cachedPreviewOption = undefined
}