@create-markdown/react 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/dist/index.cjs CHANGED
@@ -1,520 +1 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropNames = Object.getOwnPropertyNames;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __moduleCache = /* @__PURE__ */ new WeakMap;
6
- var __toCommonJS = (from) => {
7
- var entry = __moduleCache.get(from), desc;
8
- if (entry)
9
- return entry;
10
- entry = __defProp({}, "__esModule", { value: true });
11
- if (from && typeof from === "object" || typeof from === "function")
12
- __getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
13
- get: () => from[key],
14
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
- }));
16
- __moduleCache.set(from, entry);
17
- return entry;
18
- };
19
- var __export = (target, all) => {
20
- for (var name in all)
21
- __defProp(target, name, {
22
- get: all[name],
23
- enumerable: true,
24
- configurable: true,
25
- set: (newValue) => all[name] = () => newValue
26
- });
27
- };
28
-
29
- // src/index.ts
30
- var exports_src = {};
31
- __export(exports_src, {
32
- useMarkdown: () => useMarkdown,
33
- useDocument: () => useDocument,
34
- useBlockEditor: () => useBlockEditor,
35
- text: () => import_core2.text,
36
- paragraph: () => import_core2.paragraph,
37
- numberedList: () => import_core2.numberedList,
38
- link: () => import_core2.link,
39
- italic: () => import_core2.italic,
40
- image: () => import_core2.image,
41
- heading: () => import_core2.heading,
42
- h6: () => import_core2.h6,
43
- h5: () => import_core2.h5,
44
- h4: () => import_core2.h4,
45
- h3: () => import_core2.h3,
46
- h2: () => import_core2.h2,
47
- h1: () => import_core2.h1,
48
- divider: () => import_core2.divider,
49
- codeBlock: () => import_core2.codeBlock,
50
- code: () => import_core2.code,
51
- checkListItem: () => import_core2.checkListItem,
52
- checkList: () => import_core2.checkList,
53
- callout: () => import_core2.callout,
54
- bulletList: () => import_core2.bulletList,
55
- bold: () => import_core2.bold,
56
- blockquote: () => import_core2.blockquote,
57
- VERSION: () => VERSION,
58
- InlineContent: () => InlineContent,
59
- BlockRenderer: () => BlockRenderer,
60
- BlockElement: () => BlockElement
61
- });
62
- module.exports = __toCommonJS(exports_src);
63
-
64
- // src/block-renderer.tsx
65
- var jsx_dev_runtime = require("react/jsx-dev-runtime");
66
- function BlockRenderer({
67
- blocks,
68
- className,
69
- customRenderers
70
- }) {
71
- return /* @__PURE__ */ jsx_dev_runtime.jsxDEV("div", {
72
- className,
73
- children: blocks.map((block) => /* @__PURE__ */ jsx_dev_runtime.jsxDEV(BlockElement, {
74
- block,
75
- customRenderers
76
- }, block.id, false, undefined, this))
77
- }, undefined, false, undefined, this);
78
- }
79
- function BlockElement({
80
- block,
81
- customRenderers
82
- }) {
83
- const CustomRenderer = customRenderers?.[block.type];
84
- if (CustomRenderer) {
85
- return /* @__PURE__ */ jsx_dev_runtime.jsxDEV(CustomRenderer, {
86
- block
87
- }, undefined, false, undefined, this);
88
- }
89
- switch (block.type) {
90
- case "paragraph":
91
- return /* @__PURE__ */ jsx_dev_runtime.jsxDEV(ParagraphRenderer, {
92
- block
93
- }, undefined, false, undefined, this);
94
- case "heading":
95
- return /* @__PURE__ */ jsx_dev_runtime.jsxDEV(HeadingRenderer, {
96
- block
97
- }, undefined, false, undefined, this);
98
- case "bulletList":
99
- return /* @__PURE__ */ jsx_dev_runtime.jsxDEV(BulletListRenderer, {
100
- block,
101
- customRenderers
102
- }, undefined, false, undefined, this);
103
- case "numberedList":
104
- return /* @__PURE__ */ jsx_dev_runtime.jsxDEV(NumberedListRenderer, {
105
- block,
106
- customRenderers
107
- }, undefined, false, undefined, this);
108
- case "checkList":
109
- return /* @__PURE__ */ jsx_dev_runtime.jsxDEV(CheckListRenderer, {
110
- block
111
- }, undefined, false, undefined, this);
112
- case "codeBlock":
113
- return /* @__PURE__ */ jsx_dev_runtime.jsxDEV(CodeBlockRenderer, {
114
- block
115
- }, undefined, false, undefined, this);
116
- case "blockquote":
117
- return /* @__PURE__ */ jsx_dev_runtime.jsxDEV(BlockquoteRenderer, {
118
- block
119
- }, undefined, false, undefined, this);
120
- case "table":
121
- return /* @__PURE__ */ jsx_dev_runtime.jsxDEV(TableRenderer, {
122
- block
123
- }, undefined, false, undefined, this);
124
- case "image":
125
- return /* @__PURE__ */ jsx_dev_runtime.jsxDEV(ImageRenderer, {
126
- block
127
- }, undefined, false, undefined, this);
128
- case "divider":
129
- return /* @__PURE__ */ jsx_dev_runtime.jsxDEV(DividerRenderer, {}, undefined, false, undefined, this);
130
- case "callout":
131
- return /* @__PURE__ */ jsx_dev_runtime.jsxDEV(CalloutRenderer, {
132
- block
133
- }, undefined, false, undefined, this);
134
- default:
135
- return null;
136
- }
137
- }
138
- function InlineContent({ spans }) {
139
- return /* @__PURE__ */ jsx_dev_runtime.jsxDEV(jsx_dev_runtime.Fragment, {
140
- children: spans.map((span, index) => /* @__PURE__ */ jsx_dev_runtime.jsxDEV(SpanElement, {
141
- span
142
- }, index, false, undefined, this))
143
- }, undefined, false, undefined, this);
144
- }
145
- function SpanElement({ span }) {
146
- let content = span.text;
147
- const styles = span.styles;
148
- if (styles.code) {
149
- content = /* @__PURE__ */ jsx_dev_runtime.jsxDEV("code", {
150
- children: content
151
- }, undefined, false, undefined, this);
152
- }
153
- if (styles.highlight) {
154
- content = /* @__PURE__ */ jsx_dev_runtime.jsxDEV("mark", {
155
- children: content
156
- }, undefined, false, undefined, this);
157
- }
158
- if (styles.strikethrough) {
159
- content = /* @__PURE__ */ jsx_dev_runtime.jsxDEV("del", {
160
- children: content
161
- }, undefined, false, undefined, this);
162
- }
163
- if (styles.underline) {
164
- content = /* @__PURE__ */ jsx_dev_runtime.jsxDEV("u", {
165
- children: content
166
- }, undefined, false, undefined, this);
167
- }
168
- if (styles.italic) {
169
- content = /* @__PURE__ */ jsx_dev_runtime.jsxDEV("em", {
170
- children: content
171
- }, undefined, false, undefined, this);
172
- }
173
- if (styles.bold) {
174
- content = /* @__PURE__ */ jsx_dev_runtime.jsxDEV("strong", {
175
- children: content
176
- }, undefined, false, undefined, this);
177
- }
178
- if (styles.link) {
179
- content = /* @__PURE__ */ jsx_dev_runtime.jsxDEV("a", {
180
- href: styles.link.url,
181
- title: styles.link.title,
182
- children: content
183
- }, undefined, false, undefined, this);
184
- }
185
- return /* @__PURE__ */ jsx_dev_runtime.jsxDEV(jsx_dev_runtime.Fragment, {
186
- children: content
187
- }, undefined, false, undefined, this);
188
- }
189
- function ParagraphRenderer({ block }) {
190
- return /* @__PURE__ */ jsx_dev_runtime.jsxDEV("p", {
191
- children: /* @__PURE__ */ jsx_dev_runtime.jsxDEV(InlineContent, {
192
- spans: block.content
193
- }, undefined, false, undefined, this)
194
- }, undefined, false, undefined, this);
195
- }
196
- function HeadingRenderer({ block }) {
197
- const Tag = `h${block.props.level}`;
198
- return /* @__PURE__ */ jsx_dev_runtime.jsxDEV(Tag, {
199
- children: /* @__PURE__ */ jsx_dev_runtime.jsxDEV(InlineContent, {
200
- spans: block.content
201
- }, undefined, false, undefined, this)
202
- }, undefined, false, undefined, this);
203
- }
204
- function BulletListRenderer({
205
- block,
206
- customRenderers
207
- }) {
208
- return /* @__PURE__ */ jsx_dev_runtime.jsxDEV("ul", {
209
- children: block.children.map((child) => /* @__PURE__ */ jsx_dev_runtime.jsxDEV("li", {
210
- children: [
211
- /* @__PURE__ */ jsx_dev_runtime.jsxDEV(InlineContent, {
212
- spans: child.content
213
- }, undefined, false, undefined, this),
214
- child.children.length > 0 && /* @__PURE__ */ jsx_dev_runtime.jsxDEV(BlockElement, {
215
- block: child,
216
- customRenderers
217
- }, undefined, false, undefined, this)
218
- ]
219
- }, child.id, true, undefined, this))
220
- }, undefined, false, undefined, this);
221
- }
222
- function NumberedListRenderer({
223
- block,
224
- customRenderers
225
- }) {
226
- return /* @__PURE__ */ jsx_dev_runtime.jsxDEV("ol", {
227
- children: block.children.map((child) => /* @__PURE__ */ jsx_dev_runtime.jsxDEV("li", {
228
- children: [
229
- /* @__PURE__ */ jsx_dev_runtime.jsxDEV(InlineContent, {
230
- spans: child.content
231
- }, undefined, false, undefined, this),
232
- child.children.length > 0 && /* @__PURE__ */ jsx_dev_runtime.jsxDEV(BlockElement, {
233
- block: child,
234
- customRenderers
235
- }, undefined, false, undefined, this)
236
- ]
237
- }, child.id, true, undefined, this))
238
- }, undefined, false, undefined, this);
239
- }
240
- function CheckListRenderer({ block }) {
241
- return /* @__PURE__ */ jsx_dev_runtime.jsxDEV("div", {
242
- style: { display: "flex", alignItems: "flex-start", gap: "0.5rem" },
243
- children: [
244
- /* @__PURE__ */ jsx_dev_runtime.jsxDEV("input", {
245
- type: "checkbox",
246
- checked: block.props.checked,
247
- readOnly: true,
248
- style: { marginTop: "0.25rem" }
249
- }, undefined, false, undefined, this),
250
- /* @__PURE__ */ jsx_dev_runtime.jsxDEV("span", {
251
- style: { textDecoration: block.props.checked ? "line-through" : "none" },
252
- children: /* @__PURE__ */ jsx_dev_runtime.jsxDEV(InlineContent, {
253
- spans: block.content
254
- }, undefined, false, undefined, this)
255
- }, undefined, false, undefined, this)
256
- ]
257
- }, undefined, true, undefined, this);
258
- }
259
- function CodeBlockRenderer({ block }) {
260
- const code = block.content.map((span) => span.text).join("");
261
- const language = block.props.language;
262
- return /* @__PURE__ */ jsx_dev_runtime.jsxDEV("pre", {
263
- children: /* @__PURE__ */ jsx_dev_runtime.jsxDEV("code", {
264
- className: language ? `language-${language}` : undefined,
265
- children: code
266
- }, undefined, false, undefined, this)
267
- }, undefined, false, undefined, this);
268
- }
269
- function BlockquoteRenderer({ block }) {
270
- return /* @__PURE__ */ jsx_dev_runtime.jsxDEV("blockquote", {
271
- children: /* @__PURE__ */ jsx_dev_runtime.jsxDEV(InlineContent, {
272
- spans: block.content
273
- }, undefined, false, undefined, this)
274
- }, undefined, false, undefined, this);
275
- }
276
- function TableRenderer({ block }) {
277
- const { headers, rows, alignments } = block.props;
278
- const getAlignment = (index) => {
279
- return alignments?.[index] ?? undefined;
280
- };
281
- return /* @__PURE__ */ jsx_dev_runtime.jsxDEV("table", {
282
- children: [
283
- headers.length > 0 && /* @__PURE__ */ jsx_dev_runtime.jsxDEV("thead", {
284
- children: /* @__PURE__ */ jsx_dev_runtime.jsxDEV("tr", {
285
- children: headers.map((header, i) => /* @__PURE__ */ jsx_dev_runtime.jsxDEV("th", {
286
- style: { textAlign: getAlignment(i) },
287
- children: header
288
- }, i, false, undefined, this))
289
- }, undefined, false, undefined, this)
290
- }, undefined, false, undefined, this),
291
- /* @__PURE__ */ jsx_dev_runtime.jsxDEV("tbody", {
292
- children: rows.map((row, rowIndex) => /* @__PURE__ */ jsx_dev_runtime.jsxDEV("tr", {
293
- children: row.map((cell, cellIndex) => /* @__PURE__ */ jsx_dev_runtime.jsxDEV("td", {
294
- style: { textAlign: getAlignment(cellIndex) },
295
- children: cell
296
- }, cellIndex, false, undefined, this))
297
- }, rowIndex, false, undefined, this))
298
- }, undefined, false, undefined, this)
299
- ]
300
- }, undefined, true, undefined, this);
301
- }
302
- function ImageRenderer({ block }) {
303
- return /* @__PURE__ */ jsx_dev_runtime.jsxDEV("figure", {
304
- children: [
305
- /* @__PURE__ */ jsx_dev_runtime.jsxDEV("img", {
306
- src: block.props.url,
307
- alt: block.props.alt ?? "",
308
- title: block.props.title,
309
- width: block.props.width,
310
- height: block.props.height
311
- }, undefined, false, undefined, this),
312
- block.props.alt && /* @__PURE__ */ jsx_dev_runtime.jsxDEV("figcaption", {
313
- children: block.props.alt
314
- }, undefined, false, undefined, this)
315
- ]
316
- }, undefined, true, undefined, this);
317
- }
318
- function DividerRenderer() {
319
- return /* @__PURE__ */ jsx_dev_runtime.jsxDEV("hr", {}, undefined, false, undefined, this);
320
- }
321
- function CalloutRenderer({ block }) {
322
- const calloutType = block.props.type;
323
- const styles = {
324
- padding: "1rem",
325
- borderRadius: "0.25rem",
326
- borderLeft: "4px solid",
327
- marginBottom: "1rem"
328
- };
329
- const colors = {
330
- info: { borderColor: "#3b82f6", backgroundColor: "#eff6ff" },
331
- warning: { borderColor: "#f59e0b", backgroundColor: "#fffbeb" },
332
- tip: { borderColor: "#10b981", backgroundColor: "#ecfdf5" },
333
- danger: { borderColor: "#ef4444", backgroundColor: "#fef2f2" },
334
- note: { borderColor: "#6b7280", backgroundColor: "#f9fafb" }
335
- };
336
- const colorStyle = colors[calloutType] ?? colors.note;
337
- return /* @__PURE__ */ jsx_dev_runtime.jsxDEV("div", {
338
- style: {
339
- ...styles,
340
- borderLeftColor: colorStyle.borderColor,
341
- backgroundColor: colorStyle.backgroundColor
342
- },
343
- role: "alert",
344
- children: [
345
- /* @__PURE__ */ jsx_dev_runtime.jsxDEV("strong", {
346
- style: { textTransform: "capitalize", display: "block", marginBottom: "0.5rem" },
347
- children: calloutType
348
- }, undefined, false, undefined, this),
349
- /* @__PURE__ */ jsx_dev_runtime.jsxDEV(InlineContent, {
350
- spans: block.content
351
- }, undefined, false, undefined, this)
352
- ]
353
- }, undefined, true, undefined, this);
354
- }
355
- // src/hooks.ts
356
- var import_react = require("react");
357
- var import_core = require("@create-markdown/core");
358
- function useDocument(initialBlocks = [], options) {
359
- const [document, setDocument] = import_react.useState(() => import_core.createDocument(initialBlocks, options));
360
- const insert = import_react.useCallback((block, index) => {
361
- setDocument((doc) => import_core.insertBlock(doc, block, index));
362
- }, []);
363
- const append = import_react.useCallback((block) => {
364
- setDocument((doc) => import_core.appendBlock(doc, block));
365
- }, []);
366
- const remove = import_react.useCallback((blockId) => {
367
- setDocument((doc) => import_core.removeBlock(doc, blockId));
368
- }, []);
369
- const update = import_react.useCallback((blockId, updates) => {
370
- setDocument((doc) => import_core.updateBlock(doc, blockId, updates));
371
- }, []);
372
- const move = import_react.useCallback((blockId, newIndex) => {
373
- setDocument((doc) => import_core.moveBlock(doc, blockId, newIndex));
374
- }, []);
375
- const find = import_react.useCallback((blockId) => import_core.findBlock(document, blockId), [document]);
376
- const getIndex = import_react.useCallback((blockId) => import_core.getBlockIndex(document, blockId), [document]);
377
- const clear = import_react.useCallback(() => {
378
- setDocument((doc) => import_core.clearBlocks(doc));
379
- }, []);
380
- const set = import_react.useCallback((blocks) => {
381
- setDocument((doc) => import_core.setBlocks(doc, blocks));
382
- }, []);
383
- const toMd = import_react.useCallback(() => import_core.blocksToMarkdown(document.blocks), [document]);
384
- const fromMd = import_react.useCallback((markdown) => {
385
- const blocks = import_core.markdownToBlocks(markdown);
386
- setDocument((doc) => import_core.setBlocks(doc, blocks));
387
- }, []);
388
- const meta = import_react.useCallback((newMeta) => {
389
- setDocument((doc) => import_core.updateMeta(doc, newMeta));
390
- }, []);
391
- return {
392
- document,
393
- blocks: document.blocks,
394
- insertBlock: insert,
395
- appendBlock: append,
396
- removeBlock: remove,
397
- updateBlock: update,
398
- moveBlock: move,
399
- findBlock: find,
400
- getBlockIndex: getIndex,
401
- clearBlocks: clear,
402
- setBlocks: set,
403
- setDocument,
404
- toMarkdown: toMd,
405
- fromMarkdown: fromMd,
406
- updateMeta: meta
407
- };
408
- }
409
- function useMarkdown(initialMarkdown = "") {
410
- const [markdown, setMarkdownState] = import_react.useState(initialMarkdown);
411
- const blocks = import_react.useMemo(() => import_core.markdownToBlocks(markdown), [markdown]);
412
- const setMarkdown = import_react.useCallback((newMarkdown) => {
413
- setMarkdownState(newMarkdown);
414
- }, []);
415
- const setBlocksFromBlocks = import_react.useCallback((newBlocks) => {
416
- const newMarkdown = import_core.blocksToMarkdown(newBlocks);
417
- setMarkdownState(newMarkdown);
418
- }, []);
419
- return {
420
- markdown,
421
- blocks,
422
- setMarkdown,
423
- setBlocks: setBlocksFromBlocks
424
- };
425
- }
426
- function useBlockEditor(documentHook) {
427
- const { document, blocks, removeBlock: removeBlock2, updateBlock: updateBlock2, moveBlock: moveBlock2, insertBlock: insertBlock2 } = documentHook;
428
- const [selectedBlockId, setSelectedBlockId] = import_react.useState(null);
429
- const selectedBlock = import_react.useMemo(() => selectedBlockId ? import_core.findBlock(document, selectedBlockId) : undefined, [document, selectedBlockId]);
430
- const selectBlock = import_react.useCallback((blockId) => {
431
- setSelectedBlockId(blockId);
432
- }, []);
433
- const selectNext = import_react.useCallback(() => {
434
- if (!selectedBlockId) {
435
- if (blocks.length > 0) {
436
- setSelectedBlockId(blocks[0].id);
437
- }
438
- return;
439
- }
440
- const currentIndex = import_core.getBlockIndex(document, selectedBlockId);
441
- if (currentIndex < blocks.length - 1) {
442
- setSelectedBlockId(blocks[currentIndex + 1].id);
443
- }
444
- }, [document, blocks, selectedBlockId]);
445
- const selectPrevious = import_react.useCallback(() => {
446
- if (!selectedBlockId) {
447
- if (blocks.length > 0) {
448
- setSelectedBlockId(blocks[blocks.length - 1].id);
449
- }
450
- return;
451
- }
452
- const currentIndex = import_core.getBlockIndex(document, selectedBlockId);
453
- if (currentIndex > 0) {
454
- setSelectedBlockId(blocks[currentIndex - 1].id);
455
- }
456
- }, [document, blocks, selectedBlockId]);
457
- const deleteSelected = import_react.useCallback(() => {
458
- if (!selectedBlockId)
459
- return;
460
- const currentIndex = import_core.getBlockIndex(document, selectedBlockId);
461
- removeBlock2(selectedBlockId);
462
- if (blocks.length > 1) {
463
- if (currentIndex < blocks.length - 1) {
464
- setSelectedBlockId(blocks[currentIndex + 1].id);
465
- } else if (currentIndex > 0) {
466
- setSelectedBlockId(blocks[currentIndex - 1].id);
467
- }
468
- } else {
469
- setSelectedBlockId(null);
470
- }
471
- }, [document, blocks, selectedBlockId, removeBlock2]);
472
- const updateSelectedContent = import_react.useCallback((content) => {
473
- if (!selectedBlockId)
474
- return;
475
- updateBlock2(selectedBlockId, { content });
476
- }, [selectedBlockId, updateBlock2]);
477
- const duplicateSelected = import_react.useCallback(() => {
478
- if (!selectedBlockId || !selectedBlock)
479
- return;
480
- const currentIndex = import_core.getBlockIndex(document, selectedBlockId);
481
- const clonedBlock = {
482
- ...selectedBlock,
483
- id: Math.random().toString(36).substring(2, 9)
484
- };
485
- insertBlock2(clonedBlock, currentIndex + 1);
486
- setSelectedBlockId(clonedBlock.id);
487
- }, [document, selectedBlockId, selectedBlock, insertBlock2]);
488
- const moveSelectedUp = import_react.useCallback(() => {
489
- if (!selectedBlockId)
490
- return;
491
- const currentIndex = import_core.getBlockIndex(document, selectedBlockId);
492
- if (currentIndex > 0) {
493
- moveBlock2(selectedBlockId, currentIndex - 1);
494
- }
495
- }, [document, selectedBlockId, moveBlock2]);
496
- const moveSelectedDown = import_react.useCallback(() => {
497
- if (!selectedBlockId)
498
- return;
499
- const currentIndex = import_core.getBlockIndex(document, selectedBlockId);
500
- if (currentIndex < blocks.length - 1) {
501
- moveBlock2(selectedBlockId, currentIndex + 1);
502
- }
503
- }, [document, blocks, selectedBlockId, moveBlock2]);
504
- return {
505
- selectedBlockId,
506
- selectedBlock,
507
- selectBlock,
508
- selectNext,
509
- selectPrevious,
510
- deleteSelected,
511
- updateSelectedContent,
512
- duplicateSelected,
513
- moveSelectedUp,
514
- moveSelectedDown
515
- };
516
- }
517
-
518
- // src/index.ts
519
- var import_core2 = require("@create-markdown/core");
520
- var VERSION = "0.1.0";
1
+ var{defineProperty:y,getOwnPropertyNames:p,getOwnPropertyDescriptor:w}=Object,C=Object.prototype.hasOwnProperty;var M=new WeakMap,j=(J)=>{var z=M.get(J),G;if(z)return z;if(z=y({},"__esModule",{value:!0}),J&&typeof J==="object"||typeof J==="function")p(J).map((W)=>!C.call(z,W)&&y(z,W,{get:()=>J[W],enumerable:!(G=w(J,W))||G.enumerable}));return M.set(J,z),z};var i=(J,z)=>{for(var G in z)y(J,G,{get:z[G],enumerable:!0,configurable:!0,set:(W)=>z[G]=()=>W})};var o={};i(o,{useMarkdown:()=>g,useDocument:()=>S,useBlockEditor:()=>R,text:()=>O.text,paragraph:()=>O.paragraph,numberedList:()=>O.numberedList,link:()=>O.link,italic:()=>O.italic,image:()=>O.image,heading:()=>O.heading,h6:()=>O.h6,h5:()=>O.h5,h4:()=>O.h4,h3:()=>O.h3,h2:()=>O.h2,h1:()=>O.h1,divider:()=>O.divider,codeBlock:()=>O.codeBlock,code:()=>O.code,checkListItem:()=>O.checkListItem,checkList:()=>O.checkList,callout:()=>O.callout,bulletList:()=>O.bulletList,bold:()=>O.bold,blockquote:()=>O.blockquote,VERSION:()=>t,InlineContent:()=>F,BlockRenderer:()=>h,BlockElement:()=>L});module.exports=j(o);var K=require("react/jsx-runtime");function h({blocks:J,className:z,customRenderers:G}){return K.jsx("div",{className:z,children:J.map((W)=>K.jsx(L,{block:W,customRenderers:G},W.id))})}function L({block:J,customRenderers:z}){let G=z?.[J.type];if(G)return K.jsx(G,{block:J});switch(J.type){case"paragraph":return K.jsx(I,{block:J});case"heading":return K.jsx(m,{block:J});case"bulletList":return K.jsx(x,{block:J,customRenderers:z});case"numberedList":return K.jsx(u,{block:J,customRenderers:z});case"checkList":return K.jsx(a,{block:J});case"codeBlock":return K.jsx(b,{block:J});case"blockquote":return K.jsx(r,{block:J});case"table":return K.jsx(k,{block:J});case"image":return K.jsx(n,{block:J});case"divider":return K.jsx(d,{});case"callout":return K.jsx(s,{block:J});default:return null}}function F({spans:J}){return K.jsx(K.Fragment,{children:J.map((z,G)=>K.jsx(B,{span:z},G))})}function B({span:J}){let{text:z,styles:G}=J;if(G.code)z=K.jsx("code",{children:z});if(G.highlight)z=K.jsx("mark",{children:z});if(G.strikethrough)z=K.jsx("del",{children:z});if(G.underline)z=K.jsx("u",{children:z});if(G.italic)z=K.jsx("em",{children:z});if(G.bold)z=K.jsx("strong",{children:z});if(G.link)z=K.jsx("a",{href:G.link.url,title:G.link.title,children:z});return K.jsx(K.Fragment,{children:z})}function I({block:J}){return K.jsx("p",{children:K.jsx(F,{spans:J.content})})}function m({block:J}){let z=`h${J.props.level}`;return K.jsx(z,{children:K.jsx(F,{spans:J.content})})}function x({block:J,customRenderers:z}){return K.jsx("ul",{children:J.children.map((G)=>K.jsxs("li",{children:[K.jsx(F,{spans:G.content}),G.children.length>0&&K.jsx(L,{block:G,customRenderers:z})]},G.id))})}function u({block:J,customRenderers:z}){return K.jsx("ol",{children:J.children.map((G)=>K.jsxs("li",{children:[K.jsx(F,{spans:G.content}),G.children.length>0&&K.jsx(L,{block:G,customRenderers:z})]},G.id))})}function a({block:J}){return K.jsxs("div",{style:{display:"flex",alignItems:"flex-start",gap:"0.5rem"},children:[K.jsx("input",{type:"checkbox",checked:J.props.checked,readOnly:!0,style:{marginTop:"0.25rem"}}),K.jsx("span",{style:{textDecoration:J.props.checked?"line-through":"none"},children:K.jsx(F,{spans:J.content})})]})}function b({block:J}){let z=J.content.map((W)=>W.text).join(""),G=J.props.language;return K.jsx("pre",{children:K.jsx("code",{className:G?`language-${G}`:void 0,children:z})})}function r({block:J}){return K.jsx("blockquote",{children:K.jsx(F,{spans:J.content})})}function k({block:J}){let{headers:z,rows:G,alignments:W}=J.props,A=($)=>{return W?.[$]??void 0};return K.jsxs("table",{children:[z.length>0&&K.jsx("thead",{children:K.jsx("tr",{children:z.map(($,U)=>K.jsx("th",{style:{textAlign:A(U)},children:$},U))})}),K.jsx("tbody",{children:G.map(($,U)=>K.jsx("tr",{children:$.map((V,q)=>K.jsx("td",{style:{textAlign:A(q)},children:V},q))},U))})]})}function n({block:J}){return K.jsxs("figure",{children:[K.jsx("img",{src:J.props.url,alt:J.props.alt??"",title:J.props.title,width:J.props.width,height:J.props.height}),J.props.alt&&K.jsx("figcaption",{children:J.props.alt})]})}function d(){return K.jsx("hr",{})}function s({block:J}){let z=J.props.type,G={padding:"1rem",borderRadius:"0.25rem",borderLeft:"4px solid",marginBottom:"1rem"},W={info:{borderColor:"#3b82f6",backgroundColor:"#eff6ff"},warning:{borderColor:"#f59e0b",backgroundColor:"#fffbeb"},tip:{borderColor:"#10b981",backgroundColor:"#ecfdf5"},danger:{borderColor:"#ef4444",backgroundColor:"#fef2f2"},note:{borderColor:"#6b7280",backgroundColor:"#f9fafb"}},A=W[z]??W.note;return K.jsxs("div",{style:{...G,borderLeftColor:A.borderColor,backgroundColor:A.backgroundColor},role:"alert",children:[K.jsx("strong",{style:{textTransform:"capitalize",display:"block",marginBottom:"0.5rem"},children:z}),K.jsx(F,{spans:J.content})]})}var Y=require("react"),Q=require("@create-markdown/core");function S(J=[],z){let[G,W]=Y.useState(()=>Q.createDocument(J,z)),A=Y.useCallback((Z,_)=>{W((X)=>Q.insertBlock(X,Z,_))},[]),$=Y.useCallback((Z)=>{W((_)=>Q.appendBlock(_,Z))},[]),U=Y.useCallback((Z)=>{W((_)=>Q.removeBlock(_,Z))},[]),V=Y.useCallback((Z,_)=>{W((X)=>Q.updateBlock(X,Z,_))},[]),q=Y.useCallback((Z,_)=>{W((X)=>Q.moveBlock(X,Z,_))},[]),H=Y.useCallback((Z)=>Q.findBlock(G,Z),[G]),P=Y.useCallback((Z)=>Q.getBlockIndex(G,Z),[G]),E=Y.useCallback(()=>{W((Z)=>Q.clearBlocks(Z))},[]),N=Y.useCallback((Z)=>{W((_)=>Q.setBlocks(_,Z))},[]),f=Y.useCallback(()=>Q.blocksToMarkdown(G.blocks),[G]),T=Y.useCallback((Z)=>{let _=Q.markdownToBlocks(Z);W((X)=>Q.setBlocks(X,_))},[]),D=Y.useCallback((Z)=>{W((_)=>Q.updateMeta(_,Z))},[]);return{document:G,blocks:G.blocks,insertBlock:A,appendBlock:$,removeBlock:U,updateBlock:V,moveBlock:q,findBlock:H,getBlockIndex:P,clearBlocks:E,setBlocks:N,setDocument:W,toMarkdown:f,fromMarkdown:T,updateMeta:D}}function g(J=""){let[z,G]=Y.useState(J),W=Y.useMemo(()=>Q.markdownToBlocks(z),[z]),A=Y.useCallback((U)=>{G(U)},[]),$=Y.useCallback((U)=>{let V=Q.blocksToMarkdown(U);G(V)},[]);return{markdown:z,blocks:W,setMarkdown:A,setBlocks:$}}function R(J){let{document:z,blocks:G,removeBlock:W,updateBlock:A,moveBlock:$,insertBlock:U}=J,[V,q]=Y.useState(null),H=Y.useMemo(()=>V?Q.findBlock(z,V):void 0,[z,V]),P=Y.useCallback((X)=>{q(X)},[]),E=Y.useCallback(()=>{if(!V){if(G.length>0)q(G[0].id);return}let X=Q.getBlockIndex(z,V);if(X<G.length-1)q(G[X+1].id)},[z,G,V]),N=Y.useCallback(()=>{if(!V){if(G.length>0)q(G[G.length-1].id);return}let X=Q.getBlockIndex(z,V);if(X>0)q(G[X-1].id)},[z,G,V]),f=Y.useCallback(()=>{if(!V)return;let X=Q.getBlockIndex(z,V);if(W(V),G.length>1){if(X<G.length-1)q(G[X+1].id);else if(X>0)q(G[X-1].id)}else q(null)},[z,G,V,W]),T=Y.useCallback((X)=>{if(!V)return;A(V,{content:X})},[V,A]),D=Y.useCallback(()=>{if(!V||!H)return;let X=Q.getBlockIndex(z,V),v={...H,id:Math.random().toString(36).substring(2,9)};U(v,X+1),q(v.id)},[z,V,H,U]),Z=Y.useCallback(()=>{if(!V)return;let X=Q.getBlockIndex(z,V);if(X>0)$(V,X-1)},[z,V,$]),_=Y.useCallback(()=>{if(!V)return;let X=Q.getBlockIndex(z,V);if(X<G.length-1)$(V,X+1)},[z,G,V,$]);return{selectedBlockId:V,selectedBlock:H,selectBlock:P,selectNext:E,selectPrevious:N,deleteSelected:f,updateSelectedContent:T,duplicateSelected:D,moveSelectedUp:Z,moveSelectedDown:_}}var O=require("@create-markdown/core"),t="0.2.0";
package/dist/index.d.ts CHANGED
@@ -11,5 +11,5 @@ export { paragraph, heading, h1, h2, h3, h4, h5, h6, bulletList, numberedList, c
11
11
  /**
12
12
  * Package version
13
13
  */
14
- export declare const VERSION = "0.1.0";
14
+ export declare const VERSION = "0.2.0";
15
15
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,525 +1 @@
1
- // src/block-renderer.tsx
2
- import { jsxDEV, Fragment } from "react/jsx-dev-runtime";
3
- function BlockRenderer({
4
- blocks,
5
- className,
6
- customRenderers
7
- }) {
8
- return /* @__PURE__ */ jsxDEV("div", {
9
- className,
10
- children: blocks.map((block) => /* @__PURE__ */ jsxDEV(BlockElement, {
11
- block,
12
- customRenderers
13
- }, block.id, false, undefined, this))
14
- }, undefined, false, undefined, this);
15
- }
16
- function BlockElement({
17
- block,
18
- customRenderers
19
- }) {
20
- const CustomRenderer = customRenderers?.[block.type];
21
- if (CustomRenderer) {
22
- return /* @__PURE__ */ jsxDEV(CustomRenderer, {
23
- block
24
- }, undefined, false, undefined, this);
25
- }
26
- switch (block.type) {
27
- case "paragraph":
28
- return /* @__PURE__ */ jsxDEV(ParagraphRenderer, {
29
- block
30
- }, undefined, false, undefined, this);
31
- case "heading":
32
- return /* @__PURE__ */ jsxDEV(HeadingRenderer, {
33
- block
34
- }, undefined, false, undefined, this);
35
- case "bulletList":
36
- return /* @__PURE__ */ jsxDEV(BulletListRenderer, {
37
- block,
38
- customRenderers
39
- }, undefined, false, undefined, this);
40
- case "numberedList":
41
- return /* @__PURE__ */ jsxDEV(NumberedListRenderer, {
42
- block,
43
- customRenderers
44
- }, undefined, false, undefined, this);
45
- case "checkList":
46
- return /* @__PURE__ */ jsxDEV(CheckListRenderer, {
47
- block
48
- }, undefined, false, undefined, this);
49
- case "codeBlock":
50
- return /* @__PURE__ */ jsxDEV(CodeBlockRenderer, {
51
- block
52
- }, undefined, false, undefined, this);
53
- case "blockquote":
54
- return /* @__PURE__ */ jsxDEV(BlockquoteRenderer, {
55
- block
56
- }, undefined, false, undefined, this);
57
- case "table":
58
- return /* @__PURE__ */ jsxDEV(TableRenderer, {
59
- block
60
- }, undefined, false, undefined, this);
61
- case "image":
62
- return /* @__PURE__ */ jsxDEV(ImageRenderer, {
63
- block
64
- }, undefined, false, undefined, this);
65
- case "divider":
66
- return /* @__PURE__ */ jsxDEV(DividerRenderer, {}, undefined, false, undefined, this);
67
- case "callout":
68
- return /* @__PURE__ */ jsxDEV(CalloutRenderer, {
69
- block
70
- }, undefined, false, undefined, this);
71
- default:
72
- return null;
73
- }
74
- }
75
- function InlineContent({ spans }) {
76
- return /* @__PURE__ */ jsxDEV(Fragment, {
77
- children: spans.map((span, index) => /* @__PURE__ */ jsxDEV(SpanElement, {
78
- span
79
- }, index, false, undefined, this))
80
- }, undefined, false, undefined, this);
81
- }
82
- function SpanElement({ span }) {
83
- let content = span.text;
84
- const styles = span.styles;
85
- if (styles.code) {
86
- content = /* @__PURE__ */ jsxDEV("code", {
87
- children: content
88
- }, undefined, false, undefined, this);
89
- }
90
- if (styles.highlight) {
91
- content = /* @__PURE__ */ jsxDEV("mark", {
92
- children: content
93
- }, undefined, false, undefined, this);
94
- }
95
- if (styles.strikethrough) {
96
- content = /* @__PURE__ */ jsxDEV("del", {
97
- children: content
98
- }, undefined, false, undefined, this);
99
- }
100
- if (styles.underline) {
101
- content = /* @__PURE__ */ jsxDEV("u", {
102
- children: content
103
- }, undefined, false, undefined, this);
104
- }
105
- if (styles.italic) {
106
- content = /* @__PURE__ */ jsxDEV("em", {
107
- children: content
108
- }, undefined, false, undefined, this);
109
- }
110
- if (styles.bold) {
111
- content = /* @__PURE__ */ jsxDEV("strong", {
112
- children: content
113
- }, undefined, false, undefined, this);
114
- }
115
- if (styles.link) {
116
- content = /* @__PURE__ */ jsxDEV("a", {
117
- href: styles.link.url,
118
- title: styles.link.title,
119
- children: content
120
- }, undefined, false, undefined, this);
121
- }
122
- return /* @__PURE__ */ jsxDEV(Fragment, {
123
- children: content
124
- }, undefined, false, undefined, this);
125
- }
126
- function ParagraphRenderer({ block }) {
127
- return /* @__PURE__ */ jsxDEV("p", {
128
- children: /* @__PURE__ */ jsxDEV(InlineContent, {
129
- spans: block.content
130
- }, undefined, false, undefined, this)
131
- }, undefined, false, undefined, this);
132
- }
133
- function HeadingRenderer({ block }) {
134
- const Tag = `h${block.props.level}`;
135
- return /* @__PURE__ */ jsxDEV(Tag, {
136
- children: /* @__PURE__ */ jsxDEV(InlineContent, {
137
- spans: block.content
138
- }, undefined, false, undefined, this)
139
- }, undefined, false, undefined, this);
140
- }
141
- function BulletListRenderer({
142
- block,
143
- customRenderers
144
- }) {
145
- return /* @__PURE__ */ jsxDEV("ul", {
146
- children: block.children.map((child) => /* @__PURE__ */ jsxDEV("li", {
147
- children: [
148
- /* @__PURE__ */ jsxDEV(InlineContent, {
149
- spans: child.content
150
- }, undefined, false, undefined, this),
151
- child.children.length > 0 && /* @__PURE__ */ jsxDEV(BlockElement, {
152
- block: child,
153
- customRenderers
154
- }, undefined, false, undefined, this)
155
- ]
156
- }, child.id, true, undefined, this))
157
- }, undefined, false, undefined, this);
158
- }
159
- function NumberedListRenderer({
160
- block,
161
- customRenderers
162
- }) {
163
- return /* @__PURE__ */ jsxDEV("ol", {
164
- children: block.children.map((child) => /* @__PURE__ */ jsxDEV("li", {
165
- children: [
166
- /* @__PURE__ */ jsxDEV(InlineContent, {
167
- spans: child.content
168
- }, undefined, false, undefined, this),
169
- child.children.length > 0 && /* @__PURE__ */ jsxDEV(BlockElement, {
170
- block: child,
171
- customRenderers
172
- }, undefined, false, undefined, this)
173
- ]
174
- }, child.id, true, undefined, this))
175
- }, undefined, false, undefined, this);
176
- }
177
- function CheckListRenderer({ block }) {
178
- return /* @__PURE__ */ jsxDEV("div", {
179
- style: { display: "flex", alignItems: "flex-start", gap: "0.5rem" },
180
- children: [
181
- /* @__PURE__ */ jsxDEV("input", {
182
- type: "checkbox",
183
- checked: block.props.checked,
184
- readOnly: true,
185
- style: { marginTop: "0.25rem" }
186
- }, undefined, false, undefined, this),
187
- /* @__PURE__ */ jsxDEV("span", {
188
- style: { textDecoration: block.props.checked ? "line-through" : "none" },
189
- children: /* @__PURE__ */ jsxDEV(InlineContent, {
190
- spans: block.content
191
- }, undefined, false, undefined, this)
192
- }, undefined, false, undefined, this)
193
- ]
194
- }, undefined, true, undefined, this);
195
- }
196
- function CodeBlockRenderer({ block }) {
197
- const code = block.content.map((span) => span.text).join("");
198
- const language = block.props.language;
199
- return /* @__PURE__ */ jsxDEV("pre", {
200
- children: /* @__PURE__ */ jsxDEV("code", {
201
- className: language ? `language-${language}` : undefined,
202
- children: code
203
- }, undefined, false, undefined, this)
204
- }, undefined, false, undefined, this);
205
- }
206
- function BlockquoteRenderer({ block }) {
207
- return /* @__PURE__ */ jsxDEV("blockquote", {
208
- children: /* @__PURE__ */ jsxDEV(InlineContent, {
209
- spans: block.content
210
- }, undefined, false, undefined, this)
211
- }, undefined, false, undefined, this);
212
- }
213
- function TableRenderer({ block }) {
214
- const { headers, rows, alignments } = block.props;
215
- const getAlignment = (index) => {
216
- return alignments?.[index] ?? undefined;
217
- };
218
- return /* @__PURE__ */ jsxDEV("table", {
219
- children: [
220
- headers.length > 0 && /* @__PURE__ */ jsxDEV("thead", {
221
- children: /* @__PURE__ */ jsxDEV("tr", {
222
- children: headers.map((header, i) => /* @__PURE__ */ jsxDEV("th", {
223
- style: { textAlign: getAlignment(i) },
224
- children: header
225
- }, i, false, undefined, this))
226
- }, undefined, false, undefined, this)
227
- }, undefined, false, undefined, this),
228
- /* @__PURE__ */ jsxDEV("tbody", {
229
- children: rows.map((row, rowIndex) => /* @__PURE__ */ jsxDEV("tr", {
230
- children: row.map((cell, cellIndex) => /* @__PURE__ */ jsxDEV("td", {
231
- style: { textAlign: getAlignment(cellIndex) },
232
- children: cell
233
- }, cellIndex, false, undefined, this))
234
- }, rowIndex, false, undefined, this))
235
- }, undefined, false, undefined, this)
236
- ]
237
- }, undefined, true, undefined, this);
238
- }
239
- function ImageRenderer({ block }) {
240
- return /* @__PURE__ */ jsxDEV("figure", {
241
- children: [
242
- /* @__PURE__ */ jsxDEV("img", {
243
- src: block.props.url,
244
- alt: block.props.alt ?? "",
245
- title: block.props.title,
246
- width: block.props.width,
247
- height: block.props.height
248
- }, undefined, false, undefined, this),
249
- block.props.alt && /* @__PURE__ */ jsxDEV("figcaption", {
250
- children: block.props.alt
251
- }, undefined, false, undefined, this)
252
- ]
253
- }, undefined, true, undefined, this);
254
- }
255
- function DividerRenderer() {
256
- return /* @__PURE__ */ jsxDEV("hr", {}, undefined, false, undefined, this);
257
- }
258
- function CalloutRenderer({ block }) {
259
- const calloutType = block.props.type;
260
- const styles = {
261
- padding: "1rem",
262
- borderRadius: "0.25rem",
263
- borderLeft: "4px solid",
264
- marginBottom: "1rem"
265
- };
266
- const colors = {
267
- info: { borderColor: "#3b82f6", backgroundColor: "#eff6ff" },
268
- warning: { borderColor: "#f59e0b", backgroundColor: "#fffbeb" },
269
- tip: { borderColor: "#10b981", backgroundColor: "#ecfdf5" },
270
- danger: { borderColor: "#ef4444", backgroundColor: "#fef2f2" },
271
- note: { borderColor: "#6b7280", backgroundColor: "#f9fafb" }
272
- };
273
- const colorStyle = colors[calloutType] ?? colors.note;
274
- return /* @__PURE__ */ jsxDEV("div", {
275
- style: {
276
- ...styles,
277
- borderLeftColor: colorStyle.borderColor,
278
- backgroundColor: colorStyle.backgroundColor
279
- },
280
- role: "alert",
281
- children: [
282
- /* @__PURE__ */ jsxDEV("strong", {
283
- style: { textTransform: "capitalize", display: "block", marginBottom: "0.5rem" },
284
- children: calloutType
285
- }, undefined, false, undefined, this),
286
- /* @__PURE__ */ jsxDEV(InlineContent, {
287
- spans: block.content
288
- }, undefined, false, undefined, this)
289
- ]
290
- }, undefined, true, undefined, this);
291
- }
292
- // src/hooks.ts
293
- import { useState, useCallback, useMemo } from "react";
294
- import {
295
- createDocument,
296
- insertBlock,
297
- appendBlock,
298
- removeBlock,
299
- updateBlock,
300
- moveBlock,
301
- findBlock,
302
- getBlockIndex,
303
- clearBlocks,
304
- setBlocks,
305
- updateMeta,
306
- markdownToBlocks,
307
- blocksToMarkdown
308
- } from "@create-markdown/core";
309
- function useDocument(initialBlocks = [], options) {
310
- const [document, setDocument] = useState(() => createDocument(initialBlocks, options));
311
- const insert = useCallback((block, index) => {
312
- setDocument((doc) => insertBlock(doc, block, index));
313
- }, []);
314
- const append = useCallback((block) => {
315
- setDocument((doc) => appendBlock(doc, block));
316
- }, []);
317
- const remove = useCallback((blockId) => {
318
- setDocument((doc) => removeBlock(doc, blockId));
319
- }, []);
320
- const update = useCallback((blockId, updates) => {
321
- setDocument((doc) => updateBlock(doc, blockId, updates));
322
- }, []);
323
- const move = useCallback((blockId, newIndex) => {
324
- setDocument((doc) => moveBlock(doc, blockId, newIndex));
325
- }, []);
326
- const find = useCallback((blockId) => findBlock(document, blockId), [document]);
327
- const getIndex = useCallback((blockId) => getBlockIndex(document, blockId), [document]);
328
- const clear = useCallback(() => {
329
- setDocument((doc) => clearBlocks(doc));
330
- }, []);
331
- const set = useCallback((blocks) => {
332
- setDocument((doc) => setBlocks(doc, blocks));
333
- }, []);
334
- const toMd = useCallback(() => blocksToMarkdown(document.blocks), [document]);
335
- const fromMd = useCallback((markdown) => {
336
- const blocks = markdownToBlocks(markdown);
337
- setDocument((doc) => setBlocks(doc, blocks));
338
- }, []);
339
- const meta = useCallback((newMeta) => {
340
- setDocument((doc) => updateMeta(doc, newMeta));
341
- }, []);
342
- return {
343
- document,
344
- blocks: document.blocks,
345
- insertBlock: insert,
346
- appendBlock: append,
347
- removeBlock: remove,
348
- updateBlock: update,
349
- moveBlock: move,
350
- findBlock: find,
351
- getBlockIndex: getIndex,
352
- clearBlocks: clear,
353
- setBlocks: set,
354
- setDocument,
355
- toMarkdown: toMd,
356
- fromMarkdown: fromMd,
357
- updateMeta: meta
358
- };
359
- }
360
- function useMarkdown(initialMarkdown = "") {
361
- const [markdown, setMarkdownState] = useState(initialMarkdown);
362
- const blocks = useMemo(() => markdownToBlocks(markdown), [markdown]);
363
- const setMarkdown = useCallback((newMarkdown) => {
364
- setMarkdownState(newMarkdown);
365
- }, []);
366
- const setBlocksFromBlocks = useCallback((newBlocks) => {
367
- const newMarkdown = blocksToMarkdown(newBlocks);
368
- setMarkdownState(newMarkdown);
369
- }, []);
370
- return {
371
- markdown,
372
- blocks,
373
- setMarkdown,
374
- setBlocks: setBlocksFromBlocks
375
- };
376
- }
377
- function useBlockEditor(documentHook) {
378
- const { document, blocks, removeBlock: removeBlock2, updateBlock: updateBlock2, moveBlock: moveBlock2, insertBlock: insertBlock2 } = documentHook;
379
- const [selectedBlockId, setSelectedBlockId] = useState(null);
380
- const selectedBlock = useMemo(() => selectedBlockId ? findBlock(document, selectedBlockId) : undefined, [document, selectedBlockId]);
381
- const selectBlock = useCallback((blockId) => {
382
- setSelectedBlockId(blockId);
383
- }, []);
384
- const selectNext = useCallback(() => {
385
- if (!selectedBlockId) {
386
- if (blocks.length > 0) {
387
- setSelectedBlockId(blocks[0].id);
388
- }
389
- return;
390
- }
391
- const currentIndex = getBlockIndex(document, selectedBlockId);
392
- if (currentIndex < blocks.length - 1) {
393
- setSelectedBlockId(blocks[currentIndex + 1].id);
394
- }
395
- }, [document, blocks, selectedBlockId]);
396
- const selectPrevious = useCallback(() => {
397
- if (!selectedBlockId) {
398
- if (blocks.length > 0) {
399
- setSelectedBlockId(blocks[blocks.length - 1].id);
400
- }
401
- return;
402
- }
403
- const currentIndex = getBlockIndex(document, selectedBlockId);
404
- if (currentIndex > 0) {
405
- setSelectedBlockId(blocks[currentIndex - 1].id);
406
- }
407
- }, [document, blocks, selectedBlockId]);
408
- const deleteSelected = useCallback(() => {
409
- if (!selectedBlockId)
410
- return;
411
- const currentIndex = getBlockIndex(document, selectedBlockId);
412
- removeBlock2(selectedBlockId);
413
- if (blocks.length > 1) {
414
- if (currentIndex < blocks.length - 1) {
415
- setSelectedBlockId(blocks[currentIndex + 1].id);
416
- } else if (currentIndex > 0) {
417
- setSelectedBlockId(blocks[currentIndex - 1].id);
418
- }
419
- } else {
420
- setSelectedBlockId(null);
421
- }
422
- }, [document, blocks, selectedBlockId, removeBlock2]);
423
- const updateSelectedContent = useCallback((content) => {
424
- if (!selectedBlockId)
425
- return;
426
- updateBlock2(selectedBlockId, { content });
427
- }, [selectedBlockId, updateBlock2]);
428
- const duplicateSelected = useCallback(() => {
429
- if (!selectedBlockId || !selectedBlock)
430
- return;
431
- const currentIndex = getBlockIndex(document, selectedBlockId);
432
- const clonedBlock = {
433
- ...selectedBlock,
434
- id: Math.random().toString(36).substring(2, 9)
435
- };
436
- insertBlock2(clonedBlock, currentIndex + 1);
437
- setSelectedBlockId(clonedBlock.id);
438
- }, [document, selectedBlockId, selectedBlock, insertBlock2]);
439
- const moveSelectedUp = useCallback(() => {
440
- if (!selectedBlockId)
441
- return;
442
- const currentIndex = getBlockIndex(document, selectedBlockId);
443
- if (currentIndex > 0) {
444
- moveBlock2(selectedBlockId, currentIndex - 1);
445
- }
446
- }, [document, selectedBlockId, moveBlock2]);
447
- const moveSelectedDown = useCallback(() => {
448
- if (!selectedBlockId)
449
- return;
450
- const currentIndex = getBlockIndex(document, selectedBlockId);
451
- if (currentIndex < blocks.length - 1) {
452
- moveBlock2(selectedBlockId, currentIndex + 1);
453
- }
454
- }, [document, blocks, selectedBlockId, moveBlock2]);
455
- return {
456
- selectedBlockId,
457
- selectedBlock,
458
- selectBlock,
459
- selectNext,
460
- selectPrevious,
461
- deleteSelected,
462
- updateSelectedContent,
463
- duplicateSelected,
464
- moveSelectedUp,
465
- moveSelectedDown
466
- };
467
- }
468
-
469
- // src/index.ts
470
- import {
471
- paragraph,
472
- heading,
473
- h1,
474
- h2,
475
- h3,
476
- h4,
477
- h5,
478
- h6,
479
- bulletList,
480
- numberedList,
481
- checkListItem,
482
- checkList,
483
- codeBlock,
484
- blockquote,
485
- divider,
486
- image,
487
- callout,
488
- text,
489
- bold,
490
- italic,
491
- code,
492
- link
493
- } from "@create-markdown/core";
494
- var VERSION = "0.1.0";
495
- export {
496
- useMarkdown,
497
- useDocument,
498
- useBlockEditor,
499
- text,
500
- paragraph,
501
- numberedList,
502
- link,
503
- italic,
504
- image,
505
- heading,
506
- h6,
507
- h5,
508
- h4,
509
- h3,
510
- h2,
511
- h1,
512
- divider,
513
- codeBlock,
514
- code,
515
- checkListItem,
516
- checkList,
517
- callout,
518
- bulletList,
519
- bold,
520
- blockquote,
521
- VERSION,
522
- InlineContent,
523
- BlockRenderer,
524
- BlockElement
525
- };
1
+ import{jsx as K,jsxs as H,Fragment as M}from"react/jsx-runtime";function w({blocks:J,className:G,customRenderers:z}){return K("div",{className:G,children:J.map((V)=>K(L,{block:V,customRenderers:z},V.id))})}function L({block:J,customRenderers:G}){let z=G?.[J.type];if(z)return K(z,{block:J});switch(J.type){case"paragraph":return K(j,{block:J});case"heading":return K(i,{block:J});case"bulletList":return K(B,{block:J,customRenderers:G});case"numberedList":return K(I,{block:J,customRenderers:G});case"checkList":return K(m,{block:J});case"codeBlock":return K(x,{block:J});case"blockquote":return K(u,{block:J});case"table":return K(a,{block:J});case"image":return K(b,{block:J});case"divider":return K(r,{});case"callout":return K(k,{block:J});default:return null}}function U({spans:J}){return K(M,{children:J.map((G,z)=>K(C,{span:G},z))})}function C({span:J}){let{text:G,styles:z}=J;if(z.code)G=K("code",{children:G});if(z.highlight)G=K("mark",{children:G});if(z.strikethrough)G=K("del",{children:G});if(z.underline)G=K("u",{children:G});if(z.italic)G=K("em",{children:G});if(z.bold)G=K("strong",{children:G});if(z.link)G=K("a",{href:z.link.url,title:z.link.title,children:G});return K(M,{children:G})}function j({block:J}){return K("p",{children:K(U,{spans:J.content})})}function i({block:J}){let G=`h${J.props.level}`;return K(G,{children:K(U,{spans:J.content})})}function B({block:J,customRenderers:G}){return K("ul",{children:J.children.map((z)=>H("li",{children:[K(U,{spans:z.content}),z.children.length>0&&K(L,{block:z,customRenderers:G})]},z.id))})}function I({block:J,customRenderers:G}){return K("ol",{children:J.children.map((z)=>H("li",{children:[K(U,{spans:z.content}),z.children.length>0&&K(L,{block:z,customRenderers:G})]},z.id))})}function m({block:J}){return H("div",{style:{display:"flex",alignItems:"flex-start",gap:"0.5rem"},children:[K("input",{type:"checkbox",checked:J.props.checked,readOnly:!0,style:{marginTop:"0.25rem"}}),K("span",{style:{textDecoration:J.props.checked?"line-through":"none"},children:K(U,{spans:J.content})})]})}function x({block:J}){let G=J.content.map((V)=>V.text).join(""),z=J.props.language;return K("pre",{children:K("code",{className:z?`language-${z}`:void 0,children:G})})}function u({block:J}){return K("blockquote",{children:K(U,{spans:J.content})})}function a({block:J}){let{headers:G,rows:z,alignments:V}=J.props,q=(Z)=>{return V?.[Z]??void 0};return H("table",{children:[G.length>0&&K("thead",{children:K("tr",{children:G.map((Z,$)=>K("th",{style:{textAlign:q($)},children:Z},$))})}),K("tbody",{children:z.map((Z,$)=>K("tr",{children:Z.map((O,_)=>K("td",{style:{textAlign:q(_)},children:O},_))},$))})]})}function b({block:J}){return H("figure",{children:[K("img",{src:J.props.url,alt:J.props.alt??"",title:J.props.title,width:J.props.width,height:J.props.height}),J.props.alt&&K("figcaption",{children:J.props.alt})]})}function r(){return K("hr",{})}function k({block:J}){let G=J.props.type,z={padding:"1rem",borderRadius:"0.25rem",borderLeft:"4px solid",marginBottom:"1rem"},V={info:{borderColor:"#3b82f6",backgroundColor:"#eff6ff"},warning:{borderColor:"#f59e0b",backgroundColor:"#fffbeb"},tip:{borderColor:"#10b981",backgroundColor:"#ecfdf5"},danger:{borderColor:"#ef4444",backgroundColor:"#fef2f2"},note:{borderColor:"#6b7280",backgroundColor:"#f9fafb"}},q=V[G]??V.note;return H("div",{style:{...z,borderLeftColor:q.borderColor,backgroundColor:q.backgroundColor},role:"alert",children:[K("strong",{style:{textTransform:"capitalize",display:"block",marginBottom:"0.5rem"},children:G}),K(U,{spans:J.content})]})}import{useState as y,useCallback as X,useMemo as S}from"react";import{createDocument as n,insertBlock as d,appendBlock as s,removeBlock as t,updateBlock as o,moveBlock as l,findBlock as g,getBlockIndex as A,clearBlocks as c,setBlocks as h,updateMeta as e,markdownToBlocks as R,blocksToMarkdown as p}from"@create-markdown/core";function zz(J=[],G){let[z,V]=y(()=>n(J,G)),q=X((W,Y)=>{V((Q)=>d(Q,W,Y))},[]),Z=X((W)=>{V((Y)=>s(Y,W))},[]),$=X((W)=>{V((Y)=>t(Y,W))},[]),O=X((W,Y)=>{V((Q)=>o(Q,W,Y))},[]),_=X((W,Y)=>{V((Q)=>l(Q,W,Y))},[]),F=X((W)=>g(z,W),[z]),P=X((W)=>A(z,W),[z]),E=X(()=>{V((W)=>c(W))},[]),N=X((W)=>{V((Y)=>h(Y,W))},[]),f=X(()=>p(z.blocks),[z]),T=X((W)=>{let Y=R(W);V((Q)=>h(Q,Y))},[]),D=X((W)=>{V((Y)=>e(Y,W))},[]);return{document:z,blocks:z.blocks,insertBlock:q,appendBlock:Z,removeBlock:$,updateBlock:O,moveBlock:_,findBlock:F,getBlockIndex:P,clearBlocks:E,setBlocks:N,setDocument:V,toMarkdown:f,fromMarkdown:T,updateMeta:D}}function Gz(J=""){let[G,z]=y(J),V=S(()=>R(G),[G]),q=X(($)=>{z($)},[]),Z=X(($)=>{let O=p($);z(O)},[]);return{markdown:G,blocks:V,setMarkdown:q,setBlocks:Z}}function Jz(J){let{document:G,blocks:z,removeBlock:V,updateBlock:q,moveBlock:Z,insertBlock:$}=J,[O,_]=y(null),F=S(()=>O?g(G,O):void 0,[G,O]),P=X((Q)=>{_(Q)},[]),E=X(()=>{if(!O){if(z.length>0)_(z[0].id);return}let Q=A(G,O);if(Q<z.length-1)_(z[Q+1].id)},[G,z,O]),N=X(()=>{if(!O){if(z.length>0)_(z[z.length-1].id);return}let Q=A(G,O);if(Q>0)_(z[Q-1].id)},[G,z,O]),f=X(()=>{if(!O)return;let Q=A(G,O);if(V(O),z.length>1){if(Q<z.length-1)_(z[Q+1].id);else if(Q>0)_(z[Q-1].id)}else _(null)},[G,z,O,V]),T=X((Q)=>{if(!O)return;q(O,{content:Q})},[O,q]),D=X(()=>{if(!O||!F)return;let Q=A(G,O),v={...F,id:Math.random().toString(36).substring(2,9)};$(v,Q+1),_(v.id)},[G,O,F,$]),W=X(()=>{if(!O)return;let Q=A(G,O);if(Q>0)Z(O,Q-1)},[G,O,Z]),Y=X(()=>{if(!O)return;let Q=A(G,O);if(Q<z.length-1)Z(O,Q+1)},[G,z,O,Z]);return{selectedBlockId:O,selectedBlock:F,selectBlock:P,selectNext:E,selectPrevious:N,deleteSelected:f,updateSelectedContent:T,duplicateSelected:D,moveSelectedUp:W,moveSelectedDown:Y}}import{paragraph as qz,heading as Uz,h1 as Az,h2 as Fz,h3 as Hz,h4 as Lz,h5 as Pz,h6 as Ez,bulletList as Nz,numberedList as fz,checkListItem as Tz,checkList as Dz,codeBlock as yz,blockquote as vz,divider as Mz,image as hz,callout as Sz,text as gz,bold as Rz,italic as pz,code as wz,link as Cz}from"@create-markdown/core";var Xz="0.2.0";export{Gz as useMarkdown,zz as useDocument,Jz as useBlockEditor,gz as text,qz as paragraph,fz as numberedList,Cz as link,pz as italic,hz as image,Uz as heading,Ez as h6,Pz as h5,Lz as h4,Hz as h3,Fz as h2,Az as h1,Mz as divider,yz as codeBlock,wz as code,Tz as checkListItem,Dz as checkList,Sz as callout,Nz as bulletList,Rz as bold,vz as blockquote,Xz as VERSION,U as InlineContent,w as BlockRenderer,L as BlockElement};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@create-markdown/react",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "React components and hooks for @create-markdown",
5
5
  "author": "Val Alexander <val@viewdue.ai>",
6
6
  "license": "MIT",
@@ -41,8 +41,8 @@
41
41
  ],
42
42
  "scripts": {
43
43
  "build": "bun run build:esm && bun run build:cjs && bun run build:types",
44
- "build:esm": "bun build ./src/index.ts --outfile ./dist/index.js --format esm --external react --external @create-markdown/core",
45
- "build:cjs": "bun build ./src/index.ts --outfile ./dist/index.cjs --format cjs --external react --external @create-markdown/core",
44
+ "build:esm": "bun build ./src/index.ts --outfile ./dist/index.js --format esm --external react --external @create-markdown/core --production",
45
+ "build:cjs": "bun build ./src/index.ts --outfile ./dist/index.cjs --format cjs --external react --external @create-markdown/core --production",
46
46
  "build:types": "tsc --emitDeclarationOnly --declaration --outDir dist",
47
47
  "dev": "bun run --watch src/index.ts",
48
48
  "clean": "rm -rf dist",
@@ -50,9 +50,6 @@
50
50
  "test": "vitest run",
51
51
  "test:watch": "vitest"
52
52
  },
53
- "dependencies": {
54
- "@create-markdown/core": "workspace:*"
55
- },
56
53
  "devDependencies": {
57
54
  "@testing-library/react": "^16.0.0",
58
55
  "@types/react": "^18.2.0",
@@ -63,6 +60,7 @@
63
60
  "vitest": "^2.1.0"
64
61
  },
65
62
  "peerDependencies": {
63
+ "@create-markdown/core": ">=0.2.0",
66
64
  "react": ">=18.0.0"
67
65
  },
68
66
  "engines": {