@humanspeak/svelte-markdown 0.5.3 → 0.5.5

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
@@ -10,18 +10,10 @@ You can install it with
10
10
  npm i -S @humanspeak/svelte-markdown
11
11
  ```
12
12
 
13
- If you use npm or if you prefer yarn
14
-
15
- ```console
16
- yarn add @humanspeak/svelte-markdown
17
- ```
18
-
19
- If you're using Sapper you might need to install it as a dev dependency.
20
-
21
13
  ## Usage
22
14
 
23
- ```html
24
- <script>
15
+ ```svelte
16
+ <script lang="ts">
25
17
  import SvelteMarkdown from '@humanspeak/svelte-markdown'
26
18
  const source = `
27
19
  # This is a header
@@ -42,6 +42,7 @@
42
42
  align={rest.align[i] || 'center'}
43
43
  {...rest}
44
44
  >
45
+ {rest.align[i]}
45
46
  <Parser tokens={headerItem.tokens} {renderers} />
46
47
  </renderers.tablecell>
47
48
  {/each}
@@ -53,7 +54,7 @@
53
54
  {#each row ?? [] as cells, i}
54
55
  <renderers.tablecell
55
56
  header={false}
56
- align={rest.align[i] || 'center'}
57
+ align={rest.align[i] ?? 'center'}
57
58
  {...rest}
58
59
  >
59
60
  <Parser tokens={cells.tokens} {renderers} />
@@ -9,6 +9,7 @@
9
9
  type SvelteMarkdownOptions
10
10
  } from './utils/markdown-parser.js'
11
11
  import Parser from './Parser.svelte'
12
+ import { shrinkHtmlTokens } from './utils/token-cleanup.js'
12
13
 
13
14
  interface Props {
14
15
  source: Token[] | string
@@ -38,10 +39,12 @@
38
39
 
39
40
  $effect.pre(() => {
40
41
  if (Array.isArray(source)) {
41
- tokens = source as Token[]
42
+ tokens = shrinkHtmlTokens(source) as Token[]
42
43
  } else {
43
44
  lexer = new Lexer(combinedOptions)
44
- tokens = isInline ? lexer.inlineTokens(source as string) : lexer.lex(source as string)
45
+ tokens = shrinkHtmlTokens(
46
+ isInline ? lexer.inlineTokens(source as string) : lexer.lex(source as string)
47
+ )
45
48
  }
46
49
  })
47
50
  $effect(() => {
@@ -0,0 +1,6 @@
1
+ import type { Token, TokensList } from 'marked';
2
+ export declare const isHtmlOpenTag: (raw: string) => {
3
+ tag: string;
4
+ isOpening: boolean;
5
+ } | null;
6
+ export declare const shrinkHtmlTokens: (tokens: Token[] | TokensList) => Token[];
@@ -0,0 +1,45 @@
1
+ // Cache the regex pattern
2
+ const HTML_TAG_PATTERN = /<\/?([a-zA-Z][a-zA-Z0-9]*).*?>/;
3
+ export const isHtmlOpenTag = (raw) => {
4
+ const match = raw.match(HTML_TAG_PATTERN);
5
+ if (!match)
6
+ return null;
7
+ return { tag: match[1], isOpening: !raw.startsWith('</') };
8
+ };
9
+ export const shrinkHtmlTokens = (tokens) => {
10
+ const result = [];
11
+ const len = tokens.length;
12
+ for (let i = 0; i < len; i++) {
13
+ const currentToken = tokens[i];
14
+ // Handle nested tokens first
15
+ if ('tokens' in currentToken && currentToken.tokens) {
16
+ currentToken.tokens = shrinkHtmlTokens(currentToken.tokens);
17
+ result.push(currentToken);
18
+ continue;
19
+ }
20
+ // Check for HTML pattern
21
+ if (currentToken.type === 'html' && i + 2 < len) {
22
+ const openTag = isHtmlOpenTag(currentToken.raw);
23
+ if (openTag?.isOpening) {
24
+ const contentToken = tokens[i + 1];
25
+ const closingToken = tokens[i + 2];
26
+ const closeTag = isHtmlOpenTag(closingToken.raw);
27
+ if (contentToken.type === 'text' &&
28
+ closeTag &&
29
+ !closeTag.isOpening &&
30
+ openTag.tag === closeTag.tag) {
31
+ result.push({
32
+ type: 'html',
33
+ raw: currentToken.raw + contentToken.raw + closingToken.raw,
34
+ text: currentToken.raw + contentToken.raw + closingToken.raw,
35
+ tag: openTag.tag
36
+ });
37
+ i += 2;
38
+ continue;
39
+ }
40
+ }
41
+ }
42
+ result.push(currentToken);
43
+ }
44
+ return result;
45
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@humanspeak/svelte-markdown",
3
3
  "description": "A markdown renderer for Svelte",
4
- "version": "0.5.3",
4
+ "version": "0.5.5",
5
5
  "scripts": {
6
6
  "dev": "vite dev",
7
7
  "build": "vite build && npm run package",
@@ -19,14 +19,18 @@
19
19
  },
20
20
  "repository": {
21
21
  "type": "git",
22
- "url": "https://github.com/humanspeak/svelte-markdown.git"
22
+ "url": "git+https://github.com/humanspeak/svelte-markdown.git"
23
23
  },
24
24
  "author": "Humanspeak, Inc.",
25
25
  "license": "MIT",
26
26
  "bugs": {
27
27
  "url": "https://github.com/humanspeak/svelte-markdown/issues"
28
28
  },
29
- "homepage": "https://github.com/humanspeak/svelte-markdown",
29
+ "tags": [
30
+ "svelte",
31
+ "markdown"
32
+ ],
33
+ "homepage": "sveltemarkdown.com",
30
34
  "files": [
31
35
  "dist",
32
36
  "!dist/**/*.test.*",
@@ -48,36 +52,36 @@
48
52
  "svelte": "^5.0.0"
49
53
  },
50
54
  "dependencies": {
55
+ "github-slugger": "^2.0.0",
51
56
  "marked": "^14.1.3"
52
57
  },
53
58
  "devDependencies": {
54
59
  "@eslint/eslintrc": "^3.1.0",
55
- "@eslint/js": "^9.13.0",
56
- "@sveltejs/kit": "^2.7.3",
60
+ "@eslint/js": "^9.14.0",
61
+ "@sveltejs/adapter-auto": "^3.3.1",
62
+ "@sveltejs/kit": "^2.7.4",
57
63
  "@sveltejs/package": "^2.3.7",
58
64
  "@sveltejs/vite-plugin-svelte": "^4.0.0",
59
- "@testing-library/jest-dom": "^6.6.2",
65
+ "@testing-library/jest-dom": "^6.6.3",
60
66
  "@testing-library/svelte": "^5.2.4",
61
67
  "@testing-library/user-event": "^14.5.2",
62
- "@types/node": "^22.8.4",
63
- "@typescript-eslint/eslint-plugin": "^8.12.2",
64
- "@typescript-eslint/parser": "^8.12.2",
68
+ "@types/node": "^22.8.7",
69
+ "@typescript-eslint/eslint-plugin": "^8.13.0",
70
+ "@typescript-eslint/parser": "^8.13.0",
65
71
  "@vitest/coverage-v8": "^2.1.4",
66
- "eslint": "^9.13.0",
72
+ "eslint": "^9.14.0",
67
73
  "eslint-config-prettier": "^9.1.0",
68
74
  "eslint-plugin-svelte": "^2.46.0",
69
- "github-slugger": "^2.0.0",
70
- "globals": "^15.11.0",
75
+ "globals": "^15.12.0",
71
76
  "jsdom": "^25.0.1",
72
77
  "prettier": "^3.3.3",
73
78
  "prettier-plugin-organize-imports": "^4.1.0",
74
79
  "prettier-plugin-svelte": "^3.2.7",
75
80
  "prettier-plugin-tailwindcss": "^0.6.8",
76
81
  "publint": "^0.2.12",
77
- "svelte": "^5.1.4",
82
+ "svelte": "^5.1.9",
78
83
  "svelte-check": "^4.0.5",
79
84
  "typescript": "^5.6.3",
80
- "@sveltejs/adapter-auto": "^3.3.1",
81
85
  "vite": "^5.4.10",
82
86
  "vitest": "^2.1.4"
83
87
  },
@@ -86,4 +90,4 @@
86
90
  "cookie": "^0.7.0"
87
91
  }
88
92
  }
89
- }
93
+ }