quikdown 1.0.2 → 1.0.4

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/README.md CHANGED
@@ -1,29 +1,46 @@
1
1
  # quikdown
2
2
 
3
3
  [![CI](https://github.com/deftio/quikdown/actions/workflows/ci.yml/badge.svg)](https://github.com/deftio/quikdown/actions/workflows/ci.yml)
4
- [![npm version](https://badge.fury.io/js/quikdown.svg)](https://www.npmjs.com/package/quikdown)
5
- [![Coverage Status](https://img.shields.io/badge/coverage-99%25-brightgreen.svg)](https://github.com/deftio/quikdown)
4
+ [![npm version](https://img.shields.io/npm/v/quikdown.svg)](https://www.npmjs.com/package/quikdown)
5
+ [![Coverage Status](https://img.shields.io/badge/coverage-99%25-blue.svg)](https://github.com/deftio/quikdown)
6
6
  [![License: BSD-2-Clause](https://img.shields.io/badge/License-BSD%202--Clause-blue.svg)](https://opensource.org/licenses/BSD-2-Clause)
7
- [![Bundle Size](https://img.shields.io/badge/minified-<10KB-green.svg)](https://github.com/deftio/quikdown/tree/main/dist)
7
+ [![Bundle Size](https://img.shields.io/badge/core-7.4KB-blue.svg)](https://github.com/deftio/quikdown/tree/main/dist)
8
+ [![Bundle Size BD](https://img.shields.io/badge/bidirectional-10KB-blue.svg)](https://github.com/deftio/quikdown/tree/main/dist)
8
9
 
9
- A lightweight, fast markdown parser with built-in XSS protection. Quikdown works in both browser and Node.js environments. Via its fenced plug-in support it can support highlighted code blocks, diagrams, and other custom fenced content.
10
+ A lightweight, fast markdown parser with built-in XSS protection and optional bidirectional conversion support. Quikdown works in both browser and Node.js environments. Via its fenced plug-in support it can support highlighted code blocks, diagrams, and other custom fenced content.
10
11
 
11
- 🚀 **[Try Live Demo](https://deftio.github.io/quikdown/examples/quikdown-live.html)** - Interactive markdown editor with real-time preview
12
+ 🚀 **[Try Live Demo](https://deftio.github.io/quikdown/examples/quikdown-live.html)** - Interactive markdown ot HTML editor with real-time preview
13
+ 🚀 **[Try Bidirectional Demo](https://deftio.github.io/quikdown/examples/quikdown-bd-editor.html)** - Interactive markdown editor with bidirectional support (can edit either markdown or html and see other update)
12
14
  📚 **[View Examples](examples/)** - Additional demos and test pages
13
15
  📖 **[Read Documentation](docs/)** - Architecture, security, API reference, and plugin guide
14
16
 
17
+ ## Table of Contents
18
+
19
+ - [Features](#features)
20
+ - [Installation](#installation)
21
+ - [Quick Start](#quick-start)
22
+ - [Bidirectional Conversion](#bidirectional-conversion-new)
23
+ - [Supported Markdown](#supported-markdown)
24
+ - [Configuration Options](#configuration-options)
25
+ - [Plugin System](#plugin-system)
26
+ - [Framework Integration](#framework-integration)
27
+ - [API Reference](#api-reference)
28
+ - [Security](#security)
29
+ - [Contributing](#contributing)
30
+
15
31
  ## Features
16
32
 
17
- - 🚀 **Lightweight** - Under 10KB minified
18
- - 🔒 **Secure by default** - Built-in XSS protection with URL sanitization
19
- - 🎨 **Flexible styling** - Inline styles or CSS classes including examples for light and dark mode generation
20
- - 🔌 **Plugin system** - Extensible fence block handlers
21
33
  - 📦 **Zero dependencies** - No external libraries required
22
34
  - 🌐 **Universal** - Works in browsers and Node.js
35
+ - 🚀 **Lightweight** - 7.4KB minified (core), 10KB with bidirectional support
36
+ - 🔒 **Secure by default** - Built-in XSS protection with URL sanitization
37
+ - 🎨 **Flexible styling** - Inline styles or CSS classes including light and dark mode generation, custom themes
38
+ - 🔌 **Plugin system** - Extensible fence block handlers
23
39
  - ⚡ **Fast** - Optimized regex-based parsing
24
40
  - 📝 **CommonMark subset** - Supports essential markdown features
25
41
  - ✅ **Task Lists** - GitHub-style checkboxes
26
42
  - 🔗 **Autolinks** - Automatic URL detection
43
+ - 🔄 **Bidirectional** - Convert HTML back to Markdown (requires `quikdown_bd` module)
27
44
 
28
45
  ## Installation
29
46
 
@@ -32,13 +49,31 @@ npm install quikdown
32
49
  ```
33
50
 
34
51
  Or via CDN:
52
+
53
+ **ES Modules (recommended for modern applications):**
35
54
  ```html
36
- <script src="https://unpkg.com/quikdown/dist/quikdown.umd.js"></script>
55
+ <script type="module">
56
+ import quikdown from 'https://unpkg.com/quikdown/dist/quikdown.esm.min.js';
57
+
58
+ const html = quikdown('# Hello World');
59
+ document.body.innerHTML = html;
60
+ </script>
37
61
  ```
38
62
 
63
+ **UMD (for legacy browser support):**
64
+ ```html
65
+ <script src="https://unpkg.com/quikdown/dist/quikdown.umd.min.js"></script>
66
+ <script>
67
+ // Available as window.quikdown
68
+ const html = quikdown('# Hello World');
69
+ </script>
70
+ ```
71
+
72
+ > **Production tip:** Pin to a specific version for stability (e.g., `https://unpkg.com/quikdown@1.0.4/dist/quikdown.esm.min.js`)
73
+
39
74
  ## Quick Start
40
75
 
41
- ### Basic Usage
76
+ ### Basic Usage (Standard - One-way conversion)
42
77
 
43
78
  ```javascript
44
79
  import quikdown from 'quikdown';
@@ -47,6 +82,25 @@ const markdown = '# Hello World\n\nThis is **bold** text.';
47
82
  const html = quikdown(markdown);
48
83
  console.log(html);
49
84
  // Output: <h1>Hello World</h1><p>This is <strong>bold</strong> text.</p>
85
+
86
+ // Note: Regular quikdown does NOT support HTML to Markdown conversion
87
+ ```
88
+
89
+ ### Bidirectional Usage (Two-way conversion)
90
+
91
+ ```javascript
92
+ // Use quikdown_bd for bidirectional support
93
+ import quikdown_bd from 'quikdown/bd';
94
+
95
+ const markdown = '# Hello World\n\nThis is **bold** text.';
96
+
97
+ // Markdown to HTML
98
+ const html = quikdown_bd(markdown);
99
+
100
+ // HTML back to Markdown (only available in quikdown_bd)
101
+ const recoveredMarkdown = quikdown_bd.toMarkdown(html);
102
+ console.log(recoveredMarkdown);
103
+ // Output: # Hello World\n\nThis is **bold** text.
50
104
  ```
51
105
 
52
106
  ### With Options
@@ -58,6 +112,23 @@ const html = quikdown(markdown, {
58
112
  });
59
113
  ```
60
114
 
115
+ ### TypeScript Support
116
+
117
+ quikdown includes TypeScript definitions for better IDE support and type safety:
118
+
119
+ ```typescript
120
+ import quikdown, { QuikdownOptions } from 'quikdown';
121
+
122
+ const options: QuikdownOptions = {
123
+ inline_styles: true,
124
+ fence_plugin: (content: string, language: string) => {
125
+ return `<pre class="hljs ${language}">${content}</pre>`;
126
+ }
127
+ };
128
+
129
+ const html: string = quikdown(markdown, options);
130
+ ```
131
+
61
132
  ## Supported Markdown
62
133
 
63
134
  quikdown supports a practical subset of CommonMark:
@@ -229,11 +300,102 @@ const safe = quikdown(unsafe);
229
300
  // Output: &lt;script&gt;alert("XSS")&lt;/script&gt; <strong>bold</strong>
230
301
  ```
231
302
 
303
+ ## Bidirectional Conversion (1.0.4+)
304
+
305
+ **⚠️ Important:** Bidirectional conversion requires the `quikdown_bd` module, not the regular `quikdown` module.
306
+
307
+ The `quikdown_bd` module is a separate build that includes both markdown-to-HTML and HTML-to-markdown conversion capabilities. It's perfect for WYSIWYG editors and round-trip conversion scenarios.
308
+
309
+ ### Installation
310
+
311
+ ```javascript
312
+ // ES Modules - Use quikdown_bd, NOT quikdown
313
+ import quikdown_bd from 'quikdown/bd';
314
+
315
+ // CommonJS - Use quikdown_bd, NOT quikdown
316
+ const quikdown_bd = require('quikdown/bd');
317
+
318
+ // Browser - Load the quikdown_bd script, NOT the regular quikdown
319
+ <script src="https://unpkg.com/quikdown/dist/quikdown_bd.umd.min.js"></script>
320
+ <script>
321
+ // Available as window.quikdown_bd
322
+ const html = quikdown_bd(markdown);
323
+ </script>
324
+ ```
325
+
326
+ ### Basic Usage
327
+
328
+ ```javascript
329
+ // IMPORTANT: Use quikdown_bd for bidirectional support
330
+ import quikdown_bd from 'quikdown/bd';
331
+
332
+ // Markdown to HTML with source tracking
333
+ const html = quikdown_bd('**Hello** world', { bidirectional: true });
334
+ console.log(html);
335
+ // <strong data-qd="**">Hello</strong> world
336
+
337
+ // HTML back to Markdown (only available in quikdown_bd)
338
+ const markdown = quikdown_bd.toMarkdown(html);
339
+ console.log(markdown);
340
+ // **Hello** world
341
+
342
+ // Note: Regular quikdown does NOT have toMarkdown method
343
+ // This will fail: quikdown.toMarkdown(html) // ❌ Error
344
+ ```
345
+
346
+ ### Use Cases
347
+
348
+ - **Live Editors**: Build WYSIWYG markdown editors where users can edit in either view
349
+ - **Content Migration**: Convert existing HTML content to Markdown
350
+ - **Round-trip Preservation**: Maintain markdown source formatting through HTML conversion
351
+ - **Collaborative Editing**: Enable rich-text editing while storing content as Markdown
352
+
353
+ ### Browser Example
354
+
355
+ ```html
356
+ <div id="editor" contenteditable="true"></div>
357
+ <script type="module">
358
+ import quikdown_bd from 'https://unpkg.com/quikdown/dist/quikdown_bd.esm.min.js';
359
+
360
+ const editor = document.getElementById('editor');
361
+ const markdown = '# Edit me\n\n**Bold** and *italic*';
362
+
363
+ // Convert to HTML and display
364
+ editor.innerHTML = quikdown_bd(markdown, { bidirectional: true });
365
+
366
+ // Convert back to Markdown when needed
367
+ editor.addEventListener('blur', () => {
368
+ const updatedMarkdown = quikdown_bd.toMarkdown(editor);
369
+ console.log('Updated markdown:', updatedMarkdown);
370
+ });
371
+ </script>
372
+ ```
373
+
374
+ For complete documentation, see [Bidirectional Documentation](docs/quikdown-bidirectional.md).
375
+
376
+ ## Quikdown Lexer Version
377
+
378
+ An experimental lexer-based implementation is available for testing. See [docs/lexer-implementation.md](docs/lexer-implementation.md) for details.
379
+
380
+
381
+ ## Framework Integration
382
+
383
+ quikdown integrates seamlessly with modern JavaScript frameworks:
384
+
385
+ - **React** - Hooks, components, and Next.js support
386
+ - **Vue** - Composition API, Options API, and Nuxt support
387
+ - **Svelte** - Reactive statements and stores
388
+ - **Angular** - Components, services, and pipes
389
+
390
+ See the [Framework Integration Guide](docs/framework-integration.md) for detailed examples and best practices.
391
+
232
392
  ## API Reference
233
393
 
234
394
  For complete API documentation, see [docs/api-reference.md](docs/api-reference.md)
235
395
 
236
- ### `quikdown(markdown, options?)`
396
+ ### Core API
397
+
398
+ #### `quikdown(markdown, options?)`
237
399
 
238
400
  Main function to convert markdown to HTML.
239
401
 
@@ -245,7 +407,7 @@ Main function to convert markdown to HTML.
245
407
 
246
408
  **Returns:** HTML string
247
409
 
248
- ### `quikdown.configure(options)`
410
+ #### `quikdown.configure(options)`
249
411
 
250
412
  Creates a configured instance of the parser.
251
413
 
@@ -259,29 +421,100 @@ const myParser = quikdown.configure({
259
421
  const html = myParser(markdown);
260
422
  ```
261
423
 
262
- ### `quikdown.emitStyles()`
424
+ #### `quikdown.emitStyles(prefix?, theme?)`
263
425
 
264
426
  Returns CSS styles for quikdown HTML output when not using inline styles.
265
427
 
266
428
  ```javascript
267
- const styles = quikdown.emitStyles();
268
- // Add to your stylesheet or <style> tag
429
+ // Get light theme CSS
430
+ const lightStyles = quikdown.emitStyles();
431
+
432
+ // Get dark theme CSS
433
+ const darkStyles = quikdown.emitStyles('quikdown-', 'dark');
434
+ ```
435
+
436
+ ### Bidirectional API
437
+
438
+ **⚠️ These methods are only available in `quikdown_bd`, not in regular `quikdown`:**
439
+
440
+ #### `quikdown_bd(markdown, options?)`
441
+
442
+ Converts markdown to HTML with source tracking for bidirectional conversion.
443
+
444
+ #### `quikdown_bd.toMarkdown(htmlOrElement)`
445
+
446
+ Converts HTML back to Markdown. **This method only exists in `quikdown_bd`.**
447
+
448
+ **Parameters:**
449
+ - `htmlOrElement` (string | HTMLElement): HTML string or DOM element
450
+
451
+ **Returns:** Markdown string
452
+
453
+ ```javascript
454
+ // ✅ Correct - using quikdown_bd
455
+ import quikdown_bd from 'quikdown/bd';
456
+ const markdown = quikdown_bd.toMarkdown(html);
457
+
458
+ // ❌ Wrong - regular quikdown doesn't have toMarkdown
459
+ import quikdown from 'quikdown';
460
+ const markdown = quikdown.toMarkdown(html); // Error: toMarkdown is not a function
461
+ ```
462
+
463
+ See [API Reference](docs/api-reference.md) for complete documentation.
464
+
465
+ ## Theming
466
+
467
+ QuikDown supports flexible theming through container-based CSS scoping:
468
+
469
+ ### Using Pre-built Themes
470
+
471
+ ```html
472
+ <!-- Load theme CSS files -->
473
+ <link rel="stylesheet" href="quikdown.light.css">
474
+ <link rel="stylesheet" href="quikdown.dark.css">
475
+
476
+ <!-- Apply themes via container classes -->
477
+ <div class="quikdown-light">
478
+ <!-- Light themed content -->
479
+ </div>
480
+
481
+ <div class="quikdown-dark">
482
+ <!-- Dark themed content -->
483
+ </div>
484
+ ```
485
+
486
+ ### Theme Architecture
487
+
488
+ - **Structural styles**: Shared across all themes (margins, padding, font-sizes)
489
+ - **Theme colors**: Scoped to container classes (`.quikdown-light`, `.quikdown-dark`)
490
+ - **No conflicts**: Multiple themes can coexist on the same page
491
+ - **No default theme**: Without a container class, only structural styles apply
492
+
493
+ ### Inline Styles
494
+
495
+ For a batteries-included approach without CSS files:
496
+
497
+ ```javascript
498
+ // Use inline styles (always light theme currently)
499
+ const html = quikdown(markdown, { inline_styles: true });
269
500
  ```
270
501
 
271
502
  ## Browser Usage
272
503
 
273
- ### Via Script Tag
504
+ ### ES Modules (Recommended)
274
505
 
275
506
  ```html
276
507
  <!DOCTYPE html>
277
508
  <html>
278
509
  <head>
279
- <script src="https://unpkg.com/quikdown/dist/quikdown.umd.js"></script>
510
+ <meta charset="UTF-8">
280
511
  </head>
281
512
  <body>
282
513
  <div id="output"></div>
283
- <script>
284
- const markdown = '# Hello quikdown!';
514
+ <script type="module">
515
+ import quikdown from 'https://unpkg.com/quikdown/dist/quikdown.esm.min.js';
516
+
517
+ const markdown = '# Hello quikdown!\n\nSupports **bold** and *italic* text.';
285
518
  const html = quikdown(markdown, { inline_styles: true });
286
519
  document.getElementById('output').innerHTML = html;
287
520
  </script>
@@ -289,13 +522,23 @@ const styles = quikdown.emitStyles();
289
522
  </html>
290
523
  ```
291
524
 
292
- ### ES Modules
525
+ ### UMD Script Tag (Legacy)
293
526
 
294
- ```javascript
295
- import quikdown from 'quikdown';
296
-
297
- const html = quikdown('**Hello** world!');
298
- document.body.innerHTML = html;
527
+ ```html
528
+ <!DOCTYPE html>
529
+ <html>
530
+ <head>
531
+ <script src="https://unpkg.com/quikdown/dist/quikdown.umd.min.js"></script>
532
+ </head>
533
+ <body>
534
+ <div id="output"></div>
535
+ <script>
536
+ const markdown = '# Hello quikdown!';
537
+ const html = quikdown(markdown, { inline_styles: true });
538
+ document.getElementById('output').innerHTML = html;
539
+ </script>
540
+ </body>
541
+ </html>
299
542
  ```
300
543
 
301
544
  ## Node.js Usage