@humanspeak/svelte-markdown 0.5.4 → 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.
@@ -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.4",
4
+ "version": "0.5.5",
5
5
  "scripts": {
6
6
  "dev": "vite dev",
7
7
  "build": "vite build && npm run package",
@@ -57,29 +57,29 @@
57
57
  },
58
58
  "devDependencies": {
59
59
  "@eslint/eslintrc": "^3.1.0",
60
- "@eslint/js": "^9.13.0",
60
+ "@eslint/js": "^9.14.0",
61
61
  "@sveltejs/adapter-auto": "^3.3.1",
62
- "@sveltejs/kit": "^2.7.3",
62
+ "@sveltejs/kit": "^2.7.4",
63
63
  "@sveltejs/package": "^2.3.7",
64
64
  "@sveltejs/vite-plugin-svelte": "^4.0.0",
65
- "@testing-library/jest-dom": "^6.6.2",
65
+ "@testing-library/jest-dom": "^6.6.3",
66
66
  "@testing-library/svelte": "^5.2.4",
67
67
  "@testing-library/user-event": "^14.5.2",
68
- "@types/node": "^22.8.4",
69
- "@typescript-eslint/eslint-plugin": "^8.12.2",
70
- "@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",
71
71
  "@vitest/coverage-v8": "^2.1.4",
72
- "eslint": "^9.13.0",
72
+ "eslint": "^9.14.0",
73
73
  "eslint-config-prettier": "^9.1.0",
74
74
  "eslint-plugin-svelte": "^2.46.0",
75
- "globals": "^15.11.0",
75
+ "globals": "^15.12.0",
76
76
  "jsdom": "^25.0.1",
77
77
  "prettier": "^3.3.3",
78
78
  "prettier-plugin-organize-imports": "^4.1.0",
79
79
  "prettier-plugin-svelte": "^3.2.7",
80
80
  "prettier-plugin-tailwindcss": "^0.6.8",
81
81
  "publint": "^0.2.12",
82
- "svelte": "^5.1.4",
82
+ "svelte": "^5.1.9",
83
83
  "svelte-check": "^4.0.5",
84
84
  "typescript": "^5.6.3",
85
85
  "vite": "^5.4.10",
@@ -90,4 +90,4 @@
90
90
  "cookie": "^0.7.0"
91
91
  }
92
92
  }
93
- }
93
+ }