@h-ai/ui 1.0.0-beta.1 → 1.0.0-beta.3

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.en.md CHANGED
@@ -16,6 +16,8 @@ A cross-platform Svelte 5 Runes component library with DaisyUI v5 + Tailwind CSS
16
16
  - Authentication, CRUD, AI content, onboarding and a help center.
17
17
  - Svelte 5, themes, responsive layouts and Chinese/English/Japanese component messages.
18
18
 
19
+ Markdown underline and highlight markers support escaping; unclosed markers remain text. Toasts and document downloads handle unusually long text without repeated scanning, while trace identifiers remain hidden by default.
20
+
19
21
  ## Getting started
20
22
 
21
23
  ### Installation
@@ -387,6 +389,7 @@ Defaults match `@h-ai/crypto`: `SM4` for encrypted inputs, `SM3` for hashes, and
387
389
  > - `fontSize` accepts a `number` in px or any CSS length such as `1.125rem`.
388
390
  > - `allowHtmlTags` is off by default; when enabled, tags such as `<b>` / `<i>` / `<u>` / `<mark>` use a safe allowlist, with dangerous tags/attributes still sanitized.
389
391
  > - Text entities in safe HTML retain one level of meaning, so quotes, comparison operators and `&` in configuration snippets remain readable. Encoded tags remain text and are never reparsed as markup.
392
+ > - HTML code blocks such as `<pre><code class="language-yaml">` also receive safely rebuilt highlighting and copy buttons. Input styles and events are discarded. Supported languages include YAML, TypeScript, Shell, PowerShell, dotenv, Nginx, Dockerfile and Svelte.
390
393
  > - Raw `<img>` tags retain `width` / `height` as positive integer pixel values from 1 to 99999. These reserve space before lazy images load and keep document layout and anchor positions stable. Percentages, negatives, decimals, events, and arbitrary `style` are removed.
391
394
 
392
395
  ```svelte
package/README.ja.md CHANGED
@@ -16,6 +16,8 @@ Svelte 5 Runes のクロスプラットフォーム UI ライブラリーです
16
16
  - 認証、CRUD、AI コンテンツ、操作ガイド、ヘルプセンター。
17
17
  - Svelte 5、テーマ、レスポンシブ表示、中国語・英語・日本語のコンポーネント文言。
18
18
 
19
+ Markdown の下線と強調マーカーはエスケープに対応し、閉じていないマーカーはテキストとして扱います。長い異常入力でも Toast と文書出力で反復走査を避け、追跡 ID は既定で折りたたみます。
20
+
19
21
  ## 使い始める
20
22
 
21
23
  ### インストール
@@ -387,6 +389,7 @@ components/
387
389
  > - `fontSize` は px の `number`、または `1.125rem` などの CSS 長さ。
388
390
  > - `allowHtmlTags` は既定で無効です。有効化すると `<b>` / `<i>` / `<u>` / `<mark>` などを安全な許可リストで解析し、危険なタグ・属性は除去します。
389
391
  > - 安全な HTML テキストの実体参照は一段だけ解釈し、設定例の引用符、比較記号、`&` を読みやすく表示します。符号化されたタグはテキストのまま保持し、タグとして再解析しません。
392
+ > - `<pre><code class="language-yaml">` などの HTML コードブロックも、安全に再構築した構文色とコピーボタンで表示します。入力の任意スタイルやイベントは保持しません。YAML、TypeScript、Shell、PowerShell、dotenv、Nginx、Dockerfile、Svelte などに対応します。
390
393
  > - 生の `<img>` では、1~99999 の正整数ピクセル値の `width` / `height` を保持します。遅延読み込み前に領域を確保し、文書のレイアウトとアンカー位置のずれを減らします。割合、負数、小数、イベント属性、任意の `style` は保持しません。
391
394
 
392
395
  ```svelte
package/README.md CHANGED
@@ -16,6 +16,8 @@
16
16
  - 认证、CRUD、AI 内容展示、新手引导和帮助中心。
17
17
  - Svelte 5、主题切换、响应式布局及中英日文组件文案。
18
18
 
19
+ Markdown 下划线与高亮支持转义,未闭合标记按文本处理;Toast 和文档下载在异常长文本输入下保持可用,追踪编号仍默认收起。
20
+
19
21
  ## 使用入门
20
22
 
21
23
  ### 安装
@@ -386,6 +388,7 @@ components/
386
388
  > - `fontSize` 支持 `number`(按 px 解释)或任意 CSS 长度字符串(如 `1.125rem`)
387
389
  > - `allowHtmlTags` 默认关闭;开启后会按安全白名单解析 `<b>` / `<i>` / `<u>` / `<mark>` 等 HTML 标签,危险标签与属性仍会被消毒
388
390
  > - 安全 HTML 文本中的引号、比较符和 `&` 实体只保留一层含义,配置代码不会显示为实体编号;编码后的标签保持文字,不会重新作为标签执行。
391
+ > - HTML 中的 `<pre><code class="language-yaml">` 等代码块也会安全重建高亮和复制按钮,不保留输入的任意样式或事件。支持 YAML、TypeScript、Shell、PowerShell、dotenv、Nginx、Dockerfile 和 Svelte 等常用语言。
389
392
  > - 原始 `<img>` 可保留 `width` / `height`(1–99999 的正整数像素值),用于在图片延迟加载前预留空间,减少文档布局和锚点位置跳变;百分比、负数、小数、事件及任意 `style` 不保留。
390
393
 
391
394
  ```svelte
@@ -165,9 +165,11 @@ function resolveDocumentBaseName(title, filename) {
165
165
  const sanitizedName = stripControlCharacters(preferredName)
166
166
  .replace(/[<>:"/\\|?*]+/g, ' ')
167
167
  .replace(/\s+/g, ' ')
168
- .replace(/[. ]+$/g, '')
169
168
  .trim();
170
- return sanitizedName || 'ai-document';
169
+ let end = sanitizedName.length;
170
+ while (end > 0 && (sanitizedName[end - 1] === '.' || sanitizedName[end - 1] === ' '))
171
+ end--;
172
+ return sanitizedName.slice(0, end) || 'ai-document';
171
173
  }
172
174
  function downloadBlob(blob, filename) {
173
175
  const url = URL.createObjectURL(blob);
@@ -9,8 +9,9 @@ const ALIGN_VALUE_REGEX = /^(?:left|center|right|justify)$/i;
9
9
  const HAI_ALIGN_BLOCK_REGEX = /^<hai-align\s+value="(left|center|right|justify)">\n?([\s\S]+?)\n?<\/hai-align>\n{0,2}/i;
10
10
  const HAI_SPAN_INLINE_REGEX = /^<hai-span\b([^>]*)>([\s\S]+?)<\/hai-span>/i;
11
11
  const HAI_SPAN_ATTR_REGEX = /\b(color|bg)="([^"]+)"/gi;
12
- const HAI_UNDERLINE_REGEX = /^\+\+((?:\\.|[\s\S])+?)\+\+/;
13
- const HAI_HIGHLIGHT_REGEX = /^==((?:\\.|[\s\S])+?)==/;
12
+ // 转义分支与普通字符互斥,未闭合标记中的长反斜线不会触发组合回溯。
13
+ const HAI_UNDERLINE_REGEX = /^\+\+((?:\\[\s\S]|[^\\])+?)\+\+/;
14
+ const HAI_HIGHLIGHT_REGEX = /^==((?:\\[\s\S]|[^\\])+?)==/;
14
15
  /**
15
16
  * Markdown heading 提取目录时需要一个“纯文本标题”。
16
17
  * 这里显式递归常见 inline token,保证 `<hai-span>`、链接、强调等都能回落成纯文本。
@@ -13,22 +13,28 @@ import langCpp from '@shikijs/langs/cpp';
13
13
  import langCsharp from '@shikijs/langs/csharp';
14
14
  import langCss from '@shikijs/langs/css';
15
15
  import langDiff from '@shikijs/langs/diff';
16
+ import langDockerfile from '@shikijs/langs/dockerfile';
17
+ import langDotenv from '@shikijs/langs/dotenv';
16
18
  import langGo from '@shikijs/langs/go';
17
19
  import langGraphql from '@shikijs/langs/graphql';
18
20
  import langHtml from '@shikijs/langs/html';
21
+ import langIni from '@shikijs/langs/ini';
19
22
  import langJava from '@shikijs/langs/java';
20
23
  import langJavascript from '@shikijs/langs/javascript';
21
24
  import langJson from '@shikijs/langs/json';
22
25
  import langKotlin from '@shikijs/langs/kotlin';
23
26
  import langLua from '@shikijs/langs/lua';
24
27
  import langMarkdown from '@shikijs/langs/markdown';
28
+ import langNginx from '@shikijs/langs/nginx';
25
29
  import langPhp from '@shikijs/langs/php';
30
+ import langPowershell from '@shikijs/langs/powershell';
26
31
  import langPython from '@shikijs/langs/python';
27
32
  import langRuby from '@shikijs/langs/ruby';
28
33
  import langRust from '@shikijs/langs/rust';
29
34
  import langScss from '@shikijs/langs/scss';
30
35
  import langShell from '@shikijs/langs/shellscript';
31
36
  import langSql from '@shikijs/langs/sql';
37
+ import langSvelte from '@shikijs/langs/svelte';
32
38
  import langSwift from '@shikijs/langs/swift';
33
39
  import langToml from '@shikijs/langs/toml';
34
40
  import langTypescript from '@shikijs/langs/typescript';
@@ -73,6 +79,12 @@ const highlighter = createHighlighterCoreSync({
73
79
  langToml,
74
80
  langMarkdown,
75
81
  langDiff,
82
+ langDockerfile,
83
+ langDotenv,
84
+ langIni,
85
+ langNginx,
86
+ langPowershell,
87
+ langSvelte,
76
88
  langGraphql,
77
89
  langLua,
78
90
  ],
@@ -88,6 +100,9 @@ const LANG_ALIASES = {
88
100
  'sh': 'bash',
89
101
  'shell': 'bash',
90
102
  'zsh': 'bash',
103
+ 'ps1': 'powershell',
104
+ 'pwsh': 'powershell',
105
+ 'env': 'dotenv',
91
106
  'yml': 'yaml',
92
107
  'c++': 'cpp',
93
108
  'c#': 'csharp',
@@ -28,6 +28,54 @@ function createTableCellAttrs(cell) {
28
28
  }
29
29
  return attrs.length > 0 ? ` ${attrs.join(' ')}` : '';
30
30
  }
31
+ /** 围栏代码和 HTML 代码共用渲染,颜色与复制控件始终由框架生成。 */
32
+ function renderCodeBlock(text, lang, options) {
33
+ const language = lang && isLanguageSupported(lang) ? lang : '';
34
+ let highlighted;
35
+ if (options.enableHighlight && language) {
36
+ highlighted = highlightCode(text, language);
37
+ }
38
+ else if (options.enableHighlight) {
39
+ // 未指定语言时不做自动检测,直接转义
40
+ highlighted = escapeHtml(text);
41
+ }
42
+ else {
43
+ highlighted = escapeHtml(text);
44
+ }
45
+ const langLabel = language
46
+ ? `<span class="hai-md-code-lang">${escapeHtml(language)}</span>`
47
+ : '';
48
+ const copyBtn = options.showCopyButton
49
+ ? `<button type="button" class="hai-md-copy-btn" data-copy-code aria-label="${escapeHtml(uiM('markdown_copy_code'))}"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg></button>`
50
+ : '';
51
+ return `<div class="hai-md-code-block">`
52
+ + `<div class="hai-md-code-header">${langLabel}${copyBtn}</div>`
53
+ + `<pre><code class="hai-hl${language ? ` language-${escapeHtml(language)}` : ''}">${highlighted}</code></pre>`
54
+ + `</div>`;
55
+ }
56
+ /** 仅还原代码文本的实体;随后仍按普通代码转义,绝不把代码中的标签作为 HTML 执行。 */
57
+ function decodeCodeText(text) {
58
+ const named = { amp: '&', lt: '<', gt: '>', quot: '"', apos: '\'', nbsp: '\u00A0' };
59
+ return text.replace(/&(#(?:[xX][\da-fA-F]+|\d+)|amp|lt|gt|quot|apos|nbsp);/g, (entity, key) => {
60
+ if (!key.startsWith('#'))
61
+ return named[key] ?? entity;
62
+ const code = key[1]?.toLowerCase() === 'x' ? Number.parseInt(key.slice(2), 16) : Number(key.slice(1));
63
+ return code > 0 && code <= 0x10FFFF && !(code >= 0xD800 && code <= 0xDFFF) ? String.fromCodePoint(code) : entity;
64
+ });
65
+ }
66
+ /** 重建 pre/code,不保留输入的 style、事件或任意 class;其余 HTML 继续通过原有白名单。 */
67
+ function renderHtmlWithCode(source, options) {
68
+ let html = '';
69
+ let offset = 0;
70
+ for (const match of source.matchAll(/<pre\b[^>]*>\s*<code\b([^>]*)>([\s\S]*?)<\/code>\s*<\/pre>/gi)) {
71
+ html += sanitizeMarkdownHtml(source.slice(offset, match.index));
72
+ const classes = /\bclass\s*=\s*(?:"([^"]*)"|'([^']*)')/i.exec(match[1]);
73
+ const language = /(?:^|\s)language-([a-z\d#+.-]+)(?:\s|$)/i.exec(classes?.[1] ?? classes?.[2] ?? '')?.[1];
74
+ html += renderCodeBlock(decodeCodeText(match[2]), language, options);
75
+ offset = match.index + match[0].length;
76
+ }
77
+ return html + sanitizeMarkdownHtml(source.slice(offset));
78
+ }
31
79
  /**
32
80
  * 创建自定义渲染器对象
33
81
  *
@@ -38,34 +86,13 @@ function createRendererObject(options) {
38
86
  return {
39
87
  // 代码块:语法高亮 + 语言标签 + 复制按钮
40
88
  code({ text, lang }) {
41
- const language = lang && isLanguageSupported(lang) ? lang : '';
42
- let highlighted;
43
- if (options.enableHighlight && language) {
44
- highlighted = highlightCode(text, language);
45
- }
46
- else if (options.enableHighlight) {
47
- // 未指定语言时不做自动检测,直接转义
48
- highlighted = escapeHtml(text);
49
- }
50
- else {
51
- highlighted = escapeHtml(text);
52
- }
53
- const langLabel = language
54
- ? `<span class="hai-md-code-lang">${escapeHtml(language)}</span>`
55
- : '';
56
- const copyBtn = options.showCopyButton
57
- ? `<button type="button" class="hai-md-copy-btn" data-copy-code aria-label="${escapeHtml(uiM('markdown_copy_code'))}"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg></button>`
58
- : '';
59
- return `<div class="hai-md-code-block">`
60
- + `<div class="hai-md-code-header">${langLabel}${copyBtn}</div>`
61
- + `<pre><code class="hai-hl${language ? ` language-${escapeHtml(language)}` : ''}">${highlighted}</code></pre>`
62
- + `</div>`;
89
+ return renderCodeBlock(text, lang, options);
63
90
  },
64
91
  // 原始 HTML 块:转义以防止 XSS
65
92
  html(token) {
66
93
  const source = readHtmlTokenSource(token);
67
94
  return options.allowHtmlTags
68
- ? sanitizeMarkdownHtml(source)
95
+ ? renderHtmlWithCode(source, options)
69
96
  : escapeHtml(source);
70
97
  },
71
98
  // 链接:外部链接自动添加 target="_blank",阻止危险协议
@@ -9,10 +9,19 @@ import { untrack } from 'svelte';
9
9
  /** 兼容安全错误消息中的追踪编号,将排查信息从用户说明中分离。 */
10
10
  function normalizeMessage(message, explicitTraceId) {
11
11
  let traceId = explicitTraceId && /^[\w.:-]{1,128}$/.test(explicitTraceId) ? explicitTraceId : undefined;
12
- const readable = message.replace(/\s*\(traceId: ([\w.:-]{1,128})\)/g, (_, embedded) => {
13
- traceId ??= embedded;
14
- return '';
15
- }).trim();
12
+ const parts = [];
13
+ let cursor = 0;
14
+ // 先找固定标记,再移除相邻空白;无标记的长空白不会从每个字符重新回溯。
15
+ for (const match of message.matchAll(/\(traceId: ([\w.:-]{1,128})\)/g)) {
16
+ let end = match.index;
17
+ while (end > cursor && message[end - 1].trim() === '')
18
+ end--;
19
+ parts.push(message.slice(cursor, end));
20
+ cursor = match.index + match[0].length;
21
+ traceId ??= match[1];
22
+ }
23
+ parts.push(message.slice(cursor));
24
+ const readable = parts.join('').trim();
16
25
  return { message: readable || core.i18n.coreM('core_errorInternal'), traceId };
17
26
  }
18
27
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@h-ai/ui",
3
3
  "type": "module",
4
- "version": "1.0.0-beta.1",
4
+ "version": "1.0.0-beta.3",
5
5
  "description": "Hai Framework UI component library for Svelte 5 Runes.",
6
6
  "license": "Apache-2.0",
7
7
  "svelte": "./dist/index.js",
@@ -124,7 +124,7 @@
124
124
  "svelte": "^5.57.2"
125
125
  },
126
126
  "dependencies": {
127
- "@h-ai/core": "1.0.0-beta.1",
127
+ "@h-ai/core": "1.0.0-beta.3",
128
128
  "@internationalized/date": "^3.12.4",
129
129
  "@rrweb/replay": "^2.1.7",
130
130
  "@shikijs/langs": "^4.5.0",