@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.
Files changed (242) hide show
  1. package/README.md +32 -8
  2. package/esm/_dnt.polyfills.d.ts +101 -0
  3. package/esm/_dnt.polyfills.d.ts.map +1 -0
  4. package/esm/_dnt.polyfills.js +127 -0
  5. package/esm/compiler/inverses.d.ts +22 -0
  6. package/esm/compiler/inverses.d.ts.map +1 -0
  7. package/esm/compiler/inverses.js +147 -0
  8. package/esm/compiler/link_target.d.ts +19 -0
  9. package/esm/compiler/link_target.d.ts.map +1 -0
  10. package/esm/compiler/link_target.js +66 -0
  11. package/esm/compiler/mod.d.ts +43 -0
  12. package/esm/compiler/mod.d.ts.map +1 -0
  13. package/esm/compiler/mod.js +163 -0
  14. package/esm/compiler/schema.d.ts +38 -0
  15. package/esm/compiler/schema.d.ts.map +1 -0
  16. package/esm/compiler/schema.js +44 -0
  17. package/esm/config/markspec.d.ts +51 -0
  18. package/esm/config/markspec.d.ts.map +1 -0
  19. package/esm/config/markspec.js +216 -0
  20. package/esm/config/mod.d.ts +48 -0
  21. package/esm/config/mod.d.ts.map +1 -0
  22. package/esm/config/mod.js +243 -0
  23. package/esm/formatter/mod.d.ts +65 -0
  24. package/esm/formatter/mod.d.ts.map +1 -0
  25. package/esm/formatter/mod.js +306 -0
  26. package/esm/mod.d.ts +19 -0
  27. package/esm/mod.d.ts.map +1 -1
  28. package/esm/mod.js +18 -0
  29. package/esm/model/attributes.d.ts +47 -0
  30. package/esm/model/attributes.d.ts.map +1 -0
  31. package/esm/model/attributes.js +97 -0
  32. package/esm/model/mod.d.ts +381 -0
  33. package/esm/model/mod.d.ts.map +1 -0
  34. package/esm/model/mod.js +73 -0
  35. package/esm/model/profile.d.ts +189 -0
  36. package/esm/model/profile.d.ts.map +1 -0
  37. package/esm/model/profile.js +31 -0
  38. package/esm/parser/attributes.d.ts +48 -0
  39. package/esm/parser/attributes.d.ts.map +1 -0
  40. package/esm/parser/attributes.js +131 -0
  41. package/esm/parser/captions.d.ts +25 -0
  42. package/esm/parser/captions.d.ts.map +1 -0
  43. package/esm/parser/captions.js +148 -0
  44. package/esm/parser/directives.d.ts +29 -0
  45. package/esm/parser/directives.d.ts.map +1 -0
  46. package/esm/parser/directives.js +88 -0
  47. package/esm/parser/frontmatter.d.ts +53 -0
  48. package/esm/parser/frontmatter.d.ts.map +1 -0
  49. package/esm/parser/frontmatter.js +185 -0
  50. package/esm/parser/grammars.d.ts +22 -0
  51. package/esm/parser/grammars.d.ts.map +1 -0
  52. package/esm/parser/grammars.js +70 -0
  53. package/esm/parser/markdown.d.ts +32 -0
  54. package/esm/parser/markdown.d.ts.map +1 -0
  55. package/esm/parser/markdown.js +225 -0
  56. package/esm/parser/mod.d.ts +73 -0
  57. package/esm/parser/mod.d.ts.map +1 -0
  58. package/esm/parser/mod.js +89 -0
  59. package/esm/parser/references.d.ts +24 -0
  60. package/esm/parser/references.d.ts.map +1 -0
  61. package/esm/parser/references.js +71 -0
  62. package/esm/parser/remark.d.ts +9 -0
  63. package/esm/parser/remark.d.ts.map +1 -0
  64. package/esm/parser/remark.js +11 -0
  65. package/esm/parser/source.d.ts +33 -0
  66. package/esm/parser/source.d.ts.map +1 -0
  67. package/esm/parser/source.js +192 -0
  68. package/esm/profile/cache.d.ts +29 -0
  69. package/esm/profile/cache.d.ts.map +1 -0
  70. package/esm/profile/cache.js +45 -0
  71. package/esm/profile/chain.d.ts +42 -0
  72. package/esm/profile/chain.d.ts.map +1 -0
  73. package/esm/profile/chain.js +193 -0
  74. package/esm/profile/git-cache.d.ts +73 -0
  75. package/esm/profile/git-cache.d.ts.map +1 -0
  76. package/esm/profile/git-cache.js +96 -0
  77. package/esm/profile/load.d.ts +28 -0
  78. package/esm/profile/load.d.ts.map +1 -0
  79. package/esm/profile/load.js +50 -0
  80. package/esm/profile/manifest.d.ts +21 -0
  81. package/esm/profile/manifest.d.ts.map +1 -0
  82. package/esm/profile/manifest.js +830 -0
  83. package/esm/profile/merge.d.ts +26 -0
  84. package/esm/profile/merge.d.ts.map +1 -0
  85. package/esm/profile/merge.js +518 -0
  86. package/esm/profile/mod.d.ts +23 -0
  87. package/esm/profile/mod.d.ts.map +1 -0
  88. package/esm/profile/mod.js +14 -0
  89. package/esm/profile/npm.d.ts +51 -0
  90. package/esm/profile/npm.d.ts.map +1 -0
  91. package/esm/profile/npm.js +186 -0
  92. package/esm/profile/resolver.d.ts +60 -0
  93. package/esm/profile/resolver.d.ts.map +1 -0
  94. package/esm/profile/resolver.js +132 -0
  95. package/esm/reporter/mod.d.ts +31 -0
  96. package/esm/reporter/mod.d.ts.map +1 -0
  97. package/esm/reporter/mod.js +173 -0
  98. package/esm/validator/attributes.d.ts +43 -0
  99. package/esm/validator/attributes.d.ts.map +1 -0
  100. package/esm/validator/attributes.js +169 -0
  101. package/esm/validator/mod.d.ts +44 -0
  102. package/esm/validator/mod.d.ts.map +1 -0
  103. package/esm/validator/mod.js +214 -0
  104. package/esm/validator/normalize.d.ts +21 -0
  105. package/esm/validator/normalize.d.ts.map +1 -0
  106. package/esm/validator/normalize.js +58 -0
  107. package/esm/validator/pattern.d.ts +23 -0
  108. package/esm/validator/pattern.d.ts.map +1 -0
  109. package/esm/validator/pattern.js +44 -0
  110. package/esm/validator/pipeline.d.ts +30 -0
  111. package/esm/validator/pipeline.d.ts.map +1 -0
  112. package/esm/validator/pipeline.js +115 -0
  113. package/esm/validator/traceability.d.ts +48 -0
  114. package/esm/validator/traceability.d.ts.map +1 -0
  115. package/esm/validator/traceability.js +141 -0
  116. package/esm/validator/types.d.ts +48 -0
  117. package/esm/validator/types.d.ts.map +1 -0
  118. package/esm/validator/types.js +140 -0
  119. package/esm/validator/value_types.d.ts +20 -0
  120. package/esm/validator/value_types.d.ts.map +1 -0
  121. package/esm/validator/value_types.js +126 -0
  122. package/package.json +8 -1
  123. package/script/_dnt.polyfills.d.ts +101 -0
  124. package/script/_dnt.polyfills.d.ts.map +1 -0
  125. package/script/_dnt.polyfills.js +130 -0
  126. package/script/compiler/inverses.d.ts +22 -0
  127. package/script/compiler/inverses.d.ts.map +1 -0
  128. package/script/compiler/inverses.js +150 -0
  129. package/script/compiler/link_target.d.ts +19 -0
  130. package/script/compiler/link_target.d.ts.map +1 -0
  131. package/script/compiler/link_target.js +69 -0
  132. package/script/compiler/mod.d.ts +43 -0
  133. package/script/compiler/mod.d.ts.map +1 -0
  134. package/script/compiler/mod.js +169 -0
  135. package/script/compiler/schema.d.ts +38 -0
  136. package/script/compiler/schema.d.ts.map +1 -0
  137. package/script/compiler/schema.js +47 -0
  138. package/script/config/markspec.d.ts +51 -0
  139. package/script/config/markspec.d.ts.map +1 -0
  140. package/script/config/markspec.js +222 -0
  141. package/script/config/mod.d.ts +48 -0
  142. package/script/config/mod.d.ts.map +1 -0
  143. package/script/config/mod.js +250 -0
  144. package/script/formatter/mod.d.ts +65 -0
  145. package/script/formatter/mod.d.ts.map +1 -0
  146. package/script/formatter/mod.js +313 -0
  147. package/script/mod.d.ts +19 -0
  148. package/script/mod.d.ts.map +1 -1
  149. package/script/mod.js +65 -2
  150. package/script/model/attributes.d.ts +47 -0
  151. package/script/model/attributes.d.ts.map +1 -0
  152. package/script/model/attributes.js +101 -0
  153. package/script/model/mod.d.ts +381 -0
  154. package/script/model/mod.d.ts.map +1 -0
  155. package/script/model/mod.js +83 -0
  156. package/script/model/profile.d.ts +189 -0
  157. package/script/model/profile.d.ts.map +1 -0
  158. package/script/model/profile.js +34 -0
  159. package/script/parser/attributes.d.ts +48 -0
  160. package/script/parser/attributes.d.ts.map +1 -0
  161. package/script/parser/attributes.js +137 -0
  162. package/script/parser/captions.d.ts +25 -0
  163. package/script/parser/captions.d.ts.map +1 -0
  164. package/script/parser/captions.js +151 -0
  165. package/script/parser/directives.d.ts +29 -0
  166. package/script/parser/directives.d.ts.map +1 -0
  167. package/script/parser/directives.js +91 -0
  168. package/script/parser/frontmatter.d.ts +53 -0
  169. package/script/parser/frontmatter.d.ts.map +1 -0
  170. package/script/parser/frontmatter.js +188 -0
  171. package/script/parser/grammars.d.ts +22 -0
  172. package/script/parser/grammars.d.ts.map +1 -0
  173. package/script/parser/grammars.js +77 -0
  174. package/script/parser/markdown.d.ts +32 -0
  175. package/script/parser/markdown.d.ts.map +1 -0
  176. package/script/parser/markdown.js +228 -0
  177. package/script/parser/mod.d.ts +73 -0
  178. package/script/parser/mod.d.ts.map +1 -0
  179. package/script/parser/mod.js +101 -0
  180. package/script/parser/references.d.ts +24 -0
  181. package/script/parser/references.d.ts.map +1 -0
  182. package/script/parser/references.js +74 -0
  183. package/script/parser/remark.d.ts +9 -0
  184. package/script/parser/remark.d.ts.map +1 -0
  185. package/script/parser/remark.js +17 -0
  186. package/script/parser/source.d.ts +33 -0
  187. package/script/parser/source.d.ts.map +1 -0
  188. package/script/parser/source.js +198 -0
  189. package/script/profile/cache.d.ts +29 -0
  190. package/script/profile/cache.d.ts.map +1 -0
  191. package/script/profile/cache.js +48 -0
  192. package/script/profile/chain.d.ts +42 -0
  193. package/script/profile/chain.d.ts.map +1 -0
  194. package/script/profile/chain.js +229 -0
  195. package/script/profile/git-cache.d.ts +73 -0
  196. package/script/profile/git-cache.d.ts.map +1 -0
  197. package/script/profile/git-cache.js +104 -0
  198. package/script/profile/load.d.ts +28 -0
  199. package/script/profile/load.d.ts.map +1 -0
  200. package/script/profile/load.js +53 -0
  201. package/script/profile/manifest.d.ts +21 -0
  202. package/script/profile/manifest.d.ts.map +1 -0
  203. package/script/profile/manifest.js +833 -0
  204. package/script/profile/merge.d.ts +26 -0
  205. package/script/profile/merge.d.ts.map +1 -0
  206. package/script/profile/merge.js +521 -0
  207. package/script/profile/mod.d.ts +23 -0
  208. package/script/profile/mod.d.ts.map +1 -0
  209. package/script/profile/mod.js +31 -0
  210. package/script/profile/npm.d.ts +51 -0
  211. package/script/profile/npm.d.ts.map +1 -0
  212. package/script/profile/npm.js +191 -0
  213. package/script/profile/resolver.d.ts +60 -0
  214. package/script/profile/resolver.d.ts.map +1 -0
  215. package/script/profile/resolver.js +136 -0
  216. package/script/reporter/mod.d.ts +31 -0
  217. package/script/reporter/mod.d.ts.map +1 -0
  218. package/script/reporter/mod.js +176 -0
  219. package/script/validator/attributes.d.ts +43 -0
  220. package/script/validator/attributes.d.ts.map +1 -0
  221. package/script/validator/attributes.js +173 -0
  222. package/script/validator/mod.d.ts +44 -0
  223. package/script/validator/mod.d.ts.map +1 -0
  224. package/script/validator/mod.js +231 -0
  225. package/script/validator/normalize.d.ts +21 -0
  226. package/script/validator/normalize.d.ts.map +1 -0
  227. package/script/validator/normalize.js +61 -0
  228. package/script/validator/pattern.d.ts +23 -0
  229. package/script/validator/pattern.d.ts.map +1 -0
  230. package/script/validator/pattern.js +47 -0
  231. package/script/validator/pipeline.d.ts +30 -0
  232. package/script/validator/pipeline.d.ts.map +1 -0
  233. package/script/validator/pipeline.js +118 -0
  234. package/script/validator/traceability.d.ts +48 -0
  235. package/script/validator/traceability.d.ts.map +1 -0
  236. package/script/validator/traceability.js +146 -0
  237. package/script/validator/types.d.ts +48 -0
  238. package/script/validator/types.d.ts.map +1 -0
  239. package/script/validator/types.js +144 -0
  240. package/script/validator/value_types.d.ts +20 -0
  241. package/script/validator/value_types.d.ts.map +1 -0
  242. package/script/validator/value_types.js +129 -0
@@ -0,0 +1,151 @@
1
+ "use strict";
2
+ /**
3
+ * @module parser/captions
4
+ *
5
+ * Detects table and figure captions in Markdown documents.
6
+ *
7
+ * Caption patterns:
8
+ * - **Table**: `_Table: text_` paragraph immediately followed by a `table` sibling.
9
+ * - **Figure**: `image` node followed by `_Figure: text_` paragraph.
10
+ * - **Figure fallback**: `image` with non-empty alt text and no explicit caption.
11
+ */
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.detectCaptions = detectCaptions;
14
+ const remark_js_1 = require("./remark.js");
15
+ /**
16
+ * Convert caption text to a GFM-style anchor slug.
17
+ *
18
+ * Lowercase, spaces to hyphens, strip non-alphanumeric except hyphens.
19
+ */
20
+ function toSlug(prefix, text) {
21
+ const anchor = text
22
+ .toLowerCase()
23
+ .replace(/\s+/g, "-")
24
+ .replace(/[^a-z0-9-]/g, "");
25
+ return `${prefix}.${anchor}`;
26
+ }
27
+ /**
28
+ * Extract plain text from an emphasis node (single emphasis child
29
+ * containing text children).
30
+ */
31
+ function getEmphasisText(node) {
32
+ if (node.children.length === 0)
33
+ return undefined;
34
+ return node.children
35
+ .filter((c) => c.type === "text")
36
+ .map((c) => c.value)
37
+ .join("");
38
+ }
39
+ /**
40
+ * Check if a paragraph contains a single emphasis child whose text
41
+ * starts with the given prefix (e.g., "Table:" or "Figure:").
42
+ * Returns the text after the prefix, or undefined.
43
+ */
44
+ function getCaptionText(paragraph, prefix) {
45
+ // A caption paragraph has exactly one child: an emphasis node.
46
+ if (paragraph.children.length !== 1)
47
+ return undefined;
48
+ const child = paragraph.children[0];
49
+ if (child.type !== "emphasis")
50
+ return undefined;
51
+ const text = getEmphasisText(child);
52
+ if (!text)
53
+ return undefined;
54
+ if (!text.startsWith(prefix))
55
+ return undefined;
56
+ return text.slice(prefix.length).trim();
57
+ }
58
+ /**
59
+ * Check if a node is an image or a paragraph containing only an image.
60
+ * Returns the image node if found.
61
+ */
62
+ function getImage(node) {
63
+ if (node.type === "image")
64
+ return node;
65
+ if (node.type === "paragraph") {
66
+ const para = node;
67
+ if (para.children.length === 1 && para.children[0].type === "image") {
68
+ return para.children[0];
69
+ }
70
+ }
71
+ return undefined;
72
+ }
73
+ /**
74
+ * Detect table and figure captions in a Markdown string.
75
+ *
76
+ * @param markdown - Markdown source text
77
+ * @param options - Detection options (file path for source locations)
78
+ * @returns Array of detected captions
79
+ */
80
+ function detectCaptions(markdown, options) {
81
+ const file = options?.file ?? "<unknown>";
82
+ const tree = remark_js_1.processor.parse(markdown);
83
+ const captions = [];
84
+ const children = tree.children;
85
+ // Track which image nodes have explicit figure captions so we can
86
+ // apply the alt-text fallback only to uncaptioned images.
87
+ const captionedImageIndices = new Set();
88
+ // First pass: detect explicit captions.
89
+ for (let i = 0; i < children.length; i++) {
90
+ const node = children[i];
91
+ // Table caption: paragraph with _Table: text_ followed by a table.
92
+ if (node.type === "paragraph") {
93
+ const text = getCaptionText(node, "Table:");
94
+ if (text && i + 1 < children.length && children[i + 1].type === "table") {
95
+ captions.push({
96
+ kind: "table",
97
+ slug: toSlug("tbl", text),
98
+ text,
99
+ location: {
100
+ file,
101
+ line: node.position?.start.line ?? 1,
102
+ column: node.position?.start.column ?? 1,
103
+ },
104
+ });
105
+ continue;
106
+ }
107
+ }
108
+ // Figure caption: image (or paragraph containing image) followed by
109
+ // paragraph with _Figure: text_.
110
+ const image = getImage(node);
111
+ if (image && i + 1 < children.length) {
112
+ const next = children[i + 1];
113
+ if (next.type === "paragraph") {
114
+ const text = getCaptionText(next, "Figure:");
115
+ if (text) {
116
+ captionedImageIndices.add(i);
117
+ captions.push({
118
+ kind: "figure",
119
+ slug: toSlug("fig", text),
120
+ text,
121
+ location: {
122
+ file,
123
+ line: image.position?.start.line ?? 1,
124
+ column: image.position?.start.column ?? 1,
125
+ },
126
+ });
127
+ continue;
128
+ }
129
+ }
130
+ }
131
+ }
132
+ // Second pass: alt-text fallback for uncaptioned images.
133
+ for (let i = 0; i < children.length; i++) {
134
+ if (captionedImageIndices.has(i))
135
+ continue;
136
+ const image = getImage(children[i]);
137
+ if (image && image.alt && image.alt.trim() !== "") {
138
+ captions.push({
139
+ kind: "figure",
140
+ slug: toSlug("fig", image.alt),
141
+ text: image.alt,
142
+ location: {
143
+ file,
144
+ line: image.position?.start.line ?? 1,
145
+ column: image.position?.start.column ?? 1,
146
+ },
147
+ });
148
+ }
149
+ }
150
+ return captions;
151
+ }
@@ -0,0 +1,29 @@
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 type { Directive } from "../model/mod.js";
11
+ /** Options for {@linkcode detectDirectives}. */
12
+ export interface DetectDirectivesOptions {
13
+ /** File path used in source locations. */
14
+ readonly file?: string;
15
+ }
16
+ /**
17
+ * Detect MarkSpec directives in a Markdown string.
18
+ *
19
+ * Walks the mdast AST looking for `html` nodes that are HTML comments.
20
+ * Inside each comment, lines starting with `markspec:` are parsed as
21
+ * directives. Continuation lines (not starting with `markspec:`) are
22
+ * appended to the preceding directive's payload.
23
+ *
24
+ * @param markdown - Markdown source text
25
+ * @param options - Detection options (file path for source locations)
26
+ * @returns Array of detected directives
27
+ */
28
+ export declare function detectDirectives(markdown: string, options?: DetectDirectivesOptions): Directive[];
29
+ //# sourceMappingURL=directives.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"directives.d.ts","sourceRoot":"","sources":["../../src/parser/directives.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAkB,MAAM,iBAAiB,CAAC;AAGjE,gDAAgD;AAChD,MAAM,WAAW,uBAAuB;IACtC,0CAA0C;IAC1C,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;CACxB;AAQD;;;;;;;;;;;GAWG;AACH,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE,uBAAuB,GAChC,SAAS,EAAE,CAqEb"}
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ /**
3
+ * @module parser/directives
4
+ *
5
+ * Extracts MarkSpec directives from HTML comments in Markdown files.
6
+ *
7
+ * Directives use the form `<!-- markspec:<name> <payload> -->` and allow
8
+ * documents to carry processing hints such as output mode, deprecation
9
+ * notices, or slide layout declarations.
10
+ */
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.detectDirectives = detectDirectives;
13
+ const remark_js_1 = require("./remark.js");
14
+ /** Matches the opening and closing of an HTML comment. */
15
+ const COMMENT_RE = /^<!--([\s\S]*?)-->$/;
16
+ /** Matches a `markspec:` directive line within a comment body. */
17
+ const DIRECTIVE_RE = /^markspec:(\S+)\s?(.*)?$/;
18
+ /**
19
+ * Detect MarkSpec directives in a Markdown string.
20
+ *
21
+ * Walks the mdast AST looking for `html` nodes that are HTML comments.
22
+ * Inside each comment, lines starting with `markspec:` are parsed as
23
+ * directives. Continuation lines (not starting with `markspec:`) are
24
+ * appended to the preceding directive's payload.
25
+ *
26
+ * @param markdown - Markdown source text
27
+ * @param options - Detection options (file path for source locations)
28
+ * @returns Array of detected directives
29
+ */
30
+ function detectDirectives(markdown, options) {
31
+ const file = options?.file ?? "<unknown>";
32
+ const tree = remark_js_1.processor.parse(markdown);
33
+ const directives = [];
34
+ for (const node of tree.children) {
35
+ if (node.type !== "html")
36
+ continue;
37
+ const html = node;
38
+ const commentMatch = COMMENT_RE.exec(html.value.trim());
39
+ if (!commentMatch)
40
+ continue;
41
+ const commentBody = commentMatch[1];
42
+ const commentLines = commentBody.split("\n");
43
+ // The node's starting line in the source file.
44
+ const nodeStartLine = html.position?.start.line ?? 1;
45
+ const nodeStartColumn = html.position?.start.column ?? 1;
46
+ // Track the line offset within the comment for each content line.
47
+ // The first line of the comment body is on the same line as `<!--`.
48
+ let currentDirective;
49
+ for (let i = 0; i < commentLines.length; i++) {
50
+ const line = commentLines[i].trim();
51
+ if (line === "") {
52
+ // Empty lines are ignored; they do NOT continue a payload.
53
+ continue;
54
+ }
55
+ const directiveMatch = DIRECTIVE_RE.exec(line);
56
+ if (directiveMatch) {
57
+ // Flush any in-progress directive.
58
+ if (currentDirective) {
59
+ directives.push(finalizeDirective(currentDirective));
60
+ }
61
+ const location = {
62
+ file,
63
+ line: nodeStartLine + i,
64
+ column: nodeStartColumn,
65
+ };
66
+ currentDirective = {
67
+ name: directiveMatch[1],
68
+ payloadParts: directiveMatch[2] ? [directiveMatch[2]] : [],
69
+ location,
70
+ };
71
+ }
72
+ else if (currentDirective) {
73
+ // Continuation line for the current directive's payload.
74
+ currentDirective.payloadParts.push(line);
75
+ }
76
+ }
77
+ // Flush final directive from this comment.
78
+ if (currentDirective) {
79
+ directives.push(finalizeDirective(currentDirective));
80
+ }
81
+ }
82
+ return directives;
83
+ }
84
+ /** Assemble a Directive from accumulated parts. */
85
+ function finalizeDirective(pending) {
86
+ return {
87
+ name: pending.name,
88
+ payload: pending.payloadParts.join("\n").trim(),
89
+ location: pending.location,
90
+ };
91
+ }
@@ -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,188 @@
1
+ "use strict";
2
+ /**
3
+ * @module parser/frontmatter
4
+ *
5
+ * YAML front-matter parser per ADR-007.
6
+ *
7
+ * Extracts a `---`-delimited YAML block at the top of a Markdown file,
8
+ * classifies keys into core / `metadata` / allowlisted ecosystem, and rejects
9
+ * forbidden keys (MSL-D001). Returns a {@linkcode DocumentAttributes} record
10
+ * and the markdown content with the front matter stripped.
11
+ *
12
+ * Full validation of core-key value types is deferred to the validator
13
+ * (Phase 3); this module handles extraction and key-category classification.
14
+ */
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.extractFrontMatter = extractFrontMatter;
17
+ const yaml_1 = require("@std/yaml");
18
+ /**
19
+ * YAML front-matter delimiter — `---` at start of file, closing `---`, then
20
+ * any number of trailing newlines so the stripped markdown begins cleanly.
21
+ * Empty YAML body (`---\n---\n`) is accepted.
22
+ */
23
+ const FRONT_MATTER_RE = /^---\r?\n([\s\S]*?)(?:\r?\n)?---\r?\n?(?:\r?\n)*/;
24
+ /**
25
+ * Core document-level attribute keys per ADR-007. Kebab-case by convention.
26
+ */
27
+ const CORE_KEYS = new Set([
28
+ "document-id",
29
+ "document-type",
30
+ "labels",
31
+ "deprecated",
32
+ "external-id",
33
+ "supersedes",
34
+ "references",
35
+ "metadata",
36
+ ]);
37
+ /**
38
+ * Keys forbidden in front matter per ADR-007 — already expressed natively in
39
+ * Markdown (H1, body, images) or in git history (authors, dates).
40
+ */
41
+ const FORBIDDEN_KEYS = new Set([
42
+ "title",
43
+ "description",
44
+ "toc",
45
+ "authors",
46
+ "author",
47
+ "date",
48
+ "created",
49
+ "modified",
50
+ "cover",
51
+ "images",
52
+ "sections",
53
+ ]);
54
+ /**
55
+ * Extract YAML front matter from a Markdown source, validate its keys, and
56
+ * return structured attributes plus the stripped markdown.
57
+ *
58
+ * No front matter → {@linkcode FrontMatterResult.hadFrontMatter} is `false`
59
+ * and attributes are empty.
60
+ *
61
+ * @param markdown - Full source text, including any leading front matter.
62
+ * @param options - Key allowlists and file path for diagnostics.
63
+ */
64
+ function extractFrontMatter(markdown, options) {
65
+ const file = options?.file ?? "<unknown>";
66
+ const match = FRONT_MATTER_RE.exec(markdown);
67
+ if (!match) {
68
+ return {
69
+ attributes: {},
70
+ diagnostics: [],
71
+ markdown,
72
+ hadFrontMatter: false,
73
+ };
74
+ }
75
+ const yamlBody = match[1];
76
+ const remaining = markdown.slice(match[0].length);
77
+ const diagnostics = [];
78
+ let raw;
79
+ try {
80
+ raw = (0, yaml_1.parse)(yamlBody);
81
+ }
82
+ catch (err) {
83
+ diagnostics.push({
84
+ code: "MSL-D001",
85
+ severity: "error",
86
+ message: `front matter: invalid YAML — ${err instanceof Error ? err.message : String(err)}`,
87
+ location: { file, line: 1, column: 1 },
88
+ });
89
+ return {
90
+ attributes: {},
91
+ diagnostics,
92
+ markdown: remaining,
93
+ hadFrontMatter: true,
94
+ };
95
+ }
96
+ if (raw == null) {
97
+ return {
98
+ attributes: {},
99
+ diagnostics,
100
+ markdown: remaining,
101
+ hadFrontMatter: true,
102
+ };
103
+ }
104
+ if (typeof raw !== "object" || Array.isArray(raw)) {
105
+ diagnostics.push({
106
+ code: "MSL-D001",
107
+ severity: "error",
108
+ message: "front matter must be a YAML mapping",
109
+ location: { file, line: 1, column: 1 },
110
+ });
111
+ return {
112
+ attributes: {},
113
+ diagnostics,
114
+ markdown: remaining,
115
+ hadFrontMatter: true,
116
+ };
117
+ }
118
+ const entries = raw;
119
+ const profileKeys = new Set(options?.profileKeys ?? []);
120
+ const allowedKeys = new Set(options?.allowedKeys ?? []);
121
+ const core = {};
122
+ const extra = {};
123
+ let metadata;
124
+ for (const [key, value] of Object.entries(entries)) {
125
+ if (FORBIDDEN_KEYS.has(key)) {
126
+ diagnostics.push({
127
+ code: "MSL-D001",
128
+ severity: "error",
129
+ message: `front matter key '${key}' is forbidden — express it in Markdown or git history instead`,
130
+ location: { file, line: 1, column: 1 },
131
+ });
132
+ continue;
133
+ }
134
+ if (key === "metadata") {
135
+ if (value == null || typeof value !== "object" || Array.isArray(value)) {
136
+ diagnostics.push({
137
+ code: "MSL-D001",
138
+ severity: "error",
139
+ message: "front matter 'metadata' must be a mapping",
140
+ location: { file, line: 1, column: 1 },
141
+ });
142
+ continue;
143
+ }
144
+ metadata = value;
145
+ continue;
146
+ }
147
+ // Transitional: status: was removed in favor of Labels: DRAFT / deprecated:
148
+ if (key === "status") {
149
+ diagnostics.push({
150
+ code: "MSL-D002",
151
+ severity: "warning",
152
+ message: `front-matter key 'status' is no longer recognized; ` +
153
+ `use 'Labels: DRAFT' for draft state or 'deprecated: "<reason>"' for retirement`,
154
+ location: { file, line: 1, column: 1 },
155
+ });
156
+ continue;
157
+ }
158
+ if (CORE_KEYS.has(key)) {
159
+ core[key] = value;
160
+ continue;
161
+ }
162
+ if (profileKeys.has(key) || allowedKeys.has(key)) {
163
+ extra[key] = value;
164
+ continue;
165
+ }
166
+ diagnostics.push({
167
+ code: "MSL-D001",
168
+ severity: "error",
169
+ message: `unknown front matter key '${key}' — must be core, profile-declared, 'metadata', or allowlisted in .markspec.yaml`,
170
+ location: { file, line: 1, column: 1 },
171
+ });
172
+ }
173
+ const attributes = {};
174
+ const mutable = attributes;
175
+ for (const [key, value] of Object.entries(core)) {
176
+ mutable[key] = value;
177
+ }
178
+ if (metadata !== undefined)
179
+ mutable.metadata = metadata;
180
+ if (Object.keys(extra).length > 0)
181
+ mutable.extra = extra;
182
+ return {
183
+ attributes,
184
+ diagnostics,
185
+ markdown: remaining,
186
+ hadFrontMatter: true,
187
+ };
188
+ }
@@ -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,77 @@
1
+ "use strict";
2
+ /**
3
+ * @module parser/grammars
4
+ *
5
+ * Lazy-loading tree-sitter grammar resolver. Maps file extensions to
6
+ * grammar WASM files and caches loaded languages for reuse.
7
+ */
8
+ var __importDefault = (this && this.__importDefault) || function (mod) {
9
+ return (mod && mod.__esModule) ? mod : { "default": mod };
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.isSupportedExtension = isSupportedExtension;
13
+ exports.loadGrammar = loadGrammar;
14
+ const web_tree_sitter_1 = __importDefault(require("web-tree-sitter"));
15
+ const path_1 = require("@std/path");
16
+ /** Map file extensions to grammar WASM file names. */
17
+ const EXT_TO_GRAMMAR = {
18
+ ".rs": "tree-sitter-rust",
19
+ ".kt": "tree-sitter-kotlin",
20
+ ".kts": "tree-sitter-kotlin",
21
+ ".java": "tree-sitter-java",
22
+ ".c": "tree-sitter-c",
23
+ ".h": "tree-sitter-c",
24
+ ".cpp": "tree-sitter-cpp",
25
+ ".cc": "tree-sitter-cpp",
26
+ ".cxx": "tree-sitter-cpp",
27
+ ".hpp": "tree-sitter-cpp",
28
+ ".hxx": "tree-sitter-cpp",
29
+ };
30
+ /** Directory containing grammar WASM files. */
31
+ const GRAMMARS_DIR = (0, path_1.join)(globalThis[Symbol.for("import-meta-ponyfill-commonjs")](require, module).dirname, "..", "..", "..", "..", "grammars");
32
+ let initialized = false;
33
+ const cache = new Map();
34
+ /** Check whether a file extension has a supported grammar. */
35
+ function isSupportedExtension(ext) {
36
+ return ext in EXT_TO_GRAMMAR;
37
+ }
38
+ /**
39
+ * Load a tree-sitter language grammar for the given file extension.
40
+ *
41
+ * Initializes the Parser WASM runtime on first call. Grammar languages
42
+ * are cached — repeated calls with the same extension return the
43
+ * same `Parser.Language` instance. Concurrent-safe: stores the loading
44
+ * promise so parallel calls await the same load.
45
+ *
46
+ * @param ext - File extension including the dot (e.g., `.rs`, `.java`)
47
+ * @throws If the extension is unsupported or the WASM file cannot be loaded
48
+ */
49
+ function loadGrammar(ext) {
50
+ const grammarName = EXT_TO_GRAMMAR[ext];
51
+ if (!grammarName) {
52
+ throw new Error(`unsupported file extension: ${ext}`);
53
+ }
54
+ let pending = cache.get(grammarName);
55
+ if (!pending) {
56
+ pending = doLoad(grammarName);
57
+ cache.set(grammarName, pending);
58
+ }
59
+ return pending;
60
+ }
61
+ async function doLoad(grammarName) {
62
+ if (!initialized) {
63
+ await web_tree_sitter_1.default.init();
64
+ initialized = true;
65
+ }
66
+ // The compiled binary embeds grammars as .wasm.bin to bypass Deno's
67
+ // WASM-import resolver (see scripts/compile_binary.ts). Prefer the
68
+ // mirror when present; fall back to .wasm in dev mode.
69
+ const binPath = (0, path_1.join)(GRAMMARS_DIR, `${grammarName}.wasm.bin`);
70
+ try {
71
+ await Deno.stat(binPath);
72
+ return web_tree_sitter_1.default.Language.load(binPath);
73
+ }
74
+ catch {
75
+ return web_tree_sitter_1.default.Language.load((0, path_1.join)(GRAMMARS_DIR, `${grammarName}.wasm`));
76
+ }
77
+ }
@@ -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"}