slidev-theme-kotlin 0.13.2 → 0.14.1
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/CHANGELOG.md +38 -1
- package/README.md +396 -633
- package/assets/gutter/suspendCall@14x14.svg +6 -0
- package/assets/gutter/suspendCall@14x14_dark.svg +6 -0
- package/bin/present.mjs +296 -0
- package/components/DrawnAnnotation.vue +57 -9
- package/components/DrawnAnnotationEditorToolbar.vue +1 -1
- package/components/InlineCompilerError.vue +46 -15
- package/components/InlineValue.vue +3 -3
- package/components/MagicMoveBetween.vue +6 -4
- package/components/README.md +60 -2
- package/components/SmartCast.vue +3 -3
- package/components/SuspendGutter.vue +168 -0
- package/components/TypeHint.vue +3 -3
- package/components/Warning.vue +3 -3
- package/components/carried-layer.ts +29 -0
- package/components/code-highlight.ts +119 -5
- package/components/transition-ink.ts +27 -6
- package/docs/annotation-editor.png +0 -0
- package/docs/code-marks.md +76 -0
- package/docs/phone-remote.png +0 -0
- package/docs/presenting.md +122 -0
- package/docs/publishing.md +111 -0
- package/docs/snippets.md +84 -0
- package/global-top.vue +3 -8
- package/package.json +11 -4
- package/remote/RemoteControl.vue +278 -0
- package/remote/connection.ts +19 -0
- package/setup/code-imports.ts +3 -2
- package/setup/code-marks.ts +562 -0
- package/setup/kotlin-snippets.ts +4 -2
- package/setup/magic-move-between.ts +10 -2
- package/setup/main.ts +5 -1
- package/setup/remote-sync.ts +226 -0
- package/setup/routes.ts +21 -0
- package/setup/static-content.ts +39 -5
- package/setup/transformers.ts +16 -1
- package/styles/layout.css +17 -0
- package/assets/edit-3-svgrepo-com.svg +0 -5
- package/components/DrawnAnnotationEditorLauncher.vue +0 -59
|
@@ -0,0 +1,562 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Code marks: IDE decorations written inside the fence.
|
|
3
|
+
*
|
|
4
|
+
* A trailing `// [!kind …]` comment asks for one of the theme's IDE
|
|
5
|
+
* components on that line, without wrapping the fence by hand:
|
|
6
|
+
*
|
|
7
|
+
* ```kotlin
|
|
8
|
+
* fun Application.users() = routing { // [!this Routing]
|
|
9
|
+
* when (val result = insertUser(body)) {
|
|
10
|
+
* is Success -> respond(result.user) // [!smartcast result]
|
|
11
|
+
* AlreadyExists -> fail(UserAlreadyExists)
|
|
12
|
+
* }
|
|
13
|
+
* }
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* The comment leaves the code before Shiki, the handout, or the Kotlin
|
|
17
|
+
* compiler see it, and the markdown transformer (`transformers.ts`) wraps
|
|
18
|
+
* the fence in the components the marks ask for, with `:line` (and
|
|
19
|
+
* `:occurrence`) computed from where the comment sits. A mark therefore
|
|
20
|
+
* moves with its line through a Magic Move chain, and the author never
|
|
21
|
+
* counts lines, escapes quotes, or closes tags in reverse order. A comment
|
|
22
|
+
* line holding nothing but marks applies to the next code line and leaves
|
|
23
|
+
* the slide entirely, for a diagnostic too long to sit after the code.
|
|
24
|
+
*
|
|
25
|
+
* Kinds, with the component each one becomes:
|
|
26
|
+
*
|
|
27
|
+
* - `[!smartcast TEXT]`: `SmartCast` on every occurrence of `TEXT` on the line.
|
|
28
|
+
* - `[!warning TEXT MESSAGE]`: `Warning` on the first occurrence, with the message.
|
|
29
|
+
* - `[!error TEXT MESSAGE]`: `InlineCompilerError`; the fence stays out of the
|
|
30
|
+
* generated Kotlin sources, like a fence the component wraps by hand.
|
|
31
|
+
* - `[!this TYPE]` / `[!context TYPE]`: `TypeHint` after the line's last `{`.
|
|
32
|
+
* - `[!type NAME TYPE]`: `TypeHint` with the inferred type after `NAME`.
|
|
33
|
+
* - `[!value TEXT VALUE]`: `InlineValue` after the line.
|
|
34
|
+
* - `[!suspend]`: a line of `SuspendGutter`, collected over the fence.
|
|
35
|
+
*
|
|
36
|
+
* `key=value` options right after the kind pass through as attributes
|
|
37
|
+
* (`[!smartcast on=1 result]`, `[!this occurrence=1 Routing]`,
|
|
38
|
+
* `[!error text=body Missing validation]`). The first argument is a bare
|
|
39
|
+
* word, or quoted with `"…"` or `` `…` `` when it holds spaces; the last
|
|
40
|
+
* argument runs to the end of the mark, so a message needs no quotes. Several
|
|
41
|
+
* marks share one comment: `// [!this Routing] [!suspend]`.
|
|
42
|
+
*/
|
|
43
|
+
import type { Line, SourceEdit } from './code-imports'
|
|
44
|
+
import { scanLines, shouldFoldImports, splitLeadingImports } from './code-imports'
|
|
45
|
+
|
|
46
|
+
export interface CodeMark {
|
|
47
|
+
/** The kind as written, lower-cased, without hyphens. */
|
|
48
|
+
kind: string
|
|
49
|
+
/** One-based line in the displayed code. */
|
|
50
|
+
line: number
|
|
51
|
+
/** Positional arguments, as written. */
|
|
52
|
+
args: string[]
|
|
53
|
+
/** `key=value` options, as written. */
|
|
54
|
+
options: Record<string, string>
|
|
55
|
+
/** The code on the mark's line, without the comment. */
|
|
56
|
+
code: string
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
interface MarkKind {
|
|
60
|
+
component: string
|
|
61
|
+
/** Prop names the positional arguments fill, in order. The last one absorbs the rest of the mark. */
|
|
62
|
+
slots: string[]
|
|
63
|
+
/** `text` when the kind marks a token the author does not name. */
|
|
64
|
+
text?: string
|
|
65
|
+
/** Which occurrences of `text` on the line get a component when no `occurrence` option is given. */
|
|
66
|
+
occurrence: 'first' | 'all' | 'last'
|
|
67
|
+
/** Collect every marked line into one component with a `:lines` array instead. */
|
|
68
|
+
lines?: boolean
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export const CODE_MARK_KINDS: Record<string, MarkKind> = {
|
|
72
|
+
smartcast: { component: 'SmartCast', slots: ['text', 'message'], occurrence: 'all' },
|
|
73
|
+
warning: { component: 'Warning', slots: ['text', 'message'], occurrence: 'first' },
|
|
74
|
+
error: { component: 'InlineCompilerError', slots: ['text', 'message'], occurrence: 'first' },
|
|
75
|
+
this: { component: 'TypeHint', slots: ['receiver'], text: '{', occurrence: 'last' },
|
|
76
|
+
context: { component: 'TypeHint', slots: ['context'], text: '{', occurrence: 'last' },
|
|
77
|
+
type: { component: 'TypeHint', slots: ['text', 'type'], occurrence: 'first' },
|
|
78
|
+
value: { component: 'InlineValue', slots: ['text', 'value'], occurrence: 'first' },
|
|
79
|
+
suspend: { component: 'SuspendGutter', slots: [], occurrence: 'first', lines: true },
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The kind a mark names, with `smart-cast`, `SmartCast` and `smartcast` all the same. */
|
|
83
|
+
export function normalizeMarkKind(kind: string): string {
|
|
84
|
+
return kind.toLowerCase().replace(/-/g, '')
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Whether a fence carrying these marks is shown broken on purpose. */
|
|
88
|
+
export function hasCompilerErrorMark(marks: CodeMark[]): boolean {
|
|
89
|
+
return marks.some(mark => mark.kind === 'error')
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// ---------------------------------------------------------------------------
|
|
93
|
+
// Parsing one comment
|
|
94
|
+
|
|
95
|
+
interface RawMark {
|
|
96
|
+
kind: string
|
|
97
|
+
args: string[]
|
|
98
|
+
options: Record<string, string>
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const QUOTES = new Set(['"', '`'])
|
|
102
|
+
const OPTION = /^([A-Za-z][\w-]*)=(.*)$/s
|
|
103
|
+
|
|
104
|
+
/** Index just past the quote that closes the one at `start`, or -1. `"` may be escaped with a backslash. */
|
|
105
|
+
function skipQuoted(text: string, start: number): number {
|
|
106
|
+
const quote = text[start]
|
|
107
|
+
for (let i = start + 1; i < text.length; i++) {
|
|
108
|
+
if (quote === '"' && text[i] === '\\') {
|
|
109
|
+
i++
|
|
110
|
+
continue
|
|
111
|
+
}
|
|
112
|
+
if (text[i] === quote)
|
|
113
|
+
return i + 1
|
|
114
|
+
}
|
|
115
|
+
return -1
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function unescape(quoted: string): string {
|
|
119
|
+
const quote = quoted[0]
|
|
120
|
+
const inner = quoted.slice(1, -1)
|
|
121
|
+
return quote === '"' ? inner.replace(/\\(["\\])/g, '$1') : inner
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Index of the `]` closing the mark whose body starts at `start`: brackets
|
|
126
|
+
* balance, and quoted text is skipped, so `` `[]` `` and `List<[A]>` are
|
|
127
|
+
* arguments rather than the end of the mark. -1 when it never closes.
|
|
128
|
+
*/
|
|
129
|
+
function findMarkEnd(text: string, start: number): number {
|
|
130
|
+
let depth = 0
|
|
131
|
+
for (let i = start; i < text.length; i++) {
|
|
132
|
+
const c = text[i]
|
|
133
|
+
if (QUOTES.has(c)) {
|
|
134
|
+
const end = skipQuoted(text, i)
|
|
135
|
+
if (end < 0)
|
|
136
|
+
return -1
|
|
137
|
+
i = end - 1
|
|
138
|
+
continue
|
|
139
|
+
}
|
|
140
|
+
if (c === '[') {
|
|
141
|
+
depth++
|
|
142
|
+
}
|
|
143
|
+
else if (c === ']') {
|
|
144
|
+
if (depth === 0)
|
|
145
|
+
return i
|
|
146
|
+
depth--
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return -1
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** One whitespace-delimited argument starting at `start` (no leading whitespace), quoted or bare with balanced brackets. */
|
|
153
|
+
function readArgument(text: string, start: number, end: number): { value: string, next: number } {
|
|
154
|
+
if (QUOTES.has(text[start])) {
|
|
155
|
+
const close = skipQuoted(text, start)
|
|
156
|
+
if (close > 0 && close <= end)
|
|
157
|
+
return { value: unescape(text.slice(start, close)), next: close }
|
|
158
|
+
}
|
|
159
|
+
let depth = 0
|
|
160
|
+
let i = start
|
|
161
|
+
for (; i < end; i++) {
|
|
162
|
+
const c = text[i]
|
|
163
|
+
if (/\s/.test(c) && depth === 0)
|
|
164
|
+
break
|
|
165
|
+
if (c === '[')
|
|
166
|
+
depth++
|
|
167
|
+
else if (c === ']')
|
|
168
|
+
depth--
|
|
169
|
+
}
|
|
170
|
+
return { value: text.slice(start, i), next: i }
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function skipSpaces(text: string, i: number): number {
|
|
174
|
+
while (i < text.length && /[ \t]/.test(text[i]))
|
|
175
|
+
i++
|
|
176
|
+
return i
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Parse the marks of a comment body (`[!kind …] [!kind …]`), or `undefined`
|
|
181
|
+
* when the body is not made of marks only — then it is an ordinary comment
|
|
182
|
+
* and stays in the code.
|
|
183
|
+
*/
|
|
184
|
+
export function parseMarkComment(body: string): RawMark[] | undefined {
|
|
185
|
+
const marks: RawMark[] = []
|
|
186
|
+
let i = skipSpaces(body, 0)
|
|
187
|
+
while (i < body.length) {
|
|
188
|
+
if (!body.startsWith('[!', i))
|
|
189
|
+
return undefined
|
|
190
|
+
const open = i
|
|
191
|
+
i += 2
|
|
192
|
+
const kind = /^[A-Za-z][\w-]*/.exec(body.slice(i))?.[0]
|
|
193
|
+
if (!kind)
|
|
194
|
+
return undefined
|
|
195
|
+
i += kind.length
|
|
196
|
+
const end = findMarkEnd(body, open + 2)
|
|
197
|
+
if (end < 0 || (i < end && !/[\s\]]/.test(body[i])))
|
|
198
|
+
return undefined
|
|
199
|
+
const spec = CODE_MARK_KINDS[normalizeMarkKind(kind)]
|
|
200
|
+
|
|
201
|
+
const options: Record<string, string> = {}
|
|
202
|
+
const args: string[] = []
|
|
203
|
+
i = skipSpaces(body, i)
|
|
204
|
+
// Options come first; the first word that is not `key=value` starts the arguments.
|
|
205
|
+
while (i < end) {
|
|
206
|
+
const bare = /^[A-Za-z][\w-]*=/.exec(body.slice(i, end))
|
|
207
|
+
if (!bare)
|
|
208
|
+
break
|
|
209
|
+
const valueStart = i + bare[0].length
|
|
210
|
+
const { value, next } = valueStart < end
|
|
211
|
+
? readArgument(body, valueStart, end)
|
|
212
|
+
: { value: '', next: valueStart }
|
|
213
|
+
options[bare[0].slice(0, -1)] = value
|
|
214
|
+
i = skipSpaces(body, next)
|
|
215
|
+
}
|
|
216
|
+
const slots = spec?.slots.length ?? 2
|
|
217
|
+
for (let slot = 0; slot < slots && i < end; slot++) {
|
|
218
|
+
if (slot === slots - 1) {
|
|
219
|
+
// The last argument runs to the end of the mark; one quoted value
|
|
220
|
+
// loses its quotes, anything else is taken as written.
|
|
221
|
+
const rest = body.slice(i, end).trim()
|
|
222
|
+
if (rest) {
|
|
223
|
+
const quoted = QUOTES.has(rest[0]) && skipQuoted(rest, 0) === rest.length
|
|
224
|
+
args.push(quoted ? unescape(rest) : rest)
|
|
225
|
+
}
|
|
226
|
+
i = end
|
|
227
|
+
break
|
|
228
|
+
}
|
|
229
|
+
const { value, next } = readArgument(body, i, end)
|
|
230
|
+
args.push(value)
|
|
231
|
+
i = skipSpaces(body, next)
|
|
232
|
+
}
|
|
233
|
+
if (body.slice(i, end).trim())
|
|
234
|
+
return undefined
|
|
235
|
+
marks.push({ kind, args, options })
|
|
236
|
+
i = skipSpaces(body, end + 1)
|
|
237
|
+
}
|
|
238
|
+
return marks
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// ---------------------------------------------------------------------------
|
|
242
|
+
// Scanning a fence
|
|
243
|
+
|
|
244
|
+
export interface MarkLine {
|
|
245
|
+
/** Zero-based index of the line in the scanned code. */
|
|
246
|
+
index: number
|
|
247
|
+
/** Column where the comment starts, including the whitespace before it. */
|
|
248
|
+
from: number
|
|
249
|
+
/** The whole line is marks: it leaves the code. */
|
|
250
|
+
removeLine: boolean
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export interface ScannedMarks {
|
|
254
|
+
marks: CodeMark[]
|
|
255
|
+
/** Lines to strip, in order. */
|
|
256
|
+
lines: MarkLine[]
|
|
257
|
+
/** Marks that could not be placed, for the author. */
|
|
258
|
+
problems: string[]
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const COMMENT_CANDIDATE = /\/\/[ \t]*(?=\[!)/g
|
|
262
|
+
const MARK_KIND = /^\[!([A-Za-z][\w-]*)/
|
|
263
|
+
|
|
264
|
+
/** Whether a comment body opens with a kind the theme knows, so a parse failure is worth a warning. */
|
|
265
|
+
function looksLikeMark(body: string): boolean {
|
|
266
|
+
const kind = MARK_KIND.exec(body)?.[1]
|
|
267
|
+
return kind !== undefined && normalizeMarkKind(kind) in CODE_MARK_KINDS
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Find the marks in the lines of a fence. A line that is nothing but a mark
|
|
272
|
+
* comment leaves the code and marks the next code line; line numbers count
|
|
273
|
+
* the lines that stay.
|
|
274
|
+
*/
|
|
275
|
+
export function scanCodeMarks(lines: string[]): ScannedMarks {
|
|
276
|
+
const marks: CodeMark[] = []
|
|
277
|
+
const stripped: MarkLine[] = []
|
|
278
|
+
const problems: string[] = []
|
|
279
|
+
let pending: RawMark[] = []
|
|
280
|
+
let displayed = 0
|
|
281
|
+
for (let index = 0; index < lines.length; index++) {
|
|
282
|
+
const text = lines[index]
|
|
283
|
+
let found: { code: string, from: number, marks: RawMark[] } | undefined
|
|
284
|
+
for (const candidate of text.matchAll(COMMENT_CANDIDATE)) {
|
|
285
|
+
const body = text.slice(candidate.index + candidate[0].length)
|
|
286
|
+
const parsed = parseMarkComment(body)
|
|
287
|
+
if (!parsed) {
|
|
288
|
+
if (looksLikeMark(body))
|
|
289
|
+
problems.push(`Line ${index + 1}: \`${body.trim()}\` looks like a code mark but does not parse: quotes and brackets must balance, and nothing may follow the last \`]\`. The comment stays in the code.`)
|
|
290
|
+
continue
|
|
291
|
+
}
|
|
292
|
+
// A kind the theme does not know stays in the code: Shiki's own
|
|
293
|
+
// `[!code …]` notation silently, anything else with a warning.
|
|
294
|
+
const unknown = parsed.filter(mark => !(normalizeMarkKind(mark.kind) in CODE_MARK_KINDS))
|
|
295
|
+
if (unknown.length) {
|
|
296
|
+
const foreign = unknown.find(mark => normalizeMarkKind(mark.kind) !== 'code')
|
|
297
|
+
if (foreign)
|
|
298
|
+
problems.push(`[!${foreign.kind}] on line ${index + 1} is not a code mark kind (${Object.keys(CODE_MARK_KINDS).join(', ')}). The comment stays in the code.`)
|
|
299
|
+
continue
|
|
300
|
+
}
|
|
301
|
+
const code = text.slice(0, candidate.index).trimEnd()
|
|
302
|
+
found = { code, from: code.length, marks: parsed }
|
|
303
|
+
break
|
|
304
|
+
}
|
|
305
|
+
if (found && found.code.trim() === '') {
|
|
306
|
+
stripped.push({ index, from: 0, removeLine: true })
|
|
307
|
+
pending.push(...found.marks)
|
|
308
|
+
continue
|
|
309
|
+
}
|
|
310
|
+
displayed++
|
|
311
|
+
const code = found ? found.code : text
|
|
312
|
+
if (found)
|
|
313
|
+
stripped.push({ index, from: found.from, removeLine: false })
|
|
314
|
+
const own = [...pending, ...(found?.marks ?? [])]
|
|
315
|
+
pending = []
|
|
316
|
+
for (const raw of own)
|
|
317
|
+
marks.push({ kind: normalizeMarkKind(raw.kind), line: displayed, args: raw.args, options: raw.options, code })
|
|
318
|
+
}
|
|
319
|
+
for (const raw of pending)
|
|
320
|
+
problems.push(`[!${raw.kind}] on the last line marks nothing: a mark-only comment belongs above the line it marks.`)
|
|
321
|
+
return { marks, lines: stripped, problems }
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/** The code without its mark comments, and the marks found, with lines counted in the result. */
|
|
325
|
+
export function parseCodeMarks(code: string): { code: string, marks: CodeMark[], problems: string[] } {
|
|
326
|
+
const lines = code.split(/\r?\n/)
|
|
327
|
+
const scanned = scanCodeMarks(lines)
|
|
328
|
+
if (!scanned.lines.length)
|
|
329
|
+
return { code, marks: [], problems: scanned.problems }
|
|
330
|
+
const kept: string[] = []
|
|
331
|
+
const byIndex = new Map(scanned.lines.map(line => [line.index, line]))
|
|
332
|
+
lines.forEach((text, index) => {
|
|
333
|
+
const line = byIndex.get(index)
|
|
334
|
+
if (!line)
|
|
335
|
+
kept.push(text)
|
|
336
|
+
else if (!line.removeLine)
|
|
337
|
+
kept.push(text.slice(0, line.from))
|
|
338
|
+
})
|
|
339
|
+
return { code: kept.join('\n'), marks: scanned.marks, problems: scanned.problems }
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// ---------------------------------------------------------------------------
|
|
343
|
+
// Rendering the components
|
|
344
|
+
|
|
345
|
+
function attributeValue(value: string): string {
|
|
346
|
+
return value.replace(/&/g, '&').replace(/"/g, '"')
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** Non-overlapping occurrences of `needle` in `text`, as the components count them. */
|
|
350
|
+
export function countOccurrences(text: string, needle: string): number {
|
|
351
|
+
if (!needle)
|
|
352
|
+
return 0
|
|
353
|
+
let count = 0
|
|
354
|
+
for (let at = text.indexOf(needle); at >= 0; at = text.indexOf(needle, at + needle.length))
|
|
355
|
+
count++
|
|
356
|
+
return count
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
export interface CodeMarkTags {
|
|
360
|
+
/** Opening tags, outermost first, each on its own line. */
|
|
361
|
+
open: string[]
|
|
362
|
+
/** Closing tags, in the order they must follow the fence. */
|
|
363
|
+
close: string[]
|
|
364
|
+
problems: string[]
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** The wrapper components for the marks of one fence. */
|
|
368
|
+
export function renderCodeMarkTags(marks: CodeMark[]): CodeMarkTags {
|
|
369
|
+
const open: string[] = []
|
|
370
|
+
const problems: string[] = []
|
|
371
|
+
const lineLists = new Map<string, number[]>()
|
|
372
|
+
for (const mark of marks) {
|
|
373
|
+
const spec = CODE_MARK_KINDS[mark.kind]
|
|
374
|
+
if (!spec) {
|
|
375
|
+
problems.push(`[!${mark.kind}] on line ${mark.line} is not a code mark kind (${Object.keys(CODE_MARK_KINDS).join(', ')}).`)
|
|
376
|
+
continue
|
|
377
|
+
}
|
|
378
|
+
if (spec.lines) {
|
|
379
|
+
const lines = lineLists.get(spec.component) ?? []
|
|
380
|
+
lines.push(mark.line)
|
|
381
|
+
lineLists.set(spec.component, lines)
|
|
382
|
+
continue
|
|
383
|
+
}
|
|
384
|
+
const attrs: Record<string, string> = {}
|
|
385
|
+
spec.slots.forEach((slot, index) => {
|
|
386
|
+
if (mark.args[index] !== undefined)
|
|
387
|
+
attrs[slot] = mark.args[index]
|
|
388
|
+
})
|
|
389
|
+
Object.assign(attrs, mark.options)
|
|
390
|
+
const text = attrs.text ?? spec.text
|
|
391
|
+
if (text !== undefined)
|
|
392
|
+
attrs.text = text
|
|
393
|
+
if (spec.slots[0] === 'text' && text === undefined) {
|
|
394
|
+
problems.push(`[!${mark.kind}] on line ${mark.line} needs the text to mark.`)
|
|
395
|
+
continue
|
|
396
|
+
}
|
|
397
|
+
let occurrences: (number | undefined)[] = [undefined]
|
|
398
|
+
if ('occurrence' in attrs) {
|
|
399
|
+
const wanted = Number(attrs.occurrence)
|
|
400
|
+
if (!Number.isInteger(wanted) || wanted < 1) {
|
|
401
|
+
problems.push(`[!${mark.kind}] on line ${mark.line}: occurrence=${attrs.occurrence} must be a positive whole number.`)
|
|
402
|
+
continue
|
|
403
|
+
}
|
|
404
|
+
delete attrs.occurrence
|
|
405
|
+
occurrences = [wanted]
|
|
406
|
+
}
|
|
407
|
+
else if (text !== undefined) {
|
|
408
|
+
const count = countOccurrences(mark.code, text)
|
|
409
|
+
if (!count) {
|
|
410
|
+
problems.push(`[!${mark.kind}] on line ${mark.line}: "${text}" does not occur in \`${mark.code.trim()}\`.`)
|
|
411
|
+
continue
|
|
412
|
+
}
|
|
413
|
+
occurrences = spec.occurrence === 'all'
|
|
414
|
+
? Array.from({ length: count }, (_, i) => i + 1)
|
|
415
|
+
: [spec.occurrence === 'last' ? count : 1]
|
|
416
|
+
}
|
|
417
|
+
for (const occurrence of occurrences) {
|
|
418
|
+
const parts = [`<${spec.component}`]
|
|
419
|
+
// `text` first, then where it is, then what to say about it.
|
|
420
|
+
if (attrs.text !== undefined)
|
|
421
|
+
parts.push(`text="${attributeValue(attrs.text)}"`)
|
|
422
|
+
parts.push(`:line="${mark.line}"`)
|
|
423
|
+
if (occurrence !== undefined && occurrence !== 1)
|
|
424
|
+
parts.push(`:occurrence="${occurrence}"`)
|
|
425
|
+
for (const [key, value] of Object.entries(attrs)) {
|
|
426
|
+
if (key !== 'text')
|
|
427
|
+
parts.push(`${key}="${attributeValue(value)}"`)
|
|
428
|
+
}
|
|
429
|
+
open.push(`${parts.join(' ')}>`)
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
for (const [component, lines] of lineLists)
|
|
433
|
+
open.push(`<${component} :lines="[${[...new Set(lines)].sort((a, b) => a - b).join(', ')}]">`)
|
|
434
|
+
const close = open.map(tag => `</${/^<(\w+)/.exec(tag)![1]}>`).reverse()
|
|
435
|
+
return { open, close, problems }
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// ---------------------------------------------------------------------------
|
|
439
|
+
// Slide edits
|
|
440
|
+
|
|
441
|
+
export interface CodeMarkInsertion {
|
|
442
|
+
at: number
|
|
443
|
+
text: string
|
|
444
|
+
/** `left` goes before anything else inserted at `at`, `right` after. */
|
|
445
|
+
side: 'left' | 'right'
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
export interface CodeMarkEdits {
|
|
449
|
+
/** Comment removals, disjoint and in source order. */
|
|
450
|
+
removals: SourceEdit[]
|
|
451
|
+
insertions: CodeMarkInsertion[]
|
|
452
|
+
problems: string[]
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
const BACKTICK_FENCE = /^ {0,3}(`{3,})\s*([^\r\n`]*)$/
|
|
456
|
+
const TILDE_FENCE = /^ {0,3}(~{3,})(.*)$/
|
|
457
|
+
|
|
458
|
+
/** Fences that are output or the other side of the code above them, never code of their own. */
|
|
459
|
+
const ATTACHED_LANGS = new Set(['console', 'sql'])
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* The last line of the fences attached directly under the one closing at
|
|
463
|
+
* `close`: a `console` or `sql` fence that starts on the very next line is
|
|
464
|
+
* output or the other side of the same code, rendered as one window, so the
|
|
465
|
+
* wrapper a mark asks for encloses them together, as a hand-written one does.
|
|
466
|
+
* Any other fence is code of its own and gets its own wrappers.
|
|
467
|
+
*/
|
|
468
|
+
function attachedFencesEnd(lines: Line[], close: number): number {
|
|
469
|
+
let end = close
|
|
470
|
+
while (end + 1 < lines.length) {
|
|
471
|
+
const open = lines[end + 1].text.match(BACKTICK_FENCE) ?? lines[end + 1].text.match(TILDE_FENCE)
|
|
472
|
+
if (!open || !ATTACHED_LANGS.has(open[2].trim().match(/^[\w'-]+/)?.[0] ?? ''))
|
|
473
|
+
break
|
|
474
|
+
const marker = open[1]
|
|
475
|
+
let next = -1
|
|
476
|
+
for (let j = end + 2; j < lines.length; j++) {
|
|
477
|
+
if (!/^ {4}/.test(lines[j].text) && lines[j].text.trimStart().startsWith(marker)) {
|
|
478
|
+
next = j
|
|
479
|
+
break
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
if (next < 0)
|
|
483
|
+
break
|
|
484
|
+
end = next
|
|
485
|
+
}
|
|
486
|
+
return end
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* The edits that take the mark comments out of every top-level fence of a
|
|
491
|
+
* slide and wrap the fence in the components they ask for. Lines a fence
|
|
492
|
+
* folds (its imports or its `// Example` prefix, see `code-imports.ts`) are
|
|
493
|
+
* not displayed, so marks on them are left to the fold. Fences inside
|
|
494
|
+
* classic ````md magic-move```` blocks are not wrapped: one component
|
|
495
|
+
* cannot wrap a single step.
|
|
496
|
+
*/
|
|
497
|
+
export function codeMarkEdits(source: string, options: { fold?: boolean } = {}): CodeMarkEdits {
|
|
498
|
+
const fold = options.fold !== false
|
|
499
|
+
const lines = scanLines(source)
|
|
500
|
+
const edits: CodeMarkEdits = { removals: [], insertions: [], problems: [] }
|
|
501
|
+
for (let i = 0; i < lines.length; i++) {
|
|
502
|
+
const text = lines[i].text
|
|
503
|
+
const open = text.match(BACKTICK_FENCE) ?? text.match(TILDE_FENCE)
|
|
504
|
+
if (!open)
|
|
505
|
+
continue
|
|
506
|
+
const marker = open[1]
|
|
507
|
+
const info = open[2].trim()
|
|
508
|
+
let close = -1
|
|
509
|
+
for (let j = i + 1; j < lines.length; j++) {
|
|
510
|
+
if (!/^ {4}/.test(lines[j].text) && lines[j].text.trimStart().startsWith(marker)) {
|
|
511
|
+
close = j
|
|
512
|
+
break
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
if (close < 0)
|
|
516
|
+
break
|
|
517
|
+
if (marker.length > 3 && /(?:^|\s)magic-move(?:\s|$)/.test(info)) {
|
|
518
|
+
// The steps of a classic block cannot be wrapped one by one; a mark in
|
|
519
|
+
// there would stay on the slide while the handout and the snippet CLI
|
|
520
|
+
// strip it, so say so instead of leaving the author to notice.
|
|
521
|
+
for (let j = i + 1; j < close; j++) {
|
|
522
|
+
const step = lines[j].text
|
|
523
|
+
const marked = [...step.matchAll(COMMENT_CANDIDATE)].some((candidate) => {
|
|
524
|
+
const parsed = parseMarkComment(step.slice(candidate.index + candidate[0].length))
|
|
525
|
+
return parsed?.some(mark => normalizeMarkKind(mark.kind) in CODE_MARK_KINDS) ?? false
|
|
526
|
+
})
|
|
527
|
+
if (marked) {
|
|
528
|
+
edits.problems.push(`Line ${j + 1}: code marks inside a classic magic-move block are not supported; wrap the block in the component instead.`)
|
|
529
|
+
break
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
if (marker === '```' && info) {
|
|
534
|
+
const body = lines.slice(i + 1, close)
|
|
535
|
+
const bodyText = body.map(line => line.text).join('\n')
|
|
536
|
+
const split = fold && shouldFoldImports(info) ? splitLeadingImports(bodyText) : undefined
|
|
537
|
+
// What the fold keeps always ends with the tail of the fence; the kept
|
|
538
|
+
// prelude before it (a `package` line) carries no marks.
|
|
539
|
+
const tailStart = split?.bodyStartLine ?? 0
|
|
540
|
+
const displayedBefore = split?.foldStartLine === undefined ? 0 : split.body.split('\n').length - (body.length - tailStart)
|
|
541
|
+
const scanned = scanCodeMarks(body.slice(tailStart).map(line => line.text))
|
|
542
|
+
edits.problems.push(...scanned.problems)
|
|
543
|
+
for (const line of scanned.lines) {
|
|
544
|
+
const source = body[tailStart + line.index]
|
|
545
|
+
edits.removals.push(line.removeLine
|
|
546
|
+
? { start: source.start, end: source.next, text: '' }
|
|
547
|
+
: { start: source.start + line.from, end: source.end, text: '' })
|
|
548
|
+
}
|
|
549
|
+
if (scanned.marks.length) {
|
|
550
|
+
const marks = scanned.marks.map(mark => ({ ...mark, line: mark.line + displayedBefore }))
|
|
551
|
+
const tags = renderCodeMarkTags(marks)
|
|
552
|
+
edits.problems.push(...tags.problems)
|
|
553
|
+
if (tags.open.length) {
|
|
554
|
+
edits.insertions.push({ at: lines[i].start, text: `\n${tags.open.join('\n')}\n\n`, side: 'left' })
|
|
555
|
+
edits.insertions.push({ at: lines[attachedFencesEnd(lines, close)].end, text: `\n\n${tags.close.join('\n')}\n`, side: 'right' })
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
i = close
|
|
560
|
+
}
|
|
561
|
+
return edits
|
|
562
|
+
}
|
package/setup/kotlin-snippets.ts
CHANGED
|
@@ -24,7 +24,8 @@
|
|
|
24
24
|
*
|
|
25
25
|
* Fences with `no-compile` in their info (prose-like sketches, library
|
|
26
26
|
* internals), `gradle` build-script fences, and fences an
|
|
27
|
-
* `InlineCompilerError` wraps
|
|
27
|
+
* `InlineCompilerError` wraps or an `[!error …]` mark asks for (shown broken
|
|
28
|
+
* on purpose) are skipped. The CLI in
|
|
28
29
|
* `bin/kotlin-snippets.mjs` runs `collectSnippetFiles` over a deck and
|
|
29
30
|
* `syncSnippetFiles` writes or checks the result.
|
|
30
31
|
*/
|
|
@@ -32,6 +33,7 @@ import type { SlideInfo } from '@slidev/types'
|
|
|
32
33
|
import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs'
|
|
33
34
|
import { basename, dirname, extname, join, relative } from 'node:path'
|
|
34
35
|
import { NO_COMPILE_FLAG, splitImportBlock, splitLeadingImports } from './code-imports'
|
|
36
|
+
import { hasCompilerErrorMark } from './code-marks'
|
|
35
37
|
import { extractTopLevelFences } from './magic-move-between'
|
|
36
38
|
import type { ParsedFence } from './magic-move-between'
|
|
37
39
|
import { expandMagicMoveBlocks, plainTitle } from './static-content'
|
|
@@ -344,7 +346,7 @@ export function withoutCompilerErrorFences(content: string): string {
|
|
|
344
346
|
export function isCompilable(fence: ParsedFence): boolean {
|
|
345
347
|
if (!['kotlin', 'kt', 'java'].includes(fence.lang))
|
|
346
348
|
return false
|
|
347
|
-
if (fence.flags.includes(NO_COMPILE_FLAG))
|
|
349
|
+
if (fence.flags.includes(NO_COMPILE_FLAG) || hasCompilerErrorMark(fence.marks))
|
|
348
350
|
return false
|
|
349
351
|
return !fence.meta.split(/\s+/).includes('gradle')
|
|
350
352
|
}
|
|
@@ -18,6 +18,8 @@
|
|
|
18
18
|
*/
|
|
19
19
|
import { parseSync } from '@slidev/parser'
|
|
20
20
|
import { shouldFoldImports, splitLeadingImports, takeThemeFlags } from './code-imports'
|
|
21
|
+
import { parseCodeMarks } from './code-marks'
|
|
22
|
+
import type { CodeMark } from './code-marks'
|
|
21
23
|
|
|
22
24
|
export interface ParsedFence {
|
|
23
25
|
/** The Shiki language, i.e. the first word of the fence info. */
|
|
@@ -42,6 +44,8 @@ export interface ParsedFence {
|
|
|
42
44
|
imports: string[]
|
|
43
45
|
/** The code folded above a `// Example` or `// Solution` marker, imports included, if the fence has one. */
|
|
44
46
|
prefix?: string
|
|
47
|
+
/** The `// [!kind …]` marks taken out of `code` (see `code-marks.ts`), with lines counted in `code`. */
|
|
48
|
+
marks: CodeMark[]
|
|
45
49
|
}
|
|
46
50
|
|
|
47
51
|
export interface FenceParseOptions {
|
|
@@ -178,8 +182,12 @@ export function parseFenceInfo(rawInfo: string, code: string, options: FencePars
|
|
|
178
182
|
? true
|
|
179
183
|
: /\blines: *false\b/.test(optionsRaw ?? '') ? false : undefined
|
|
180
184
|
const trimmed = code.replace(/\r?\n$/, '')
|
|
181
|
-
const
|
|
182
|
-
|
|
185
|
+
const split = fold ? splitLeadingImports(trimmed) : { imports: [], body: trimmed, prefix: undefined }
|
|
186
|
+
// Marks leave the code everywhere it is used — rendered, compiled, or
|
|
187
|
+
// printed — and their lines count in the code as displayed, after the fold.
|
|
188
|
+
const { code: body, marks } = parseCodeMarks(split.body)
|
|
189
|
+
const prefix = split.prefix === undefined ? undefined : parseCodeMarks(split.prefix).code
|
|
190
|
+
return { lang, meta, info, flags, title, lines, ranges, optionsRaw, code: body, imports: split.imports, marks, ...(prefix !== undefined && { prefix }) }
|
|
183
191
|
}
|
|
184
192
|
|
|
185
193
|
/**
|
package/setup/main.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { defineAppSetup } from '@slidev/types'
|
|
|
2
2
|
// @ts-ignore Slidev's virtual module, typed by @slidev/client's shim when it is loaded
|
|
3
3
|
import { slides } from '#slidev/slides'
|
|
4
4
|
import { countSlideView, slideNumberFromRoute } from './analytics'
|
|
5
|
+
import { setupRemoteSync } from './remote-sync'
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Count one GoatCounter pageview per slide (see `analytics.ts`). The hook
|
|
@@ -13,9 +14,12 @@ import { countSlideView, slideNumberFromRoute } from './analytics'
|
|
|
13
14
|
* carries no slide data, so the title comes from the slides list, the way
|
|
14
15
|
* Slidev's own table of contents reads it.
|
|
15
16
|
*/
|
|
16
|
-
export default defineAppSetup((
|
|
17
|
+
export default defineAppSetup((context) => {
|
|
18
|
+
const { router } = context
|
|
17
19
|
if (typeof window === 'undefined')
|
|
18
20
|
return
|
|
21
|
+
// Phone remote for a deck served by `slidev-kotlin-present` (see `remote-sync.ts`).
|
|
22
|
+
setupRemoteSync(context)
|
|
19
23
|
let last: number | undefined
|
|
20
24
|
router.afterEach((to) => {
|
|
21
25
|
const no = slideNumberFromRoute(to.path)
|