@compmd/lezer 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +68 -2
- package/dist/compmd.d.ts +14 -0
- package/dist/compmd.js +41 -0
- package/dist/component-line.d.ts +17 -0
- package/dist/component-line.js +167 -0
- package/dist/component-parts.d.ts +11 -0
- package/dist/component-parts.js +33 -0
- package/dist/context.d.ts +6 -0
- package/dist/context.js +3 -0
- package/dist/definition.d.ts +3 -0
- package/dist/definition.js +118 -0
- package/dist/fallback.d.ts +10 -0
- package/dist/fallback.js +31 -0
- package/dist/frontmatter.d.ts +3 -0
- package/dist/frontmatter.js +38 -0
- package/dist/highlighting.d.ts +12 -0
- package/dist/highlighting.js +66 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/inline-component.d.ts +3 -0
- package/dist/inline-component.js +36 -0
- package/dist/interpolation.d.ts +6 -0
- package/dist/interpolation.js +44 -0
- package/dist/math.d.ts +10 -0
- package/dist/math.js +59 -0
- package/dist/nodes.d.ts +13 -0
- package/dist/nodes.js +67 -0
- package/dist/params.d.ts +33 -0
- package/dist/params.js +153 -0
- package/dist/syntax.d.ts +52 -0
- package/dist/syntax.js +52 -0
- package/dist/wiki-link.d.ts +3 -0
- package/dist/wiki-link.js +25 -0
- package/package.json +43 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 comp-markdown
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,69 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @compmd/lezer
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
CompMD's grammar for CodeMirror: an [`@lezer/markdown`](https://github.com/lezer-parser/markdown) extension that adds
|
|
4
|
+
[CompMD](https://compmd.dev)'s syntax ([spec](https://compmd.dev/spec/) v1) to CommonMark and GFM, with highlighting tags.
|
|
5
|
+
|
|
6
|
+
```ts
|
|
7
|
+
import { markdown, markdownLanguage } from "@codemirror/lang-markdown";
|
|
8
|
+
import { compmd } from "@compmd/lezer";
|
|
9
|
+
|
|
10
|
+
const language = markdown({ base: markdownLanguage, extensions: [compmd] });
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## What it parses
|
|
14
|
+
|
|
15
|
+
| Construct | Nodes | Spec |
|
|
16
|
+
|---|---|---|
|
|
17
|
+
| Frontmatter at the document's start, YAML between `---` lines or TOML between `+++` lines, never Markdown; its `md` key | `Frontmatter`, `FrontmatterKey` | §2.1 |
|
|
18
|
+
| A container's opener, closer and continuation lines | `ComponentOpen`, `ComponentClose`, `ComponentContinuation` | §3 |
|
|
19
|
+
| Leaves and slots | `ComponentLeaf`, `ComponentSlot` | §3, §4 |
|
|
20
|
+
| A line starting with `::` that matches no form | `ComponentMalformed` | §10 (F016) |
|
|
21
|
+
| Inline components, `:name[fallback]{…}` | `InlineComponent` | §3.2 |
|
|
22
|
+
| Params: keys, breakpoints, strings with their escapes, expressions with their parentheses, JSON, bare values, `.class`, `#id`; a list that isn't closed, invalid to the line's end | `ParamList`, `ParamKey`, `ParamOperator`, `ParamString`, `ParamEscape`, … `ParamInvalid` | §3.1, §5 |
|
|
23
|
+
| Component definitions and signature fences: the info string in the param syntax, the signature, the `---` line, and the body parsed as CompMD again | `ComponentDefinition`, `DefinitionInfo`, `DefinitionSignature`, `DefinitionSeparator`, `DefinitionBody` | §7.1, §7.2 |
|
|
24
|
+
| A fallback `[…]`, its inside parsed as inline Markdown | `ComponentFallback` | §4.4 |
|
|
25
|
+
| Interpolation, `{{path}}`, in text, fallbacks and quoted params | `Interpolation`, `InterpolationExpression` | §8.1 |
|
|
26
|
+
| Wiki links, `[[Target]]` and `[[Target\|label]]`, always | `WikiLink`, `WikiLinkTarget`, `WikiLinkLabel` | §2.3 |
|
|
27
|
+
| Math: inline by Pandoc's rule, display on `$$` lines | `InlineMath`, `MathBlock` | §2.3 |
|
|
28
|
+
|
|
29
|
+
A component line is one block whose children are the line's parts; a container's content between its opener and its
|
|
30
|
+
closer stays ordinary Markdown. Nothing inside a component line or an inline component is parsed as Markdown, except
|
|
31
|
+
its fallback. `COMPMD_NODES` names the nodes that are CompMD's own, for an editor that draws Markdown in place and
|
|
32
|
+
leaves these as their source.
|
|
33
|
+
|
|
34
|
+
## What it shares with CompMD's other grammars
|
|
35
|
+
|
|
36
|
+
It lives in [`compmd-grammars`](../README.md), beside the tree-sitter grammar, and reads the same text the same way:
|
|
37
|
+
|
|
38
|
+
- **The syntax's pieces** (a name, the line forms, a bare value, an escape, an interpolation, the frontmatter's key) are
|
|
39
|
+
`syntax/patterns.js`'s, and **each node's highlighting tag** is its token's in `syntax/tokens.json` (`NODE_TOKENS`
|
|
40
|
+
names each node's token, `TOKEN_TAGS` each token's tag), so a component's name has the same colour group as in every
|
|
41
|
+
other editor. The package is published on its own, so it carries a copy of both, `src/syntax.ts`, which
|
|
42
|
+
`pnpm run generate` writes and `pnpm test` checks.
|
|
43
|
+
- **The shared test set** (`tests/`) runs on it as on tree-sitter: each span's tag, and the spec's documents and the
|
|
44
|
+
conformance set, on which both grammars mark the same lines as malformed.
|
|
45
|
+
|
|
46
|
+
## Limits
|
|
47
|
+
|
|
48
|
+
- **Interpolation in a link's destination** (`[x]({{s.url}})`) isn't parsed.
|
|
49
|
+
- **CEL's triple-quoted, raw and bytes literals** inside an expression param aren't told apart from other strings.
|
|
50
|
+
- **`$$` inside a paragraph** isn't display math.
|
|
51
|
+
- **A definition's body inside a list item or a block quote** isn't parsed again: its lines aren't the document's as
|
|
52
|
+
they are.
|
|
53
|
+
- **A fenced code block's info string** is read in the param syntax only for a definition; other fences' stay
|
|
54
|
+
@lezer/markdown's.
|
|
55
|
+
|
|
56
|
+
## Development
|
|
57
|
+
|
|
58
|
+
From the repository's root (`pnpm install` there):
|
|
59
|
+
|
|
60
|
+
```sh
|
|
61
|
+
pnpm --dir lezer run generate # src/syntax.ts, after a change to syntax/
|
|
62
|
+
pnpm --dir lezer test # Vitest, and src/syntax.ts is current
|
|
63
|
+
pnpm --dir lezer lint # tsc
|
|
64
|
+
pnpm --dir lezer build # dist/
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## License
|
|
68
|
+
|
|
69
|
+
MIT
|
package/dist/compmd.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { MarkdownConfig } from "@lezer/markdown";
|
|
2
|
+
/**
|
|
3
|
+
* CompMD's syntax (spec v1 §2.1, §2.3, §3, §7.1, §7.2, §8.1) for `@lezer/markdown`: frontmatter, component lines,
|
|
4
|
+
* inline components with their params and fallbacks, component definitions and signature fences, interpolation, wiki
|
|
5
|
+
* links, and math by Pandoc's rule. Use it with GFM,
|
|
6
|
+
* which CompMD's base syntax includes.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* import { markdown, markdownLanguage } from "@codemirror/lang-markdown";
|
|
10
|
+
* import { compmd } from "@compmd/lezer";
|
|
11
|
+
*
|
|
12
|
+
* const language = markdown({ base: markdownLanguage, extensions: [compmd] });
|
|
13
|
+
*/
|
|
14
|
+
export declare const compmd: MarkdownConfig;
|
package/dist/compmd.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// The grammar: CompMD's syntax as one @lezer/markdown extension.
|
|
2
|
+
import { endLeafAtComponentLine, parseComponentLineBlock } from "./component-line.js";
|
|
3
|
+
import { parseDefinition } from "./definition.js";
|
|
4
|
+
import { parseFrontmatter } from "./frontmatter.js";
|
|
5
|
+
import { compmdHighlighting } from "./highlighting.js";
|
|
6
|
+
import { parseInlineComponent } from "./inline-component.js";
|
|
7
|
+
import { parseInterpolation } from "./interpolation.js";
|
|
8
|
+
import { endLeafAtMathBlock, parseInlineMath, parseMathBlock } from "./math.js";
|
|
9
|
+
import { COMPONENT_LINES, OTHER_BLOCKS, PARTS } from "./nodes.js";
|
|
10
|
+
import { parseWikiLink } from "./wiki-link.js";
|
|
11
|
+
/**
|
|
12
|
+
* CompMD's syntax (spec v1 §2.1, §2.3, §3, §7.1, §7.2, §8.1) for `@lezer/markdown`: frontmatter, component lines,
|
|
13
|
+
* inline components with their params and fallbacks, component definitions and signature fences, interpolation, wiki
|
|
14
|
+
* links, and math by Pandoc's rule. Use it with GFM,
|
|
15
|
+
* which CompMD's base syntax includes.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* import { markdown, markdownLanguage } from "@codemirror/lang-markdown";
|
|
19
|
+
* import { compmd } from "@compmd/lezer";
|
|
20
|
+
*
|
|
21
|
+
* const language = markdown({ base: markdownLanguage, extensions: [compmd] });
|
|
22
|
+
*/
|
|
23
|
+
export const compmd = {
|
|
24
|
+
defineNodes: [
|
|
25
|
+
...[...COMPONENT_LINES, ...OTHER_BLOCKS].map((name) => ({ name, block: true })),
|
|
26
|
+
...PARTS,
|
|
27
|
+
],
|
|
28
|
+
props: [compmdHighlighting],
|
|
29
|
+
parseBlock: [
|
|
30
|
+
{ name: "Frontmatter", parse: parseFrontmatter, before: "HorizontalRule" },
|
|
31
|
+
{ name: "ComponentDefinition", parse: parseDefinition, before: "FencedCode" },
|
|
32
|
+
{ name: "ComponentLine", parse: parseComponentLineBlock, before: "FencedCode", endLeaf: endLeafAtComponentLine },
|
|
33
|
+
{ name: "MathBlock", parse: parseMathBlock, before: "FencedCode", endLeaf: endLeafAtMathBlock },
|
|
34
|
+
],
|
|
35
|
+
parseInline: [
|
|
36
|
+
{ name: "Interpolation", parse: parseInterpolation, before: "Emphasis" },
|
|
37
|
+
{ name: "InlineComponent", parse: parseInlineComponent, before: "Emphasis" },
|
|
38
|
+
{ name: "WikiLink", parse: parseWikiLink, before: "Link" },
|
|
39
|
+
{ name: "InlineMath", parse: parseInlineMath, before: "Emphasis" },
|
|
40
|
+
],
|
|
41
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { BlockContext, Element, Line } from "@lezer/markdown";
|
|
2
|
+
import type { ElementContext } from "./context.ts";
|
|
3
|
+
import type { ComponentLine } from "./nodes.ts";
|
|
4
|
+
/** A parsed component line: its node type and its children. */
|
|
5
|
+
interface Parsed {
|
|
6
|
+
readonly type: ComponentLine;
|
|
7
|
+
readonly children: readonly Element[];
|
|
8
|
+
}
|
|
9
|
+
/** Parses one line that starts with `::` (`rest`), at document position `start`; a line matching no form is malformed (F016). */
|
|
10
|
+
export declare function parseComponentLine(cx: ElementContext, rest: string, start: number): Parsed;
|
|
11
|
+
/** Whether `line` starts a component line: `::` at its start, indented less than code. */
|
|
12
|
+
export declare function startsComponentLine(line: Line): boolean;
|
|
13
|
+
/** The block parser: one component line, one block. */
|
|
14
|
+
export declare function parseComponentLineBlock(cx: BlockContext, line: Line): boolean;
|
|
15
|
+
/** Ends a paragraph at a component line, so it is never a paragraph's lazy continuation. */
|
|
16
|
+
export declare const endLeafAtComponentLine: (_cx: BlockContext, line: Line) => boolean;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
// Component lines (spec §3): a container's opener `::: name {…}`, its closer `::: /name`, a continuation `::: + …`, a
|
|
2
|
+
// slot `:: #name`, and a leaf `:: name[fallback] {…}`. Each is a single-line block whose children are its parts; a
|
|
3
|
+
// container's content between opener and closer stays ordinary Markdown. The forms are `syntax/patterns.js`'s.
|
|
4
|
+
import { fallbackElement, invalidParamsElement, paramListElement, spanElements } from "./component-parts.js";
|
|
5
|
+
import { fallbackEnd } from "./fallback.js";
|
|
6
|
+
import { scanParamBody, scanParamList } from "./params.js";
|
|
7
|
+
import { PATTERNS } from "./syntax.js";
|
|
8
|
+
/** A line form's pattern, at the line's start. */
|
|
9
|
+
const form = (source) => new RegExp(`^${source}`);
|
|
10
|
+
const CONTINUATION = form(PATTERNS.lines.continuation);
|
|
11
|
+
const CLOSER = form(PATTERNS.lines.containerCloser);
|
|
12
|
+
const OPENER = form(PATTERNS.lines.containerOpener);
|
|
13
|
+
const SLOT = form(PATTERNS.lines.slot);
|
|
14
|
+
const LEAF = form(PATTERNS.lines.leaf);
|
|
15
|
+
/** A form's marks without what must follow them: a line with them that matches no form is malformed, not text. */
|
|
16
|
+
const CONTAINER_MARKS = form(`:::${PATTERNS.space}+`);
|
|
17
|
+
const CLOSER_MARKS = form(`:::${PATTERNS.space}+/`);
|
|
18
|
+
const SLOT_MARKS = form(`::${PATTERNS.space}+#`);
|
|
19
|
+
const MALFORMED = { type: "ComponentMalformed", children: [] };
|
|
20
|
+
/** Where each group of a form's match ends, as offsets into the line. */
|
|
21
|
+
function groupEnds(match) {
|
|
22
|
+
let at = 0;
|
|
23
|
+
return match.slice(1).map((group) => (at += group?.length ?? 0));
|
|
24
|
+
}
|
|
25
|
+
/** Builds the parts of one line, `rest` (the line from its `::`), which starts at document position `start`. */
|
|
26
|
+
class LineReader {
|
|
27
|
+
cx;
|
|
28
|
+
rest;
|
|
29
|
+
start;
|
|
30
|
+
constructor(cx, rest, start) {
|
|
31
|
+
this.cx = cx;
|
|
32
|
+
this.rest = rest;
|
|
33
|
+
this.start = start;
|
|
34
|
+
}
|
|
35
|
+
part(type, from, to) {
|
|
36
|
+
return this.cx.elt(type, this.start + from, this.start + to);
|
|
37
|
+
}
|
|
38
|
+
/** Whether only white space follows `from`. */
|
|
39
|
+
endsAt(from) {
|
|
40
|
+
return this.rest.slice(from).trim() === "";
|
|
41
|
+
}
|
|
42
|
+
/** The offset after white space from `from`. */
|
|
43
|
+
skipSpace(from) {
|
|
44
|
+
return from + (/^[ \t]*/.exec(this.rest.slice(from))?.[0].length ?? 0);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The param list at `from` as elements, and the offset after it; a list that isn't closed is invalid to the line's
|
|
48
|
+
* end, so the line keeps its form (spec §5). `undefined` without a `{` there.
|
|
49
|
+
*/
|
|
50
|
+
params(from) {
|
|
51
|
+
if (this.rest[from] !== "{")
|
|
52
|
+
return undefined;
|
|
53
|
+
const list = scanParamList(this.rest, from);
|
|
54
|
+
if (list)
|
|
55
|
+
return { elements: [paramListElement(this.cx, list, this.rest, from, this.start)], end: list.end };
|
|
56
|
+
const end = this.rest.trimEnd().length;
|
|
57
|
+
return { elements: [invalidParamsElement(this.cx, from, end, this.start)], end };
|
|
58
|
+
}
|
|
59
|
+
paramBody(from) {
|
|
60
|
+
const body = scanParamBody(this.rest, from);
|
|
61
|
+
return { elements: spanElements(this.cx, body, this.rest, this.start), end: body.end };
|
|
62
|
+
}
|
|
63
|
+
fallback(from, to) {
|
|
64
|
+
return fallbackElement(this.cx, this.rest, from, to, this.start);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function continuation(line) {
|
|
68
|
+
const match = CONTINUATION.exec(line.rest);
|
|
69
|
+
if (!match)
|
|
70
|
+
return undefined;
|
|
71
|
+
const [marker, , plus] = groupEnds(match);
|
|
72
|
+
const body = line.paramBody(plus);
|
|
73
|
+
if (!line.endsAt(body.end))
|
|
74
|
+
return undefined;
|
|
75
|
+
return {
|
|
76
|
+
type: "ComponentContinuation",
|
|
77
|
+
children: [line.part("ComponentMark", 0, marker), line.part("ComponentMark", plus - 1, plus), ...body.elements],
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
function closer(line) {
|
|
81
|
+
const match = CLOSER.exec(line.rest);
|
|
82
|
+
if (!match)
|
|
83
|
+
return CLOSER_MARKS.test(line.rest) ? MALFORMED : undefined;
|
|
84
|
+
const [marker, , slash, name] = groupEnds(match);
|
|
85
|
+
if (!line.endsAt(name))
|
|
86
|
+
return MALFORMED;
|
|
87
|
+
return {
|
|
88
|
+
type: "ComponentClose",
|
|
89
|
+
children: [line.part("ComponentMark", 0, marker), line.part("ComponentMark", slash - 1, slash), line.part("ComponentName", slash, name)],
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function opener(line) {
|
|
93
|
+
if (!CONTAINER_MARKS.test(line.rest))
|
|
94
|
+
return undefined;
|
|
95
|
+
const match = OPENER.exec(line.rest);
|
|
96
|
+
if (!match)
|
|
97
|
+
return MALFORMED;
|
|
98
|
+
const [marker, space, name] = groupEnds(match);
|
|
99
|
+
const params = line.params(line.skipSpace(name));
|
|
100
|
+
if (!line.endsAt(params?.end ?? name))
|
|
101
|
+
return MALFORMED;
|
|
102
|
+
return {
|
|
103
|
+
type: "ComponentOpen",
|
|
104
|
+
children: [line.part("ComponentMark", 0, marker), line.part("ComponentName", space, name), ...(params?.elements ?? [])],
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
function slot(line) {
|
|
108
|
+
const match = SLOT.exec(line.rest);
|
|
109
|
+
if (!match)
|
|
110
|
+
return SLOT_MARKS.test(line.rest) ? MALFORMED : undefined;
|
|
111
|
+
const [marker, , hash, name] = groupEnds(match);
|
|
112
|
+
if (!line.endsAt(name))
|
|
113
|
+
return MALFORMED;
|
|
114
|
+
return {
|
|
115
|
+
type: "ComponentSlot",
|
|
116
|
+
children: [line.part("ComponentMark", 0, marker), line.part("ComponentMark", hash - 1, hash), line.part("ComponentSlotName", hash, name)],
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
function leaf(line) {
|
|
120
|
+
const match = LEAF.exec(line.rest);
|
|
121
|
+
if (!match)
|
|
122
|
+
return undefined;
|
|
123
|
+
const [marker, space, name] = groupEnds(match);
|
|
124
|
+
const children = [line.part("ComponentMark", 0, marker), line.part("ComponentName", space, name)];
|
|
125
|
+
let end = name;
|
|
126
|
+
if (line.rest[end] === "[") {
|
|
127
|
+
const closed = fallbackEnd(line.rest, end);
|
|
128
|
+
if (closed === undefined)
|
|
129
|
+
return undefined;
|
|
130
|
+
children.push(line.fallback(end, closed));
|
|
131
|
+
end = closed;
|
|
132
|
+
}
|
|
133
|
+
const params = line.params(line.skipSpace(end));
|
|
134
|
+
if (params) {
|
|
135
|
+
children.push(...params.elements);
|
|
136
|
+
end = params.end;
|
|
137
|
+
}
|
|
138
|
+
return line.endsAt(end) ? { type: "ComponentLeaf", children } : undefined;
|
|
139
|
+
}
|
|
140
|
+
/** The forms in the order they are tried: the longest marks first. */
|
|
141
|
+
const FORMS = [continuation, closer, opener, slot, leaf];
|
|
142
|
+
/** Parses one line that starts with `::` (`rest`), at document position `start`; a line matching no form is malformed (F016). */
|
|
143
|
+
export function parseComponentLine(cx, rest, start) {
|
|
144
|
+
const line = new LineReader(cx, rest, start);
|
|
145
|
+
for (const tryForm of FORMS) {
|
|
146
|
+
const parsed = tryForm(line);
|
|
147
|
+
if (parsed)
|
|
148
|
+
return parsed;
|
|
149
|
+
}
|
|
150
|
+
return MALFORMED;
|
|
151
|
+
}
|
|
152
|
+
/** Whether `line` starts a component line: `::` at its start, indented less than code. */
|
|
153
|
+
export function startsComponentLine(line) {
|
|
154
|
+
return line.indent < line.baseIndent + 4 && line.text.startsWith("::", line.pos);
|
|
155
|
+
}
|
|
156
|
+
/** The block parser: one component line, one block. */
|
|
157
|
+
export function parseComponentLineBlock(cx, line) {
|
|
158
|
+
if (!startsComponentLine(line))
|
|
159
|
+
return false;
|
|
160
|
+
const start = cx.lineStart + line.pos;
|
|
161
|
+
const { type, children } = parseComponentLine(cx, line.text.slice(line.pos), start);
|
|
162
|
+
cx.addElement(cx.elt(type, start, cx.lineStart + line.text.trimEnd().length, children));
|
|
163
|
+
cx.nextLine();
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
/** Ends a paragraph at a component line, so it is never a paragraph's lazy continuation. */
|
|
167
|
+
export const endLeafAtComponentLine = (_cx, line) => startsComponentLine(line);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Element } from "@lezer/markdown";
|
|
2
|
+
import type { ElementContext } from "./context.ts";
|
|
3
|
+
import type { Scanned } from "./params.ts";
|
|
4
|
+
/** The spans of `scanned` (offsets into `text`) as elements, `text` starting at document position `base`. */
|
|
5
|
+
export declare function spanElements(cx: ElementContext, scanned: Scanned, text: string, base: number): Element[];
|
|
6
|
+
/** A `ParamList` element for the list `{…}` from `from` (offsets into `text`, which starts at `base`). */
|
|
7
|
+
export declare function paramListElement(cx: ElementContext, list: Scanned, text: string, from: number, base: number): Element;
|
|
8
|
+
/** A `ParamInvalid` element: a param list that isn't closed, from its `{` at `from` to `to` (offsets into `text`). */
|
|
9
|
+
export declare function invalidParamsElement(cx: ElementContext, from: number, to: number, base: number): Element;
|
|
10
|
+
/** A `ComponentFallback` element for `[…]` from `from` to `to` (offsets into `text`), its inside parsed as inline Markdown. */
|
|
11
|
+
export declare function fallbackElement(cx: ElementContext, text: string, from: number, to: number, base: number): Element;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// The parts component lines and inline components share: a param list, and a fallback `[…]`.
|
|
2
|
+
import { interpolationsIn } from "./interpolation.js";
|
|
3
|
+
/** A span as an element, with its parts and, in a quoted string, its interpolations, in document order. */
|
|
4
|
+
function spanElement(cx, span, text, base) {
|
|
5
|
+
const parts = (span.parts ?? []).map((part) => spanElement(cx, part, text, base));
|
|
6
|
+
const interpolations = span.type === "ParamString" ? interpolationsIn(cx, text.slice(span.from, span.to), base + span.from) : [];
|
|
7
|
+
const children = [...parts, ...interpolations].sort((a, b) => a.from - b.from);
|
|
8
|
+
return cx.elt(span.type, base + span.from, base + span.to, children);
|
|
9
|
+
}
|
|
10
|
+
/** The spans of `scanned` (offsets into `text`) as elements, `text` starting at document position `base`. */
|
|
11
|
+
export function spanElements(cx, scanned, text, base) {
|
|
12
|
+
return scanned.spans.map((span) => spanElement(cx, span, text, base));
|
|
13
|
+
}
|
|
14
|
+
/** A `ParamList` element for the list `{…}` from `from` (offsets into `text`, which starts at `base`). */
|
|
15
|
+
export function paramListElement(cx, list, text, from, base) {
|
|
16
|
+
return cx.elt("ParamList", base + from, base + list.end, [
|
|
17
|
+
cx.elt("ParamBrace", base + from, base + from + 1),
|
|
18
|
+
...spanElements(cx, list, text, base),
|
|
19
|
+
cx.elt("ParamBrace", base + list.end - 1, base + list.end),
|
|
20
|
+
]);
|
|
21
|
+
}
|
|
22
|
+
/** A `ParamInvalid` element: a param list that isn't closed, from its `{` at `from` to `to` (offsets into `text`). */
|
|
23
|
+
export function invalidParamsElement(cx, from, to, base) {
|
|
24
|
+
return cx.elt("ParamInvalid", base + from, base + to);
|
|
25
|
+
}
|
|
26
|
+
/** A `ComponentFallback` element for `[…]` from `from` to `to` (offsets into `text`), its inside parsed as inline Markdown. */
|
|
27
|
+
export function fallbackElement(cx, text, from, to, base) {
|
|
28
|
+
return cx.elt("ComponentFallback", base + from, base + to, [
|
|
29
|
+
cx.elt("ComponentFallbackMark", base + from, base + from + 1),
|
|
30
|
+
...cx.parser.parseInline(text.slice(from + 1, to - 1), base + from + 1),
|
|
31
|
+
cx.elt("ComponentFallbackMark", base + to - 1, base + to),
|
|
32
|
+
]);
|
|
33
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Element, MarkdownParser } from "@lezer/markdown";
|
|
2
|
+
/** The part of a `BlockContext` or `InlineContext` the grammar's element builders use. */
|
|
3
|
+
export interface ElementContext {
|
|
4
|
+
elt(type: string, from: number, to: number, children?: readonly Element[]): Element;
|
|
5
|
+
readonly parser: MarkdownParser;
|
|
6
|
+
}
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// A component definition (spec §7.2) and a signature fence (§7.1): a fenced code block whose info string is `compmd`
|
|
2
|
+
// with a `component=` attribute. Its info string is read in the param syntax, the component's name as a name; its
|
|
3
|
+
// signature is the lines before the first `---` line, and a definition's body, after it, is CompMD again, parsed by
|
|
4
|
+
// the same parser. Any other fence stays @lezer/markdown's.
|
|
5
|
+
import { paramListElement, spanElements } from "./component-parts.js";
|
|
6
|
+
import { scanParamBody, scanParamList } from "./params.js";
|
|
7
|
+
/** A fence's opening line: its fence, then `compmd` and the rest of the info string. */
|
|
8
|
+
const OPENING = /^(`{3,}|~{3,})([ \t]*)compmd(?=[ \t]|$)/;
|
|
9
|
+
const LANGUAGE = "compmd";
|
|
10
|
+
/** The info string's attributes and param lists from `from`, until text that is neither. */
|
|
11
|
+
function scanInfo(text, from) {
|
|
12
|
+
const parts = [];
|
|
13
|
+
let at = from;
|
|
14
|
+
for (;;) {
|
|
15
|
+
while (text[at] === " " || text[at] === "\t")
|
|
16
|
+
at++;
|
|
17
|
+
const list = text[at] === "{";
|
|
18
|
+
const scanned = list ? scanParamList(text, at) : scanParamBody(text, at, "{");
|
|
19
|
+
if (!scanned || scanned.end === at)
|
|
20
|
+
return parts;
|
|
21
|
+
parts.push({ list, from: at, scanned });
|
|
22
|
+
at = scanned.end;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/** Whether `span` is the value of a `component` attribute, among `spans`. */
|
|
26
|
+
function isComponentName(text, spans, index) {
|
|
27
|
+
const key = spans[index - 2];
|
|
28
|
+
return spans[index].type === "ParamBare" && key?.type === "ParamKey" && text.slice(key.from, key.to) === "component";
|
|
29
|
+
}
|
|
30
|
+
/** Whether the info string names a component. */
|
|
31
|
+
function namesComponent(text, parts) {
|
|
32
|
+
return parts.some((part) => !part.list && part.scanned.spans.some((_, i) => isComponentName(text, part.scanned.spans, i)));
|
|
33
|
+
}
|
|
34
|
+
/** The info string's elements, `text` starting at document position `base`; the component's name is a `ComponentName`. */
|
|
35
|
+
function infoElements(cx, text, parts, base) {
|
|
36
|
+
return parts.flatMap(({ list, from, scanned }) => {
|
|
37
|
+
if (list)
|
|
38
|
+
return [paramListElement(cx, scanned, text, from, base)];
|
|
39
|
+
const elements = spanElements(cx, scanned, text, base);
|
|
40
|
+
return elements.map((element, i) => isComponentName(text, scanned.spans, i) ? cx.elt("ComponentName", element.from, element.to) : element);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/** Whether `line` closes a fence opened by `fence`: the same character, at least as many, then only white space. */
|
|
44
|
+
function closes(line, fence) {
|
|
45
|
+
const run = /^(`+|~+)[ \t]*$/.exec(line.text.slice(line.pos));
|
|
46
|
+
return line.indent < line.baseIndent + 4 && !!run && run[1][0] === fence[0] && run[1].length >= fence.length;
|
|
47
|
+
}
|
|
48
|
+
/** `lines` as one element of `type`; a body's CompMD is parsed again when its lines are the document's as they are. */
|
|
49
|
+
function linesElement(cx, type, lines, nested) {
|
|
50
|
+
if (lines.length === 0)
|
|
51
|
+
return [];
|
|
52
|
+
const from = lines[0].from;
|
|
53
|
+
const to = lines[lines.length - 1].to;
|
|
54
|
+
const parsed = nested && lines.every((line) => line.whole);
|
|
55
|
+
const children = parsed ? [cx.elt(cx.parser.parse(lines.map((line) => line.text).join("\n")), from)] : [];
|
|
56
|
+
return [cx.elt(type, from, to, children)];
|
|
57
|
+
}
|
|
58
|
+
/** The block parser: a definition or a signature fence, to its closing fence or the end of its container. */
|
|
59
|
+
export function parseDefinition(cx, line) {
|
|
60
|
+
if (line.indent >= line.baseIndent + 4)
|
|
61
|
+
return false;
|
|
62
|
+
const text = line.text.slice(line.pos);
|
|
63
|
+
const opening = OPENING.exec(text);
|
|
64
|
+
if (!opening)
|
|
65
|
+
return false;
|
|
66
|
+
const fence = opening[1];
|
|
67
|
+
const languageFrom = opening[0].length - LANGUAGE.length;
|
|
68
|
+
if (fence[0] === "`" && text.includes("`", fence.length))
|
|
69
|
+
return false;
|
|
70
|
+
const parts = scanInfo(text, opening[0].length);
|
|
71
|
+
if (!namesComponent(text, parts))
|
|
72
|
+
return false;
|
|
73
|
+
const start = cx.lineStart + line.pos;
|
|
74
|
+
const opened = [
|
|
75
|
+
cx.elt("CodeMark", start, start + fence.length),
|
|
76
|
+
cx.elt("CodeInfo", start + languageFrom, start + opening[0].length),
|
|
77
|
+
cx.elt("DefinitionInfo", start + opening[0].length, start + text.trimEnd().length, infoElements(cx, text, parts, start)),
|
|
78
|
+
];
|
|
79
|
+
const signature = [];
|
|
80
|
+
const body = [];
|
|
81
|
+
let separator;
|
|
82
|
+
let closing;
|
|
83
|
+
let end = start + text.trimEnd().length;
|
|
84
|
+
while (cx.nextLine()) {
|
|
85
|
+
if (closes(line, fence)) {
|
|
86
|
+
const from = cx.lineStart + line.pos;
|
|
87
|
+
end = from + line.text.slice(line.pos).trimEnd().length;
|
|
88
|
+
closing = cx.elt("CodeMark", from, end);
|
|
89
|
+
cx.nextLine();
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
const content = {
|
|
93
|
+
from: cx.lineStart + line.basePos,
|
|
94
|
+
to: cx.lineStart + line.text.length,
|
|
95
|
+
text: line.text.slice(line.basePos),
|
|
96
|
+
whole: line.basePos === 0,
|
|
97
|
+
};
|
|
98
|
+
if (!separator && content.text.trimEnd() === "---")
|
|
99
|
+
separator = cx.elt("DefinitionSeparator", content.from, content.from + 3);
|
|
100
|
+
else
|
|
101
|
+
(separator ? body : signature).push(content);
|
|
102
|
+
}
|
|
103
|
+
if (!closing) {
|
|
104
|
+
const last = separator ? body : signature;
|
|
105
|
+
while (last.length && last[last.length - 1].text.trim() === "")
|
|
106
|
+
last.pop();
|
|
107
|
+
end = last[last.length - 1]?.to ?? separator?.to ?? end;
|
|
108
|
+
}
|
|
109
|
+
const children = [
|
|
110
|
+
...opened,
|
|
111
|
+
...linesElement(cx, "DefinitionSignature", signature, false),
|
|
112
|
+
...(separator ? [separator] : []),
|
|
113
|
+
...linesElement(cx, "DefinitionBody", body, true),
|
|
114
|
+
...(closing ? [closing] : []),
|
|
115
|
+
];
|
|
116
|
+
cx.addElement(cx.elt("ComponentDefinition", start, end, children));
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The offset after the `]` that closes a fallback whose `[` is at `from`. Brackets nest, `\[` and `\]` don't count, and
|
|
3
|
+
* code spans bind tighter, so a `]` inside backticks doesn't close it.
|
|
4
|
+
*
|
|
5
|
+
* @returns the offset after the closing `]`, or `undefined` when the fallback isn't closed in `text`.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* fallbackEnd("[a [b] c] rest", 0); // 9
|
|
9
|
+
*/
|
|
10
|
+
export declare function fallbackEnd(text: string, from: number): number | undefined;
|
package/dist/fallback.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// A component's fallback text, `[…]` (spec §4.4): where it ends.
|
|
2
|
+
/**
|
|
3
|
+
* The offset after the `]` that closes a fallback whose `[` is at `from`. Brackets nest, `\[` and `\]` don't count, and
|
|
4
|
+
* code spans bind tighter, so a `]` inside backticks doesn't close it.
|
|
5
|
+
*
|
|
6
|
+
* @returns the offset after the closing `]`, or `undefined` when the fallback isn't closed in `text`.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* fallbackEnd("[a [b] c] rest", 0); // 9
|
|
10
|
+
*/
|
|
11
|
+
export function fallbackEnd(text, from) {
|
|
12
|
+
let depth = 0;
|
|
13
|
+
for (let at = from; at < text.length; at++) {
|
|
14
|
+
const char = text[at];
|
|
15
|
+
if (char === "\\")
|
|
16
|
+
at++;
|
|
17
|
+
else if (char === "`")
|
|
18
|
+
at = codeSpanEnd(text, at) - 1;
|
|
19
|
+
else if (char === "[")
|
|
20
|
+
depth++;
|
|
21
|
+
else if (char === "]" && --depth === 0)
|
|
22
|
+
return at + 1;
|
|
23
|
+
}
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
/** The offset after the code span whose opening backticks are at `from`, or after the backticks if it isn't closed. */
|
|
27
|
+
function codeSpanEnd(text, from) {
|
|
28
|
+
const run = /^`+/.exec(text.slice(from))[0];
|
|
29
|
+
const close = text.indexOf(run, from + run.length);
|
|
30
|
+
return close >= 0 ? close + run.length : from + run.length;
|
|
31
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// Frontmatter (spec §2.1): a block at the document's start between `---` lines (YAML) or `+++` lines (TOML). Its
|
|
2
|
+
// lines are never Markdown, so `---` doesn't become a rule or a setext heading. The `md` key, everything CompMD
|
|
3
|
+
// reads, is found by `syntax/patterns.js`'s `frontmatterKey`.
|
|
4
|
+
import { PATTERNS } from "./syntax.js";
|
|
5
|
+
/** Each delimiter line, with the pattern of its format's `md` key at a line's start. */
|
|
6
|
+
const FORMATS = {
|
|
7
|
+
"---": new RegExp(PATTERNS.frontmatterKey.yaml),
|
|
8
|
+
"+++": new RegExp(PATTERNS.frontmatterKey.toml),
|
|
9
|
+
};
|
|
10
|
+
const KEY = "md";
|
|
11
|
+
/** The `FrontmatterKey` on the line `text`, which starts at document position `start`, if it has one. */
|
|
12
|
+
function keyOn(cx, key, text, start) {
|
|
13
|
+
const match = key.exec(text);
|
|
14
|
+
if (!match)
|
|
15
|
+
return undefined;
|
|
16
|
+
const from = start + match[0].lastIndexOf(KEY);
|
|
17
|
+
return cx.elt("FrontmatterKey", from, from + KEY.length);
|
|
18
|
+
}
|
|
19
|
+
/** The block parser: from a first line `---` or `+++` to the next such line. Without a closing line, it isn't frontmatter. */
|
|
20
|
+
export function parseFrontmatter(cx, line) {
|
|
21
|
+
const key = FORMATS[line.text];
|
|
22
|
+
if (cx.lineStart !== 0 || !key)
|
|
23
|
+
return false;
|
|
24
|
+
const delimiter = line.text;
|
|
25
|
+
const keys = [];
|
|
26
|
+
while (cx.nextLine()) {
|
|
27
|
+
if (line.text === delimiter) {
|
|
28
|
+
const end = cx.lineStart + delimiter.length;
|
|
29
|
+
cx.addElement(cx.elt("Frontmatter", 0, end, [cx.elt("FrontmatterMark", 0, 3), ...keys, cx.elt("FrontmatterMark", end - 3, end)]));
|
|
30
|
+
cx.nextLine();
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
const found = keyOn(cx, key, line.text, cx.lineStart);
|
|
34
|
+
if (found)
|
|
35
|
+
keys.push(found);
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Tag } from "@lezer/highlight";
|
|
2
|
+
import { type Token } from "./syntax.ts";
|
|
3
|
+
/** The token each of the grammar's nodes stands for. */
|
|
4
|
+
export declare const NODE_TOKENS: Readonly<Record<string, Token>>;
|
|
5
|
+
/**
|
|
6
|
+
* The tag a token's tag name stands for: a `@lezer/highlight` tag (`tagName`), or one modified (`special(string)`).
|
|
7
|
+
*
|
|
8
|
+
* @throws when the name isn't one: `tokens.json` names a tag `@lezer/highlight` doesn't have.
|
|
9
|
+
*/
|
|
10
|
+
export declare function tagNamed(name: string): Tag;
|
|
11
|
+
/** The `styleTags` prop for the grammar's nodes. */
|
|
12
|
+
export declare const compmdHighlighting: import("@lezer/common").NodePropSource;
|