@origints/markdown 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Franco Ponticelli
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,223 @@
1
+ # @origints/markdown
2
+
3
+ > Markdown parsing and manipulation for Origins with full lineage tracking.
4
+
5
+ ---
6
+
7
+ ## Features
8
+
9
+ - Parse Markdown with GFM (GitHub Flavored Markdown) support
10
+ - YAML frontmatter extraction
11
+ - Source position tracking for all nodes
12
+ - Type-safe navigation and extraction
13
+ - Convert to HTML
14
+ - Support for tables, task lists, and footnotes
15
+ - Integrates with Origins transform registry
16
+
17
+ ---
18
+
19
+ ## Installation
20
+
21
+ ```bash
22
+ npm install @origints/markdown @origints/core
23
+ ```
24
+
25
+ ---
26
+
27
+ ## Usage with Planner
28
+
29
+ ### Extract content from a Markdown file
30
+
31
+ ```ts
32
+ import { Planner, loadFile, run } from '@origints/core'
33
+ import { parseMarkdown } from '@origints/markdown'
34
+
35
+ const plan = new Planner()
36
+ .in(loadFile('README.md'))
37
+ .mapIn(parseMarkdown())
38
+ .emit((out, $) => out.add('title', $.select('heading').text()))
39
+ .compile()
40
+
41
+ const result = await run(plan, { readFile, registry })
42
+ // result.value: { title: 'My Project' }
43
+ ```
44
+
45
+ ### Extract collections with selectAll
46
+
47
+ Use `selectAll()` to extract data from all matching nodes as an array:
48
+
49
+ ```ts
50
+ // Extract all heading texts from a document
51
+ const plan = new Planner()
52
+ .in(loadFile('README.md'))
53
+ .mapIn(parseMarkdown())
54
+ .emit((out, $) =>
55
+ out.add(
56
+ 'headings',
57
+ $.selectAll('heading', node => node.text())
58
+ )
59
+ )
60
+ .compile()
61
+
62
+ const result = await run(plan, { readFile, registry })
63
+ // result.value: { headings: ['Introduction', 'Getting Started', 'API'] }
64
+ ```
65
+
66
+ ### Extract structured data from repeated nodes
67
+
68
+ ```ts
69
+ // Extract all link URLs and labels
70
+ const plan = new Planner()
71
+ .in(loadFile('README.md'))
72
+ .mapIn(parseMarkdown())
73
+ .emit((out, $) =>
74
+ out.add(
75
+ 'links',
76
+ $.selectAll('link', node => node.text())
77
+ )
78
+ )
79
+ .compile()
80
+ ```
81
+
82
+ ### Extract top-level children
83
+
84
+ Use `children()` to extract each direct child of a node:
85
+
86
+ ```ts
87
+ const plan = new Planner()
88
+ .in(loadFile('README.md'))
89
+ .mapIn(parseMarkdown())
90
+ .emit((out, $) =>
91
+ out.add(
92
+ 'blocks',
93
+ $.children(node => node.text())
94
+ )
95
+ )
96
+ .compile()
97
+ ```
98
+
99
+ ### Extract frontmatter fields
100
+
101
+ ```ts
102
+ // doc.md:
103
+ // ---
104
+ // title: My Post
105
+ // date: 2024-01-15
106
+ // tags:
107
+ // - typescript
108
+ // - origins
109
+ // ---
110
+ // # Content here
111
+
112
+ const plan = new Planner()
113
+ .in(loadFile('doc.md'))
114
+ .mapIn(parseMarkdown())
115
+ .emit((out, $) => out.add('title', $.select('yaml').text()))
116
+ .compile()
117
+ ```
118
+
119
+ ### Combine Markdown with other sources
120
+
121
+ ```ts
122
+ const plan = new Planner()
123
+ .in(loadFile('README.md'))
124
+ .mapIn(parseMarkdown())
125
+ .emit((out, $) => out.add('title', $.select('heading').text()))
126
+ .in(loadFile('package.json'))
127
+ .mapIn(parseJson())
128
+ .emit((out, $) =>
129
+ out
130
+ .add('version', $.get('version').string())
131
+ .add('name', $.get('name').string())
132
+ )
133
+ .compile()
134
+ ```
135
+
136
+ ### Standalone usage (without Planner)
137
+
138
+ For direct Markdown navigation:
139
+
140
+ ```ts
141
+ import { parseMarkdownImpl, MarkdownNode } from '@origints/markdown'
142
+
143
+ const node = parseMarkdownImpl.execute(markdownString) as MarkdownNode
144
+
145
+ // Select nodes using CSS-like selectors
146
+ const headingResult = node.select('heading')
147
+ if (headingResult.ok) {
148
+ console.log(headingResult.value.text())
149
+ }
150
+
151
+ // Select by attribute
152
+ const h1Result = node.select('heading[depth=1]')
153
+ const codeResult = node.select('code[lang="typescript"]')
154
+
155
+ // Nested selectors
156
+ const listItems = node.selectAll('list > listItem')
157
+
158
+ // Get all text content
159
+ console.log(node.text())
160
+ ```
161
+
162
+ ### Typed node extraction
163
+
164
+ ```ts
165
+ const headingResult = node.select('heading')
166
+ if (headingResult.ok) {
167
+ const data = headingResult.value.asHeading()
168
+ if (data.ok) {
169
+ console.log(data.value.depth) // 1, 2, 3, etc.
170
+ }
171
+ }
172
+
173
+ const linkResult = node.select('link')
174
+ if (linkResult.ok) {
175
+ const data = linkResult.value.asLink()
176
+ if (data.ok) {
177
+ console.log(data.value.url)
178
+ }
179
+ }
180
+ ```
181
+
182
+ ### Converting to HTML
183
+
184
+ ```ts
185
+ import { parseMarkdownImpl, toHtml } from '@origints/markdown'
186
+
187
+ const node = parseMarkdownImpl.execute('# Hello\n\nWorld') as MarkdownNode
188
+ const html = toHtml(node)
189
+ // <h1>Hello</h1>\n<p>World</p>
190
+ ```
191
+
192
+ ### Frontmatter extraction (standalone)
193
+
194
+ ```ts
195
+ import { parseMarkdownImpl, extractFrontmatter } from '@origints/markdown'
196
+
197
+ const node = parseMarkdownImpl.execute(markdownWithFrontmatter) as MarkdownNode
198
+ const frontmatter = extractFrontmatter(node)
199
+ if (frontmatter) {
200
+ console.log(frontmatter.title)
201
+ }
202
+ ```
203
+
204
+ ---
205
+
206
+ ## API
207
+
208
+ | Export | Description |
209
+ | -------------------------------------- | ----------------------------------------------------- |
210
+ | `parseMarkdown(options?)` | Create a transform AST for use with `Planner.mapIn()` |
211
+ | `parseMarkdownImpl` | Sync transform implementation (string input) |
212
+ | `parseMarkdownAsyncImpl` | Async transform implementation (string or stream) |
213
+ | `registerMarkdownTransforms(registry)` | Register all Markdown transforms with a registry |
214
+ | `MarkdownNode` | Navigable wrapper with selector support |
215
+ | `toHtml(node)` | Convert Markdown to HTML |
216
+ | `toJson(node, options?)` | Convert MarkdownNode to JSON |
217
+ | `extractFrontmatter(node)` | Extract YAML frontmatter |
218
+
219
+ ---
220
+
221
+ ## License
222
+
223
+ MIT