@mdsvex/parse 1.0.0-next.2 → 1.0.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.
- package/PLUGINS.md +41 -1
- package/dist/main.d.ts +1 -1
- package/dist/main.js +2354 -2113
- package/dist/plugin_dispatch.d.ts +18 -0
- package/dist/plugin_tasks.d.ts +4 -0
- package/dist/plugin_types.d.ts +7 -1
- package/dist/utils.d.ts +64 -0
- package/dist/utils.js +272 -187
- package/package.json +1 -1
package/PLUGINS.md
CHANGED
|
@@ -845,7 +845,7 @@ An entry can hold three keys. Any other key is ignored, so a layer above can kee
|
|
|
845
845
|
|
|
846
846
|
A handler is a function, not a `{ parse }` object. Like a plugin handler it may return a function, which runs when the node closes. A key that is not a node type throws when the tables are first used, and so does `text`: text goes to the [text handler](#text-handlers) of a sequential plugin. The one key of `children` that is no node type is `'*'`, see [A child of any type](#a-child-of-any-type).
|
|
847
847
|
|
|
848
|
-
`directive_handlers` is an array of tables.
|
|
848
|
+
`directive_handlers` is an array of tables. A table's entry owns its name, with or without handlers, and a name has one owner: two tables with an entry for the same name throw a `DirectiveOwnerError` when the tables are first used, with `directive` and the two plugin names as `plugins`. Another plugin that wants to see the directive [observes it](#observing-a-directive). The tables are read once per array, so pass the same array for every document and build a new one to change a handler.
|
|
849
849
|
|
|
850
850
|
A table can come with the name of the plugin it belongs to, as `{ name: 'steps', directives: bound }`. The name keys what its handlers write with `ctx.set` and `ctx.add` and is the `plugin` of their warnings. `mdsvex` passes each plugin's `directives` this way.
|
|
851
851
|
|
|
@@ -957,6 +957,46 @@ A container with `children` or `descendants` handlers opens a scope when its nam
|
|
|
957
957
|
|
|
958
958
|
Bound handlers run in the fused pass only. A sequential plugin sees the tree they left.
|
|
959
959
|
|
|
960
|
+
### Observing a directive
|
|
961
|
+
|
|
962
|
+
A directive has one owner, the table that binds its name. Another plugin reads it by observing it: numbering a figure, collecting the tabs of a page, checking an arg.
|
|
963
|
+
|
|
964
|
+
```js
|
|
965
|
+
const numbering = {
|
|
966
|
+
name: 'numbering',
|
|
967
|
+
observe: {
|
|
968
|
+
figure: {
|
|
969
|
+
parse(figure, ctx) {
|
|
970
|
+
// a block directive is never revoked, so a count in the scratch stays right
|
|
971
|
+
const number = (ctx.scratch.figures = (ctx.scratch.figures ?? 0) + 1);
|
|
972
|
+
const id = figure.attrs.args?.id;
|
|
973
|
+
if (id === undefined) return;
|
|
974
|
+
return () => {
|
|
975
|
+
const label = figure.first_child;
|
|
976
|
+
ctx.set('target:' + id, {
|
|
977
|
+
number,
|
|
978
|
+
caption: label?.type === 'directive_label' ? label.text_content : null,
|
|
979
|
+
});
|
|
980
|
+
};
|
|
981
|
+
},
|
|
982
|
+
},
|
|
983
|
+
},
|
|
984
|
+
};
|
|
985
|
+
```
|
|
986
|
+
|
|
987
|
+
`observe` is a key of a parse plugin, a table keyed by directive name with the entries a table of bound handlers has: `parse`, `children` (`'*'` too) and `descendants`. Handlers get the same context as the owner's, with a `state`, an `index` and counts of their own.
|
|
988
|
+
|
|
989
|
+
- **It runs whoever owns the name.** With an owner, with only a component of the host for it, or with neither. Which plugins are installed changes nothing for an observer.
|
|
990
|
+
- **It changes no node.** Not the directive, not what is inside it, not anything else: a write to `attrs` or `type`, a structural method and `remove` throw a `TypeError` that names the observer, the directive and its owner. A deferred task an observer starts is held to the same. The owner decides what the directive is and how it renders, so what an observer finds goes into its plugin data, which a component reads through Svelte context.
|
|
991
|
+
- **What it may do:** `ctx.set` and `ctx.add`, owned by the node whose handler runs and revoked with it, `ctx.warn`, `ctx.id`, `ctx.scratch`, `ctx.defer`, and the close callbacks it returns.
|
|
992
|
+
- **After the owner.** At the open the global open handlers of the directive run first, then the owner's `parse`, then the observers in plugin order. At the close the observers' callbacks run last, after the owner's and the global ones. For a child the owner's `children` and `descendants` handlers run first, then the observers', and their close callbacks in the same order. An observer can change nothing the owner would see, so the only thing the order decides is what the observer sees, and this way it is the directive as the owner left it: the args it set at the open, the wrappers it made, the args it wrote at the close.
|
|
993
|
+
- **As the directive streams.** `children` and `descendants` handlers of an observer run as each node opens, as the owner's do, so an observer sees the label of a container when it arrives and need not wait for the close.
|
|
994
|
+
- **In the fused pass,** as bound handlers run, also for a plugin that is `sequential`.
|
|
995
|
+
- **A plugin does not observe its own directive.** A plugin whose name owns a table entry for the name and observes it too throws a `TypeError`, its own handlers already see the directive.
|
|
996
|
+
- **Revocation** is as for the owner: an inline directive whose bracket never closes takes what its observers wrote with it, and a directive inside a construct that goes, an emphasis that never closes or a link with no destination, stays with what they wrote.
|
|
997
|
+
|
|
998
|
+
`mdsvex` reads `observe` from a parse plugin of `parse_plugins` and from the `parse` of a plugin bundle, which carries the bundle's name. The global directive handlers of either still never see a directive a component takes.
|
|
999
|
+
|
|
960
1000
|
## Dirty Tracking
|
|
961
1001
|
|
|
962
1002
|
Every mutation through the node view marks the affected node dirty. This is how the system knows what to re-render after parse plugins have run.
|
package/dist/main.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ export type { NodeView, WrapperView, Point, Position } from './node_view.js';
|
|
|
10
10
|
export { NodeKind, NodeBuffer, take_src } from './utils.js';
|
|
11
11
|
export { HeadingIds, HeadingIdError, heading_slug } from './heading_ids.js';
|
|
12
12
|
export type { HeadingIdErrorCode, HeadingInfo, HeadingSlug, } from './heading_ids.js';
|
|
13
|
-
export { PluginDispatcher, has_directive_handlers } from './plugin_dispatch.js';
|
|
13
|
+
export { DirectiveOwnerError, PluginDispatcher, has_directive_handlers, } from './plugin_dispatch.js';
|
|
14
14
|
export { SourceTextSource, WireTextSource } from './node_view.js';
|
|
15
15
|
export { WireEmitter, WireOp } from './wire_emitter.js';
|
|
16
16
|
export type { Emitter } from './opcodes.js';
|