@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
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// Highlighting tags for the grammar's nodes. A node that is one of CompMD's shared tokens takes the token's tag
|
|
2
|
+
// (`syntax/tokens.json`, through `syntax.ts`), so a name has the same colour group as in every other editor; the rest
|
|
3
|
+
// are the grammar's own.
|
|
4
|
+
import { styleTags, Tag, tags as t } from "@lezer/highlight";
|
|
5
|
+
import { TOKEN_TAGS } from "./syntax.js";
|
|
6
|
+
/** The token each of the grammar's nodes stands for. */
|
|
7
|
+
export const NODE_TOKENS = {
|
|
8
|
+
ComponentMark: "component-marker",
|
|
9
|
+
ComponentName: "component-name",
|
|
10
|
+
ComponentSlotName: "slot-name",
|
|
11
|
+
ComponentFallbackMark: "fallback-bracket",
|
|
12
|
+
ParamBrace: "param-brace",
|
|
13
|
+
ParamKey: "param-key",
|
|
14
|
+
ParamBreakpoint: "breakpoint",
|
|
15
|
+
ParamOperator: "param-operator",
|
|
16
|
+
ParamClassMark: "param-class",
|
|
17
|
+
ParamClassName: "param-class",
|
|
18
|
+
ParamIdMark: "param-id",
|
|
19
|
+
ParamIdName: "param-id",
|
|
20
|
+
ParamString: "param-string",
|
|
21
|
+
ParamEscape: "string-escape",
|
|
22
|
+
ParamBare: "param-bare",
|
|
23
|
+
ParamExpressionMark: "expression-paren",
|
|
24
|
+
InterpolationMark: "interpolation-brace",
|
|
25
|
+
InterpolationExpression: "interpolation-path",
|
|
26
|
+
WikiLinkTarget: "wiki-link-target",
|
|
27
|
+
WikiLinkLabel: "wiki-link-label",
|
|
28
|
+
InlineMath: "math",
|
|
29
|
+
MathBlock: "math",
|
|
30
|
+
FrontmatterKey: "frontmatter-key",
|
|
31
|
+
DefinitionSeparator: "definition-separator",
|
|
32
|
+
};
|
|
33
|
+
/** The nodes that are no token of their own: what is inside them is another language's, or a mark. */
|
|
34
|
+
const OWN_TAGS = {
|
|
35
|
+
"WikiLinkMark InlineMathMark MathBlockMark FrontmatterMark": t.processingInstruction,
|
|
36
|
+
ParamExpression: t.special(t.variableName),
|
|
37
|
+
ParamJson: t.literal,
|
|
38
|
+
"ComponentMalformed ParamInvalid": t.invalid,
|
|
39
|
+
Frontmatter: t.meta,
|
|
40
|
+
};
|
|
41
|
+
const MODIFIERS = {
|
|
42
|
+
special: t.special,
|
|
43
|
+
definition: t.definition,
|
|
44
|
+
constant: t.constant,
|
|
45
|
+
function: t.function,
|
|
46
|
+
standard: t.standard,
|
|
47
|
+
local: t.local,
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* The tag a token's tag name stands for: a `@lezer/highlight` tag (`tagName`), or one modified (`special(string)`).
|
|
51
|
+
*
|
|
52
|
+
* @throws when the name isn't one: `tokens.json` names a tag `@lezer/highlight` doesn't have.
|
|
53
|
+
*/
|
|
54
|
+
export function tagNamed(name) {
|
|
55
|
+
const modified = /^(\w+)\((\w+)\)$/.exec(name);
|
|
56
|
+
const modifier = modified ? MODIFIERS[modified[1]] : undefined;
|
|
57
|
+
const base = t[modified ? modified[2] : name];
|
|
58
|
+
if (!(base instanceof Tag) || (modified && !modifier))
|
|
59
|
+
throw new Error(`no Lezer tag ${name}`);
|
|
60
|
+
return modifier ? modifier(base) : base;
|
|
61
|
+
}
|
|
62
|
+
/** The `styleTags` prop for the grammar's nodes. */
|
|
63
|
+
export const compmdHighlighting = styleTags({
|
|
64
|
+
...Object.fromEntries(Object.entries(NODE_TOKENS).map(([node, token]) => [node, tagNamed(TOKEN_TAGS[token])])),
|
|
65
|
+
...OWN_TAGS,
|
|
66
|
+
});
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { InlineContext } from "@lezer/markdown";
|
|
2
|
+
/** The inline parser. A fallback is required; `{{` right after it starts an interpolation, not a param list (spec §3.1). */
|
|
3
|
+
export declare function parseInlineComponent(cx: InlineContext, next: number, pos: number): number;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Inline components, `:name[fallback]{…}` (spec §3.2).
|
|
2
|
+
import { fallbackElement, paramListElement } from "./component-parts.js";
|
|
3
|
+
import { fallbackEnd } from "./fallback.js";
|
|
4
|
+
import { scanParamList } from "./params.js";
|
|
5
|
+
import { PATTERNS } from "./syntax.js";
|
|
6
|
+
const COLON = 58;
|
|
7
|
+
/** `:name[`, an inline component's start (`syntax/patterns.js`). */
|
|
8
|
+
const START = new RegExp(`^${PATTERNS.inlineComponentStart}`);
|
|
9
|
+
/** Where an inline component may start (spec §3.2 rule 4): at the start, or after white space or punctuation other than `:` and `\`. */
|
|
10
|
+
function mayStartAt(cx, pos) {
|
|
11
|
+
if (pos === cx.offset)
|
|
12
|
+
return true;
|
|
13
|
+
const before = String.fromCharCode(cx.char(pos - 1));
|
|
14
|
+
return /\s/.test(before) || (/[\p{P}\p{S}]/u.test(before) && before !== ":" && before !== "\\");
|
|
15
|
+
}
|
|
16
|
+
/** The inline parser. A fallback is required; `{{` right after it starts an interpolation, not a param list (spec §3.1). */
|
|
17
|
+
export function parseInlineComponent(cx, next, pos) {
|
|
18
|
+
if (next !== COLON || !mayStartAt(cx, pos))
|
|
19
|
+
return -1;
|
|
20
|
+
const text = cx.slice(pos, cx.end);
|
|
21
|
+
const start = START.exec(text);
|
|
22
|
+
if (!start)
|
|
23
|
+
return -1;
|
|
24
|
+
const fallbackAt = start[0].length - 1;
|
|
25
|
+
const fallback = fallbackEnd(text, fallbackAt);
|
|
26
|
+
if (fallback === undefined)
|
|
27
|
+
return -1;
|
|
28
|
+
const list = text.startsWith("{{", fallback) ? undefined : scanParamList(text, fallback);
|
|
29
|
+
const children = [
|
|
30
|
+
cx.elt("ComponentMark", pos, pos + 1),
|
|
31
|
+
cx.elt("ComponentName", pos + 1, pos + fallbackAt),
|
|
32
|
+
fallbackElement(cx, text, fallbackAt, fallback, pos),
|
|
33
|
+
...(list ? [paramListElement(cx, list, text, fallback, pos)] : []),
|
|
34
|
+
];
|
|
35
|
+
return cx.addElement(cx.elt("InlineComponent", pos, pos + (list?.end ?? fallback), children));
|
|
36
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Element, InlineContext } from "@lezer/markdown";
|
|
2
|
+
import type { ElementContext } from "./context.ts";
|
|
3
|
+
/** The interpolations in a quoted param string `text`, which starts at document position `base`. `\{{` isn't one. */
|
|
4
|
+
export declare function interpolationsIn(cx: ElementContext, text: string, base: number): Element[];
|
|
5
|
+
/** The inline parser: `{{path}}` in text and fallbacks. An escaped `\{{` is left to Markdown's escapes. */
|
|
6
|
+
export declare function parseInterpolation(cx: InlineContext, next: number, pos: number): number;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// Interpolation, `{{path}}` (spec §8.1): in text, in a fallback and in a quoted param string. The form is
|
|
2
|
+
// `syntax/patterns.js`'s.
|
|
3
|
+
import { PATTERNS } from "./syntax.js";
|
|
4
|
+
const BRACE = 123;
|
|
5
|
+
/**
|
|
6
|
+
* An interpolation's form: `{{`, a path of CEL identifiers with white space around it allowed, `}}`. Anything else
|
|
7
|
+
* stays text (W106).
|
|
8
|
+
*/
|
|
9
|
+
const INTERPOLATION = new RegExp(`^${PATTERNS.interpolation}`);
|
|
10
|
+
/** The interpolation at the start of `text`: its length, and where its path is. */
|
|
11
|
+
function interpolationAt(text) {
|
|
12
|
+
const match = INTERPOLATION.exec(text);
|
|
13
|
+
if (!match)
|
|
14
|
+
return undefined;
|
|
15
|
+
const [, open, before, path] = match;
|
|
16
|
+
const pathStart = open.length + before.length;
|
|
17
|
+
return { length: match[0].length, path: [pathStart, pathStart + path.length] };
|
|
18
|
+
}
|
|
19
|
+
/** The `Interpolation` element for the interpolation at `from`. */
|
|
20
|
+
function interpolationElement(cx, from, found) {
|
|
21
|
+
const to = from + found.length;
|
|
22
|
+
return cx.elt("Interpolation", from, to, [
|
|
23
|
+
cx.elt("InterpolationMark", from, from + 2),
|
|
24
|
+
cx.elt("InterpolationExpression", from + found.path[0], from + found.path[1]),
|
|
25
|
+
cx.elt("InterpolationMark", to - 2, to),
|
|
26
|
+
]);
|
|
27
|
+
}
|
|
28
|
+
/** The interpolations in a quoted param string `text`, which starts at document position `base`. `\{{` isn't one. */
|
|
29
|
+
export function interpolationsIn(cx, text, base) {
|
|
30
|
+
const found = [];
|
|
31
|
+
for (const match of text.matchAll(/(?<!\\)\{\{/g)) {
|
|
32
|
+
const interpolation = interpolationAt(text.slice(match.index));
|
|
33
|
+
if (interpolation)
|
|
34
|
+
found.push(interpolationElement(cx, base + match.index, interpolation));
|
|
35
|
+
}
|
|
36
|
+
return found;
|
|
37
|
+
}
|
|
38
|
+
/** The inline parser: `{{path}}` in text and fallbacks. An escaped `\{{` is left to Markdown's escapes. */
|
|
39
|
+
export function parseInterpolation(cx, next, pos) {
|
|
40
|
+
if (next !== BRACE || cx.char(pos + 1) !== BRACE)
|
|
41
|
+
return -1;
|
|
42
|
+
const interpolation = interpolationAt(cx.slice(pos, cx.end));
|
|
43
|
+
return interpolation ? cx.addElement(interpolationElement(cx, pos, interpolation)) : -1;
|
|
44
|
+
}
|
package/dist/math.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { BlockContext, InlineContext, Line } from "@lezer/markdown";
|
|
2
|
+
/**
|
|
3
|
+
* The inline parser, by Pandoc's rule: the opener is followed by a non-space, the closer preceded by one and not
|
|
4
|
+
* followed by a digit, braces group (a `$` inside them doesn't close). So "costs $5 and $10" stays text.
|
|
5
|
+
*/
|
|
6
|
+
export declare function parseInlineMath(cx: InlineContext, next: number, pos: number): number;
|
|
7
|
+
/** The block parser: from a line starting with `$$` to the first line ending with `$$`, which may be the same line. */
|
|
8
|
+
export declare function parseMathBlock(cx: BlockContext, line: Line): boolean;
|
|
9
|
+
/** Ends a paragraph at a `$$` line. */
|
|
10
|
+
export declare const endLeafAtMathBlock: (_cx: BlockContext, line: Line) => boolean;
|
package/dist/math.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// Math (spec §2.3): inline `$…$` by Pandoc's rule, and display math on `$$` lines.
|
|
2
|
+
const DOLLAR = 36;
|
|
3
|
+
const BACKSLASH = 92;
|
|
4
|
+
const OPEN_BRACE = 123;
|
|
5
|
+
const CLOSE_BRACE = 125;
|
|
6
|
+
const isSpace = (code) => code === 32 || code === 9 || code === 10 || code === 13 || code === -1;
|
|
7
|
+
const isDigit = (code) => code >= 48 && code <= 57;
|
|
8
|
+
/**
|
|
9
|
+
* The inline parser, by Pandoc's rule: the opener is followed by a non-space, the closer preceded by one and not
|
|
10
|
+
* followed by a digit, braces group (a `$` inside them doesn't close). So "costs $5 and $10" stays text.
|
|
11
|
+
*/
|
|
12
|
+
export function parseInlineMath(cx, next, pos) {
|
|
13
|
+
if (next !== DOLLAR || cx.char(pos + 1) === DOLLAR || isSpace(cx.char(pos + 1)))
|
|
14
|
+
return -1;
|
|
15
|
+
let depth = 0;
|
|
16
|
+
for (let at = pos + 1; at < cx.end; at++) {
|
|
17
|
+
const char = cx.char(at);
|
|
18
|
+
if (char === BACKSLASH)
|
|
19
|
+
at++;
|
|
20
|
+
else if (char === OPEN_BRACE)
|
|
21
|
+
depth++;
|
|
22
|
+
else if (char === CLOSE_BRACE)
|
|
23
|
+
depth = Math.max(0, depth - 1);
|
|
24
|
+
else if (char === DOLLAR && depth === 0) {
|
|
25
|
+
if (isSpace(cx.char(at - 1)))
|
|
26
|
+
return -1;
|
|
27
|
+
if (isDigit(cx.char(at + 1)))
|
|
28
|
+
continue;
|
|
29
|
+
return cx.addElement(cx.elt("InlineMath", pos, at + 1, [cx.elt("InlineMathMark", pos, pos + 1), cx.elt("InlineMathMark", at, at + 1)]));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return -1;
|
|
33
|
+
}
|
|
34
|
+
function startsMathBlock(line) {
|
|
35
|
+
return line.indent < line.baseIndent + 4 && line.text.startsWith("$$", line.pos);
|
|
36
|
+
}
|
|
37
|
+
/** Adds the `MathBlock` from `start` to the end of the current line, and moves past it. */
|
|
38
|
+
function closeMathBlock(cx, line, start) {
|
|
39
|
+
const end = cx.lineStart + line.text.trimEnd().length;
|
|
40
|
+
cx.addElement(cx.elt("MathBlock", start, end, [cx.elt("MathBlockMark", start, start + 2), cx.elt("MathBlockMark", end - 2, end)]));
|
|
41
|
+
cx.nextLine();
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
/** The block parser: from a line starting with `$$` to the first line ending with `$$`, which may be the same line. */
|
|
45
|
+
export function parseMathBlock(cx, line) {
|
|
46
|
+
if (!startsMathBlock(line))
|
|
47
|
+
return false;
|
|
48
|
+
const start = cx.lineStart + line.pos;
|
|
49
|
+
const first = line.text.slice(line.pos).trimEnd();
|
|
50
|
+
if (first.length > 4 && first.endsWith("$$"))
|
|
51
|
+
return closeMathBlock(cx, line, start);
|
|
52
|
+
while (cx.nextLine()) {
|
|
53
|
+
if (line.text.trimEnd().endsWith("$$"))
|
|
54
|
+
return closeMathBlock(cx, line, start);
|
|
55
|
+
}
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
/** Ends a paragraph at a `$$` line. */
|
|
59
|
+
export const endLeafAtMathBlock = (_cx, line) => startsMathBlock(line);
|
package/dist/nodes.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** The block nodes a component line becomes (CompMD spec §3), and a line that starts with `::` but matches no form. */
|
|
2
|
+
export declare const COMPONENT_LINES: readonly ["ComponentOpen", "ComponentClose", "ComponentLeaf", "ComponentSlot", "ComponentContinuation", "ComponentMalformed"];
|
|
3
|
+
/** One of {@link COMPONENT_LINES}. */
|
|
4
|
+
export type ComponentLine = (typeof COMPONENT_LINES)[number];
|
|
5
|
+
/** Block nodes other than component lines. */
|
|
6
|
+
export declare const OTHER_BLOCKS: readonly ["Frontmatter", "MathBlock", "ComponentDefinition"];
|
|
7
|
+
/** Inline and part nodes: the pieces of a component line, an inline component, an interpolation, a wiki link, math. */
|
|
8
|
+
export declare const PARTS: readonly ["InlineComponent", "ComponentMark", "ComponentName", "ComponentSlotName", "ComponentFallback", "ComponentFallbackMark", "ParamList", "ParamBrace", "ParamKey", "ParamBreakpoint", "ParamOperator", "ParamString", "ParamEscape", "ParamExpression", "ParamExpressionMark", "ParamJson", "ParamBare", "ParamClass", "ParamClassMark", "ParamClassName", "ParamId", "ParamIdMark", "ParamIdName", "ParamInvalid", "DefinitionInfo", "DefinitionSignature", "DefinitionSeparator", "DefinitionBody", "Interpolation", "InterpolationMark", "InterpolationExpression", "WikiLink", "WikiLinkMark", "WikiLinkTarget", "WikiLinkLabel", "InlineMath", "InlineMathMark", "MathBlockMark", "FrontmatterMark", "FrontmatterKey"];
|
|
9
|
+
/**
|
|
10
|
+
* The nodes that are CompMD's own, not Markdown: an editor that draws Markdown in place shows these as their source.
|
|
11
|
+
* A component's fallback (`ComponentFallback`) is inside them but holds ordinary inline Markdown.
|
|
12
|
+
*/
|
|
13
|
+
export declare const COMPMD_NODES: ReadonlySet<string>;
|
package/dist/nodes.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// The node names the grammar adds to @lezer/markdown's tree, in one place so the parsers, the highlighting and the
|
|
2
|
+
// grammar's definition agree.
|
|
3
|
+
/** The block nodes a component line becomes (CompMD spec §3), and a line that starts with `::` but matches no form. */
|
|
4
|
+
export const COMPONENT_LINES = [
|
|
5
|
+
"ComponentOpen",
|
|
6
|
+
"ComponentClose",
|
|
7
|
+
"ComponentLeaf",
|
|
8
|
+
"ComponentSlot",
|
|
9
|
+
"ComponentContinuation",
|
|
10
|
+
"ComponentMalformed",
|
|
11
|
+
];
|
|
12
|
+
/** Block nodes other than component lines. */
|
|
13
|
+
export const OTHER_BLOCKS = ["Frontmatter", "MathBlock", "ComponentDefinition"];
|
|
14
|
+
/** Inline and part nodes: the pieces of a component line, an inline component, an interpolation, a wiki link, math. */
|
|
15
|
+
export const PARTS = [
|
|
16
|
+
"InlineComponent",
|
|
17
|
+
"ComponentMark",
|
|
18
|
+
"ComponentName",
|
|
19
|
+
"ComponentSlotName",
|
|
20
|
+
"ComponentFallback",
|
|
21
|
+
"ComponentFallbackMark",
|
|
22
|
+
"ParamList",
|
|
23
|
+
"ParamBrace",
|
|
24
|
+
"ParamKey",
|
|
25
|
+
"ParamBreakpoint",
|
|
26
|
+
"ParamOperator",
|
|
27
|
+
"ParamString",
|
|
28
|
+
"ParamEscape",
|
|
29
|
+
"ParamExpression",
|
|
30
|
+
"ParamExpressionMark",
|
|
31
|
+
"ParamJson",
|
|
32
|
+
"ParamBare",
|
|
33
|
+
"ParamClass",
|
|
34
|
+
"ParamClassMark",
|
|
35
|
+
"ParamClassName",
|
|
36
|
+
"ParamId",
|
|
37
|
+
"ParamIdMark",
|
|
38
|
+
"ParamIdName",
|
|
39
|
+
"ParamInvalid",
|
|
40
|
+
"DefinitionInfo",
|
|
41
|
+
"DefinitionSignature",
|
|
42
|
+
"DefinitionSeparator",
|
|
43
|
+
"DefinitionBody",
|
|
44
|
+
"Interpolation",
|
|
45
|
+
"InterpolationMark",
|
|
46
|
+
"InterpolationExpression",
|
|
47
|
+
"WikiLink",
|
|
48
|
+
"WikiLinkMark",
|
|
49
|
+
"WikiLinkTarget",
|
|
50
|
+
"WikiLinkLabel",
|
|
51
|
+
"InlineMath",
|
|
52
|
+
"InlineMathMark",
|
|
53
|
+
"MathBlockMark",
|
|
54
|
+
"FrontmatterMark",
|
|
55
|
+
"FrontmatterKey",
|
|
56
|
+
];
|
|
57
|
+
/**
|
|
58
|
+
* The nodes that are CompMD's own, not Markdown: an editor that draws Markdown in place shows these as their source.
|
|
59
|
+
* A component's fallback (`ComponentFallback`) is inside them but holds ordinary inline Markdown.
|
|
60
|
+
*/
|
|
61
|
+
export const COMPMD_NODES = new Set([
|
|
62
|
+
...COMPONENT_LINES,
|
|
63
|
+
"InlineComponent",
|
|
64
|
+
"Interpolation",
|
|
65
|
+
"Frontmatter",
|
|
66
|
+
"ComponentDefinition",
|
|
67
|
+
]);
|
package/dist/params.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** The kind of a span inside a param list. */
|
|
2
|
+
export type ParamPart = "ParamKey" | "ParamBreakpoint" | "ParamOperator" | "ParamString" | "ParamEscape" | "ParamExpression" | "ParamExpressionMark" | "ParamJson" | "ParamBare" | "ParamClass" | "ParamClassMark" | "ParamClassName" | "ParamId" | "ParamIdMark" | "ParamIdName";
|
|
3
|
+
/** One span of a param list, as offsets into the scanned text, with the spans inside it. */
|
|
4
|
+
export interface Span {
|
|
5
|
+
readonly type: ParamPart;
|
|
6
|
+
readonly from: number;
|
|
7
|
+
readonly to: number;
|
|
8
|
+
readonly parts?: readonly Span[];
|
|
9
|
+
}
|
|
10
|
+
/** Spans found, and the offset where scanning stopped. */
|
|
11
|
+
export interface Scanned {
|
|
12
|
+
readonly spans: readonly Span[];
|
|
13
|
+
readonly end: number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The params of a param body (no braces) from `from`, up to `stop` (a `}`) or the text's end.
|
|
17
|
+
*
|
|
18
|
+
* @returns the spans, and where the body ended: at `stop`, the text's end, or the first thing that isn't a param.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* scanParamBody('md:gap=l title="A"', 0); // spans ParamBreakpoint, ParamOperator, ParamKey, ParamOperator, ParamBare, …
|
|
22
|
+
*/
|
|
23
|
+
export declare function scanParamBody(text: string, from: number, stop?: string): Scanned;
|
|
24
|
+
/**
|
|
25
|
+
* A param list `{…}` whose `{` is at `from`.
|
|
26
|
+
*
|
|
27
|
+
* @returns its spans and the offset after its `}`, or `undefined` when there is no `{` at `from` or the list isn't
|
|
28
|
+
* closed.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* scanParamList('{title="A" .wide}', 0); // spans ParamKey, ParamOperator, ParamString, ParamClass; end 17
|
|
32
|
+
*/
|
|
33
|
+
export declare function scanParamList(text: string, from: number): Scanned | undefined;
|
package/dist/params.js
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// A CompMD param list (spec §3.1, §5.1), scanned into spans. Each value finds its own end from its first character,
|
|
2
|
+
// so a broken quote or bracket damages only its own line (spec §5). The pieces every grammar shares (a name, a bare
|
|
3
|
+
// value, an escape) are `syntax/patterns.js`'s.
|
|
4
|
+
import { PATTERNS } from "./syntax.js";
|
|
5
|
+
/** A component's, slot's or param key's name (spec §3.1). */
|
|
6
|
+
const NAME = new RegExp(`^${PATTERNS.name}`);
|
|
7
|
+
const BARE_VALUE = new RegExp(`^${PATTERNS.bareValue}`);
|
|
8
|
+
const ESCAPE = new RegExp(PATTERNS.stringEscape, "g");
|
|
9
|
+
const isSpace = (char) => char === " " || char === "\t";
|
|
10
|
+
/** After the `"` that closes a string starting at `from`, or the text's end. */
|
|
11
|
+
function stringEnd(text, from) {
|
|
12
|
+
for (let at = from + 1; at < text.length; at++) {
|
|
13
|
+
if (text[at] === "\\")
|
|
14
|
+
at++;
|
|
15
|
+
else if (text[at] === '"')
|
|
16
|
+
return at + 1;
|
|
17
|
+
}
|
|
18
|
+
return text.length;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* After the bracket matching the one at `from`, counting only outside strings, or the text's end. An expression's
|
|
22
|
+
* strings are CEL's (`"…"`, `'…'`), a JSON value's are `"…"`. CEL's triple-quoted, raw and bytes literals are not
|
|
23
|
+
* told apart (see the README's limits).
|
|
24
|
+
*/
|
|
25
|
+
function bracketedEnd(text, from, open, close, quotes) {
|
|
26
|
+
let depth = 0;
|
|
27
|
+
for (let at = from; at < text.length; at++) {
|
|
28
|
+
const char = text[at];
|
|
29
|
+
if (quotes.includes(char)) {
|
|
30
|
+
for (at++; at < text.length && text[at] !== char; at++)
|
|
31
|
+
if (text[at] === "\\")
|
|
32
|
+
at++;
|
|
33
|
+
}
|
|
34
|
+
else if (char === open)
|
|
35
|
+
depth++;
|
|
36
|
+
else if (char === close && --depth === 0)
|
|
37
|
+
return at + 1;
|
|
38
|
+
}
|
|
39
|
+
return text.length;
|
|
40
|
+
}
|
|
41
|
+
/** A quoted string from `from`, with its escapes. */
|
|
42
|
+
function stringSpan(text, from) {
|
|
43
|
+
const to = stringEnd(text, from);
|
|
44
|
+
const parts = [...text.slice(from, to).matchAll(ESCAPE)].map((match) => ({ type: "ParamEscape", from: from + match.index, to: from + match.index + match[0].length }));
|
|
45
|
+
return { type: "ParamString", from, to, parts };
|
|
46
|
+
}
|
|
47
|
+
/** An expression `(…)` from `from`, with its parentheses when it is closed. */
|
|
48
|
+
function expressionSpan(text, from) {
|
|
49
|
+
const to = bracketedEnd(text, from, "(", ")", "\"'");
|
|
50
|
+
const open = { type: "ParamExpressionMark", from, to: from + 1 };
|
|
51
|
+
const closed = to - from > 1 && text[to - 1] === ")";
|
|
52
|
+
const parts = closed ? [open, { type: "ParamExpressionMark", from: to - 1, to }] : [open];
|
|
53
|
+
return { type: "ParamExpression", from, to, parts };
|
|
54
|
+
}
|
|
55
|
+
function valueSpan(text, from) {
|
|
56
|
+
switch (text[from]) {
|
|
57
|
+
case '"':
|
|
58
|
+
return stringSpan(text, from);
|
|
59
|
+
case "(":
|
|
60
|
+
return expressionSpan(text, from);
|
|
61
|
+
case "[":
|
|
62
|
+
return { type: "ParamJson", from, to: bracketedEnd(text, from, "[", "]", '"') };
|
|
63
|
+
case "{":
|
|
64
|
+
return { type: "ParamJson", from, to: bracketedEnd(text, from, "{", "}", '"') };
|
|
65
|
+
default:
|
|
66
|
+
return { type: "ParamBare", from, to: from + (BARE_VALUE.exec(text.slice(from))?.[0].length ?? 0) };
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/** A class `.name` or an id `#name` at `from`, its mark and its name each a span of their own. */
|
|
70
|
+
function shorthandAt(text, from) {
|
|
71
|
+
const name = NAME.exec(text.slice(from + 1));
|
|
72
|
+
if (!name)
|
|
73
|
+
return undefined;
|
|
74
|
+
const to = from + 1 + name[0].length;
|
|
75
|
+
const [type, mark, named] = text[from] === "." ? ["ParamClass", "ParamClassMark", "ParamClassName"] : ["ParamId", "ParamIdMark", "ParamIdName"];
|
|
76
|
+
const parts = [
|
|
77
|
+
{ type: mark, from, to: from + 1 },
|
|
78
|
+
{ type: named, from: from + 1, to },
|
|
79
|
+
];
|
|
80
|
+
return { spans: [{ type, from, to, parts }], end: to };
|
|
81
|
+
}
|
|
82
|
+
/** A key at `from`, with its breakpoint and the `:` after it when written `bp:key`. */
|
|
83
|
+
function keyAt(text, from) {
|
|
84
|
+
const first = NAME.exec(text.slice(from));
|
|
85
|
+
if (!first)
|
|
86
|
+
return undefined;
|
|
87
|
+
const afterFirst = from + first[0].length;
|
|
88
|
+
const key = text[afterFirst] === ":" ? NAME.exec(text.slice(afterFirst + 1)) : null;
|
|
89
|
+
if (!key)
|
|
90
|
+
return { spans: [{ type: "ParamKey", from, to: afterFirst }], end: afterFirst };
|
|
91
|
+
const end = afterFirst + 1 + key[0].length;
|
|
92
|
+
return {
|
|
93
|
+
spans: [
|
|
94
|
+
{ type: "ParamBreakpoint", from, to: afterFirst },
|
|
95
|
+
{ type: "ParamOperator", from: afterFirst, to: afterFirst + 1 },
|
|
96
|
+
{ type: "ParamKey", from: afterFirst + 1, to: end },
|
|
97
|
+
],
|
|
98
|
+
end,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/** One param at `from`: `key=value`, `bp:key=value`, `.class`, `#id` or a lone `key`. */
|
|
102
|
+
function paramAt(text, from) {
|
|
103
|
+
if (text[from] === "." || text[from] === "#")
|
|
104
|
+
return shorthandAt(text, from);
|
|
105
|
+
const key = keyAt(text, from);
|
|
106
|
+
if (!key || text[key.end] !== "=")
|
|
107
|
+
return key;
|
|
108
|
+
const operator = { type: "ParamOperator", from: key.end, to: key.end + 1 };
|
|
109
|
+
const valueAt = key.end + 1;
|
|
110
|
+
if (valueAt >= text.length || isSpace(text[valueAt]) || text[valueAt] === "}") {
|
|
111
|
+
return { spans: [...key.spans, operator], end: valueAt };
|
|
112
|
+
}
|
|
113
|
+
const value = valueSpan(text, valueAt);
|
|
114
|
+
return { spans: [...key.spans, operator, value], end: value.to };
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* The params of a param body (no braces) from `from`, up to `stop` (a `}`) or the text's end.
|
|
118
|
+
*
|
|
119
|
+
* @returns the spans, and where the body ended: at `stop`, the text's end, or the first thing that isn't a param.
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* scanParamBody('md:gap=l title="A"', 0); // spans ParamBreakpoint, ParamOperator, ParamKey, ParamOperator, ParamBare, …
|
|
123
|
+
*/
|
|
124
|
+
export function scanParamBody(text, from, stop) {
|
|
125
|
+
const spans = [];
|
|
126
|
+
let at = from;
|
|
127
|
+
for (;;) {
|
|
128
|
+
while (isSpace(text[at]))
|
|
129
|
+
at++;
|
|
130
|
+
if (at >= text.length || text[at] === stop)
|
|
131
|
+
return { spans, end: at };
|
|
132
|
+
const param = paramAt(text, at);
|
|
133
|
+
if (!param)
|
|
134
|
+
return { spans, end: at };
|
|
135
|
+
spans.push(...param.spans);
|
|
136
|
+
at = param.end;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* A param list `{…}` whose `{` is at `from`.
|
|
141
|
+
*
|
|
142
|
+
* @returns its spans and the offset after its `}`, or `undefined` when there is no `{` at `from` or the list isn't
|
|
143
|
+
* closed.
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* scanParamList('{title="A" .wide}', 0); // spans ParamKey, ParamOperator, ParamString, ParamClass; end 17
|
|
147
|
+
*/
|
|
148
|
+
export function scanParamList(text, from) {
|
|
149
|
+
if (text[from] !== "{")
|
|
150
|
+
return undefined;
|
|
151
|
+
const body = scanParamBody(text, from + 1, "}");
|
|
152
|
+
return text[body.end] === "}" ? { spans: body.spans, end: body.end + 1 } : undefined;
|
|
153
|
+
}
|
package/dist/syntax.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/** Each token's Lezer highlighting tag, by the token's name (`syntax/tokens.json`). */
|
|
2
|
+
export declare const TOKEN_TAGS: {
|
|
3
|
+
readonly "component-marker": "processingInstruction";
|
|
4
|
+
readonly "component-name": "tagName";
|
|
5
|
+
readonly "slot-name": "labelName";
|
|
6
|
+
readonly "fallback-bracket": "squareBracket";
|
|
7
|
+
readonly "param-brace": "brace";
|
|
8
|
+
readonly "param-key": "attributeName";
|
|
9
|
+
readonly breakpoint: "modifier";
|
|
10
|
+
readonly "param-class": "attributeName";
|
|
11
|
+
readonly "param-id": "attributeName";
|
|
12
|
+
readonly "param-operator": "operator";
|
|
13
|
+
readonly "param-string": "string";
|
|
14
|
+
readonly "string-escape": "escape";
|
|
15
|
+
readonly "param-bare": "special(string)";
|
|
16
|
+
readonly "expression-paren": "processingInstruction";
|
|
17
|
+
readonly "interpolation-brace": "processingInstruction";
|
|
18
|
+
readonly "interpolation-path": "variableName";
|
|
19
|
+
readonly "definition-separator": "separator";
|
|
20
|
+
readonly "wiki-link-target": "url";
|
|
21
|
+
readonly "wiki-link-label": "link";
|
|
22
|
+
readonly math: "monospace";
|
|
23
|
+
readonly "frontmatter-key": "keyword";
|
|
24
|
+
};
|
|
25
|
+
/** A token's name. */
|
|
26
|
+
export type Token = keyof typeof TOKEN_TAGS;
|
|
27
|
+
/** The syntax's pieces as regular-expression sources (`syntax/patterns.js`). */
|
|
28
|
+
export declare const PATTERNS: {
|
|
29
|
+
readonly name: "[A-Za-z][A-Za-z0-9_-]*";
|
|
30
|
+
readonly identifier: "[A-Za-z_][A-Za-z0-9_]*";
|
|
31
|
+
readonly interpolationPath: "[A-Za-z_][A-Za-z0-9_]*(?:\\.[A-Za-z_][A-Za-z0-9_]*)*";
|
|
32
|
+
readonly space: "[ \\t]";
|
|
33
|
+
readonly bareValue: "[^ \\t\\r\\n\"(){}\\[\\]]+";
|
|
34
|
+
readonly stringEscape: "\\\\[!-/:-@\\[-`{-~]";
|
|
35
|
+
readonly celString: "\"(?:[^\"\\\\\\r\\n]|\\\\.)*\"|'(?:[^'\\\\\\r\\n]|\\\\.)*'";
|
|
36
|
+
readonly jsonString: "\"(?:[^\"\\\\\\r\\n]|\\\\.)*\"";
|
|
37
|
+
readonly lines: {
|
|
38
|
+
readonly containerOpener: "(:::)([ \\t]+)([A-Za-z][A-Za-z0-9_-]*)";
|
|
39
|
+
readonly containerCloser: "(:::)([ \\t]+)(/)([A-Za-z][A-Za-z0-9_-]*)";
|
|
40
|
+
readonly continuation: "(:::)([ \\t]+)(\\+)(?=[ \\t])";
|
|
41
|
+
readonly leaf: "(::)([ \\t]+)([A-Za-z][A-Za-z0-9_-]*)";
|
|
42
|
+
readonly slot: "(::)([ \\t]+)(#)([A-Za-z][A-Za-z0-9_-]*)";
|
|
43
|
+
readonly malformed: "(::+)";
|
|
44
|
+
};
|
|
45
|
+
readonly inlineComponentStart: "(:)([A-Za-z][A-Za-z0-9_-]*)(\\[)";
|
|
46
|
+
readonly interpolation: "(\\{\\{)([ \\t]*)([A-Za-z_][A-Za-z0-9_]*(?:\\.[A-Za-z_][A-Za-z0-9_]*)*)([ \\t]*)(\\}\\})";
|
|
47
|
+
readonly wikiLink: "(\\[\\[)([^\\[\\]|\\r\\n]+)(?:(\\|)([^\\[\\]\\r\\n]+))?(\\]\\])";
|
|
48
|
+
readonly frontmatterKey: {
|
|
49
|
+
readonly yaml: "^(md)(:)";
|
|
50
|
+
readonly toml: "^(?:\\[\\[?(md)(?=[.\\]])|(md)(?=[ \\t]*[.=]))";
|
|
51
|
+
};
|
|
52
|
+
};
|
package/dist/syntax.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// Generated by `node generate.js` from compmd-grammars' `syntax/`: edit `syntax/tokens.json` or `syntax/patterns.js`,
|
|
2
|
+
// then generate again.
|
|
3
|
+
/** Each token's Lezer highlighting tag, by the token's name (`syntax/tokens.json`). */
|
|
4
|
+
export const TOKEN_TAGS = {
|
|
5
|
+
"component-marker": "processingInstruction",
|
|
6
|
+
"component-name": "tagName",
|
|
7
|
+
"slot-name": "labelName",
|
|
8
|
+
"fallback-bracket": "squareBracket",
|
|
9
|
+
"param-brace": "brace",
|
|
10
|
+
"param-key": "attributeName",
|
|
11
|
+
"breakpoint": "modifier",
|
|
12
|
+
"param-class": "attributeName",
|
|
13
|
+
"param-id": "attributeName",
|
|
14
|
+
"param-operator": "operator",
|
|
15
|
+
"param-string": "string",
|
|
16
|
+
"string-escape": "escape",
|
|
17
|
+
"param-bare": "special(string)",
|
|
18
|
+
"expression-paren": "processingInstruction",
|
|
19
|
+
"interpolation-brace": "processingInstruction",
|
|
20
|
+
"interpolation-path": "variableName",
|
|
21
|
+
"definition-separator": "separator",
|
|
22
|
+
"wiki-link-target": "url",
|
|
23
|
+
"wiki-link-label": "link",
|
|
24
|
+
"math": "monospace",
|
|
25
|
+
"frontmatter-key": "keyword"
|
|
26
|
+
};
|
|
27
|
+
/** The syntax's pieces as regular-expression sources (`syntax/patterns.js`). */
|
|
28
|
+
export const PATTERNS = {
|
|
29
|
+
"name": "[A-Za-z][A-Za-z0-9_-]*",
|
|
30
|
+
"identifier": "[A-Za-z_][A-Za-z0-9_]*",
|
|
31
|
+
"interpolationPath": "[A-Za-z_][A-Za-z0-9_]*(?:\\.[A-Za-z_][A-Za-z0-9_]*)*",
|
|
32
|
+
"space": "[ \\t]",
|
|
33
|
+
"bareValue": "[^ \\t\\r\\n\"(){}\\[\\]]+",
|
|
34
|
+
"stringEscape": "\\\\[!-/:-@\\[-`{-~]",
|
|
35
|
+
"celString": "\"(?:[^\"\\\\\\r\\n]|\\\\.)*\"|'(?:[^'\\\\\\r\\n]|\\\\.)*'",
|
|
36
|
+
"jsonString": "\"(?:[^\"\\\\\\r\\n]|\\\\.)*\"",
|
|
37
|
+
"lines": {
|
|
38
|
+
"containerOpener": "(:::)([ \\t]+)([A-Za-z][A-Za-z0-9_-]*)",
|
|
39
|
+
"containerCloser": "(:::)([ \\t]+)(/)([A-Za-z][A-Za-z0-9_-]*)",
|
|
40
|
+
"continuation": "(:::)([ \\t]+)(\\+)(?=[ \\t])",
|
|
41
|
+
"leaf": "(::)([ \\t]+)([A-Za-z][A-Za-z0-9_-]*)",
|
|
42
|
+
"slot": "(::)([ \\t]+)(#)([A-Za-z][A-Za-z0-9_-]*)",
|
|
43
|
+
"malformed": "(::+)"
|
|
44
|
+
},
|
|
45
|
+
"inlineComponentStart": "(:)([A-Za-z][A-Za-z0-9_-]*)(\\[)",
|
|
46
|
+
"interpolation": "(\\{\\{)([ \\t]*)([A-Za-z_][A-Za-z0-9_]*(?:\\.[A-Za-z_][A-Za-z0-9_]*)*)([ \\t]*)(\\}\\})",
|
|
47
|
+
"wikiLink": "(\\[\\[)([^\\[\\]|\\r\\n]+)(?:(\\|)([^\\[\\]\\r\\n]+))?(\\]\\])",
|
|
48
|
+
"frontmatterKey": {
|
|
49
|
+
"yaml": "^(md)(:)",
|
|
50
|
+
"toml": "^(?:\\[\\[?(md)(?=[.\\]])|(md)(?=[ \\t]*[.=]))"
|
|
51
|
+
}
|
|
52
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// Wiki links, `[[Target]]` and `[[Target|label]]` (spec §2.3). Parsed always: a grammar can't read the document's
|
|
2
|
+
// opt-in (`syntax: [wiki-links]`), as CompMD's other grammars decide.
|
|
3
|
+
const BRACKET = 91;
|
|
4
|
+
/** The inline parser. A `|` not escaped splits the target from the label; a link never spans lines. */
|
|
5
|
+
export function parseWikiLink(cx, next, pos) {
|
|
6
|
+
if (next !== BRACKET || cx.char(pos + 1) !== BRACKET)
|
|
7
|
+
return -1;
|
|
8
|
+
const text = cx.slice(pos, cx.end);
|
|
9
|
+
const close = text.indexOf("]]", 2);
|
|
10
|
+
if (close < 0)
|
|
11
|
+
return -1;
|
|
12
|
+
const inner = text.slice(2, close);
|
|
13
|
+
if (inner.includes("\n"))
|
|
14
|
+
return -1;
|
|
15
|
+
const end = pos + close + 2;
|
|
16
|
+
const bar = /(?<!\\)\|/.exec(inner)?.index;
|
|
17
|
+
const parts = bar === undefined
|
|
18
|
+
? [cx.elt("WikiLinkTarget", pos + 2, end - 2)]
|
|
19
|
+
: [
|
|
20
|
+
cx.elt("WikiLinkTarget", pos + 2, pos + 2 + bar),
|
|
21
|
+
cx.elt("WikiLinkMark", pos + 2 + bar, pos + 3 + bar),
|
|
22
|
+
cx.elt("WikiLinkLabel", pos + 3 + bar, end - 2),
|
|
23
|
+
];
|
|
24
|
+
return cx.addElement(cx.elt("WikiLink", pos, end, [cx.elt("WikiLinkMark", pos, pos + 2), ...parts, cx.elt("WikiLinkMark", end - 2, end)]));
|
|
25
|
+
}
|