@driftsys/markspec 0.1.0 → 0.3.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/README.md +32 -8
- package/esm/_dnt.polyfills.d.ts +101 -0
- package/esm/_dnt.polyfills.d.ts.map +1 -0
- package/esm/_dnt.polyfills.js +127 -0
- package/esm/compiler/inverses.d.ts +22 -0
- package/esm/compiler/inverses.d.ts.map +1 -0
- package/esm/compiler/inverses.js +147 -0
- package/esm/compiler/link_target.d.ts +19 -0
- package/esm/compiler/link_target.d.ts.map +1 -0
- package/esm/compiler/link_target.js +66 -0
- package/esm/compiler/mod.d.ts +43 -0
- package/esm/compiler/mod.d.ts.map +1 -0
- package/esm/compiler/mod.js +163 -0
- package/esm/compiler/schema.d.ts +38 -0
- package/esm/compiler/schema.d.ts.map +1 -0
- package/esm/compiler/schema.js +44 -0
- package/esm/config/markspec.d.ts +51 -0
- package/esm/config/markspec.d.ts.map +1 -0
- package/esm/config/markspec.js +216 -0
- package/esm/config/mod.d.ts +48 -0
- package/esm/config/mod.d.ts.map +1 -0
- package/esm/config/mod.js +243 -0
- package/esm/formatter/mod.d.ts +65 -0
- package/esm/formatter/mod.d.ts.map +1 -0
- package/esm/formatter/mod.js +306 -0
- package/esm/mod.d.ts +19 -0
- package/esm/mod.d.ts.map +1 -1
- package/esm/mod.js +18 -0
- package/esm/model/attributes.d.ts +47 -0
- package/esm/model/attributes.d.ts.map +1 -0
- package/esm/model/attributes.js +97 -0
- package/esm/model/mod.d.ts +381 -0
- package/esm/model/mod.d.ts.map +1 -0
- package/esm/model/mod.js +73 -0
- package/esm/model/profile.d.ts +189 -0
- package/esm/model/profile.d.ts.map +1 -0
- package/esm/model/profile.js +31 -0
- package/esm/parser/attributes.d.ts +48 -0
- package/esm/parser/attributes.d.ts.map +1 -0
- package/esm/parser/attributes.js +131 -0
- package/esm/parser/captions.d.ts +25 -0
- package/esm/parser/captions.d.ts.map +1 -0
- package/esm/parser/captions.js +148 -0
- package/esm/parser/directives.d.ts +29 -0
- package/esm/parser/directives.d.ts.map +1 -0
- package/esm/parser/directives.js +88 -0
- package/esm/parser/frontmatter.d.ts +53 -0
- package/esm/parser/frontmatter.d.ts.map +1 -0
- package/esm/parser/frontmatter.js +185 -0
- package/esm/parser/grammars.d.ts +22 -0
- package/esm/parser/grammars.d.ts.map +1 -0
- package/esm/parser/grammars.js +70 -0
- package/esm/parser/markdown.d.ts +32 -0
- package/esm/parser/markdown.d.ts.map +1 -0
- package/esm/parser/markdown.js +225 -0
- package/esm/parser/mod.d.ts +73 -0
- package/esm/parser/mod.d.ts.map +1 -0
- package/esm/parser/mod.js +89 -0
- package/esm/parser/references.d.ts +24 -0
- package/esm/parser/references.d.ts.map +1 -0
- package/esm/parser/references.js +71 -0
- package/esm/parser/remark.d.ts +9 -0
- package/esm/parser/remark.d.ts.map +1 -0
- package/esm/parser/remark.js +11 -0
- package/esm/parser/source.d.ts +33 -0
- package/esm/parser/source.d.ts.map +1 -0
- package/esm/parser/source.js +192 -0
- package/esm/profile/cache.d.ts +29 -0
- package/esm/profile/cache.d.ts.map +1 -0
- package/esm/profile/cache.js +45 -0
- package/esm/profile/chain.d.ts +42 -0
- package/esm/profile/chain.d.ts.map +1 -0
- package/esm/profile/chain.js +193 -0
- package/esm/profile/git-cache.d.ts +73 -0
- package/esm/profile/git-cache.d.ts.map +1 -0
- package/esm/profile/git-cache.js +96 -0
- package/esm/profile/load.d.ts +28 -0
- package/esm/profile/load.d.ts.map +1 -0
- package/esm/profile/load.js +50 -0
- package/esm/profile/manifest.d.ts +21 -0
- package/esm/profile/manifest.d.ts.map +1 -0
- package/esm/profile/manifest.js +830 -0
- package/esm/profile/merge.d.ts +26 -0
- package/esm/profile/merge.d.ts.map +1 -0
- package/esm/profile/merge.js +518 -0
- package/esm/profile/mod.d.ts +23 -0
- package/esm/profile/mod.d.ts.map +1 -0
- package/esm/profile/mod.js +14 -0
- package/esm/profile/npm.d.ts +51 -0
- package/esm/profile/npm.d.ts.map +1 -0
- package/esm/profile/npm.js +186 -0
- package/esm/profile/resolver.d.ts +60 -0
- package/esm/profile/resolver.d.ts.map +1 -0
- package/esm/profile/resolver.js +132 -0
- package/esm/reporter/mod.d.ts +31 -0
- package/esm/reporter/mod.d.ts.map +1 -0
- package/esm/reporter/mod.js +173 -0
- package/esm/validator/attributes.d.ts +43 -0
- package/esm/validator/attributes.d.ts.map +1 -0
- package/esm/validator/attributes.js +169 -0
- package/esm/validator/mod.d.ts +44 -0
- package/esm/validator/mod.d.ts.map +1 -0
- package/esm/validator/mod.js +214 -0
- package/esm/validator/normalize.d.ts +21 -0
- package/esm/validator/normalize.d.ts.map +1 -0
- package/esm/validator/normalize.js +58 -0
- package/esm/validator/pattern.d.ts +23 -0
- package/esm/validator/pattern.d.ts.map +1 -0
- package/esm/validator/pattern.js +44 -0
- package/esm/validator/pipeline.d.ts +30 -0
- package/esm/validator/pipeline.d.ts.map +1 -0
- package/esm/validator/pipeline.js +115 -0
- package/esm/validator/traceability.d.ts +48 -0
- package/esm/validator/traceability.d.ts.map +1 -0
- package/esm/validator/traceability.js +141 -0
- package/esm/validator/types.d.ts +48 -0
- package/esm/validator/types.d.ts.map +1 -0
- package/esm/validator/types.js +140 -0
- package/esm/validator/value_types.d.ts +20 -0
- package/esm/validator/value_types.d.ts.map +1 -0
- package/esm/validator/value_types.js +126 -0
- package/package.json +8 -1
- package/script/_dnt.polyfills.d.ts +101 -0
- package/script/_dnt.polyfills.d.ts.map +1 -0
- package/script/_dnt.polyfills.js +130 -0
- package/script/compiler/inverses.d.ts +22 -0
- package/script/compiler/inverses.d.ts.map +1 -0
- package/script/compiler/inverses.js +150 -0
- package/script/compiler/link_target.d.ts +19 -0
- package/script/compiler/link_target.d.ts.map +1 -0
- package/script/compiler/link_target.js +69 -0
- package/script/compiler/mod.d.ts +43 -0
- package/script/compiler/mod.d.ts.map +1 -0
- package/script/compiler/mod.js +169 -0
- package/script/compiler/schema.d.ts +38 -0
- package/script/compiler/schema.d.ts.map +1 -0
- package/script/compiler/schema.js +47 -0
- package/script/config/markspec.d.ts +51 -0
- package/script/config/markspec.d.ts.map +1 -0
- package/script/config/markspec.js +222 -0
- package/script/config/mod.d.ts +48 -0
- package/script/config/mod.d.ts.map +1 -0
- package/script/config/mod.js +250 -0
- package/script/formatter/mod.d.ts +65 -0
- package/script/formatter/mod.d.ts.map +1 -0
- package/script/formatter/mod.js +313 -0
- package/script/mod.d.ts +19 -0
- package/script/mod.d.ts.map +1 -1
- package/script/mod.js +65 -2
- package/script/model/attributes.d.ts +47 -0
- package/script/model/attributes.d.ts.map +1 -0
- package/script/model/attributes.js +101 -0
- package/script/model/mod.d.ts +381 -0
- package/script/model/mod.d.ts.map +1 -0
- package/script/model/mod.js +83 -0
- package/script/model/profile.d.ts +189 -0
- package/script/model/profile.d.ts.map +1 -0
- package/script/model/profile.js +34 -0
- package/script/parser/attributes.d.ts +48 -0
- package/script/parser/attributes.d.ts.map +1 -0
- package/script/parser/attributes.js +137 -0
- package/script/parser/captions.d.ts +25 -0
- package/script/parser/captions.d.ts.map +1 -0
- package/script/parser/captions.js +151 -0
- package/script/parser/directives.d.ts +29 -0
- package/script/parser/directives.d.ts.map +1 -0
- package/script/parser/directives.js +91 -0
- package/script/parser/frontmatter.d.ts +53 -0
- package/script/parser/frontmatter.d.ts.map +1 -0
- package/script/parser/frontmatter.js +188 -0
- package/script/parser/grammars.d.ts +22 -0
- package/script/parser/grammars.d.ts.map +1 -0
- package/script/parser/grammars.js +77 -0
- package/script/parser/markdown.d.ts +32 -0
- package/script/parser/markdown.d.ts.map +1 -0
- package/script/parser/markdown.js +228 -0
- package/script/parser/mod.d.ts +73 -0
- package/script/parser/mod.d.ts.map +1 -0
- package/script/parser/mod.js +101 -0
- package/script/parser/references.d.ts +24 -0
- package/script/parser/references.d.ts.map +1 -0
- package/script/parser/references.js +74 -0
- package/script/parser/remark.d.ts +9 -0
- package/script/parser/remark.d.ts.map +1 -0
- package/script/parser/remark.js +17 -0
- package/script/parser/source.d.ts +33 -0
- package/script/parser/source.d.ts.map +1 -0
- package/script/parser/source.js +198 -0
- package/script/profile/cache.d.ts +29 -0
- package/script/profile/cache.d.ts.map +1 -0
- package/script/profile/cache.js +48 -0
- package/script/profile/chain.d.ts +42 -0
- package/script/profile/chain.d.ts.map +1 -0
- package/script/profile/chain.js +229 -0
- package/script/profile/git-cache.d.ts +73 -0
- package/script/profile/git-cache.d.ts.map +1 -0
- package/script/profile/git-cache.js +104 -0
- package/script/profile/load.d.ts +28 -0
- package/script/profile/load.d.ts.map +1 -0
- package/script/profile/load.js +53 -0
- package/script/profile/manifest.d.ts +21 -0
- package/script/profile/manifest.d.ts.map +1 -0
- package/script/profile/manifest.js +833 -0
- package/script/profile/merge.d.ts +26 -0
- package/script/profile/merge.d.ts.map +1 -0
- package/script/profile/merge.js +521 -0
- package/script/profile/mod.d.ts +23 -0
- package/script/profile/mod.d.ts.map +1 -0
- package/script/profile/mod.js +31 -0
- package/script/profile/npm.d.ts +51 -0
- package/script/profile/npm.d.ts.map +1 -0
- package/script/profile/npm.js +191 -0
- package/script/profile/resolver.d.ts +60 -0
- package/script/profile/resolver.d.ts.map +1 -0
- package/script/profile/resolver.js +136 -0
- package/script/reporter/mod.d.ts +31 -0
- package/script/reporter/mod.d.ts.map +1 -0
- package/script/reporter/mod.js +176 -0
- package/script/validator/attributes.d.ts +43 -0
- package/script/validator/attributes.d.ts.map +1 -0
- package/script/validator/attributes.js +173 -0
- package/script/validator/mod.d.ts +44 -0
- package/script/validator/mod.d.ts.map +1 -0
- package/script/validator/mod.js +231 -0
- package/script/validator/normalize.d.ts +21 -0
- package/script/validator/normalize.d.ts.map +1 -0
- package/script/validator/normalize.js +61 -0
- package/script/validator/pattern.d.ts +23 -0
- package/script/validator/pattern.d.ts.map +1 -0
- package/script/validator/pattern.js +47 -0
- package/script/validator/pipeline.d.ts +30 -0
- package/script/validator/pipeline.d.ts.map +1 -0
- package/script/validator/pipeline.js +118 -0
- package/script/validator/traceability.d.ts +48 -0
- package/script/validator/traceability.d.ts.map +1 -0
- package/script/validator/traceability.js +146 -0
- package/script/validator/types.d.ts +48 -0
- package/script/validator/types.d.ts.map +1 -0
- package/script/validator/types.js +144 -0
- package/script/validator/value_types.d.ts +20 -0
- package/script/validator/value_types.d.ts.map +1 -0
- package/script/validator/value_types.js +129 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module parser/directives
|
|
3
|
+
*
|
|
4
|
+
* Extracts MarkSpec directives from HTML comments in Markdown files.
|
|
5
|
+
*
|
|
6
|
+
* Directives use the form `<!-- markspec:<name> <payload> -->` and allow
|
|
7
|
+
* documents to carry processing hints such as output mode, deprecation
|
|
8
|
+
* notices, or slide layout declarations.
|
|
9
|
+
*/
|
|
10
|
+
import { processor } from "./remark.js";
|
|
11
|
+
/** Matches the opening and closing of an HTML comment. */
|
|
12
|
+
const COMMENT_RE = /^<!--([\s\S]*?)-->$/;
|
|
13
|
+
/** Matches a `markspec:` directive line within a comment body. */
|
|
14
|
+
const DIRECTIVE_RE = /^markspec:(\S+)\s?(.*)?$/;
|
|
15
|
+
/**
|
|
16
|
+
* Detect MarkSpec directives in a Markdown string.
|
|
17
|
+
*
|
|
18
|
+
* Walks the mdast AST looking for `html` nodes that are HTML comments.
|
|
19
|
+
* Inside each comment, lines starting with `markspec:` are parsed as
|
|
20
|
+
* directives. Continuation lines (not starting with `markspec:`) are
|
|
21
|
+
* appended to the preceding directive's payload.
|
|
22
|
+
*
|
|
23
|
+
* @param markdown - Markdown source text
|
|
24
|
+
* @param options - Detection options (file path for source locations)
|
|
25
|
+
* @returns Array of detected directives
|
|
26
|
+
*/
|
|
27
|
+
export function detectDirectives(markdown, options) {
|
|
28
|
+
const file = options?.file ?? "<unknown>";
|
|
29
|
+
const tree = processor.parse(markdown);
|
|
30
|
+
const directives = [];
|
|
31
|
+
for (const node of tree.children) {
|
|
32
|
+
if (node.type !== "html")
|
|
33
|
+
continue;
|
|
34
|
+
const html = node;
|
|
35
|
+
const commentMatch = COMMENT_RE.exec(html.value.trim());
|
|
36
|
+
if (!commentMatch)
|
|
37
|
+
continue;
|
|
38
|
+
const commentBody = commentMatch[1];
|
|
39
|
+
const commentLines = commentBody.split("\n");
|
|
40
|
+
// The node's starting line in the source file.
|
|
41
|
+
const nodeStartLine = html.position?.start.line ?? 1;
|
|
42
|
+
const nodeStartColumn = html.position?.start.column ?? 1;
|
|
43
|
+
// Track the line offset within the comment for each content line.
|
|
44
|
+
// The first line of the comment body is on the same line as `<!--`.
|
|
45
|
+
let currentDirective;
|
|
46
|
+
for (let i = 0; i < commentLines.length; i++) {
|
|
47
|
+
const line = commentLines[i].trim();
|
|
48
|
+
if (line === "") {
|
|
49
|
+
// Empty lines are ignored; they do NOT continue a payload.
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
const directiveMatch = DIRECTIVE_RE.exec(line);
|
|
53
|
+
if (directiveMatch) {
|
|
54
|
+
// Flush any in-progress directive.
|
|
55
|
+
if (currentDirective) {
|
|
56
|
+
directives.push(finalizeDirective(currentDirective));
|
|
57
|
+
}
|
|
58
|
+
const location = {
|
|
59
|
+
file,
|
|
60
|
+
line: nodeStartLine + i,
|
|
61
|
+
column: nodeStartColumn,
|
|
62
|
+
};
|
|
63
|
+
currentDirective = {
|
|
64
|
+
name: directiveMatch[1],
|
|
65
|
+
payloadParts: directiveMatch[2] ? [directiveMatch[2]] : [],
|
|
66
|
+
location,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
else if (currentDirective) {
|
|
70
|
+
// Continuation line for the current directive's payload.
|
|
71
|
+
currentDirective.payloadParts.push(line);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
// Flush final directive from this comment.
|
|
75
|
+
if (currentDirective) {
|
|
76
|
+
directives.push(finalizeDirective(currentDirective));
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return directives;
|
|
80
|
+
}
|
|
81
|
+
/** Assemble a Directive from accumulated parts. */
|
|
82
|
+
function finalizeDirective(pending) {
|
|
83
|
+
return {
|
|
84
|
+
name: pending.name,
|
|
85
|
+
payload: pending.payloadParts.join("\n").trim(),
|
|
86
|
+
location: pending.location,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module parser/frontmatter
|
|
3
|
+
*
|
|
4
|
+
* YAML front-matter parser per ADR-007.
|
|
5
|
+
*
|
|
6
|
+
* Extracts a `---`-delimited YAML block at the top of a Markdown file,
|
|
7
|
+
* classifies keys into core / `metadata` / allowlisted ecosystem, and rejects
|
|
8
|
+
* forbidden keys (MSL-D001). Returns a {@linkcode DocumentAttributes} record
|
|
9
|
+
* and the markdown content with the front matter stripped.
|
|
10
|
+
*
|
|
11
|
+
* Full validation of core-key value types is deferred to the validator
|
|
12
|
+
* (Phase 3); this module handles extraction and key-category classification.
|
|
13
|
+
*/
|
|
14
|
+
import type { Diagnostic, DocumentAttributes } from "../model/mod.js";
|
|
15
|
+
/** Options for {@linkcode extractFrontMatter}. */
|
|
16
|
+
export interface ExtractFrontMatterOptions {
|
|
17
|
+
/** File path used in diagnostic source locations. */
|
|
18
|
+
readonly file?: string;
|
|
19
|
+
/**
|
|
20
|
+
* Profile-declared keys. Accepted alongside core keys, passed through on
|
|
21
|
+
* {@linkcode FrontMatterResult.attributes.extra}.
|
|
22
|
+
*/
|
|
23
|
+
readonly profileKeys?: readonly string[];
|
|
24
|
+
/**
|
|
25
|
+
* Allowlisted ecosystem keys from `.markspec.yaml`
|
|
26
|
+
* (`frontMatter.allowedKeys`). Accepted and passed through on
|
|
27
|
+
* {@linkcode FrontMatterResult.attributes.extra}.
|
|
28
|
+
*/
|
|
29
|
+
readonly allowedKeys?: readonly string[];
|
|
30
|
+
}
|
|
31
|
+
/** Result of front-matter extraction. */
|
|
32
|
+
export interface FrontMatterResult {
|
|
33
|
+
/** Parsed document attributes. Empty when no front matter is present. */
|
|
34
|
+
readonly attributes: DocumentAttributes;
|
|
35
|
+
/** Diagnostics (forbidden keys, unknown keys, YAML errors). */
|
|
36
|
+
readonly diagnostics: readonly Diagnostic[];
|
|
37
|
+
/** Markdown content with front matter stripped. */
|
|
38
|
+
readonly markdown: string;
|
|
39
|
+
/** Whether front matter was present. */
|
|
40
|
+
readonly hadFrontMatter: boolean;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Extract YAML front matter from a Markdown source, validate its keys, and
|
|
44
|
+
* return structured attributes plus the stripped markdown.
|
|
45
|
+
*
|
|
46
|
+
* No front matter → {@linkcode FrontMatterResult.hadFrontMatter} is `false`
|
|
47
|
+
* and attributes are empty.
|
|
48
|
+
*
|
|
49
|
+
* @param markdown - Full source text, including any leading front matter.
|
|
50
|
+
* @param options - Key allowlists and file path for diagnostics.
|
|
51
|
+
*/
|
|
52
|
+
export declare function extractFrontMatter(markdown: string, options?: ExtractFrontMatterOptions): FrontMatterResult;
|
|
53
|
+
//# sourceMappingURL=frontmatter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"frontmatter.d.ts","sourceRoot":"","sources":["../../src/parser/frontmatter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAGH,OAAO,KAAK,EAAE,UAAU,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAyCtE,kDAAkD;AAClD,MAAM,WAAW,yBAAyB;IACxC,qDAAqD;IACrD,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB;;;OAGG;IACH,QAAQ,CAAC,WAAW,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACzC;;;;OAIG;IACH,QAAQ,CAAC,WAAW,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CAC1C;AAED,yCAAyC;AACzC,MAAM,WAAW,iBAAiB;IAChC,yEAAyE;IACzE,QAAQ,CAAC,UAAU,EAAE,kBAAkB,CAAC;IACxC,+DAA+D;IAC/D,QAAQ,CAAC,WAAW,EAAE,SAAS,UAAU,EAAE,CAAC;IAC5C,mDAAmD;IACnD,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,wCAAwC;IACxC,QAAQ,CAAC,cAAc,EAAE,OAAO,CAAC;CAClC;AAED;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAChC,QAAQ,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE,yBAAyB,GAClC,iBAAiB,CA8InB"}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module parser/frontmatter
|
|
3
|
+
*
|
|
4
|
+
* YAML front-matter parser per ADR-007.
|
|
5
|
+
*
|
|
6
|
+
* Extracts a `---`-delimited YAML block at the top of a Markdown file,
|
|
7
|
+
* classifies keys into core / `metadata` / allowlisted ecosystem, and rejects
|
|
8
|
+
* forbidden keys (MSL-D001). Returns a {@linkcode DocumentAttributes} record
|
|
9
|
+
* and the markdown content with the front matter stripped.
|
|
10
|
+
*
|
|
11
|
+
* Full validation of core-key value types is deferred to the validator
|
|
12
|
+
* (Phase 3); this module handles extraction and key-category classification.
|
|
13
|
+
*/
|
|
14
|
+
import { parse as parseYaml } from "@std/yaml";
|
|
15
|
+
/**
|
|
16
|
+
* YAML front-matter delimiter — `---` at start of file, closing `---`, then
|
|
17
|
+
* any number of trailing newlines so the stripped markdown begins cleanly.
|
|
18
|
+
* Empty YAML body (`---\n---\n`) is accepted.
|
|
19
|
+
*/
|
|
20
|
+
const FRONT_MATTER_RE = /^---\r?\n([\s\S]*?)(?:\r?\n)?---\r?\n?(?:\r?\n)*/;
|
|
21
|
+
/**
|
|
22
|
+
* Core document-level attribute keys per ADR-007. Kebab-case by convention.
|
|
23
|
+
*/
|
|
24
|
+
const CORE_KEYS = new Set([
|
|
25
|
+
"document-id",
|
|
26
|
+
"document-type",
|
|
27
|
+
"labels",
|
|
28
|
+
"deprecated",
|
|
29
|
+
"external-id",
|
|
30
|
+
"supersedes",
|
|
31
|
+
"references",
|
|
32
|
+
"metadata",
|
|
33
|
+
]);
|
|
34
|
+
/**
|
|
35
|
+
* Keys forbidden in front matter per ADR-007 — already expressed natively in
|
|
36
|
+
* Markdown (H1, body, images) or in git history (authors, dates).
|
|
37
|
+
*/
|
|
38
|
+
const FORBIDDEN_KEYS = new Set([
|
|
39
|
+
"title",
|
|
40
|
+
"description",
|
|
41
|
+
"toc",
|
|
42
|
+
"authors",
|
|
43
|
+
"author",
|
|
44
|
+
"date",
|
|
45
|
+
"created",
|
|
46
|
+
"modified",
|
|
47
|
+
"cover",
|
|
48
|
+
"images",
|
|
49
|
+
"sections",
|
|
50
|
+
]);
|
|
51
|
+
/**
|
|
52
|
+
* Extract YAML front matter from a Markdown source, validate its keys, and
|
|
53
|
+
* return structured attributes plus the stripped markdown.
|
|
54
|
+
*
|
|
55
|
+
* No front matter → {@linkcode FrontMatterResult.hadFrontMatter} is `false`
|
|
56
|
+
* and attributes are empty.
|
|
57
|
+
*
|
|
58
|
+
* @param markdown - Full source text, including any leading front matter.
|
|
59
|
+
* @param options - Key allowlists and file path for diagnostics.
|
|
60
|
+
*/
|
|
61
|
+
export function extractFrontMatter(markdown, options) {
|
|
62
|
+
const file = options?.file ?? "<unknown>";
|
|
63
|
+
const match = FRONT_MATTER_RE.exec(markdown);
|
|
64
|
+
if (!match) {
|
|
65
|
+
return {
|
|
66
|
+
attributes: {},
|
|
67
|
+
diagnostics: [],
|
|
68
|
+
markdown,
|
|
69
|
+
hadFrontMatter: false,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
const yamlBody = match[1];
|
|
73
|
+
const remaining = markdown.slice(match[0].length);
|
|
74
|
+
const diagnostics = [];
|
|
75
|
+
let raw;
|
|
76
|
+
try {
|
|
77
|
+
raw = parseYaml(yamlBody);
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
diagnostics.push({
|
|
81
|
+
code: "MSL-D001",
|
|
82
|
+
severity: "error",
|
|
83
|
+
message: `front matter: invalid YAML — ${err instanceof Error ? err.message : String(err)}`,
|
|
84
|
+
location: { file, line: 1, column: 1 },
|
|
85
|
+
});
|
|
86
|
+
return {
|
|
87
|
+
attributes: {},
|
|
88
|
+
diagnostics,
|
|
89
|
+
markdown: remaining,
|
|
90
|
+
hadFrontMatter: true,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
if (raw == null) {
|
|
94
|
+
return {
|
|
95
|
+
attributes: {},
|
|
96
|
+
diagnostics,
|
|
97
|
+
markdown: remaining,
|
|
98
|
+
hadFrontMatter: true,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
if (typeof raw !== "object" || Array.isArray(raw)) {
|
|
102
|
+
diagnostics.push({
|
|
103
|
+
code: "MSL-D001",
|
|
104
|
+
severity: "error",
|
|
105
|
+
message: "front matter must be a YAML mapping",
|
|
106
|
+
location: { file, line: 1, column: 1 },
|
|
107
|
+
});
|
|
108
|
+
return {
|
|
109
|
+
attributes: {},
|
|
110
|
+
diagnostics,
|
|
111
|
+
markdown: remaining,
|
|
112
|
+
hadFrontMatter: true,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
const entries = raw;
|
|
116
|
+
const profileKeys = new Set(options?.profileKeys ?? []);
|
|
117
|
+
const allowedKeys = new Set(options?.allowedKeys ?? []);
|
|
118
|
+
const core = {};
|
|
119
|
+
const extra = {};
|
|
120
|
+
let metadata;
|
|
121
|
+
for (const [key, value] of Object.entries(entries)) {
|
|
122
|
+
if (FORBIDDEN_KEYS.has(key)) {
|
|
123
|
+
diagnostics.push({
|
|
124
|
+
code: "MSL-D001",
|
|
125
|
+
severity: "error",
|
|
126
|
+
message: `front matter key '${key}' is forbidden — express it in Markdown or git history instead`,
|
|
127
|
+
location: { file, line: 1, column: 1 },
|
|
128
|
+
});
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
if (key === "metadata") {
|
|
132
|
+
if (value == null || typeof value !== "object" || Array.isArray(value)) {
|
|
133
|
+
diagnostics.push({
|
|
134
|
+
code: "MSL-D001",
|
|
135
|
+
severity: "error",
|
|
136
|
+
message: "front matter 'metadata' must be a mapping",
|
|
137
|
+
location: { file, line: 1, column: 1 },
|
|
138
|
+
});
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
metadata = value;
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
// Transitional: status: was removed in favor of Labels: DRAFT / deprecated:
|
|
145
|
+
if (key === "status") {
|
|
146
|
+
diagnostics.push({
|
|
147
|
+
code: "MSL-D002",
|
|
148
|
+
severity: "warning",
|
|
149
|
+
message: `front-matter key 'status' is no longer recognized; ` +
|
|
150
|
+
`use 'Labels: DRAFT' for draft state or 'deprecated: "<reason>"' for retirement`,
|
|
151
|
+
location: { file, line: 1, column: 1 },
|
|
152
|
+
});
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
if (CORE_KEYS.has(key)) {
|
|
156
|
+
core[key] = value;
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
if (profileKeys.has(key) || allowedKeys.has(key)) {
|
|
160
|
+
extra[key] = value;
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
diagnostics.push({
|
|
164
|
+
code: "MSL-D001",
|
|
165
|
+
severity: "error",
|
|
166
|
+
message: `unknown front matter key '${key}' — must be core, profile-declared, 'metadata', or allowlisted in .markspec.yaml`,
|
|
167
|
+
location: { file, line: 1, column: 1 },
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
const attributes = {};
|
|
171
|
+
const mutable = attributes;
|
|
172
|
+
for (const [key, value] of Object.entries(core)) {
|
|
173
|
+
mutable[key] = value;
|
|
174
|
+
}
|
|
175
|
+
if (metadata !== undefined)
|
|
176
|
+
mutable.metadata = metadata;
|
|
177
|
+
if (Object.keys(extra).length > 0)
|
|
178
|
+
mutable.extra = extra;
|
|
179
|
+
return {
|
|
180
|
+
attributes,
|
|
181
|
+
diagnostics,
|
|
182
|
+
markdown: remaining,
|
|
183
|
+
hadFrontMatter: true,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module parser/grammars
|
|
3
|
+
*
|
|
4
|
+
* Lazy-loading tree-sitter grammar resolver. Maps file extensions to
|
|
5
|
+
* grammar WASM files and caches loaded languages for reuse.
|
|
6
|
+
*/
|
|
7
|
+
import Parser from "web-tree-sitter";
|
|
8
|
+
/** Check whether a file extension has a supported grammar. */
|
|
9
|
+
export declare function isSupportedExtension(ext: string): boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Load a tree-sitter language grammar for the given file extension.
|
|
12
|
+
*
|
|
13
|
+
* Initializes the Parser WASM runtime on first call. Grammar languages
|
|
14
|
+
* are cached — repeated calls with the same extension return the
|
|
15
|
+
* same `Parser.Language` instance. Concurrent-safe: stores the loading
|
|
16
|
+
* promise so parallel calls await the same load.
|
|
17
|
+
*
|
|
18
|
+
* @param ext - File extension including the dot (e.g., `.rs`, `.java`)
|
|
19
|
+
* @throws If the extension is unsupported or the WASM file cannot be loaded
|
|
20
|
+
*/
|
|
21
|
+
export declare function loadGrammar(ext: string): Promise<Parser.Language>;
|
|
22
|
+
//# sourceMappingURL=grammars.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grammars.d.ts","sourceRoot":"","sources":["../../src/parser/grammars.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,MAAM,MAAM,iBAAiB,CAAC;AA+BrC,8DAA8D;AAC9D,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAEzD;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,CAYjE"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module parser/grammars
|
|
3
|
+
*
|
|
4
|
+
* Lazy-loading tree-sitter grammar resolver. Maps file extensions to
|
|
5
|
+
* grammar WASM files and caches loaded languages for reuse.
|
|
6
|
+
*/
|
|
7
|
+
import Parser from "web-tree-sitter";
|
|
8
|
+
import { join } from "@std/path";
|
|
9
|
+
/** Map file extensions to grammar WASM file names. */
|
|
10
|
+
const EXT_TO_GRAMMAR = {
|
|
11
|
+
".rs": "tree-sitter-rust",
|
|
12
|
+
".kt": "tree-sitter-kotlin",
|
|
13
|
+
".kts": "tree-sitter-kotlin",
|
|
14
|
+
".java": "tree-sitter-java",
|
|
15
|
+
".c": "tree-sitter-c",
|
|
16
|
+
".h": "tree-sitter-c",
|
|
17
|
+
".cpp": "tree-sitter-cpp",
|
|
18
|
+
".cc": "tree-sitter-cpp",
|
|
19
|
+
".cxx": "tree-sitter-cpp",
|
|
20
|
+
".hpp": "tree-sitter-cpp",
|
|
21
|
+
".hxx": "tree-sitter-cpp",
|
|
22
|
+
};
|
|
23
|
+
/** Directory containing grammar WASM files. */
|
|
24
|
+
const GRAMMARS_DIR = join(globalThis[Symbol.for("import-meta-ponyfill-esmodule")](import.meta).dirname, "..", "..", "..", "..", "grammars");
|
|
25
|
+
let initialized = false;
|
|
26
|
+
const cache = new Map();
|
|
27
|
+
/** Check whether a file extension has a supported grammar. */
|
|
28
|
+
export function isSupportedExtension(ext) {
|
|
29
|
+
return ext in EXT_TO_GRAMMAR;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Load a tree-sitter language grammar for the given file extension.
|
|
33
|
+
*
|
|
34
|
+
* Initializes the Parser WASM runtime on first call. Grammar languages
|
|
35
|
+
* are cached — repeated calls with the same extension return the
|
|
36
|
+
* same `Parser.Language` instance. Concurrent-safe: stores the loading
|
|
37
|
+
* promise so parallel calls await the same load.
|
|
38
|
+
*
|
|
39
|
+
* @param ext - File extension including the dot (e.g., `.rs`, `.java`)
|
|
40
|
+
* @throws If the extension is unsupported or the WASM file cannot be loaded
|
|
41
|
+
*/
|
|
42
|
+
export function loadGrammar(ext) {
|
|
43
|
+
const grammarName = EXT_TO_GRAMMAR[ext];
|
|
44
|
+
if (!grammarName) {
|
|
45
|
+
throw new Error(`unsupported file extension: ${ext}`);
|
|
46
|
+
}
|
|
47
|
+
let pending = cache.get(grammarName);
|
|
48
|
+
if (!pending) {
|
|
49
|
+
pending = doLoad(grammarName);
|
|
50
|
+
cache.set(grammarName, pending);
|
|
51
|
+
}
|
|
52
|
+
return pending;
|
|
53
|
+
}
|
|
54
|
+
async function doLoad(grammarName) {
|
|
55
|
+
if (!initialized) {
|
|
56
|
+
await Parser.init();
|
|
57
|
+
initialized = true;
|
|
58
|
+
}
|
|
59
|
+
// The compiled binary embeds grammars as .wasm.bin to bypass Deno's
|
|
60
|
+
// WASM-import resolver (see scripts/compile_binary.ts). Prefer the
|
|
61
|
+
// mirror when present; fall back to .wasm in dev mode.
|
|
62
|
+
const binPath = join(GRAMMARS_DIR, `${grammarName}.wasm.bin`);
|
|
63
|
+
try {
|
|
64
|
+
await Deno.stat(binPath);
|
|
65
|
+
return Parser.Language.load(binPath);
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
return Parser.Language.load(join(GRAMMARS_DIR, `${grammarName}.wasm`));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module parser/markdown
|
|
3
|
+
*
|
|
4
|
+
* CommonMark + MarkSpec extension parser. Walks the mdast AST to detect
|
|
5
|
+
* `- [DISPLAY_ID] Title` entry blocks and extract structured attributes.
|
|
6
|
+
*/
|
|
7
|
+
import type { Entry } from "../model/mod.js";
|
|
8
|
+
/** Options for {@linkcode parseMarkdown}. */
|
|
9
|
+
export interface ParseMarkdownOptions {
|
|
10
|
+
/** File path used in source locations. */
|
|
11
|
+
readonly file?: string;
|
|
12
|
+
/**
|
|
13
|
+
* Is this a references document? If undefined, auto-detect from file path.
|
|
14
|
+
* When true, `[slug]` items without an `Id:` attribute are still admitted
|
|
15
|
+
* as referenced-entry candidates (the validator flags the missing `Id:`).
|
|
16
|
+
*/
|
|
17
|
+
readonly isReferencesDoc?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Parse a Markdown string and return all MarkSpec entries found.
|
|
21
|
+
*
|
|
22
|
+
* Walks the mdast AST to detect `- [DISPLAY_ID] Title` list items with
|
|
23
|
+
* indented body content. Extracts display ID, title, body, and trailing
|
|
24
|
+
* attribute blocks. Entry shape (identified or referenced) is decided by
|
|
25
|
+
* the `Id:` attribute's value format.
|
|
26
|
+
*
|
|
27
|
+
* @param markdown - Markdown source text
|
|
28
|
+
* @param options - Parse options (file path for source locations)
|
|
29
|
+
* @returns Array of parsed entries
|
|
30
|
+
*/
|
|
31
|
+
export declare function parseMarkdown(markdown: string, options?: ParseMarkdownOptions): Entry[];
|
|
32
|
+
//# sourceMappingURL=markdown.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"markdown.d.ts","sourceRoot":"","sources":["../../src/parser/markdown.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAa,KAAK,EAAc,MAAM,iBAAiB,CAAC;AASpE,6CAA6C;AAC7C,MAAM,WAAW,oBAAoB;IACnC,0CAA0C;IAC1C,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB;;;;OAIG;IACH,QAAQ,CAAC,eAAe,CAAC,EAAE,OAAO,CAAC;CACpC;AA4BD;;;;;;;;;;;GAWG;AACH,wBAAgB,aAAa,CAC3B,QAAQ,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE,oBAAoB,GAC7B,KAAK,EAAE,CAoCT"}
|