@mdsvex/render 1.0.0-next.0 → 1.0.0-next.2
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/dist/component.d.ts +43 -0
- package/dist/component.js +32 -11
- package/dist/entities.d.ts +2 -0
- package/dist/html_cursor.d.ts +397 -0
- package/dist/html_cursor.js +4754 -436
- package/dist/index.d.ts +9 -0
- package/dist/index.js +24 -18
- package/dist/mappings.d.ts +117 -0
- package/dist/mappings.js +151 -8
- package/dist/scope.d.ts +52 -0
- package/dist/sourcemap.d.ts +108 -0
- package/dist/sourcemap.js +832 -68
- package/dist/stream.d.ts +87 -0
- package/dist/stream.js +123 -0
- package/package.json +22 -14
- package/src/Node.svelte +290 -114
- package/src/component.ts +31 -8
- package/src/entities.ts +226 -0
- package/src/html_cursor.ts +7166 -593
- package/src/index.ts +36 -9
- package/src/mappings.ts +239 -4
- package/src/scope.ts +127 -0
- package/src/sourcemap.ts +1713 -99
- package/src/stream.ts +223 -0
- package/CHANGELOG.md +0 -16
- package/test/_gen_chain.mjs +0 -67
- package/test/_vite_debug.mjs +0 -91
- package/test/_vite_debug2.mjs +0 -34
- package/test/component.spec.ts +0 -120
- package/test/html_cursor.spec.ts +0 -231
- package/test/mappings.spec.ts +0 -436
- package/test/wire_tree_builder.spec.ts +0 -139
- package/tsconfig.json +0 -11
- package/vite.config.build.ts +0 -33
package/src/stream.ts
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* what a streaming view needs from plugins, what they wrote, live, for the
|
|
3
|
+
* components that read it, and the nodes their deferred work changed
|
|
4
|
+
*
|
|
5
|
+
* const stream = new PluginStream({ plugins });
|
|
6
|
+
* stream.renderers.push(renderer);
|
|
7
|
+
* stream.on_change(() => renderer.update(stream.buffer, ''));
|
|
8
|
+
* for await (const batch of batches) stream.apply(batch);
|
|
9
|
+
* await stream.end();
|
|
10
|
+
*
|
|
11
|
+
* a component reads the data from the context under PLUGIN_DATA, which a
|
|
12
|
+
* compiled document sets too, so it reads the same in a stream and a compile
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { PluginDispatcher, WireTextSource } from '@mdsvex/parse';
|
|
16
|
+
import type {
|
|
17
|
+
DataChange,
|
|
18
|
+
DirectiveTable,
|
|
19
|
+
HeadingIds,
|
|
20
|
+
ParsePlugin,
|
|
21
|
+
PluginDocument,
|
|
22
|
+
} from '@mdsvex/parse';
|
|
23
|
+
import type { NodeBuffer } from '@mdsvex/parse/utils';
|
|
24
|
+
import { WireTreeBuilder } from '@mdsvex/parse/wire-tree-builder';
|
|
25
|
+
import { createSubscriber } from 'svelte/reactivity';
|
|
26
|
+
|
|
27
|
+
const NONE = 0xffffffff;
|
|
28
|
+
|
|
29
|
+
/** the context key of what plugins wrote, the same in every copy of this module */
|
|
30
|
+
export const PLUGIN_DATA: unique symbol = Symbol.for(
|
|
31
|
+
'mdsvex.plugin_data'
|
|
32
|
+
) as any;
|
|
33
|
+
|
|
34
|
+
/** what plugins wrote, by plugin name, undefined while none wrote anything */
|
|
35
|
+
export type PluginDataValue = Readonly<
|
|
36
|
+
Record<string, Readonly<Record<string, unknown>>>
|
|
37
|
+
>;
|
|
38
|
+
|
|
39
|
+
/** the value under PLUGIN_DATA, current is read again as it changes */
|
|
40
|
+
export interface PluginDataContext {
|
|
41
|
+
readonly current: PluginDataValue | undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* what plugins wrote, followed through on_data, current is a new object after
|
|
46
|
+
* each change, read in a component or an effect it runs again when that comes
|
|
47
|
+
*/
|
|
48
|
+
export class LiveData implements PluginDataContext {
|
|
49
|
+
#data: PluginDataValue | undefined = undefined;
|
|
50
|
+
#update: (() => void) | null = null;
|
|
51
|
+
#subscribe = createSubscriber((update) => {
|
|
52
|
+
this.#update = update;
|
|
53
|
+
return () => {
|
|
54
|
+
this.#update = null;
|
|
55
|
+
};
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
get current(): PluginDataValue | undefined {
|
|
59
|
+
this.#subscribe();
|
|
60
|
+
return this.#data;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** the value of a key after a change, undefined once it holds nothing */
|
|
64
|
+
change({ plugin, key, value }: DataChange): void {
|
|
65
|
+
const data = this.#data;
|
|
66
|
+
const own = data?.[plugin];
|
|
67
|
+
const next: Record<string, unknown> = { ...own };
|
|
68
|
+
if (value === undefined) delete next[key];
|
|
69
|
+
else next[key] = value;
|
|
70
|
+
const all: Record<string, Readonly<Record<string, unknown>>> = { ...data };
|
|
71
|
+
if (Object.keys(next).length === 0) delete all[plugin];
|
|
72
|
+
else all[plugin] = next;
|
|
73
|
+
this.#data = Object.keys(all).length === 0 ? undefined : all;
|
|
74
|
+
this.#update?.();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** follows a dispatcher, returns what stops it */
|
|
78
|
+
follow(dispatcher: PluginDispatcher): () => void {
|
|
79
|
+
return dispatcher.on_data((change) => this.change(change));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** nothing written, for the next document */
|
|
83
|
+
reset(): void {
|
|
84
|
+
if (this.#data === undefined) return;
|
|
85
|
+
this.#data = undefined;
|
|
86
|
+
this.#update?.();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** a renderer that caches blocks, a patched node renders its block again */
|
|
91
|
+
export interface PatchedBlocks {
|
|
92
|
+
invalidate(buf: NodeBuffer, nodes: readonly number[]): void;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export interface PluginStreamOptions {
|
|
96
|
+
plugins?: ParsePlugin[];
|
|
97
|
+
directive_handlers?: DirectiveTable[];
|
|
98
|
+
/** makes the id of each heading as it arrives, make_heading_ids in mdsvex */
|
|
99
|
+
headings?: HeadingIds;
|
|
100
|
+
/** what the handlers learn about the document, see set_document */
|
|
101
|
+
document?: PluginDocument;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* a document arriving over the wire with its plugins, the builder, the
|
|
106
|
+
* dispatcher, the data as it changes, and the nodes deferred tasks patch
|
|
107
|
+
*/
|
|
108
|
+
export class PluginStream {
|
|
109
|
+
readonly builder: WireTreeBuilder;
|
|
110
|
+
/** null for a stream with no plugin */
|
|
111
|
+
readonly dispatcher: PluginDispatcher | null;
|
|
112
|
+
/** what plugins wrote, for the components that read it */
|
|
113
|
+
readonly data = new LiveData();
|
|
114
|
+
/** renderers whose cached blocks a patch drops */
|
|
115
|
+
readonly renderers: PatchedBlocks[] = [];
|
|
116
|
+
private listeners: (() => void)[] = [];
|
|
117
|
+
private stops: (() => void)[] = [];
|
|
118
|
+
private ended = false;
|
|
119
|
+
|
|
120
|
+
constructor(options: PluginStreamOptions = {}) {
|
|
121
|
+
const { plugins, directive_handlers } = options;
|
|
122
|
+
const plugged =
|
|
123
|
+
(plugins !== undefined && plugins.length > 0) ||
|
|
124
|
+
(directive_handlers !== undefined && directive_handlers.length > 0);
|
|
125
|
+
const dispatcher = plugged
|
|
126
|
+
? new PluginDispatcher(
|
|
127
|
+
plugins,
|
|
128
|
+
new WireTextSource([]),
|
|
129
|
+
directive_handlers
|
|
130
|
+
)
|
|
131
|
+
: null;
|
|
132
|
+
this.dispatcher = dispatcher;
|
|
133
|
+
this.builder = new WireTreeBuilder(128, dispatcher ?? undefined);
|
|
134
|
+
if (options.headings !== undefined)
|
|
135
|
+
this.builder.set_headings(options.headings);
|
|
136
|
+
if (dispatcher !== null) {
|
|
137
|
+
dispatcher.set_document(options.document);
|
|
138
|
+
this.stops.push(
|
|
139
|
+
this.data.follow(dispatcher),
|
|
140
|
+
// a task that settled applies at once, and its node renders again
|
|
141
|
+
dispatcher.on_settled(() => {
|
|
142
|
+
if (this.patch()) this.changed();
|
|
143
|
+
})
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** the buffer the builder fills */
|
|
149
|
+
get buffer(): NodeBuffer {
|
|
150
|
+
return this.builder.get_buffer();
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** a batch of the wire, then the deferred work that is ready */
|
|
154
|
+
apply(batch: unknown[][]): void {
|
|
155
|
+
this.builder.apply(batch);
|
|
156
|
+
this.patch();
|
|
157
|
+
this.changed();
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* the stream ended, after finish of the parser, runs the sequential passes,
|
|
162
|
+
* waits for the deferred tasks still running, then runs the document hooks,
|
|
163
|
+
* a promise when any of that waits, every block renders again after it
|
|
164
|
+
*/
|
|
165
|
+
end(): Promise<void> | undefined {
|
|
166
|
+
if (this.ended) return undefined;
|
|
167
|
+
this.ended = true;
|
|
168
|
+
this.builder.end();
|
|
169
|
+
const dispatcher = this.dispatcher;
|
|
170
|
+
if (dispatcher === null) return undefined;
|
|
171
|
+
const buf = this.buffer;
|
|
172
|
+
dispatcher.run_sequential(buf);
|
|
173
|
+
const finished = dispatcher.finish(buf);
|
|
174
|
+
this.everything();
|
|
175
|
+
if (finished === undefined) return undefined;
|
|
176
|
+
return finished.then(() => this.everything());
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** applies the deferred tasks that are ready, true when one patched a node */
|
|
180
|
+
patch(): boolean {
|
|
181
|
+
const dispatcher = this.dispatcher;
|
|
182
|
+
if (dispatcher === null) return false;
|
|
183
|
+
const nodes = dispatcher.apply_ready();
|
|
184
|
+
if (nodes === null) return false;
|
|
185
|
+
const buf = this.buffer;
|
|
186
|
+
for (const renderer of this.renderers) renderer.invalidate(buf, nodes);
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** listener runs after each batch and each patch, so a view renders again */
|
|
191
|
+
on_change(listener: () => void): () => void {
|
|
192
|
+
this.listeners.push(listener);
|
|
193
|
+
return () => {
|
|
194
|
+
const at = this.listeners.indexOf(listener);
|
|
195
|
+
if (at !== -1) this.listeners.splice(at, 1);
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** stops following the dispatcher, a task still running applies nowhere */
|
|
200
|
+
stop(): void {
|
|
201
|
+
for (const stop of this.stops) stop();
|
|
202
|
+
this.stops.length = 0;
|
|
203
|
+
this.listeners.length = 0;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** the whole tree may have changed, every block renders again */
|
|
207
|
+
private everything(): void {
|
|
208
|
+
const buf = this.buffer;
|
|
209
|
+
const blocks: number[] = [];
|
|
210
|
+
let child = buf.first_child_at(0);
|
|
211
|
+
while (child !== NONE && buf.parent_at(child) === 0) {
|
|
212
|
+
blocks.push(child);
|
|
213
|
+
child = buf.next_at(child);
|
|
214
|
+
}
|
|
215
|
+
if (blocks.length !== 0)
|
|
216
|
+
for (const renderer of this.renderers) renderer.invalidate(buf, blocks);
|
|
217
|
+
this.changed();
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
private changed(): void {
|
|
221
|
+
for (const listener of this.listeners.slice()) listener();
|
|
222
|
+
}
|
|
223
|
+
}
|
package/CHANGELOG.md
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
## 1.0.0-next.0
|
|
2
|
-
|
|
3
|
-
### Major Changes
|
|
4
|
-
|
|
5
|
-
- [#795](https://github.com/pngwn/MDsveX/pull/795) [`6ac3826`](https://github.com/pngwn/MDsveX/commit/6ac382615eb410230e8423d5ca4202005588869e) Thanks [@pngwn](https://github.com/pngwn)! - Add new PFM parser, renderers
|
|
6
|
-
|
|
7
|
-
### Minor Changes
|
|
8
|
-
|
|
9
|
-
- [#798](https://github.com/pngwn/MDsveX/pull/798) [`e5b7abe`](https://github.com/pngwn/MDsveX/commit/e5b7abe1ebf868b1df1e934fb96e1e363696f3e9) Thanks [@pngwn](https://github.com/pngwn)! - Add plugin system and autolink plugin
|
|
10
|
-
|
|
11
|
-
- [#799](https://github.com/pngwn/MDsveX/pull/799) [`5b54692`](https://github.com/pngwn/MDsveX/commit/5b54692d6c895f02c947ce620a46c7a9beb856e9) Thanks [@pngwn](https://github.com/pngwn)! - Add language tools
|
|
12
|
-
|
|
13
|
-
### Patch Changes
|
|
14
|
-
|
|
15
|
-
- Updated dependencies [[`6ac3826`](https://github.com/pngwn/MDsveX/commit/6ac382615eb410230e8423d5ca4202005588869e), [`46f655f`](https://github.com/pngwn/MDsveX/commit/46f655f90a838726eda34e1b07667e86fa61e7cb), [`e5b7abe`](https://github.com/pngwn/MDsveX/commit/e5b7abe1ebf868b1df1e934fb96e1e363696f3e9), [`5b54692`](https://github.com/pngwn/MDsveX/commit/5b54692d6c895f02c947ce620a46c7a9beb856e9)]:
|
|
16
|
-
- @mdsvex/parse@1.0.0-next.0
|
package/test/_gen_chain.mjs
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { PFMParser } from '@mdsvex/parse';
|
|
2
|
-
import { TreeBuilder } from '@mdsvex/parse/tree-builder';
|
|
3
|
-
import { CursorHTMLRenderer } from '../src/html_cursor.ts';
|
|
4
|
-
import { mappings_to_v3 } from '../src/sourcemap.ts';
|
|
5
|
-
import * as svelte from 'svelte/compiler';
|
|
6
|
-
import remapping from '@ampproject/remapping';
|
|
7
|
-
import { writeFileSync } from 'node:fs';
|
|
8
|
-
import { resolve } from 'node:path';
|
|
9
|
-
|
|
10
|
-
const source = `<script>
|
|
11
|
-
let name = 'world';
|
|
12
|
-
let count = 0;
|
|
13
|
-
</script>
|
|
14
|
-
|
|
15
|
-
# Hello {name}
|
|
16
|
-
|
|
17
|
-
The count is {count}.
|
|
18
|
-
|
|
19
|
-
Some _formatted_ text with *bold* and \`code\`.
|
|
20
|
-
`;
|
|
21
|
-
|
|
22
|
-
const outDir = resolve(import.meta.dirname, '../../..');
|
|
23
|
-
|
|
24
|
-
// Step 1: PFM → HTML + map (HTML → markdown)
|
|
25
|
-
const tree = new TreeBuilder(128);
|
|
26
|
-
const parser = new PFMParser(tree);
|
|
27
|
-
parser.parse(source);
|
|
28
|
-
const renderer = new CursorHTMLRenderer({ cache: false });
|
|
29
|
-
const { mappings } = renderer.update_mapped(tree.get_buffer(), source);
|
|
30
|
-
const html = renderer.html;
|
|
31
|
-
const pfmMap = mappings_to_v3(mappings, source, html, 'test.svx');
|
|
32
|
-
|
|
33
|
-
// Step 2: Svelte compile (produces JS → HTML map)
|
|
34
|
-
// Don't pass sourcemap option — compiler doesn't chain, just compile clean
|
|
35
|
-
const compiled = svelte.compile(html, {
|
|
36
|
-
filename: 'test.svx',
|
|
37
|
-
generate: 'client',
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
const compilerMap = compiled.js.map;
|
|
41
|
-
|
|
42
|
-
// Step 3: Chain the two maps: (JS → HTML) + (HTML → markdown) = (JS → markdown)
|
|
43
|
-
// @ampproject/remapping takes the outermost map and a loader for inner maps
|
|
44
|
-
const chained = remapping(
|
|
45
|
-
{ ...compilerMap, version: 3 },
|
|
46
|
-
(file) => {
|
|
47
|
-
// When remapping asks "give me the sourcemap for test.svx", return our PFM map
|
|
48
|
-
if (file === 'test.svx') {
|
|
49
|
-
return { ...pfmMap, version: 3 };
|
|
50
|
-
}
|
|
51
|
-
return null;
|
|
52
|
-
}
|
|
53
|
-
);
|
|
54
|
-
|
|
55
|
-
// Fix up sourcesContent to have the original markdown
|
|
56
|
-
chained.sourcesContent = [source];
|
|
57
|
-
|
|
58
|
-
writeFileSync(`${outDir}/test.svx`, source);
|
|
59
|
-
writeFileSync(`${outDir}/test.svx.js`, compiled.js.code + '\n//# sourceMappingURL=test.svx.js.map\n');
|
|
60
|
-
writeFileSync(`${outDir}/test.svx.js.map`, JSON.stringify(chained, null, 2));
|
|
61
|
-
|
|
62
|
-
console.log('source:', source.length, 'bytes');
|
|
63
|
-
console.log('js: ', compiled.js.code.length, 'bytes');
|
|
64
|
-
console.log('compiler map:', compilerMap.mappings.length, 'VLQ chars');
|
|
65
|
-
console.log('chained map: ', chained.mappings.length, 'VLQ chars');
|
|
66
|
-
console.log('sources:', chained.sources);
|
|
67
|
-
console.log('files: test.svx, test.svx.js, test.svx.js.map');
|
package/test/_vite_debug.mjs
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import { PFMParser } from '@mdsvex/parse';
|
|
2
|
-
import { TreeBuilder } from '@mdsvex/parse/tree-builder';
|
|
3
|
-
import { CursorHTMLRenderer } from '../src/html_cursor.ts';
|
|
4
|
-
import { mappings_to_v3 } from '../src/sourcemap.ts';
|
|
5
|
-
import * as svelte from 'svelte/compiler';
|
|
6
|
-
import remapping from '@ampproject/remapping';
|
|
7
|
-
import { TraceMap, originalPositionFor } from '@jridgewell/trace-mapping';
|
|
8
|
-
|
|
9
|
-
const FILE = '/src/routes/+page.svx';
|
|
10
|
-
|
|
11
|
-
const md = `<script>
|
|
12
|
-
let name = 'world';
|
|
13
|
-
</script>
|
|
14
|
-
|
|
15
|
-
# Hello {name}
|
|
16
|
-
|
|
17
|
-
Some _formatted_ text.
|
|
18
|
-
`;
|
|
19
|
-
|
|
20
|
-
// Step 1: mdsvex vite plugin transform
|
|
21
|
-
const tree = new TreeBuilder(128);
|
|
22
|
-
const parser = new PFMParser(tree);
|
|
23
|
-
parser.parse(md);
|
|
24
|
-
const renderer = new CursorHTMLRenderer({ cache: false });
|
|
25
|
-
const { mappings } = renderer.update_mapped(tree.get_buffer(), md);
|
|
26
|
-
const html = renderer.html;
|
|
27
|
-
const mdsvexMap = mappings_to_v3(mappings, md, html, FILE);
|
|
28
|
-
|
|
29
|
-
console.log('=== Step 1: mdsvex transform ===');
|
|
30
|
-
console.log('map.sources:', mdsvexMap.sources);
|
|
31
|
-
console.log('map.file:', mdsvexMap.file);
|
|
32
|
-
console.log('map.sourcesContent present:', !!mdsvexMap.sourcesContent?.[0]);
|
|
33
|
-
console.log('map.mappings length:', mdsvexMap.mappings.length);
|
|
34
|
-
|
|
35
|
-
// Step 2: svelte.preprocess (vite-plugin-svelte preprocess stage)
|
|
36
|
-
// In Vite, this gets the code from step 1's output
|
|
37
|
-
const preprocessed = await svelte.preprocess(html, [/* no preprocessors */], { filename: FILE });
|
|
38
|
-
console.log('\n=== Step 2: svelte.preprocess ===');
|
|
39
|
-
console.log('map present:', !!preprocessed.map);
|
|
40
|
-
// If no preprocessors change the code, map may be null/identity
|
|
41
|
-
|
|
42
|
-
// Step 3: svelte.compile
|
|
43
|
-
const compiled = svelte.compile(preprocessed.code, {
|
|
44
|
-
filename: FILE,
|
|
45
|
-
generate: 'client',
|
|
46
|
-
});
|
|
47
|
-
const svelteMap = compiled.js.map;
|
|
48
|
-
|
|
49
|
-
console.log('\n=== Step 3: svelte.compile ===');
|
|
50
|
-
console.log('map.sources:', svelteMap.sources);
|
|
51
|
-
console.log('map.file:', svelteMap.file);
|
|
52
|
-
console.log('map.mappings length:', svelteMap.mappings.length);
|
|
53
|
-
|
|
54
|
-
// Step 4: What Vite's getCombinedSourcemap does
|
|
55
|
-
// It chains: [compile_map, preprocess_map, mdsvex_map]
|
|
56
|
-
// Since preprocess_map is null/identity, it's effectively [compile_map, mdsvex_map]
|
|
57
|
-
console.log('\n=== Step 4: Chain (simulating getCombinedSourcemap) ===');
|
|
58
|
-
|
|
59
|
-
const chained = remapping([svelteMap, mdsvexMap], () => null);
|
|
60
|
-
console.log('chained.sources:', chained.sources);
|
|
61
|
-
console.log('chained.file:', chained.file);
|
|
62
|
-
console.log('chained.sourcesContent?.[0]?.slice(0,50):', chained.sourcesContent?.[0]?.slice(0, 50));
|
|
63
|
-
console.log('chained.mappings length:', chained.mappings.length);
|
|
64
|
-
|
|
65
|
-
// Step 5: verify trace
|
|
66
|
-
console.log('\n=== Trace verification ===');
|
|
67
|
-
const t = new TraceMap(chained);
|
|
68
|
-
const jsLines = compiled.js.code.split('\n');
|
|
69
|
-
const mdLines = md.split('\n');
|
|
70
|
-
|
|
71
|
-
// Find the 'let name' line in JS
|
|
72
|
-
for (let line = 0; line < jsLines.length; line++) {
|
|
73
|
-
const idx = jsLines[line].indexOf('name');
|
|
74
|
-
if (idx !== -1 && jsLines[line].includes("'world'")) {
|
|
75
|
-
const orig = originalPositionFor(t, { line: line + 1, column: idx });
|
|
76
|
-
console.log(`js ${line+1}:${idx} "${jsLines[line].trim()}" → ${orig.source}:${orig.line}:${orig.column}`);
|
|
77
|
-
if (orig.line) {
|
|
78
|
-
console.log(` source content: "${mdLines[orig.line-1]?.trim()}"`);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// What the browser sees:
|
|
84
|
-
console.log('\n=== What browser devtools see ===');
|
|
85
|
-
console.log('Source URL in map:', chained.sources?.[0]);
|
|
86
|
-
console.log('Can browser fetch this?', chained.sources?.[0]?.startsWith('/') ? 'Absolute path — Vite resolves via /@fs/ or relative' : 'Relative path');
|
|
87
|
-
console.log('sourcesContent included:', !!chained.sourcesContent?.[0]);
|
|
88
|
-
if (chained.sourcesContent?.[0]) {
|
|
89
|
-
const content = chained.sourcesContent[0];
|
|
90
|
-
console.log('sourcesContent is:', content.includes('<script>') && content.includes('# Hello') ? 'ORIGINAL MARKDOWN ✓' : content.includes('<h1>') ? 'INTERMEDIATE HTML ✗' : 'UNKNOWN');
|
|
91
|
-
}
|
package/test/_vite_debug2.mjs
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import remapping from '@ampproject/remapping';
|
|
2
|
-
import { TraceMap, originalPositionFor } from '@jridgewell/trace-mapping';
|
|
3
|
-
|
|
4
|
-
// Simulate the mismatch: svelte uses basename, mdsvex uses full path
|
|
5
|
-
const svelteMap = {
|
|
6
|
-
version: 3,
|
|
7
|
-
sources: ['+page.svx'], // basename — what svelte.compile produces
|
|
8
|
-
mappings: 'AAAA',
|
|
9
|
-
names: [],
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
const mdsvexMap = {
|
|
13
|
-
version: 3,
|
|
14
|
-
sources: ['/src/routes/+page.svx'], // full path — what we produce
|
|
15
|
-
sourcesContent: ['# Hello World\n'],
|
|
16
|
-
mappings: 'AAAA',
|
|
17
|
-
names: [],
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
// Try chaining with loader (how Vite does it internally)
|
|
21
|
-
console.log('=== Loader-based chaining ===');
|
|
22
|
-
const chained1 = remapping(svelteMap, (file) => {
|
|
23
|
-
console.log(' loader asked for:', JSON.stringify(file));
|
|
24
|
-
if (file === '+page.svx') return mdsvexMap;
|
|
25
|
-
return null;
|
|
26
|
-
});
|
|
27
|
-
console.log('result sources:', chained1.sources);
|
|
28
|
-
console.log('result sourcesContent:', chained1.sourcesContent);
|
|
29
|
-
|
|
30
|
-
// Try array-based chaining
|
|
31
|
-
console.log('\n=== Array-based chaining ===');
|
|
32
|
-
const chained2 = remapping([svelteMap, mdsvexMap], () => null);
|
|
33
|
-
console.log('result sources:', chained2.sources);
|
|
34
|
-
console.log('result sourcesContent:', chained2.sourcesContent);
|
package/test/component.spec.ts
DELETED
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
|
2
|
-
import { PFMParser, WireEmitter } from '@mdsvex/parse';
|
|
3
|
-
import { TreeBuilder } from '@mdsvex/parse/tree-builder';
|
|
4
|
-
import { WireTreeBuilder } from '@mdsvex/parse/wire-tree-builder';
|
|
5
|
-
import { ComponentRenderer } from '../src/component';
|
|
6
|
-
|
|
7
|
-
// Helpers
|
|
8
|
-
|
|
9
|
-
function parse_to_buf(source: string) {
|
|
10
|
-
const tree = new TreeBuilder(source.length >> 3 || 128);
|
|
11
|
-
const parser = new PFMParser(tree);
|
|
12
|
-
parser.parse(source);
|
|
13
|
-
return { buf: tree.get_buffer(), source };
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
// ComponentRenderer
|
|
17
|
-
|
|
18
|
-
describe('ComponentRenderer', () => {
|
|
19
|
-
it('produces blocks with buffer indices', () => {
|
|
20
|
-
const { buf, source } = parse_to_buf('# Hello\n\nworld\n');
|
|
21
|
-
const renderer = new ComponentRenderer();
|
|
22
|
-
renderer.update(buf, source);
|
|
23
|
-
|
|
24
|
-
expect(renderer.blocks.length).toBe(2);
|
|
25
|
-
// Verify the indices point to real nodes
|
|
26
|
-
expect(buf._kinds[renderer.blocks[0].idx]).toBe(3); // heading
|
|
27
|
-
expect(buf._kinds[renderer.blocks[1].idx]).toBe(7); // paragraph
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it('each block has a unique idx', () => {
|
|
31
|
-
const { buf, source } = parse_to_buf('# Hello\n\nworld\n');
|
|
32
|
-
const renderer = new ComponentRenderer();
|
|
33
|
-
renderer.update(buf, source);
|
|
34
|
-
|
|
35
|
-
expect(renderer.blocks[0].idx).not.toBe(renderer.blocks[1].idx);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it('blocks have version 0 on first update', () => {
|
|
39
|
-
const { buf, source } = parse_to_buf('# Hello\n');
|
|
40
|
-
const renderer = new ComponentRenderer();
|
|
41
|
-
renderer.update(buf, source);
|
|
42
|
-
|
|
43
|
-
expect(renderer.blocks[0].v).toBe(0);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it('skips line_break nodes', () => {
|
|
47
|
-
const { buf, source } = parse_to_buf('# Hello\n\nworld\n');
|
|
48
|
-
const renderer = new ComponentRenderer();
|
|
49
|
-
renderer.update(buf, source);
|
|
50
|
-
|
|
51
|
-
for (const block of renderer.blocks) {
|
|
52
|
-
expect(buf._kinds[block.idx]).not.toBe(6); // line_break
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
it('incremental: closed blocks stop version bumping', () => {
|
|
57
|
-
const tree = new TreeBuilder(128);
|
|
58
|
-
const parser = new PFMParser(tree);
|
|
59
|
-
const renderer = new ComponentRenderer();
|
|
60
|
-
|
|
61
|
-
parser.init();
|
|
62
|
-
|
|
63
|
-
parser.feed('# First\n\n');
|
|
64
|
-
renderer.update(tree.get_buffer(), '# First\n\n');
|
|
65
|
-
const heading_v = renderer.blocks[0]?.v;
|
|
66
|
-
|
|
67
|
-
parser.feed('second\n');
|
|
68
|
-
parser.finish();
|
|
69
|
-
renderer.update(tree.get_buffer(), '# First\n\nsecond\n');
|
|
70
|
-
|
|
71
|
-
expect(renderer.blocks[0].v).toBe(heading_v);
|
|
72
|
-
expect(renderer.blocks.length).toBe(2);
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
it('incremental: open blocks bump version', () => {
|
|
76
|
-
const tree = new TreeBuilder(128);
|
|
77
|
-
const parser = new PFMParser(tree);
|
|
78
|
-
const renderer = new ComponentRenderer();
|
|
79
|
-
|
|
80
|
-
parser.init();
|
|
81
|
-
|
|
82
|
-
parser.feed('hel');
|
|
83
|
-
renderer.update(tree.get_buffer(), 'hel');
|
|
84
|
-
const v1 = renderer.blocks[0]?.v ?? -1;
|
|
85
|
-
|
|
86
|
-
parser.feed('lo\n');
|
|
87
|
-
renderer.update(tree.get_buffer(), 'hello\n');
|
|
88
|
-
|
|
89
|
-
expect(renderer.blocks[0].v).toBeGreaterThan(v1);
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
it('returns new array reference on change', () => {
|
|
93
|
-
const { buf, source } = parse_to_buf('hello\n');
|
|
94
|
-
const renderer = new ComponentRenderer();
|
|
95
|
-
const first = renderer.update(buf, source);
|
|
96
|
-
const second = renderer.update(buf, source);
|
|
97
|
-
|
|
98
|
-
// Second update with same closed doc, no change, same reference
|
|
99
|
-
expect(second).toBe(first);
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
it('reset clears all state', () => {
|
|
103
|
-
const { buf, source } = parse_to_buf('hello\n');
|
|
104
|
-
const renderer = new ComponentRenderer();
|
|
105
|
-
renderer.update(buf, source);
|
|
106
|
-
expect(renderer.blocks.length).toBe(1);
|
|
107
|
-
|
|
108
|
-
renderer.reset();
|
|
109
|
-
expect(renderer.blocks.length).toBe(0);
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
it('stores buffer and source references', () => {
|
|
113
|
-
const { buf, source } = parse_to_buf('# Title\n');
|
|
114
|
-
const renderer = new ComponentRenderer();
|
|
115
|
-
renderer.update(buf, source);
|
|
116
|
-
|
|
117
|
-
expect(renderer.buf).toBe(buf);
|
|
118
|
-
expect(renderer.source).toBe(source);
|
|
119
|
-
});
|
|
120
|
-
});
|