@humanspeak/svelte-markdown 0.7.5 → 0.7.7
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 +152 -11
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,14 +4,8 @@
|
|
|
4
4
|
[](https://github.com/humanspeak/svelte-markdown/actions/workflows/npm-publish.yml)
|
|
5
5
|
[](https://coveralls.io/github/humanspeak/svelte-markdown?branch=main)
|
|
6
6
|
[](https://github.com/humanspeak/svelte-markdown/blob/main/LICENSE)
|
|
7
|
-
|
|
8
|
-
<!-- [](https://bundlephobia.com/package/@humanspeak/svelte-markdown) -->
|
|
9
|
-
|
|
10
7
|
[](https://www.npmjs.com/package/@humanspeak/svelte-markdown)
|
|
11
8
|
[](https://github.com/humanspeak/svelte-markdown/actions/workflows/codeql.yml)
|
|
12
|
-
|
|
13
|
-
<!-- [](CODE_OF_CONDUCT.md) -->
|
|
14
|
-
|
|
15
9
|
[](http://www.typescriptlang.org/)
|
|
16
10
|
[](https://www.npmjs.com/package/@humanspeak/svelte-markdown)
|
|
17
11
|
[](https://github.com/humanspeak/svelte-markdown/graphs/commit-activity)
|
|
@@ -26,10 +20,74 @@ Rewriten for Svelte5 and all the updated goodies that have happened over the las
|
|
|
26
20
|
|
|
27
21
|
You can install it with
|
|
28
22
|
|
|
29
|
-
```
|
|
23
|
+
```bash
|
|
30
24
|
npm i -S @humanspeak/svelte-markdown
|
|
31
25
|
```
|
|
32
26
|
|
|
27
|
+
Or with your preferred package manager:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
pnpm add @humanspeak/svelte-markdown
|
|
31
|
+
yarn add @humanspeak/svelte-markdown
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Usage with Svelte 5
|
|
35
|
+
|
|
36
|
+
```svelte
|
|
37
|
+
<script lang="ts">
|
|
38
|
+
import SvelteMarkdown from '@humanspeak/svelte-markdown'
|
|
39
|
+
|
|
40
|
+
const source = `
|
|
41
|
+
# This is a header
|
|
42
|
+
|
|
43
|
+
This is a paragraph with **bold** and <em>mixed HTML</em>.
|
|
44
|
+
|
|
45
|
+
* List item with \`inline code\`
|
|
46
|
+
* And a [link](https://svelte.dev)
|
|
47
|
+
* With nested items
|
|
48
|
+
* Supporting full markdown
|
|
49
|
+
`
|
|
50
|
+
</script>
|
|
51
|
+
|
|
52
|
+
<SvelteMarkdown {source} />
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## TypeScript Support
|
|
56
|
+
|
|
57
|
+
The package is written in TypeScript and includes full type definitions. You can import types for custom renderers:
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
import type {
|
|
61
|
+
Renderers,
|
|
62
|
+
Token,
|
|
63
|
+
TokensList,
|
|
64
|
+
SvelteMarkdownOptions
|
|
65
|
+
} from '@humanspeak/svelte-markdown'
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Custom Renderer Example
|
|
69
|
+
|
|
70
|
+
Here's a complete example of a custom renderer with TypeScript support:
|
|
71
|
+
|
|
72
|
+
```svelte
|
|
73
|
+
<script lang="ts">
|
|
74
|
+
import type { Snippet } from 'svelte'
|
|
75
|
+
|
|
76
|
+
interface Props {
|
|
77
|
+
children?: Snippet
|
|
78
|
+
href?: string
|
|
79
|
+
title?: string
|
|
80
|
+
text?: string
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const { href = '', title = '', text = '', children }: Props = $props()
|
|
84
|
+
</script>
|
|
85
|
+
|
|
86
|
+
<a {href} {title} class="custom-link">
|
|
87
|
+
{@render children?.() ?? text}
|
|
88
|
+
</a>
|
|
89
|
+
```
|
|
90
|
+
|
|
33
91
|
## Usage
|
|
34
92
|
|
|
35
93
|
```svelte
|
|
@@ -240,15 +298,98 @@ To use [inline markdown](https://marked.js.org/using_advanced#inline), you can a
|
|
|
240
298
|
|
|
241
299
|
## HTML rendering
|
|
242
300
|
|
|
243
|
-
|
|
301
|
+
The package supports mixing HTML and Markdown content seamlessly within the same document. You can use HTML tags alongside Markdown syntax, and both will be properly rendered through their respective components.
|
|
302
|
+
|
|
303
|
+
### Basic HTML in Markdown
|
|
304
|
+
|
|
305
|
+
You can freely mix HTML tags with Markdown syntax:
|
|
244
306
|
|
|
245
307
|
```markdown
|
|
246
|
-
This is a **markdown** paragraph.
|
|
308
|
+
This is a **markdown** paragraph with <em>HTML emphasis</em> mixed in.
|
|
309
|
+
|
|
310
|
+
<div style="color: blue">
|
|
311
|
+
### This is a Markdown heading inside HTML
|
|
312
|
+
And here's some **bold** text too!
|
|
313
|
+
</div>
|
|
314
|
+
```
|
|
247
315
|
|
|
248
|
-
|
|
316
|
+
### Tables with Mixed Content
|
|
317
|
+
|
|
318
|
+
Tables support both Markdown and HTML formatting in cells:
|
|
319
|
+
|
|
320
|
+
```markdown
|
|
321
|
+
| Feature | Markdown | HTML |
|
|
322
|
+
|---------|:--------:|-----:|
|
|
323
|
+
| Bold | **text** | <strong>text</strong> |
|
|
324
|
+
| Italic | *text* | <em>text</em> |
|
|
325
|
+
| Links | [text](url) | <a href="url">text</a> |
|
|
249
326
|
```
|
|
250
327
|
|
|
251
|
-
|
|
328
|
+
### Interactive HTML Elements
|
|
329
|
+
|
|
330
|
+
HTML interactive elements like `<details>` work seamlessly:
|
|
331
|
+
|
|
332
|
+
```markdown
|
|
333
|
+
<details>
|
|
334
|
+
<summary>Click to expand</summary>
|
|
335
|
+
|
|
336
|
+
- This is a markdown list
|
|
337
|
+
- Inside an HTML details element
|
|
338
|
+
- Supporting **bold** and *italic* text
|
|
339
|
+
|
|
340
|
+
</details>
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
### HTML Block Elements
|
|
344
|
+
|
|
345
|
+
HTML block elements can contain Markdown content:
|
|
346
|
+
|
|
347
|
+
```markdown
|
|
348
|
+
<blockquote>
|
|
349
|
+
### Markdown Heading in Blockquote
|
|
350
|
+
|
|
351
|
+
- List item with **bold**
|
|
352
|
+
- Another item with *italic*
|
|
353
|
+
</blockquote>
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
### Component Customization
|
|
357
|
+
|
|
358
|
+
You can customize how HTML elements are rendered by providing custom components in the `renderers.html` prop:
|
|
359
|
+
|
|
360
|
+
```svelte
|
|
361
|
+
<script lang="ts">
|
|
362
|
+
import SvelteMarkdown from '@humanspeak/svelte-markdown'
|
|
363
|
+
import CustomBlockquote from './renderers/CustomBlockquote.svelte'
|
|
364
|
+
|
|
365
|
+
const source = `
|
|
366
|
+
<blockquote>
|
|
367
|
+
This will use the custom renderer
|
|
368
|
+
</blockquote>
|
|
369
|
+
`
|
|
370
|
+
</script>
|
|
371
|
+
|
|
372
|
+
<SvelteMarkdown
|
|
373
|
+
{source}
|
|
374
|
+
renderers={{
|
|
375
|
+
html: {
|
|
376
|
+
blockquote: CustomBlockquote
|
|
377
|
+
}
|
|
378
|
+
}}
|
|
379
|
+
/>
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
### Important Notes
|
|
383
|
+
|
|
384
|
+
1. The HTML rendering is handled by dedicated Svelte components, ensuring proper integration with Svelte's reactivity system.
|
|
385
|
+
|
|
386
|
+
2. All HTML elements are sanitized by default for security. Custom HTML attributes are preserved and passed to the corresponding components.
|
|
387
|
+
|
|
388
|
+
3. The package includes renderers for all standard HTML5 elements, which can be found in the source code at `src/renderers/html/`.
|
|
389
|
+
|
|
390
|
+
4. When mixing HTML and Markdown, the parser maintains proper nesting and hierarchy of elements.
|
|
391
|
+
|
|
392
|
+
5. For security reasons, script tags and potentially harmful HTML attributes are stripped out during parsing.
|
|
252
393
|
|
|
253
394
|
## Developing
|
|
254
395
|
|