@mdsvex/source-map 0.1.0-next.1 → 0.1.0-next.3

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.
@@ -0,0 +1,942 @@
1
+ import { compile, RunError, DirectiveError, FrontmatterError, MathError, FootnoteError, SourceError, highlight_error_at } from "mdsvex/compile";
2
+ import { source_math } from "mdsvex/math";
3
+ import { PFMParser } from "@mdsvex/parse";
4
+ import { TreeBuilder } from "@mdsvex/parse/tree-builder";
5
+ import { Cursor } from "@mdsvex/parse/cursor";
6
+ import { K_HTML, K_FRONTMATTER, K_IMPORT_STATEMENT } from "@mdsvex/render/html-cursor";
7
+ function plain(m) {
8
+ const out = {
9
+ sourceOffsets: m.sourceOffsets.slice(),
10
+ generatedOffsets: m.generatedOffsets.slice(),
11
+ lengths: m.lengths.slice(),
12
+ data: m.data
13
+ };
14
+ if (m.generatedLengths) out.generatedLengths = m.generatedLengths.slice();
15
+ return out;
16
+ }
17
+ function apply_edits(code, mappings, edits) {
18
+ if (edits.length === 0) return { code, mappings };
19
+ const sorted = edits.slice().sort((a, b) => a.start - b.start || a.end - b.end);
20
+ let out = "";
21
+ let at = 0;
22
+ const placed = [];
23
+ for (const e of sorted) {
24
+ out += code.slice(at, e.start);
25
+ placed.push(out.length);
26
+ out += e.text;
27
+ at = e.end;
28
+ }
29
+ out += code.slice(at);
30
+ const start_of = (p) => {
31
+ let d = 0;
32
+ for (const e of sorted) {
33
+ if (e.end <= p) d += e.text.length - (e.end - e.start);
34
+ else break;
35
+ }
36
+ return p + d;
37
+ };
38
+ const end_of = (p) => {
39
+ let d = 0;
40
+ for (const e of sorted) {
41
+ if (e.start < p) d += e.text.length - (e.end - e.start);
42
+ else break;
43
+ }
44
+ return p + d;
45
+ };
46
+ const result = [];
47
+ for (const m of mappings) {
48
+ const moved = plain(m);
49
+ for (let i = 0; i < moved.generatedOffsets.length; i++) {
50
+ const g = moved.generatedOffsets[i];
51
+ const len = moved.generatedLengths ? moved.generatedLengths[i] : moved.lengths[i];
52
+ const s = start_of(g);
53
+ const e = len === 0 ? s : end_of(g + len);
54
+ moved.generatedOffsets[i] = s;
55
+ if (e - s !== len) moved.generatedLengths ??= moved.lengths.slice();
56
+ if (moved.generatedLengths) moved.generatedLengths[i] = e - s;
57
+ }
58
+ result.push(moved);
59
+ }
60
+ for (let k = 0; k < sorted.length; k++) {
61
+ const e = sorted[k];
62
+ if (!e.mappings) continue;
63
+ for (const m of e.mappings) {
64
+ const moved = plain(m);
65
+ moved.generatedOffsets = moved.generatedOffsets.map((g) => g + placed[k]);
66
+ result.push(moved);
67
+ }
68
+ }
69
+ return { code: out, mappings: result };
70
+ }
71
+ const PLAIN_KEY = /^([^\s#'"\-?:[\]{},&*!|>%@`][^\n]*?)[ \t]*:(?=[ \t]|$)/;
72
+ function yaml_keys(yaml, base) {
73
+ const keys = /* @__PURE__ */ new Map();
74
+ let line_start = 0;
75
+ while (line_start <= yaml.length) {
76
+ let line_end = yaml.indexOf("\n", line_start);
77
+ if (line_end < 0) line_end = yaml.length;
78
+ const line = yaml.slice(line_start, line_end).replace(/\r$/, "");
79
+ const found = top_key(line);
80
+ if (found !== null && !keys.has(found.name)) {
81
+ keys.set(found.name, {
82
+ start: base + line_start + found.start,
83
+ end: base + line_start + found.end
84
+ });
85
+ }
86
+ line_start = line_end + 1;
87
+ }
88
+ return keys;
89
+ }
90
+ function top_key(line) {
91
+ const q = line[0];
92
+ if (q === '"' || q === "'") {
93
+ let i = 1;
94
+ let name = "";
95
+ for (; i < line.length; i++) {
96
+ const c = line[i];
97
+ if (q === '"' && c === "\\") {
98
+ i++;
99
+ continue;
100
+ }
101
+ if (c === q) {
102
+ if (q === "'" && line[i + 1] === "'") {
103
+ i++;
104
+ continue;
105
+ }
106
+ break;
107
+ }
108
+ }
109
+ if (i >= line.length) return null;
110
+ const inner = line.slice(1, i);
111
+ if (!/^[ \t]*:(?:[ \t]|$)/.test(line.slice(i + 1))) return null;
112
+ if (q === "'") name = inner.replace(/''/g, "'");
113
+ else {
114
+ try {
115
+ name = JSON.parse('"' + inner + '"');
116
+ } catch {
117
+ name = inner;
118
+ }
119
+ }
120
+ return { name, start: 1, end: i };
121
+ }
122
+ const m = PLAIN_KEY.exec(line);
123
+ if (m === null) return null;
124
+ return { name: m[1], start: 0, end: m[1].length };
125
+ }
126
+ const TEMPLATE_KEY = /^(["']?)template\1[ \t]*:[ \t]*/;
127
+ function template_value_at(source, offset, frontmatter) {
128
+ if (offset < frontmatter.start || offset > frontmatter.end) return null;
129
+ const line_start = source.lastIndexOf("\n", offset - 1) + 1;
130
+ let line_end = source.indexOf("\n", offset);
131
+ if (line_end < 0 || line_end > frontmatter.end) line_end = frontmatter.end;
132
+ const line = source.slice(line_start, line_end).replace(/\r$/, "");
133
+ const key = TEMPLATE_KEY.exec(line);
134
+ if (key === null) return null;
135
+ const start = line_start + key[0].length;
136
+ if (offset < start) return null;
137
+ const rest = line.slice(key[0].length).replace(/[ \t]+#.*$/, "");
138
+ const end = Math.max(start + rest.trimEnd().length, offset);
139
+ return { start, end };
140
+ }
141
+ const HOVER = { semantic: true, navigation: true };
142
+ const CHECK = { verification: true };
143
+ const PROP = {
144
+ verification: true,
145
+ semantic: true,
146
+ navigation: true
147
+ };
148
+ const NONE = {};
149
+ const UNKNOWN_PROP = /* @__PURE__ */ new Set([2353, 2561]);
150
+ const MARKDOWN_PROP = {
151
+ verification: {
152
+ shouldReport: (_source, code) => !UNKNOWN_PROP.has(Number(code))
153
+ }
154
+ };
155
+ function editor_data(caps) {
156
+ return { ...caps, nodeIndex: -1, role: "content", editor: true };
157
+ }
158
+ function mapping(source, source_length, generated, generated_length, caps) {
159
+ const m = {
160
+ sourceOffsets: [source],
161
+ generatedOffsets: [generated],
162
+ lengths: [source_length],
163
+ data: editor_data(caps)
164
+ };
165
+ if (generated_length !== source_length)
166
+ m.generatedLengths = [generated_length];
167
+ return m;
168
+ }
169
+ function classify(source, syntax) {
170
+ const tree = new TreeBuilder(source.length >> 3 || 128);
171
+ new PFMParser(tree, 2, syntax).parse(source);
172
+ const cursor = new Cursor(tree.get_buffer(), source);
173
+ cursor.reset();
174
+ const regions = { frontmatter: null, excluded: [], styles: [] };
175
+ if (!cursor.goto_first_child()) return regions;
176
+ do {
177
+ const kind = cursor.kind;
178
+ const tag = kind === K_HTML ? cursor.meta()?.tag : void 0;
179
+ if (kind === K_FRONTMATTER) {
180
+ regions.frontmatter = {
181
+ start: cursor.value_start,
182
+ end: cursor.value_end
183
+ };
184
+ } else if (kind === K_IMPORT_STATEMENT || tag === "script") ;
185
+ else if (tag === "style") {
186
+ regions.styles.push({
187
+ valueStart: cursor.value_start,
188
+ valueEnd: cursor.value_end
189
+ });
190
+ } else continue;
191
+ regions.excluded.push({ start: cursor.start, end: cursor.end });
192
+ } while (cursor.goto_next_sibling());
193
+ return regions;
194
+ }
195
+ function offset_of(source, line, column) {
196
+ let at = 0;
197
+ for (let l = 1; l < line; l++) {
198
+ const nl = source.indexOf("\n", at);
199
+ if (nl < 0) return source.length;
200
+ at = nl + 1;
201
+ }
202
+ return Math.min(at + Math.max(column - 1, 0), source.length);
203
+ }
204
+ function line_range(source, line) {
205
+ const start = offset_of(source, line, 1);
206
+ let end = source.indexOf("\n", start);
207
+ if (end < 0) end = source.length;
208
+ let s = start;
209
+ while (s < end && /\s/.test(source[s])) s++;
210
+ let e = end;
211
+ while (e > s && /\s/.test(source[e - 1])) e--;
212
+ return e > s ? { start: s, end: e } : { start, end };
213
+ }
214
+ function without_directives(templates) {
215
+ if (templates === void 0) return void 0;
216
+ const out = {};
217
+ for (const name in templates) {
218
+ const { directives: _, ...rest } = templates[name];
219
+ out[name] = rest;
220
+ }
221
+ return out;
222
+ }
223
+ function compile_leniently(source, options, diagnostics, frontmatter) {
224
+ let opts = {
225
+ ...options.compile,
226
+ sourcemap: true,
227
+ strict_directives: options.report_directives === true,
228
+ // a first compile with a specifier nothing reads says whether each block can run
229
+ snippets: () => "mdsvex:snippet"
230
+ };
231
+ let metadata_known = true;
232
+ let last;
233
+ for (let attempt = 0; attempt < 8; attempt++) {
234
+ try {
235
+ const result = compile(source, opts);
236
+ if (result.snippets !== void 0 && opts.snippets !== false) {
237
+ opts = { ...opts, snippets: false };
238
+ continue;
239
+ }
240
+ return { result, metadata_known };
241
+ } catch (e) {
242
+ last = e;
243
+ if (e instanceof RunError && opts.snippets !== false) {
244
+ if (e.code !== "run_pre" || options.report_missing_pre === true) {
245
+ diagnostics.push({
246
+ ...line_range(source, e.line),
247
+ message: e.message,
248
+ severity: "error"
249
+ });
250
+ }
251
+ opts = { ...opts, snippets: false };
252
+ continue;
253
+ }
254
+ if (e instanceof DirectiveError) {
255
+ const start = offset_of(source, e.line, e.column);
256
+ diagnostics.push({
257
+ start,
258
+ end: Math.min(start + e.directive.length, source.length),
259
+ message: e.message,
260
+ severity: "error"
261
+ });
262
+ opts = opts.strict_directives ? { ...opts, strict_directives: false } : {
263
+ ...opts,
264
+ directives: void 0,
265
+ templates: without_directives(opts.templates)
266
+ };
267
+ continue;
268
+ }
269
+ if (e instanceof FrontmatterError) {
270
+ const unparsed = /^(?:Unsupported frontmatter|frontmatter\.parse)/.test(
271
+ e.message
272
+ );
273
+ if (!(unparsed && options.lenient_frontmatter)) {
274
+ const line = line_range(source, e.line);
275
+ const value = unparsed || frontmatter === null ? null : template_value_at(source, line.end, frontmatter);
276
+ diagnostics.push({
277
+ ...value !== null && value.end > value.start ? value : line,
278
+ message: e.message,
279
+ severity: "error"
280
+ });
281
+ }
282
+ if (unparsed) {
283
+ opts = { ...opts, frontmatter: { parse: () => ({}) } };
284
+ metadata_known = false;
285
+ } else opts = { ...opts, template: false };
286
+ continue;
287
+ }
288
+ if (e instanceof MathError && opts.math !== source_math) {
289
+ const start = offset_of(source, e.line, e.column);
290
+ diagnostics.push({
291
+ start,
292
+ end: Math.min(start + 1, source.length),
293
+ message: e.message,
294
+ severity: "error"
295
+ });
296
+ opts = { ...opts, math: source_math };
297
+ continue;
298
+ }
299
+ if (e instanceof FootnoteError && opts.footnotes?.strict !== false) {
300
+ for (const problem of e.problems) {
301
+ const start = offset_of(source, problem.line, problem.column);
302
+ const end = problem.code === "footnote_undefined" ? start + problem.label.length + 3 : start + problem.label.length + 4;
303
+ diagnostics.push({
304
+ start,
305
+ end: Math.min(end, source.length),
306
+ message: problem === e.problems[0] ? e.message : problem.code === "footnote_undefined" ? `the footnote reference [^${problem.label}] has no definition` : `the footnote [^${problem.label}] is defined twice`,
307
+ severity: "error"
308
+ });
309
+ }
310
+ opts = { ...opts, footnotes: { ...opts.footnotes, strict: false } };
311
+ continue;
312
+ }
313
+ if (e instanceof SourceError && opts.sources !== false) {
314
+ if (e.code !== "src_missing") {
315
+ const start = offset_of(source, e.line, e.column);
316
+ diagnostics.push({
317
+ start,
318
+ end: Math.min(start + 1, source.length),
319
+ message: e.message,
320
+ severity: "error"
321
+ });
322
+ }
323
+ opts = { ...opts, sources: false };
324
+ continue;
325
+ }
326
+ const at = highlight_error_at(e);
327
+ if (at !== null && opts.highlight) {
328
+ const start = offset_of(source, at.line, at.column);
329
+ diagnostics.push({
330
+ start,
331
+ end: Math.min(start + 1, source.length),
332
+ message: e.message,
333
+ severity: "error"
334
+ });
335
+ opts = { ...opts, highlight: false };
336
+ continue;
337
+ }
338
+ throw e;
339
+ }
340
+ }
341
+ throw last;
342
+ }
343
+ const METADATA_EXPORT = "export const metadata = ";
344
+ const PLUGIN_DATA_EXPORT = "export const plugin_data = ";
345
+ const PLUGIN_DATA_DECLARATION = "export declare const plugin_data: import('mdsvex').PluginData;\n";
346
+ const MODULE_SCRIPT = /<script\b[^>]*\bmodule\b[^>]*>/;
347
+ function declare_plugin_data(code, metadata_at) {
348
+ if (metadata_at >= 0)
349
+ return {
350
+ start: metadata_at,
351
+ end: metadata_at,
352
+ text: PLUGIN_DATA_DECLARATION
353
+ };
354
+ const own = MODULE_SCRIPT.exec(code);
355
+ if (own === null)
356
+ return {
357
+ start: 0,
358
+ end: 0,
359
+ text: "<script module>\n" + PLUGIN_DATA_DECLARATION + "<\/script>"
360
+ };
361
+ const at = own.index + own[0].length;
362
+ return { start: at, end: at, text: "\n" + PLUGIN_DATA_DECLARATION };
363
+ }
364
+ const IMPORT_LINE = /^import (.+) from '(mdsvex:[^']*)';$/gm;
365
+ const NAMED = /^\{ (.*) \}$/;
366
+ const IDENTIFIER = /^[A-Za-z_$][\w$]*$/;
367
+ const has_own = Object.prototype.hasOwnProperty;
368
+ const TAG = /<(?=\/?(?:script|style))/gi;
369
+ function props_probes(options) {
370
+ const list = [];
371
+ let text = "";
372
+ const resolve = options.resolve;
373
+ if (resolve === void 0) return { text, list };
374
+ const add = (kind, name, template, specifier, member) => {
375
+ const file = resolve(specifier, member);
376
+ if (file === void 0) return;
377
+ const alias = `__mdsvex_props_${list.length}`;
378
+ text += `type ${alias} = import('svelte').ComponentProps<typeof import(${JSON.stringify(file)})[${JSON.stringify(member)}]>;
379
+ `;
380
+ list.push(
381
+ template === void 0 ? { kind, name, alias } : { kind, name, template, alias }
382
+ );
383
+ };
384
+ const templates = options.compile?.templates ?? {};
385
+ for (const name of Object.keys(templates)) {
386
+ const entry = templates[name];
387
+ add("template", name, void 0, entry.specifier, "default");
388
+ for (const d of entry.directives ?? [])
389
+ for (const n of d.names) add("directive", n, name, d.specifier, n);
390
+ }
391
+ for (const d of options.compile?.directives ?? [])
392
+ for (const n of d.names) add("directive", n, void 0, d.specifier, n);
393
+ return { text, list };
394
+ }
395
+ function object_literal(metadata, keys, caps, open, close, types = {}) {
396
+ let text = open;
397
+ const mappings = [];
398
+ let first = true;
399
+ for (const name of Object.keys(metadata)) {
400
+ const json = JSON.stringify(metadata[name]);
401
+ if (json === void 0) continue;
402
+ if (!first) text += ", ";
403
+ first = false;
404
+ const written = IDENTIFIER.test(name) ? name : JSON.stringify(name);
405
+ const range = keys.get(name);
406
+ if (range !== void 0)
407
+ mappings.push(
408
+ mapping(
409
+ range.start,
410
+ range.end - range.start,
411
+ text.length,
412
+ written.length,
413
+ caps
414
+ )
415
+ );
416
+ text += `${written}: ${json.replace(TAG, "\\u003c")}`;
417
+ if (has_own.call(types, name)) text += ` as ${types[name]}`;
418
+ }
419
+ return { text: text + close, mappings };
420
+ }
421
+ function rewrite_imports(code, resolve, edits) {
422
+ const imported = /* @__PURE__ */ new Map();
423
+ if (resolve === void 0) return imported;
424
+ IMPORT_LINE.lastIndex = 0;
425
+ let line;
426
+ while ((line = IMPORT_LINE.exec(code)) !== null) {
427
+ const id = line[2];
428
+ const bindings = [];
429
+ for (const part of split_clause(line[1])) {
430
+ const named = NAMED.exec(part);
431
+ if (named === null) bindings.push({ name: "default", local: part });
432
+ else
433
+ for (const spec of named[1].split(", ")) {
434
+ const as = spec.lastIndexOf(" as ");
435
+ const name = spec.slice(0, as);
436
+ bindings.push({
437
+ name: name.startsWith('"') ? JSON.parse(name) : name,
438
+ local: spec.slice(as + 4)
439
+ });
440
+ }
441
+ }
442
+ const edit = {
443
+ start: line.index,
444
+ end: line.index + line[0].length,
445
+ text: "",
446
+ mappings: []
447
+ };
448
+ const kept = [];
449
+ for (const b of bindings) {
450
+ const spec = resolve(id, b.name);
451
+ if (spec === void 0) {
452
+ const written = IDENTIFIER.test(b.name) ? b.name : JSON.stringify(b.name);
453
+ kept.push(`${written} as ${b.local}`);
454
+ continue;
455
+ }
456
+ const from = ` from ${JSON.stringify(spec)};
457
+ `;
458
+ if (b.name === "default") {
459
+ edit.text += "import ";
460
+ imported.set(b.local, {
461
+ name: "default",
462
+ edit,
463
+ at: edit.text.length,
464
+ length: b.local.length
465
+ });
466
+ edit.text += b.local + from;
467
+ } else {
468
+ const written = IDENTIFIER.test(b.name) ? b.name : JSON.stringify(b.name);
469
+ edit.text += "import { ";
470
+ imported.set(b.local, {
471
+ name: b.name,
472
+ edit,
473
+ at: edit.text.length,
474
+ length: written.length
475
+ });
476
+ edit.text += `${written} as ${b.local} }` + from;
477
+ }
478
+ }
479
+ if (kept.length === bindings.length) continue;
480
+ if (kept.length !== 0)
481
+ edit.text += `import { ${kept.join(", ")} } from '${id}';
482
+ `;
483
+ edit.text = edit.text.slice(0, -1);
484
+ edits.push(edit);
485
+ }
486
+ return imported;
487
+ }
488
+ function split_clause(clause) {
489
+ const brace = clause.indexOf("{");
490
+ if (brace <= 0) return [clause];
491
+ return [clause.slice(0, brace).replace(/,\s*$/, ""), clause.slice(brace)];
492
+ }
493
+ function generated_range(m) {
494
+ const g = m.generatedOffsets[0];
495
+ return [g, g + (m.generatedLengths ? m.generatedLengths[0] : m.lengths[0])];
496
+ }
497
+ function trimmed(source, start, end) {
498
+ while (start < end && /\s/.test(source[start])) start++;
499
+ while (end > start && /\s/.test(source[end - 1])) end--;
500
+ return end > start ? { start, end } : null;
501
+ }
502
+ function anchor_of(source, nodes) {
503
+ const node = nodes.node;
504
+ const ns = node.sourceOffsets[0];
505
+ const name = /^(<|:+)([A-Za-z_][\w:-]*)/.exec(source.slice(ns, ns + 128));
506
+ if (name !== null) {
507
+ const at = ns + name[1].length;
508
+ return { start: at, end: at + name[2].length };
509
+ }
510
+ const open = nodes.open;
511
+ if (open !== void 0 && open.lengths[0] > 0) {
512
+ const s = open.sourceOffsets[0];
513
+ const t = trimmed(source, s, s + open.lengths[0]);
514
+ if (t !== null) return t;
515
+ }
516
+ const close = nodes.close;
517
+ if (close !== void 0 && close.lengths[0] > 0) {
518
+ const s = close.sourceOffsets[0];
519
+ const t = trimmed(source, s, s + close.lengths[0]);
520
+ if (t !== null) return t;
521
+ }
522
+ if (node.lengths[0] === 0) return null;
523
+ return { start: ns, end: ns + 1 };
524
+ }
525
+ function attribute_parts(text) {
526
+ const parts = [];
527
+ let i = 0;
528
+ const braces = (from) => {
529
+ let depth = 0;
530
+ for (let j = from; j < text.length; j++) {
531
+ const c = text[j];
532
+ if (c === '"' || c === "'" || c === "`") {
533
+ const end = text.indexOf(c, j + 1);
534
+ j = end < 0 ? text.length : end;
535
+ } else if (c === "{") depth++;
536
+ else if (c === "}" && --depth === 0) return j + 1;
537
+ }
538
+ return text.length;
539
+ };
540
+ while (i < text.length) {
541
+ const c = text[i];
542
+ if (/\s|\//.test(c)) {
543
+ i++;
544
+ } else if (c === "{") {
545
+ const end = braces(i);
546
+ parts.push({ start: i, end, name: false });
547
+ i = end;
548
+ } else {
549
+ const name = /^[^\s=>/"'{]+/.exec(text.slice(i));
550
+ if (name === null) {
551
+ i++;
552
+ continue;
553
+ }
554
+ parts.push({ start: i, end: i + name[0].length, name: true });
555
+ i += name[0].length;
556
+ if (text[i] !== "=") continue;
557
+ i++;
558
+ const q = text[i];
559
+ let end;
560
+ if (q === '"' || q === "'") {
561
+ const close = text.indexOf(q, i + 1);
562
+ end = close < 0 ? text.length : close + 1;
563
+ } else if (q === "{") end = braces(i);
564
+ else end = i + (/^[^\s>]*/.exec(text.slice(i))?.[0].length ?? 0);
565
+ parts.push({ start: i, end, name: false });
566
+ i = end;
567
+ }
568
+ }
569
+ return parts;
570
+ }
571
+ function directive_args(source, start, end) {
572
+ let i = start;
573
+ while (i < end && source[i] === ":") i++;
574
+ while (i < end && /[\w-]/.test(source[i])) i++;
575
+ if (source[i] === "[") {
576
+ let depth = 0;
577
+ for (; i < end; i++) {
578
+ const c = source[i];
579
+ if (c === "\\") i++;
580
+ else if (c === "[") depth++;
581
+ else if (c === "]" && --depth === 0) break;
582
+ }
583
+ i++;
584
+ }
585
+ if (source[i] !== "(") return [];
586
+ const args = [];
587
+ i++;
588
+ const ARG = /\s*([A-Za-z_][\w-]*)\s*=\s*(?:"([^"]*)"|'([^']*)'|([^,)\s]*))\s*(,|\))/y;
589
+ ARG.lastIndex = i;
590
+ let m;
591
+ while (ARG.lastIndex < end && (m = ARG.exec(source)) !== null) {
592
+ const key_at = m.index + m[0].indexOf(m[1]);
593
+ const raw = m[2] ?? m[3] ?? m[4];
594
+ const quoted = m[2] !== void 0 || m[3] !== void 0;
595
+ const close = m.index + m[0].length - 1;
596
+ let value_end = close;
597
+ while (/[\s,)]/.test(source[value_end - 1]) && value_end > key_at)
598
+ value_end--;
599
+ if (quoted) value_end--;
600
+ args.push({
601
+ key: { start: key_at, end: key_at + m[1].length },
602
+ value: { start: value_end - raw.length, end: value_end }
603
+ });
604
+ if (m[5] === ")") break;
605
+ }
606
+ return args;
607
+ }
608
+ function tag_end(code, from) {
609
+ let depth = 0;
610
+ for (let i = from; i < code.length; i++) {
611
+ const c = code[i];
612
+ if (c === '"' || c === "'" || c === "`") {
613
+ const close = code.indexOf(c, i + 1);
614
+ if (close < 0) return code.length;
615
+ i = close;
616
+ } else if (c === "{") depth++;
617
+ else if (c === "}") depth--;
618
+ else if (c === ">" && depth === 0) return i;
619
+ }
620
+ return code.length;
621
+ }
622
+ function closing(source, start, end, open, close) {
623
+ let depth = 0;
624
+ for (let i = start; i < end; i++) {
625
+ const c = source[i];
626
+ if (c === "\\") i++;
627
+ else if (c === open) depth++;
628
+ else if (c === close && --depth === 0) return i;
629
+ }
630
+ return -1;
631
+ }
632
+ function inline_link(source, start, end) {
633
+ let i = source[start] === "!" ? start + 1 : start;
634
+ if (source[i] !== "[") return null;
635
+ const label_end = closing(source, i, end, "[", "]");
636
+ if (label_end < 0 || source[label_end + 1] !== "(") return null;
637
+ const link = {
638
+ label: { start: i + 1, end: label_end },
639
+ destination: null,
640
+ title: null
641
+ };
642
+ const close = closing(source, label_end + 1, end, "(", ")");
643
+ if (close < 0) return link;
644
+ i = label_end + 2;
645
+ while (i < close && /\s/.test(source[i])) i++;
646
+ if (source[i] === "<") {
647
+ const d = source.indexOf(">", i);
648
+ if (d < 0 || d > close) return link;
649
+ link.destination = { start: i + 1, end: d };
650
+ i = d + 1;
651
+ } else {
652
+ const d = i;
653
+ let depth = 0;
654
+ for (; i < close; i++) {
655
+ const c = source[i];
656
+ if (c === "\\") i++;
657
+ else if (/\s/.test(c)) break;
658
+ else if (c === "(") depth++;
659
+ else if (c === ")" && --depth < 0) break;
660
+ }
661
+ if (i > d) link.destination = { start: d, end: i };
662
+ }
663
+ while (i < close && /\s/.test(source[i])) i++;
664
+ const q = source[i];
665
+ const title_end = q === "(" ? closing(source, i, close + 1, "(", ")") : source.indexOf(q, i + 1);
666
+ if ((q === '"' || q === "'" || q === "(") && title_end > i && title_end <= close)
667
+ link.title = { start: i + 1, end: title_end };
668
+ return link;
669
+ }
670
+ function markdown_props(source, code, nodes, local_length, anchor) {
671
+ const node = nodes.node;
672
+ const ns = node.sourceOffsets[0];
673
+ const whole = trimmed(source, ns, ns + node.lengths[0]);
674
+ const [g] = generated_range(node);
675
+ const from = g + 1 + local_length;
676
+ const attributes = code.slice(from, tag_end(code, from));
677
+ const linked = source[ns] === "!" || source[ns] === "[";
678
+ const link = linked ? inline_link(source, ns, ns + node.lengths[0]) : null;
679
+ const out = [];
680
+ for (const part of attribute_parts(attributes)) {
681
+ if (!part.name) continue;
682
+ const name = attributes.slice(part.start, part.end);
683
+ let at = linked ? whole : anchor;
684
+ if (link !== null) {
685
+ if (name === "src" || name === "href") at = link.destination ?? at;
686
+ else if (name === "alt") at = link.label;
687
+ else if (name === "title") at = link.title ?? at;
688
+ }
689
+ if (at === null || at.end <= at.start) at = whole;
690
+ if (at === null) continue;
691
+ out.push(
692
+ mapping(
693
+ at.start,
694
+ at.end - at.start,
695
+ from + part.start,
696
+ name.length,
697
+ MARKDOWN_PROP
698
+ )
699
+ );
700
+ }
701
+ return out;
702
+ }
703
+ const ATTRIBUTE_NAME = {
704
+ completion: true,
705
+ semantic: true,
706
+ navigation: true
707
+ };
708
+ const ATTRIBUTE_VALUE = {
709
+ verification: true,
710
+ completion: true,
711
+ semantic: true,
712
+ navigation: true
713
+ };
714
+ function split_attributes(m, code) {
715
+ const len = m.lengths[0];
716
+ if (m.generatedLengths && m.generatedLengths[0] !== len) return [];
717
+ const g = m.generatedOffsets[0];
718
+ const s = m.sourceOffsets[0];
719
+ m.data = editor_data(NONE);
720
+ return attribute_parts(code.slice(g, g + len)).map(
721
+ (p) => mapping(
722
+ s + p.start,
723
+ p.end - p.start,
724
+ g + p.start,
725
+ p.end - p.start,
726
+ p.name ? ATTRIBUTE_NAME : ATTRIBUTE_VALUE
727
+ )
728
+ );
729
+ }
730
+ function pfmToSvelte(source, options = {}) {
731
+ const regions = classify(source, options.compile);
732
+ const diagnostics = [];
733
+ const { result, metadata_known } = compile_leniently(
734
+ source,
735
+ options,
736
+ diagnostics,
737
+ regions.frontmatter
738
+ );
739
+ const original = result.code;
740
+ const mappings = (result.mappings ?? []).map(plain);
741
+ const edits = [];
742
+ const metadata = metadata_known ? result.metadata : void 0;
743
+ for (const w of result.warnings ?? []) {
744
+ const start = offset_of(source, w.start.line, w.start.column + 1);
745
+ if ((w.code === "footnote_undefined" || w.code === "footnote_duplicate") && diagnostics.some((d) => d.start === start && d.severity === "error"))
746
+ continue;
747
+ const tag = /^<([^\s>]*)/.exec(w.message)?.[1] ?? "";
748
+ diagnostics.push({
749
+ start,
750
+ end: Math.min(start + 1 + tag.length, source.length),
751
+ message: w.message,
752
+ severity: "warning"
753
+ });
754
+ }
755
+ const keys = regions.frontmatter === null ? /* @__PURE__ */ new Map() : yaml_keys(
756
+ source.slice(regions.frontmatter.start, regions.frontmatter.end),
757
+ regions.frontmatter.start
758
+ );
759
+ const exported = original.indexOf(METADATA_EXPORT);
760
+ if (exported >= 0) {
761
+ const json = exported + METADATA_EXPORT.length;
762
+ if (!metadata_known) {
763
+ edits.push({
764
+ start: exported,
765
+ end: json,
766
+ text: "export const metadata: Record<string, any> = "
767
+ });
768
+ } else if (metadata !== void 0) {
769
+ const end = original.indexOf(";\n", json);
770
+ const names = Object.keys(options.compile?.templates ?? {});
771
+ const types = names.length === 0 ? {} : {
772
+ template: [...names.map((n) => JSON.stringify(n)), "false"].join(
773
+ " | "
774
+ )
775
+ };
776
+ const object = object_literal(metadata, keys, HOVER, "{", "}", types);
777
+ edits.push({
778
+ start: json,
779
+ end: end < 0 ? original.length : end,
780
+ text: object.text,
781
+ mappings: object.mappings
782
+ });
783
+ }
784
+ }
785
+ if (options.declare_plugin_data === true && original.indexOf(PLUGIN_DATA_EXPORT) < 0)
786
+ edits.push(declare_plugin_data(original, exported));
787
+ const imported = rewrite_imports(original, options.resolve, edits);
788
+ if (imported.size !== 0) {
789
+ const by_node = /* @__PURE__ */ new Map();
790
+ const typed = /* @__PURE__ */ new Map();
791
+ for (const m of mappings) {
792
+ const data = m.data;
793
+ if (data.editor || data.nodeIndex < 0) continue;
794
+ let entry = by_node.get(data.nodeIndex);
795
+ if (entry === void 0) by_node.set(data.nodeIndex, entry = {});
796
+ if (data.role === "node") entry.node ??= m;
797
+ else if (data.role === "open_syntax") entry.open ??= m;
798
+ else if (data.role === "close_syntax") entry.close ??= m;
799
+ else if (data.role === "content") {
800
+ const list = typed.get(data.nodeIndex);
801
+ if (list === void 0) typed.set(data.nodeIndex, [m]);
802
+ else list.push(m);
803
+ }
804
+ }
805
+ for (const nodes of by_node.values()) {
806
+ if (nodes.node === void 0) continue;
807
+ const [g] = generated_range(nodes.node);
808
+ const local = /^<([A-Za-z_$][\w$]*)/.exec(
809
+ original.slice(g, g + 200)
810
+ )?.[1];
811
+ const target = local === void 0 ? void 0 : imported.get(local);
812
+ if (target === void 0) continue;
813
+ for (const m of [nodes.open, nodes.close])
814
+ if (m !== void 0) m.data = editor_data(NONE);
815
+ for (const m of typed.get(nodes.node.data.nodeIndex) ?? [])
816
+ mappings.push(...split_attributes(m, original));
817
+ const anchor = anchor_of(source, nodes);
818
+ if (anchor !== null) {
819
+ target.edit.mappings.push(
820
+ mapping(
821
+ anchor.start,
822
+ anchor.end - anchor.start,
823
+ target.at,
824
+ target.length,
825
+ HOVER
826
+ )
827
+ );
828
+ }
829
+ if (!local.includes("_MDSVEX_D_")) {
830
+ if (!typed.has(nodes.node.data.nodeIndex))
831
+ mappings.push(
832
+ ...markdown_props(source, original, nodes, local.length, anchor)
833
+ );
834
+ continue;
835
+ }
836
+ if (nodes.open === void 0) continue;
837
+ const [open_start, open_end] = generated_range(nodes.open);
838
+ const tag_end2 = original.indexOf(">", open_start);
839
+ const node_start = nodes.node.sourceOffsets[0];
840
+ let from = open_start;
841
+ for (const arg of directive_args(
842
+ source,
843
+ node_start,
844
+ node_start + nodes.node.lengths[0]
845
+ )) {
846
+ const key = source.slice(arg.key.start, arg.key.end);
847
+ const at = original.indexOf(` ${key}=`, from);
848
+ if (at < 0 || at > Math.min(tag_end2, open_end)) break;
849
+ const key_at = at + 1;
850
+ mappings.push(
851
+ mapping(arg.key.start, key.length, key_at, key.length, PROP)
852
+ );
853
+ const value_at = key_at + key.length + 1;
854
+ if (original[value_at] === '"') {
855
+ const value_end = original.indexOf('"', value_at + 1);
856
+ mappings.push(
857
+ mapping(
858
+ arg.value.start,
859
+ arg.value.end - arg.value.start,
860
+ value_at + 1,
861
+ value_end - value_at - 1,
862
+ PROP
863
+ )
864
+ );
865
+ from = value_end;
866
+ } else from = value_at;
867
+ }
868
+ }
869
+ }
870
+ const template_import = [...imported].find(
871
+ ([, i]) => i.name === "default" && result.template !== void 0
872
+ );
873
+ if (template_import !== void 0 && metadata !== void 0) {
874
+ const [local, target] = template_import;
875
+ const value = metadata.template;
876
+ const at = keys.get("template");
877
+ if (typeof value === "string" && at !== void 0) {
878
+ const v = source.indexOf(value, at.end);
879
+ const eol = source.indexOf("\n", at.end);
880
+ if (v >= 0 && (eol < 0 || v < eol))
881
+ target.edit.mappings.push(
882
+ mapping(v, value.length, target.at, target.length, HOVER)
883
+ );
884
+ }
885
+ const script = /<script(?![^>]*\bmodule\b)[^>]*>/.exec(original);
886
+ const close = script === null ? -1 : original.indexOf("<\/script>", script.index);
887
+ if (close >= 0) {
888
+ const object = object_literal(metadata, keys, CHECK, "\n;({ ", " })");
889
+ edits.push({
890
+ start: close,
891
+ end: close,
892
+ text: object.text + ` satisfies Partial<import('svelte').ComponentProps<typeof ${local}>> & Record<string, unknown>;
893
+ `,
894
+ mappings: object.mappings
895
+ });
896
+ }
897
+ }
898
+ const probes = props_probes(options);
899
+ if (probes.text !== "") {
900
+ const script = /<script(?![^>]*\bmodule\b)[^>]*>/.exec(original);
901
+ const close = script === null ? -1 : original.indexOf("<\/script>", script.index);
902
+ edits.push(
903
+ close >= 0 ? { start: close, end: close, text: "\n" + probes.text } : {
904
+ start: 0,
905
+ end: 0,
906
+ text: `<script lang="ts">
907
+ ${probes.text}<\/script>
908
+ `
909
+ }
910
+ );
911
+ }
912
+ const { code, mappings: moved } = apply_edits(original, mappings, edits);
913
+ const styleBlocks = [];
914
+ for (const sp of regions.styles) {
915
+ const css = source.slice(sp.valueStart, sp.valueEnd);
916
+ const at = code.indexOf(css);
917
+ if (at !== -1) {
918
+ styleBlocks.push({
919
+ sourceStart: sp.valueStart,
920
+ sourceEnd: sp.valueEnd,
921
+ generatedStart: at,
922
+ generatedEnd: at + css.length
923
+ });
924
+ }
925
+ }
926
+ return {
927
+ code,
928
+ mappings: moved,
929
+ styleBlocks,
930
+ excludedRegions: regions.excluded,
931
+ diagnostics,
932
+ metadata,
933
+ template: result.template,
934
+ frontmatter: regions.frontmatter,
935
+ probes: probes.list
936
+ };
937
+ }
938
+ export {
939
+ pfmToSvelte,
940
+ template_value_at,
941
+ yaml_keys
942
+ };