@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,225 @@
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 { IDENTITY_KEY, shapeFromIdValue } from "../model/mod.js";
8
+ import { collateAttributes, parseAttributes, splitBodyAndAttributes, } from "./attributes.js";
9
+ import { processor } from "./remark.js";
10
+ /**
11
+ * Slug pattern for referenced-entry display IDs.
12
+ *
13
+ * Pandoc/BibTeX cite-key convention, restricted to a portable character set
14
+ * (`.`, `/`, `_`, `-` accepted inside; must start with a letter and end
15
+ * with an alphanumeric).
16
+ */
17
+ const SLUG_RE = /^[A-Za-z]([A-Za-z0-9._/-]*[A-Za-z0-9])?$/;
18
+ /** Match `[...]` at the start of a list item paragraph. Captures: [1] = display ID, [2] = title. */
19
+ const ENTRY_START_RE = /^\[([^\]]+)\]\s*(.*)$/;
20
+ /**
21
+ * Find the `Id:` attribute on an entry. Returns undefined when absent. If
22
+ * more than one `Id:` is present, returns the first — the validator flags
23
+ * this as MSL-R003.
24
+ */
25
+ function findIdentityAttribute(attributes) {
26
+ for (const attr of attributes) {
27
+ if (attr.key === IDENTITY_KEY)
28
+ return attr;
29
+ }
30
+ return undefined;
31
+ }
32
+ /**
33
+ * Parse a Markdown string and return all MarkSpec entries found.
34
+ *
35
+ * Walks the mdast AST to detect `- [DISPLAY_ID] Title` list items with
36
+ * indented body content. Extracts display ID, title, body, and trailing
37
+ * attribute blocks. Entry shape (identified or referenced) is decided by
38
+ * the `Id:` attribute's value format.
39
+ *
40
+ * @param markdown - Markdown source text
41
+ * @param options - Parse options (file path for source locations)
42
+ * @returns Array of parsed entries
43
+ */
44
+ export function parseMarkdown(markdown, options) {
45
+ const file = options?.file ?? "<unknown>";
46
+ const isReferencesDoc = detectReferencesDocument(file, options?.isReferencesDoc);
47
+ const tree = processor.parse(markdown);
48
+ const entries = [];
49
+ // Collect link definition identifiers for shortcut reference exclusion.
50
+ const definitions = new Set(tree.children
51
+ .filter((n) => n.type === "definition")
52
+ .map((n) => n.identifier));
53
+ for (const node of tree.children) {
54
+ if (node.type !== "list")
55
+ continue;
56
+ const list = node;
57
+ // Ordered lists never contain entry blocks.
58
+ if (list.ordered)
59
+ continue;
60
+ for (const item of list.children) {
61
+ const entry = extractEntry(item, markdown, file, definitions, isReferencesDoc);
62
+ if (entry)
63
+ entries.push(entry);
64
+ }
65
+ }
66
+ return entries;
67
+ }
68
+ /**
69
+ * Detect if a file is a references document.
70
+ * References context enables recognition of reference entries (slugs).
71
+ * @param file - File path
72
+ * @param explicit - Explicit override (undefined = auto-detect)
73
+ */
74
+ function detectReferencesDocument(file, explicit) {
75
+ if (explicit !== undefined)
76
+ return explicit;
77
+ const basename = file.split("/").pop() ?? "";
78
+ return basename === "references.md" || file.includes("/references/");
79
+ }
80
+ /**
81
+ * Attempt to extract a MarkSpec entry from a list item.
82
+ * Returns undefined if the list item is not an entry block.
83
+ */
84
+ function extractEntry(item, markdown, file, definitions, isReferencesDoc) {
85
+ // Task list items (remark-gfm sets checked to true/false) are not entries.
86
+ if (item.checked != null)
87
+ return undefined;
88
+ // An entry block must have children (body content).
89
+ // The first child must be a paragraph starting with `[DISPLAY_ID]`.
90
+ if (!item.children.length)
91
+ return undefined;
92
+ const firstChild = item.children[0];
93
+ if (firstChild.type !== "paragraph")
94
+ return undefined;
95
+ const paragraph = firstChild;
96
+ if (!paragraph.children.length)
97
+ return undefined;
98
+ const firstInline = paragraph.children[0];
99
+ // Inline link: [text](url) — not an entry.
100
+ if (firstInline.type === "link")
101
+ return undefined;
102
+ let displayId;
103
+ let title;
104
+ if (firstInline.type === "text") {
105
+ // remark may parse `[ID] Title` as a single text node
106
+ const match = ENTRY_START_RE.exec(firstInline.value);
107
+ if (match) {
108
+ displayId = match[1];
109
+ title = match[2].trim();
110
+ }
111
+ }
112
+ // Try linkReference pattern: remark sometimes parses [ID] as a linkReference
113
+ if (!displayId && firstInline.type === "linkReference") {
114
+ const ref = firstInline;
115
+ // Full [text][ref] and collapsed [text][] references are links, not entries.
116
+ if (ref.referenceType === "full" || ref.referenceType === "collapsed") {
117
+ return undefined;
118
+ }
119
+ // Shortcut [text] with a matching definition is a resolved link, not an entry.
120
+ if (ref.referenceType === "shortcut" && ref.identifier != null) {
121
+ if (definitions.has(ref.identifier))
122
+ return undefined;
123
+ }
124
+ displayId = ref.label ?? ref.children?.[0]?.value;
125
+ // Title comes from subsequent text nodes in the paragraph
126
+ if (displayId && paragraph.children.length > 1) {
127
+ const rest = paragraph.children.slice(1);
128
+ title = rest
129
+ .filter((n) => n.type === "text")
130
+ .map((n) => n.value)
131
+ .join("")
132
+ .trim();
133
+ }
134
+ }
135
+ if (!displayId)
136
+ return undefined;
137
+ // Strip optional leading `@` on referenced-entry display IDs for Pandoc
138
+ // citation compatibility. Canonical slug never contains `@`.
139
+ if (displayId.startsWith("@"))
140
+ displayId = displayId.slice(1);
141
+ // Extract body content and attributes.
142
+ const bodyContent = extractBodyContent(item, markdown);
143
+ const [body, attrLines] = splitBodyAndAttributes(bodyContent);
144
+ const attributes = parseAttributes(attrLines);
145
+ // Discriminate shape by the `Id:` attribute's value format.
146
+ let shape;
147
+ let id;
148
+ const identityAttr = findIdentityAttribute(attributes);
149
+ if (identityAttr) {
150
+ id = identityAttr.value;
151
+ shape = shapeFromIdValue(identityAttr.value);
152
+ if (shape === undefined) {
153
+ // `Id:` is neither a ULID nor a scheme-qualified URI. Accept as an
154
+ // identified entry so the validator can surface MSL-R004; missing
155
+ // shape on a parsed entry is not useful downstream.
156
+ shape = "identified";
157
+ }
158
+ }
159
+ else {
160
+ // No `Id:`. Fall back on display-ID shape + document context to decide
161
+ // whether to admit the entry at all.
162
+ if (isReferencesDoc && SLUG_RE.test(displayId)) {
163
+ shape = "referenced";
164
+ }
165
+ else {
166
+ // No identity attribute and no references-doc context — admit as
167
+ // identified so the validator can surface the missing-`Id:` error.
168
+ shape = "identified";
169
+ }
170
+ }
171
+ // Inferred type from display-ID prefix — left to the profile layer. For
172
+ // now the core records no type; downstream profile-aware code populates
173
+ // this field when a profile is loaded.
174
+ const type = undefined;
175
+ // Body requirement: identified entries require a body; referenced entries
176
+ // may omit it. If there's only one child (the title paragraph), admit
177
+ // only referenced entries.
178
+ if (shape !== "referenced" && item.children.length < 2)
179
+ return undefined;
180
+ // Source location
181
+ const line = item.position?.start.line ?? 1;
182
+ const column = item.position?.start.column ?? 1;
183
+ return {
184
+ displayId,
185
+ title: title ?? "",
186
+ body,
187
+ rawAttributes: attributes,
188
+ typedAttributes: collateAttributes(attributes),
189
+ id,
190
+ type,
191
+ shape,
192
+ location: { file, line, column },
193
+ source: "markdown",
194
+ properties: { file: { path: file, line, column } },
195
+ };
196
+ }
197
+ /**
198
+ * Extract body content from a list item's children (excluding the first paragraph
199
+ * which contains the display ID and title).
200
+ *
201
+ * Reconstructs the text content from the source markdown using position info.
202
+ */
203
+ function extractBodyContent(item, markdown) {
204
+ const children = item.children.slice(1); // Skip title paragraph
205
+ if (!children.length)
206
+ return "";
207
+ const lines = markdown.split("\n");
208
+ // Get the range from the second child's start to the last child's end
209
+ const startLine = children[0].position?.start.line;
210
+ const endLine = children[children.length - 1].position?.end.line;
211
+ if (!startLine || !endLine)
212
+ return "";
213
+ // Compute indent width from the list item's column position.
214
+ // column is 1-based, plus 2 for the `- ` marker.
215
+ const indent = (item.position?.start.column ?? 1) - 1 + 2;
216
+ const indentStr = " ".repeat(indent);
217
+ // Extract the raw lines and strip the list item continuation indent
218
+ const rawLines = lines.slice(startLine - 1, endLine);
219
+ const stripped = rawLines.map((line) => {
220
+ if (line.startsWith(indentStr))
221
+ return line.slice(indent);
222
+ return line;
223
+ });
224
+ return stripped.join("\n").trim();
225
+ }
@@ -0,0 +1,73 @@
1
+ /**
2
+ * @module parser
3
+ *
4
+ * MarkSpec parser — file → Entry[].
5
+ *
6
+ * Three sub-modules:
7
+ * - markdown: CommonMark AST walk, entry block detection, attribute extraction
8
+ * - captions: table and figure caption detection
9
+ * - directives: HTML comment directive extraction
10
+ * - source: doc comment extraction from Rust, Kotlin, C, C++, Java
11
+ */
12
+ import type { Caption, Diagnostic, Document, Entry } from "../model/mod.js";
13
+ import { type DetectCaptionsOptions } from "./captions.js";
14
+ export type { DetectCaptionsOptions } from "./captions.js";
15
+ export { detectDirectives } from "./directives.js";
16
+ export type { DetectDirectivesOptions } from "./directives.js";
17
+ export { extractFrontMatter } from "./frontmatter.js";
18
+ export type { ExtractFrontMatterOptions, FrontMatterResult, } from "./frontmatter.js";
19
+ export { detectInlineRefs } from "./references.js";
20
+ export type { DetectInlineRefsOptions } from "./references.js";
21
+ export { parseSource } from "./source.js";
22
+ export type { ParseSourceOptions, ParseSourceResult } from "./source.js";
23
+ export { isSupportedExtension, loadGrammar } from "./grammars.js";
24
+ /** Options for {@linkcode parse}. */
25
+ export interface ParseOptions {
26
+ /** File path used in source locations. */
27
+ readonly file?: string;
28
+ }
29
+ /**
30
+ * Parse a Markdown string and return all MarkSpec entries found.
31
+ *
32
+ * @param markdown - Markdown source text
33
+ * @param options - Parse options
34
+ * @returns Array of parsed entries
35
+ */
36
+ export declare function parse(markdown: string, options?: ParseOptions): Entry[];
37
+ /** Result of parsing a file (entries + optional document + diagnostics). */
38
+ export interface ParseFileResult {
39
+ /** Parsed entries. */
40
+ readonly entries: Entry[];
41
+ /**
42
+ * Document-level metadata parsed from YAML front matter. `undefined` for
43
+ * source files (Rust/Kotlin/C/...) and for Markdown files without front
44
+ * matter; present otherwise so downstream consumers can resolve
45
+ * `{{document.*}}` inline references.
46
+ */
47
+ readonly document?: Document;
48
+ /**
49
+ * Parse-level diagnostics (forbidden front-matter keys, malformed YAML).
50
+ * Merged into the compile result's diagnostics.
51
+ */
52
+ readonly diagnostics: readonly Diagnostic[];
53
+ }
54
+ /**
55
+ * Parse a file and return entries, annotation links, and (for Markdown
56
+ * files) a Document with front-matter attributes.
57
+ *
58
+ * Source files (`.rs`, `.java`, `.c`, `.cpp`, etc.) are parsed with
59
+ * tree-sitter to extract doc comment entries; no front matter is
60
+ * recognized on source files.
61
+ */
62
+ export declare function parseFile(content: string, options: {
63
+ readonly file: string;
64
+ }): Promise<ParseFileResult>;
65
+ /**
66
+ * Detect table and figure captions in a Markdown string.
67
+ *
68
+ * @param markdown - Markdown source text
69
+ * @param options - Detection options (file path for source locations)
70
+ * @returns Array of detected captions
71
+ */
72
+ export declare function detectCaptions(markdown: string, options?: DetectCaptionsOptions): Caption[];
73
+ //# sourceMappingURL=mod.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../src/parser/mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAGH,OAAO,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAK5E,OAAO,EAEL,KAAK,qBAAqB,EAC3B,MAAM,eAAe,CAAC;AAEvB,YAAY,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;AAE3D,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AACnD,YAAY,EAAE,uBAAuB,EAAE,MAAM,iBAAiB,CAAC;AAE/D,OAAO,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AACtD,YAAY,EACV,yBAAyB,EACzB,iBAAiB,GAClB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AACnD,YAAY,EAAE,uBAAuB,EAAE,MAAM,iBAAiB,CAAC;AAE/D,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,YAAY,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAEzE,OAAO,EAAE,oBAAoB,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAElE,qCAAqC;AACrC,MAAM,WAAW,YAAY;IAC3B,0CAA0C;IAC1C,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;CACxB;AAED;;;;;;GAMG;AACH,wBAAgB,KAAK,CACnB,QAAQ,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE,YAAY,GACrB,KAAK,EAAE,CAET;AAED,4EAA4E;AAC5E,MAAM,WAAW,eAAe;IAC9B,sBAAsB;IACtB,QAAQ,CAAC,OAAO,EAAE,KAAK,EAAE,CAAC;IAC1B;;;;;OAKG;IACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC;IAC7B;;;OAGG;IACH,QAAQ,CAAC,WAAW,EAAE,SAAS,UAAU,EAAE,CAAC;CAC7C;AAYD;;;;;;;GAOG;AACH,wBAAsB,SAAS,CAC7B,OAAO,EAAE,MAAM,EACf,OAAO,EAAE;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GACjC,OAAO,CAAC,eAAe,CAAC,CAiC1B;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAC5B,QAAQ,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE,qBAAqB,GAC9B,OAAO,EAAE,CAEX"}
@@ -0,0 +1,89 @@
1
+ /**
2
+ * @module parser
3
+ *
4
+ * MarkSpec parser — file → Entry[].
5
+ *
6
+ * Three sub-modules:
7
+ * - markdown: CommonMark AST walk, entry block detection, attribute extraction
8
+ * - captions: table and figure caption detection
9
+ * - directives: HTML comment directive extraction
10
+ * - source: doc comment extraction from Rust, Kotlin, C, C++, Java
11
+ */
12
+ import { extname } from "@std/path";
13
+ import { parseMarkdown } from "./markdown.js";
14
+ import { isSupportedExtension, loadGrammar } from "./grammars.js";
15
+ import { parseSource } from "./source.js";
16
+ import { extractFrontMatter } from "./frontmatter.js";
17
+ import { detectCaptions as detectCaptionsImpl, } from "./captions.js";
18
+ export { detectDirectives } from "./directives.js";
19
+ export { extractFrontMatter } from "./frontmatter.js";
20
+ export { detectInlineRefs } from "./references.js";
21
+ export { parseSource } from "./source.js";
22
+ export { isSupportedExtension, loadGrammar } from "./grammars.js";
23
+ /**
24
+ * Parse a Markdown string and return all MarkSpec entries found.
25
+ *
26
+ * @param markdown - Markdown source text
27
+ * @param options - Parse options
28
+ * @returns Array of parsed entries
29
+ */
30
+ export function parse(markdown, options) {
31
+ return parseMarkdown(markdown, options);
32
+ }
33
+ /**
34
+ * Detect if a file is a references document.
35
+ * References context enables recognition of reference entries (slugs).
36
+ * @param file - File path
37
+ */
38
+ function isReferencesDocument(file) {
39
+ const basename = file.split("/").pop() ?? "";
40
+ return basename === "references.md" || file.includes("/references/");
41
+ }
42
+ /**
43
+ * Parse a file and return entries, annotation links, and (for Markdown
44
+ * files) a Document with front-matter attributes.
45
+ *
46
+ * Source files (`.rs`, `.java`, `.c`, `.cpp`, etc.) are parsed with
47
+ * tree-sitter to extract doc comment entries; no front matter is
48
+ * recognized on source files.
49
+ */
50
+ export async function parseFile(content, options) {
51
+ const ext = extname(options.file);
52
+ if (isSupportedExtension(ext)) {
53
+ const language = await loadGrammar(ext);
54
+ const result = parseSource(content, {
55
+ file: options.file,
56
+ language,
57
+ });
58
+ return { entries: result.entries, diagnostics: [] };
59
+ }
60
+ const fm = extractFrontMatter(content, { file: options.file });
61
+ const body = fm.hadFrontMatter ? fm.markdown : content;
62
+ const isReferencesDoc = isReferencesDocument(options.file);
63
+ const entries = parseMarkdown(body, {
64
+ file: options.file,
65
+ isReferencesDoc,
66
+ });
67
+ const document = fm.hadFrontMatter
68
+ ? {
69
+ file: options.file,
70
+ attributes: fm.attributes,
71
+ properties: {},
72
+ }
73
+ : undefined;
74
+ return {
75
+ entries,
76
+ document,
77
+ diagnostics: fm.diagnostics,
78
+ };
79
+ }
80
+ /**
81
+ * Detect table and figure captions in a Markdown string.
82
+ *
83
+ * @param markdown - Markdown source text
84
+ * @param options - Detection options (file path for source locations)
85
+ * @returns Array of detected captions
86
+ */
87
+ export function detectCaptions(markdown, options) {
88
+ return detectCaptionsImpl(markdown, options);
89
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * @module parser/references
3
+ *
4
+ * Inline reference detection. Scans Markdown prose for `{{namespace.id}}`
5
+ * patterns, skipping code spans and fenced code blocks.
6
+ */
7
+ import type { InlineRef } from "../model/mod.js";
8
+ /** Options for {@linkcode detectInlineRefs}. */
9
+ export interface DetectInlineRefsOptions {
10
+ /** File path used in source locations. */
11
+ readonly file?: string;
12
+ }
13
+ /**
14
+ * Detect `{{namespace.id}}` inline references in Markdown prose.
15
+ *
16
+ * Walks the mdast AST and scans every `text` node for the pattern,
17
+ * skipping text inside `code` (fenced code blocks) and `inlineCode` nodes.
18
+ *
19
+ * @param markdown - Markdown source text
20
+ * @param options - Detection options (file path for source locations)
21
+ * @returns Array of detected inline references
22
+ */
23
+ export declare function detectInlineRefs(markdown: string, options?: DetectInlineRefsOptions): InlineRef[];
24
+ //# sourceMappingURL=references.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"references.d.ts","sourceRoot":"","sources":["../../src/parser/references.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AASjD,gDAAgD;AAChD,MAAM,WAAW,uBAAuB;IACtC,0CAA0C;IAC1C,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;CACxB;AAED;;;;;;;;;GASG;AACH,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE,uBAAuB,GAChC,SAAS,EAAE,CAkCb"}
@@ -0,0 +1,71 @@
1
+ /**
2
+ * @module parser/references
3
+ *
4
+ * Inline reference detection. Scans Markdown prose for `{{namespace.id}}`
5
+ * patterns, skipping code spans and fenced code blocks.
6
+ */
7
+ import { processor } from "./remark.js";
8
+ /** Valid inline ref pattern: `{{namespace.id}}`. */
9
+ const INLINE_REF_RE = /\{\{([a-z]+)\.([A-Za-z0-9_-]+)\}\}/g;
10
+ /** Node types whose text children should be skipped. */
11
+ const CODE_TYPES = new Set(["code", "inlineCode"]);
12
+ /**
13
+ * Detect `{{namespace.id}}` inline references in Markdown prose.
14
+ *
15
+ * Walks the mdast AST and scans every `text` node for the pattern,
16
+ * skipping text inside `code` (fenced code blocks) and `inlineCode` nodes.
17
+ *
18
+ * @param markdown - Markdown source text
19
+ * @param options - Detection options (file path for source locations)
20
+ * @returns Array of detected inline references
21
+ */
22
+ export function detectInlineRefs(markdown, options) {
23
+ const file = options?.file ?? "<unknown>";
24
+ const tree = processor.parse(markdown);
25
+ const refs = [];
26
+ walkText(tree, false, (text, line, column) => {
27
+ INLINE_REF_RE.lastIndex = 0;
28
+ let match;
29
+ while ((match = INLINE_REF_RE.exec(text.value)) !== null) {
30
+ // Compute the column offset of this match within the text node.
31
+ // For single-line text nodes, add match index to the starting column.
32
+ // For multi-line, count from the last newline.
33
+ const before = text.value.slice(0, match.index);
34
+ const lastNewline = before.lastIndexOf("\n");
35
+ let matchLine;
36
+ let matchColumn;
37
+ if (lastNewline === -1) {
38
+ matchLine = line;
39
+ matchColumn = column + match.index;
40
+ }
41
+ else {
42
+ const newlineCount = before.split("\n").length - 1;
43
+ matchLine = line + newlineCount;
44
+ matchColumn = match.index - lastNewline;
45
+ }
46
+ refs.push({
47
+ namespace: match[1],
48
+ refId: match[2],
49
+ location: { file, line: matchLine, column: matchColumn },
50
+ });
51
+ }
52
+ });
53
+ return refs;
54
+ }
55
+ /**
56
+ * Recursively walk the AST and invoke `callback` for every `text` node
57
+ * that is NOT inside a code or inlineCode parent.
58
+ */
59
+ function walkText(node, insideCode, callback) {
60
+ const inCode = insideCode || CODE_TYPES.has(node.type);
61
+ if (node.type === "text" && !inCode && node.value !== undefined) {
62
+ const line = node.position?.start.line ?? 1;
63
+ const column = node.position?.start.column ?? 1;
64
+ callback({ value: node.value }, line, column);
65
+ }
66
+ if (node.children) {
67
+ for (const child of node.children) {
68
+ walkText(child, inCode, callback);
69
+ }
70
+ }
71
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * @module parser/remark
3
+ *
4
+ * Shared remark processor instance for Markdown parsing.
5
+ * Used by both the entry parser and the caption detector.
6
+ */
7
+ /** Shared remark + GFM processor. Built once, reused across modules. */
8
+ export declare const processor: import("unified").Processor<import("mdast").Root, undefined, undefined, undefined, undefined>;
9
+ //# sourceMappingURL=remark.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"remark.d.ts","sourceRoot":"","sources":["../../src/parser/remark.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAMH,wEAAwE;AACxE,eAAO,MAAM,SAAS,+FAA4C,CAAC"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * @module parser/remark
3
+ *
4
+ * Shared remark processor instance for Markdown parsing.
5
+ * Used by both the entry parser and the caption detector.
6
+ */
7
+ import { unified } from "unified";
8
+ import remarkParse from "remark-parse";
9
+ import remarkGfm from "remark-gfm";
10
+ /** Shared remark + GFM processor. Built once, reused across modules. */
11
+ export const processor = unified().use(remarkParse).use(remarkGfm);
@@ -0,0 +1,33 @@
1
+ /**
2
+ * @module parser/source
3
+ *
4
+ * Source-code doc-comment parser. Extracts MarkSpec entry blocks from doc
5
+ * comments in Rust, Kotlin, C, C++, and Java source files. Delegates parsed
6
+ * content to the markdown parser for entry extraction.
7
+ */
8
+ import Parser from "web-tree-sitter";
9
+ import type { Entry } from "../model/mod.js";
10
+ /** Options for {@linkcode parseSource}. */
11
+ export interface ParseSourceOptions {
12
+ /** File path used in source locations. */
13
+ readonly file?: string;
14
+ /** Pre-loaded tree-sitter language grammar. */
15
+ readonly language: Parser.Language;
16
+ }
17
+ /** Result of parsing a source file. */
18
+ export interface ParseSourceResult {
19
+ /** Entries found in doc comment blocks. */
20
+ readonly entries: Entry[];
21
+ }
22
+ /**
23
+ * Parse a source file and return entries extracted from doc comments.
24
+ *
25
+ * Uses tree-sitter to parse the source, walks the AST to find doc comment
26
+ * nodes, strips comment prefixes, and delegates to the markdown parser for
27
+ * entry extraction. Doc comments that don't contain entry blocks are ignored.
28
+ *
29
+ * @param content - Source file text
30
+ * @param options - Parse options (language grammar, file path)
31
+ */
32
+ export declare function parseSource(content: string, options: ParseSourceOptions): ParseSourceResult;
33
+ //# sourceMappingURL=source.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"source.d.ts","sourceRoot":"","sources":["../../src/parser/source.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,MAAM,MAAM,iBAAiB,CAAC;AACrC,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAG7C,2CAA2C;AAC3C,MAAM,WAAW,kBAAkB;IACjC,0CAA0C;IAC1C,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,+CAA+C;IAC/C,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC,QAAQ,CAAC;CACpC;AAED,uCAAuC;AACvC,MAAM,WAAW,iBAAiB;IAChC,2CAA2C;IAC3C,QAAQ,CAAC,OAAO,EAAE,KAAK,EAAE,CAAC;CAC3B;AAYD;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CACzB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,kBAAkB,GAC1B,iBAAiB,CAsCnB"}