|
1 | 1 | --- |
2 | | -title: Core API |
3 | | -description: Block-based markdown parsing and serialization |
| 2 | +title: "Core API" |
| 3 | +description: "Block-based markdown parsing and serialization" |
4 | 4 | --- |
5 | 5 |
|
6 | | -# Core API |
| 6 | +# @create-markdown/core |
7 | 7 |
|
8 | | -The `@create-markdown/core` package provides block-based markdown parsing and serialization with zero dependencies. |
| 8 | +Block-based markdown parsing and serialization with zero dependencies. |
9 | 9 |
|
10 | 10 | ## Installation |
11 | 11 |
|
12 | 12 | ```bash |
| 13 | +# Using bun |
13 | 14 | bun add @create-markdown/core |
14 | | -``` |
15 | 15 |
|
16 | | -## Parsing |
| 16 | +# Using npm |
| 17 | +npm install @create-markdown/core |
| 18 | + |
| 19 | +# Using yarn |
| 20 | +yarn add @create-markdown/core |
| 21 | + |
| 22 | +# Using pnpm |
| 23 | +pnpm add @create-markdown/core |
| 24 | +``` |
17 | 25 |
|
18 | | -### parse |
| 26 | +## Quick Start |
19 | 27 |
|
20 | | -Parse a markdown string to an array of blocks. |
| 28 | +### Parse Markdown to Blocks |
21 | 29 |
|
22 | 30 | ```typescript |
23 | 31 | import { parse } from '@create-markdown/core'; |
24 | 32 |
|
25 | 33 | const blocks = parse(`# Hello World |
26 | 34 |
|
27 | | -This is **bold** text. |
| 35 | +This is **bold** and *italic* text. |
28 | 36 |
|
29 | 37 | - Item one |
30 | 38 | - Item two |
31 | 39 | `); |
32 | 40 | ``` |
33 | 41 |
|
34 | | -### markdownToBlocks |
35 | | - |
36 | | -Full parser with options. |
37 | | - |
38 | | -```typescript |
39 | | -import { markdownToBlocks } from '@create-markdown/core'; |
40 | | - |
41 | | -const blocks = markdownToBlocks(markdown, { |
42 | | - generateId: () => crypto.randomUUID(), |
43 | | - strict: false, |
44 | | -}); |
45 | | -``` |
46 | | - |
47 | | -### markdownToDocument |
48 | | - |
49 | | -Parse to a Document object with metadata. |
| 42 | +### Create Blocks Programmatically |
50 | 43 |
|
51 | 44 | ```typescript |
52 | | -import { markdownToDocument } from '@create-markdown/core'; |
| 45 | +import { h1, paragraph, bulletList, bold, italic, spans } from '@create-markdown/core'; |
53 | 46 |
|
54 | | -const doc = markdownToDocument(markdown); |
55 | | -console.log(doc.blocks); |
56 | | -console.log(doc.meta); |
| 47 | +const blocks = [ |
| 48 | + h1('My Document'), |
| 49 | + paragraph(spans( |
| 50 | + bold('Important: '), |
| 51 | + { text: 'This is ', styles: {} }, |
| 52 | + italic('really'), |
| 53 | + { text: ' cool!', styles: {} } |
| 54 | + )), |
| 55 | + bulletList(['First item', 'Second item', 'Third item']), |
| 56 | +]; |
57 | 57 | ``` |
58 | 58 |
|
59 | | -## Serialization |
60 | | - |
61 | | -### stringify |
62 | | - |
63 | | -Serialize blocks to markdown. |
| 59 | +### Serialize Blocks to Markdown |
64 | 60 |
|
65 | 61 | ```typescript |
66 | | -import { stringify, h1, paragraph } from '@create-markdown/core'; |
| 62 | +import { stringify, h1, paragraph, codeBlock } from '@create-markdown/core'; |
67 | 63 |
|
68 | 64 | const markdown = stringify([ |
69 | 65 | h1('Hello'), |
70 | 66 | paragraph('World'), |
| 67 | + codeBlock('console.log("Hi!");', 'javascript'), |
71 | 68 | ]); |
72 | 69 | ``` |
73 | 70 |
|
74 | | -### blocksToMarkdown |
75 | | - |
76 | | -Full serializer with options. |
77 | | - |
78 | | -```typescript |
79 | | -import { blocksToMarkdown } from '@create-markdown/core'; |
80 | | - |
81 | | -const markdown = blocksToMarkdown(blocks, { |
82 | | - bulletChar: '-', |
83 | | - emphasisChar: '*', |
84 | | -}); |
85 | | -``` |
86 | | - |
87 | 71 | ## Block Types |
88 | 72 |
|
89 | 73 | | Type | Factory Function | Description | |
90 | | -|------|-----------------|-------------| |
91 | | -| paragraph | `paragraph(content)` | Text paragraph | |
92 | | -| heading | `h1` - `h6` | Heading levels 1-6 | |
93 | | -| bulletList | `bulletList(items)` | Unordered list | |
94 | | -| numberedList | `numberedList(items)` | Ordered list | |
95 | | -| checkList | `checkList(items)` | Task list with checkboxes | |
96 | | -| codeBlock | `codeBlock(code, lang?)` | Fenced code block | |
97 | | -| blockquote | `blockquote(content)` | Block quote | |
98 | | -| image | `image(url, alt?)` | Image | |
99 | | -| divider | `divider()` | Horizontal rule | |
100 | | -| table | `table(headers, rows)` | Table with headers | |
101 | | -| callout | `callout(type, content)` | Callout/admonition | |
102 | | - |
103 | | -## Block Factory Functions |
104 | | - |
105 | | -```typescript |
106 | | -import { |
107 | | - h1, h2, h3, h4, h5, h6, |
108 | | - paragraph, |
109 | | - bulletList, |
110 | | - numberedList, |
111 | | - codeBlock, |
112 | | - blockquote, |
113 | | - image, |
114 | | - divider, |
115 | | - table, |
116 | | - callout, |
117 | | -} from '@create-markdown/core'; |
118 | | - |
119 | | -const blocks = [ |
120 | | - h1('Welcome'), |
121 | | - paragraph('This is a paragraph.'), |
122 | | - codeBlock('const x = 1;', 'typescript'), |
123 | | - bulletList(['Item 1', 'Item 2']), |
124 | | - divider(), |
125 | | -]; |
126 | | -``` |
127 | | - |
128 | | -## Inline Styles |
129 | | - |
130 | | -```typescript |
131 | | -import { bold, italic, code, link, strikethrough, highlight } from '@create-markdown/core'; |
132 | | - |
133 | | -const content = [ |
134 | | - bold('Bold text'), |
135 | | - italic('Italic text'), |
136 | | - code('inline code'), |
137 | | - link('Click here', 'https://example.com'), |
138 | | - strikethrough('deleted'), |
139 | | - highlight('highlighted'), |
140 | | -]; |
141 | | -``` |
142 | | - |
143 | | -## Document Operations |
144 | | - |
145 | | -```typescript |
146 | | -import { |
147 | | - createDocument, |
148 | | - appendBlock, |
149 | | - removeBlock, |
150 | | - updateBlock, |
151 | | - moveBlock, |
152 | | - findBlock, |
153 | | -} from '@create-markdown/core'; |
154 | | - |
155 | | -// Create a document |
156 | | -let doc = createDocument([paragraph('First')]); |
157 | | - |
158 | | -// Add a block |
159 | | -doc = appendBlock(doc, paragraph('Second')); |
160 | | - |
161 | | -// Find a block |
162 | | -const block = findBlock(doc, 'some-id'); |
163 | | - |
164 | | -// Remove a block |
165 | | -doc = removeBlock(doc, 'some-id'); |
166 | | - |
167 | | -// Move a block |
168 | | -doc = moveBlock(doc, 'block-id', 0); |
169 | | -``` |
| 74 | +| --- | --- | --- | |
| 75 | +| `paragraph` | `paragraph(content)` | Text paragraph | |
| 76 | +| `heading` | `heading(level, content)` or `h1`-`h6` | Heading levels 1-6 | |
| 77 | +| `bulletList` | `bulletList(items)` | Unordered list | |
| 78 | +| `numberedList` | `numberedList(items)` | Ordered list | |
| 79 | +| `checkList` | `checkList(items)` | Task list with checkboxes | |
| 80 | +| `codeBlock` | `codeBlock(code, language?)` | Fenced code block | |
| 81 | +| `blockquote` | `blockquote(content)` | Block quote | |
| 82 | +| `image` | `image(url, alt?)` | Image | |
| 83 | +| `divider` | `divider()` | Horizontal rule | |
| 84 | +| `table` | `table(headers, rows)` | Table with headers | |
| 85 | +| `callout` | `callout(type, content)` | Callout/admonition | |
| 86 | + |
| 87 | +## API Reference |
| 88 | + |
| 89 | +### Parsing |
| 90 | + |
| 91 | +- `parse(markdown)` - Parse markdown string to blocks |
| 92 | +- `markdownToBlocks(markdown, options?)` - Full parser with options |
| 93 | +- `markdownToDocument(markdown)` - Parse to a Document object |
| 94 | + |
| 95 | +### Serialization |
| 96 | + |
| 97 | +- `stringify(blocks)` - Serialize blocks to markdown |
| 98 | +- `blocksToMarkdown(blocks, options?)` - Full serializer with options |
| 99 | +- `documentToMarkdown(doc)` - Serialize a Document |
| 100 | + |
| 101 | +### Document Operations |
| 102 | + |
| 103 | +- `createDocument(blocks?, options?)` - Create a new document |
| 104 | +- `insertBlock(doc, block, index?)` - Insert block at position |
| 105 | +- `appendBlock(doc, block)` - Add block at end |
| 106 | +- `removeBlock(doc, blockId)` - Remove block by ID |
| 107 | +- `updateBlock(doc, blockId, updates)` - Update block properties |
| 108 | +- `moveBlock(doc, blockId, newIndex)` - Reorder blocks |
| 109 | +- `findBlock(doc, blockId)` - Find block by ID |
| 110 | + |
| 111 | +## License |
| 112 | + |
| 113 | +MIT |
0 commit comments