peisar 0.0.1 → 0.1.2

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 (5) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +156 -0
  3. package/index.d.ts +499 -0
  4. package/index.js +784 -0
  5. package/package.json +36 -5
package/index.d.ts ADDED
@@ -0,0 +1,499 @@
1
+ /* auto-generated by NAPI-RS */
2
+ /* eslint-disable */
3
+
4
+ /**
5
+ * Which binding artifact the generated loader actually loaded: `'native'` for
6
+ * a native addon, otherwise the `platformArchABI` of the WASI flavor. Every
7
+ * flavor napi-rs can build is listed, because `NAPI_RS_NATIVE_LIBRARY_PATH`
8
+ * can point the loader at a WASI artifact this package does not build itself.
9
+ */
10
+ export declare const __napiBindingTarget: 'native' | 'wasm32-wasi' | 'wasm32-wasip1'
11
+
12
+ /**
13
+ * Parses one Markdown document and exposes its AST, HTML, and front matter
14
+ * to JavaScript.
15
+ *
16
+ * Construct this class with the Markdown source and an optional
17
+ * [`PeisarOptions`] object. Properties are computed from the source document;
18
+ * accessing `ast`, `html`, or `astJson` also applies registered visitors.
19
+ */
20
+ export declare class Peisar {
21
+ /**
22
+ * Creates a parser for `rawMd`.
23
+ *
24
+ * When `options` is omitted, GFM and Kramdown parsing are enabled and
25
+ * `html` returns a complete HTML document.
26
+ */
27
+ constructor(rawMd: string, options?: PeisarOptions | undefined | null)
28
+ /**
29
+ * Gets the parsed Markdown document as a JavaScript AST object.
30
+ *
31
+ * Registered visitors run before the AST is returned. The returned value
32
+ * is a clone, so reading this property does not consume the parser state.
33
+ */
34
+ get ast(): Document
35
+ /**
36
+ * Registers a JavaScript AST visitor.
37
+ *
38
+ * The visitor can provide `visitBlock` and/or `visitInline` callbacks.
39
+ * Each callback receives a one-item node tuple and may return a control object that
40
+ * changes the node or determines whether its children are visited.
41
+ */
42
+ useVisitor(visitor: Visitor): void
43
+ /**
44
+ * Renders the parsed Markdown as HTML using the configured render options.
45
+ *
46
+ * Registered visitors run before rendering.
47
+ */
48
+ get html(): string
49
+ /** Gets the deserialized YAML front matter, or `null` when none exists. */
50
+ get frontmatter(): any | null
51
+ /**
52
+ * Gets the parsed Markdown AST serialized as JSON.
53
+ *
54
+ * Registered visitors run before serialization.
55
+ */
56
+ get astJson(): string
57
+ }
58
+
59
+ /**
60
+ * Options that control how Markdown is parsed.
61
+ *
62
+ * # Defaults
63
+ *
64
+ * Both GFM and Kramdown extensions are enabled by default. To parse
65
+ * strict CommonMark only:
66
+ *
67
+ * ```ignore
68
+ * use peisar_ast::AstOptions;
69
+ *
70
+ * let opts = AstOptions {
71
+ * gfm: false,
72
+ * kramdown: false,
73
+ * file_name: None,
74
+ * };
75
+ * ```
76
+ */
77
+ export interface AstOptions {
78
+ /**
79
+ * Enable GitHub Flavored Markdown (tables, strikethrough, task lists,
80
+ * autolinks). Default: `true`.
81
+ */
82
+ gfm: boolean
83
+ /**
84
+ * Enable Kramdown-style block attributes (`{:#id .class key="val"}`).
85
+ * Default: `true`.
86
+ */
87
+ kramdown: boolean
88
+ /** Optional file name to attach to the parsed [`Document`](crate::Document). */
89
+ fileName?: string
90
+ }
91
+
92
+ /**
93
+ * Parsed Kramdown block attributes (`{:#id .class key="val"}`).
94
+ *
95
+ * # Example
96
+ *
97
+ * ```ignore
98
+ * use peisar_ast::tokens::Attributes;
99
+ *
100
+ * let attrs = Attributes {
101
+ * id: Some("intro".into()),
102
+ * classes: Some(vec!["banner".into(), "wide".into()]),
103
+ * attributes: Some(vec![("data-index".into(), "42".into())]),
104
+ * };
105
+ * assert_eq!(
106
+ * attrs.to_html_attr_string(),
107
+ * "id=\"intro\" class=\"banner wide\" data-index=\"42\""
108
+ * );
109
+ * ```
110
+ */
111
+ export interface Attributes {
112
+ /** HTML `id` attribute. */
113
+ id?: string
114
+ /** CSS class list. */
115
+ classes?: Array<string>
116
+ /** Arbitrary key/value attributes. */
117
+ attributes?: Array<[string, string]>
118
+ }
119
+
120
+ /** Block-level nodes. */
121
+ export type Block =
122
+ | { type: 'Heading'; /** Heading level (1–6). */
123
+ level: number; /** Inline content of the heading. */
124
+ children: Array<Inline>; /** Source span. */
125
+ pos: Span; /** Optional Kramdown attributes. */
126
+ attrs?: Attributes }
127
+ | { type: 'Paragraph'; /** Inline content of the paragraph. */
128
+ children: Array<Inline>; /** Source span. */
129
+ pos: Span; /** Optional Kramdown attributes. */
130
+ attrs?: Attributes }
131
+ | { type: 'CodeBlock'; /** Language hint from the info string (e.g. `rust` from ` ```rust `). */
132
+ lang?: string; /** The raw code content. */
133
+ code: string; /** Source span. */
134
+ pos: Span; /** Optional Kramdown attributes. */
135
+ attrs?: Attributes }
136
+ | { type: 'BlockQuote'; /** Nested block content inside the quote. */
137
+ children: Array<Block>; /** Source span. */
138
+ pos: Span; /** Optional Kramdown attributes. */
139
+ attrs?: Attributes }
140
+ | { type: 'List'; /** `true` for ordered lists, `false` for unordered. */
141
+ ordered: boolean; /** The list items. */
142
+ items: Array<ListItem>; /** Source span. */
143
+ pos: Span; /** Optional Kramdown attributes. */
144
+ attrs?: Attributes }
145
+ | { type: 'ThematicBreak'; /** Source span. */
146
+ pos: Span }
147
+ | { type: 'HtmlBlock'; /** The raw HTML content. */
148
+ html: string; /** Source span. */
149
+ pos: Span; /** Optional Kramdown attributes. */
150
+ attrs?: Attributes }
151
+ | { type: 'Table'; /** The table structure (header, rows, alignments). */
152
+ table: Table; /** Source span. */
153
+ pos: Span; /** Optional Kramdown attributes. */
154
+ attrs?: Attributes }
155
+ | { type: 'LinkReferenceDefinition'; /** Normalised label (lowercased, trimmed). */
156
+ label: string; /** The destination URL. */
157
+ url: string; /** Optional link title. */
158
+ title?: string; /** Source span. */
159
+ pos: Span }
160
+ | { type: 'Comment'; /** The comment text (markers stripped). */
161
+ value: string; /** Source span. */
162
+ pos: Span }
163
+
164
+ /**
165
+ * Synchronous block-visitor callback. Receives a `Block`, returns
166
+ * `VisitControlJs`.
167
+ */
168
+ export type BlockCallback = (arg: [Block]) => VisitControlJs
169
+
170
+ /**
171
+ * A Markdown document — the root of the AST.
172
+ *
173
+ * Contains the top-level block children and all link reference definitions
174
+ * collected from the source text.
175
+ *
176
+ * # Example
177
+ *
178
+ * ```ignore
179
+ * use peisar_ast::Document;
180
+ * use peisar_ast::AstOptions;
181
+ *
182
+ * let doc = Document::parse("# Hello
183
+ ", &AstOptions::default(), None);
184
+ * assert_eq!(doc.node_type, "root");
185
+ * assert_eq!(doc.children.len(), 1);
186
+ * ```
187
+ */
188
+ export interface Document {
189
+ /** Always `"root"`. */
190
+ nodeType: string
191
+ /** Optional source file name. */
192
+ fileName?: string
193
+ /** Span of the whole document in the source text. */
194
+ pos: Span
195
+ /** Top-level block children. */
196
+ children: Array<Block>
197
+ /** All link reference definitions collected from the document. */
198
+ linkReferences: Array<LinkReferenceDefinition>
199
+ }
200
+
201
+ /** Emphasis strength. */
202
+ export declare const enum EmphasisLevel {
203
+ /** `*italic*` / `_italic_` */
204
+ Italic = 0,
205
+ /** `**bold**` / `__bold__` */
206
+ Bold = 1,
207
+ }
208
+
209
+ /** Inline-level nodes. */
210
+ export type Inline =
211
+ | { type: 'Text'; /** The text value. */
212
+ value: string; /** Source span. */
213
+ pos: Span }
214
+ | { type: 'Emphasis'; /** Emphasis level (italic or bold). */
215
+ level: EmphasisLevel; /** Nested inline content. */
216
+ children: Array<Inline>; /** Source span. */
217
+ pos: Span }
218
+ | { type: 'Code'; /** The code text. */
219
+ code: string; /** Source span. */
220
+ pos: Span }
221
+ | { type: 'HtmlInline'; /** The raw HTML string. */
222
+ html: string; /** Source span. */
223
+ pos: Span }
224
+ | { type: 'Strikethrough'; /** Nested inline content. */
225
+ children: Array<Inline>; /** Source span. */
226
+ pos: Span }
227
+ | { type: 'HardBreak'; /** Source span. */
228
+ pos: Span }
229
+ | { type: 'SoftBreak'; /** Source span. */
230
+ pos: Span }
231
+ | { type: 'Image'; /** Alternative text. */
232
+ alt: string; /** Image URL. */
233
+ url: string; /** Optional title. */
234
+ title?: string; /** Source span. */
235
+ pos: Span }
236
+ | { type: 'Link'; /** Link text (inline children). */
237
+ text: Array<Inline>; /** Destination URL. */
238
+ url: string; /** Optional link title. */
239
+ title?: string; /** `true` if this is a GFM autolink (bare URL). */
240
+ autolink: boolean; /** Source span. */
241
+ pos: Span }
242
+ | { type: 'LinkReference'; /** Link text (inline children). */
243
+ text: Array<Inline>; /** Normalised label used to look up the reference. */
244
+ label: string; /** Resolved destination URL. */
245
+ url: string; /** Optional resolved title. */
246
+ title?: string; /** Source span. */
247
+ pos: Span }
248
+
249
+ /**
250
+ * Synchronous inline-visitor callback. Receives an `Inline`, returns
251
+ * `InlineVisitControlJs`.
252
+ */
253
+ export type InlineCallback = (arg: [Inline]) => InlineVisitControlJs
254
+
255
+ /**
256
+ * JS-facing mirror of [`InlineVisitControl`].
257
+ *
258
+ * Returned from the JS `visitInline` callback. All fields are optional;
259
+ * omitting a field means "no change" for that operation.
260
+ */
261
+ export interface InlineVisitControlJs {
262
+ /** Nodes to insert before the current node. */
263
+ insertBefore?: Array<Inline>
264
+ /** Nodes to insert after the current node. */
265
+ insertAfter?: Array<Inline>
266
+ /** Replace the current node with these nodes. */
267
+ replaceWith?: Array<Inline>
268
+ /** Remove the current node entirely. */
269
+ remove?: boolean
270
+ /** Whether to recurse into this node's children. */
271
+ recurse?: boolean
272
+ }
273
+
274
+ /** A link reference definition collected at the document level. */
275
+ export interface LinkReferenceDefinition {
276
+ /** Normalised label (lowercased, trimmed). */
277
+ label: string
278
+ /** The destination URL. */
279
+ url: string
280
+ /** Optional link title. */
281
+ title?: string
282
+ /** Source span. */
283
+ pos: Span
284
+ }
285
+
286
+ /** A single list item (an `<li>`). Contains nested block content. */
287
+ export interface ListItem {
288
+ /** Nested block content of the item. */
289
+ children: Array<Block>
290
+ /** GFM task-list state: `None` = not a task, `Some` = checked / unchecked. */
291
+ task?: TaskState
292
+ /** Span of the item in the *sub-document* of its enclosing list. */
293
+ pos: Span
294
+ }
295
+
296
+ /** The Markdown body and YAML metadata parsed from a front-matter document. */
297
+ export interface ParseResult {
298
+ /** Markdown source after the leading YAML front matter is removed. */
299
+ pureMarkdownContent: string
300
+ /** Deserialized YAML front matter, or `null` when no front matter exists. */
301
+ yamlData?: Record<string, any>
302
+ }
303
+
304
+ /**
305
+ * JavaScript options for Markdown parsing and HTML rendering.
306
+ *
307
+ * Every property is optional. Omitted parsing options enable GFM and
308
+ * Kramdown; omitted rendering options produce a complete HTML document with
309
+ * charset and viewport metadata.
310
+ */
311
+ export interface PeisarOptions {
312
+ /**
313
+ * Enable GitHub Flavored Markdown (tables, strikethrough, task lists,
314
+ * autolinks). Default: `true`.
315
+ */
316
+ gfm?: boolean
317
+ /**
318
+ * Enable Kramdown-style block attributes (`{:#id .class key="val"}`).
319
+ * Default: `true`.
320
+ */
321
+ kramdown?: boolean
322
+ /** Optional file name to attach to the parsed [`Document`](crate::Document). */
323
+ fileName?: string
324
+ /**
325
+ * If `true`, emit only the body content (no `<!DOCTYPE>`, `<html>`,
326
+ * `<head>`, or `<body>` wrapper). If `false`, emit a full HTML
327
+ * document. Default: `true` (fragment).
328
+ */
329
+ fragment?: boolean
330
+ /**
331
+ * Include a `<meta charset="utf-8">` in the head (only relevant when
332
+ * `fragment` is `false`). Default: `true`.
333
+ */
334
+ charset?: boolean
335
+ /**
336
+ * Include a `<meta name="viewport" content="width=device-width,
337
+ * initial-scale=1.0">` in the head (only relevant when `fragment` is
338
+ * `false`). Default: `true`.
339
+ */
340
+ viewport?: boolean
341
+ /**
342
+ * Optional `<title>` for the HTML head (only relevant when
343
+ * `fragment` is `false`). Default: `None`.
344
+ */
345
+ title?: string
346
+ /**
347
+ * Optional additional CSS classes to add to `<body>` (only relevant
348
+ * when `fragment` is `false`). Default: `None`.
349
+ */
350
+ bodyClass?: string
351
+ /**
352
+ * Optional inline CSS to inject in a `<style>` tag in the head.
353
+ * Default: `None`.
354
+ */
355
+ style?: string
356
+ }
357
+
358
+ /** A zero-based point in the source text. */
359
+ export interface Position {
360
+ /** Line number, 0-based. */
361
+ line: number
362
+ /** Column number, 0-based (in characters). */
363
+ column: number
364
+ /** Byte offset from the start of the input. */
365
+ offset: number
366
+ }
367
+
368
+ /** Options that control how the AST is rendered to HTML. */
369
+ export interface RenderOptions {
370
+ /**
371
+ * If `true`, emit only the body content (no `<!DOCTYPE>`, `<html>`,
372
+ * `<head>`, or `<body>` wrapper). If `false`, emit a full HTML
373
+ * document. Default: `true` (fragment).
374
+ */
375
+ fragment: boolean
376
+ /**
377
+ * Include a `<meta charset="utf-8">` in the head (only relevant when
378
+ * `fragment` is `false`). Default: `true`.
379
+ */
380
+ charset: boolean
381
+ /**
382
+ * Include a `<meta name="viewport" content="width=device-width,
383
+ * initial-scale=1.0">` in the head (only relevant when `fragment` is
384
+ * `false`). Default: `true`.
385
+ */
386
+ viewport: boolean
387
+ /**
388
+ * Optional `<title>` for the HTML head (only relevant when
389
+ * `fragment` is `false`). Default: `None`.
390
+ */
391
+ title?: string
392
+ /**
393
+ * Optional additional CSS classes to add to `<body>` (only relevant
394
+ * when `fragment` is `false`). Default: `None`.
395
+ */
396
+ bodyClass?: string
397
+ /**
398
+ * Optional inline CSS to inject in a `<style>` tag in the head.
399
+ * Default: `None`.
400
+ */
401
+ style?: string
402
+ }
403
+
404
+ /**
405
+ * A half-open span `[start, end)` covering a node's source text.
406
+ *
407
+ * `start` is inclusive and `end` is exclusive; both positions point into the
408
+ * original source string.
409
+ */
410
+ export interface Span {
411
+ /** The start position (inclusive). */
412
+ start: Position
413
+ /** The end position (exclusive). */
414
+ end: Position
415
+ }
416
+
417
+ /** A GFM table. */
418
+ export interface Table {
419
+ /** The header row. */
420
+ header: TableRow
421
+ /** Body rows. */
422
+ rows: Array<TableRow>
423
+ /** Column alignment specifications (one per column). */
424
+ alignments: Array<TableCellAlignment>
425
+ }
426
+
427
+ /** A single table cell. */
428
+ export interface TableCell {
429
+ /** Inline content of the cell. */
430
+ children: Array<Inline>
431
+ }
432
+
433
+ /** Column alignment for table cells. */
434
+ export declare const enum TableCellAlignment {
435
+ /** `:---` or `---` — default (left) */
436
+ Default = 0,
437
+ /** `:---` */
438
+ Left = 1,
439
+ /** `:---:` */
440
+ Center = 2,
441
+ /** `---:` */
442
+ Right = 3,
443
+ }
444
+
445
+ /** A single table row (header or body). */
446
+ export interface TableRow {
447
+ /** The cells in this row. */
448
+ cells: Array<TableCell>
449
+ }
450
+
451
+ /** GFM task-list checkbox state. */
452
+ export declare const enum TaskState {
453
+ /** `[ ]` — unchecked */
454
+ Unchecked = 0,
455
+ /** `[x]` / `[X]` — checked */
456
+ Checked = 1,
457
+ }
458
+
459
+ /**
460
+ * JS-facing mirror of [`VisitControl`].
461
+ *
462
+ * Returned from the JS `visitBlock` callback. All fields are optional;
463
+ * omitting a field means "no change" for that operation.
464
+ */
465
+ export interface VisitControlJs {
466
+ /** Nodes to insert before the current node. */
467
+ insertBefore?: Array<Block>
468
+ /** Nodes to insert after the current node. */
469
+ insertAfter?: Array<Block>
470
+ /** Replace the current node with these nodes. */
471
+ replaceWith?: Array<Block>
472
+ /** Remove the current node entirely. */
473
+ remove?: boolean
474
+ /** Whether to recurse into this node's children. */
475
+ recurse?: boolean
476
+ }
477
+
478
+ /**
479
+ * JavaScript object shape for a visitor callback pair.
480
+ *
481
+ * On the JS side it is a plain object with two optional
482
+ * function properties:
483
+ *
484
+ * ```js
485
+ * const myPlugin = {
486
+ * visitBlock(block) { return { recurse: true }; },
487
+ * visitInline(inline) { return {}; },
488
+ * };
489
+ * ast.addVisitor(myPlugin);
490
+ * ```
491
+ *
492
+ * Either property may be omitted / `null` to skip that node kind.
493
+ */
494
+ export interface Visitor {
495
+ /** Optional JS callback for block nodes (JS: `visitBlock`). */
496
+ visitBlock?: BlockCallback
497
+ /** Optional JS callback for inline nodes (JS: `visitInline`). */
498
+ visitInline?: InlineCallback
499
+ }