@h-ai/ui 1.0.0-beta.2 → 1.0.0-beta.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.en.md CHANGED
@@ -389,6 +389,7 @@ Defaults match `@h-ai/crypto`: `SM4` for encrypted inputs, `SM3` for hashes, and
389
389
  > - `fontSize` accepts a `number` in px or any CSS length such as `1.125rem`.
390
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.
391
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.
392
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.
393
394
 
394
395
  ```svelte
package/README.ja.md CHANGED
@@ -389,6 +389,7 @@ components/
389
389
  > - `fontSize` は px の `number`、または `1.125rem` などの CSS 長さ。
390
390
  > - `allowHtmlTags` は既定で無効です。有効化すると `<b>` / `<i>` / `<u>` / `<mark>` などを安全な許可リストで解析し、危険なタグ・属性は除去します。
391
391
  > - 安全な HTML テキストの実体参照は一段だけ解釈し、設定例の引用符、比較記号、`&` を読みやすく表示します。符号化されたタグはテキストのまま保持し、タグとして再解析しません。
392
+ > - `<pre><code class="language-yaml">` などの HTML コードブロックも、安全に再構築した構文色とコピーボタンで表示します。入力の任意スタイルやイベントは保持しません。YAML、TypeScript、Shell、PowerShell、dotenv、Nginx、Dockerfile、Svelte などに対応します。
392
393
  > - 生の `<img>` では、1~99999 の正整数ピクセル値の `width` / `height` を保持します。遅延読み込み前に領域を確保し、文書のレイアウトとアンカー位置のずれを減らします。割合、負数、小数、イベント属性、任意の `style` は保持しません。
393
394
 
394
395
  ```svelte
package/README.md CHANGED
@@ -388,6 +388,7 @@ components/
388
388
  > - `fontSize` 支持 `number`(按 px 解释)或任意 CSS 长度字符串(如 `1.125rem`)
389
389
  > - `allowHtmlTags` 默认关闭;开启后会按安全白名单解析 `<b>` / `<i>` / `<u>` / `<mark>` 等 HTML 标签,危险标签与属性仍会被消毒
390
390
  > - 安全 HTML 文本中的引号、比较符和 `&` 实体只保留一层含义,配置代码不会显示为实体编号;编码后的标签保持文字,不会重新作为标签执行。
391
+ > - HTML 中的 `<pre><code class="language-yaml">` 等代码块也会安全重建高亮和复制按钮,不保留输入的任意样式或事件。支持 YAML、TypeScript、Shell、PowerShell、dotenv、Nginx、Dockerfile 和 Svelte 等常用语言。
391
392
  > - 原始 `<img>` 可保留 `width` / `height`(1–99999 的正整数像素值),用于在图片延迟加载前预留空间,减少文档布局和锚点位置跳变;百分比、负数、小数、事件及任意 `style` 不保留。
392
393
 
393
394
  ```svelte
@@ -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",阻止危险协议
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.2",
4
+ "version": "1.0.0-beta.4",
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.2",
127
+ "@h-ai/core": "1.0.0-beta.4",
128
128
  "@internationalized/date": "^3.12.4",
129
129
  "@rrweb/replay": "^2.1.7",
130
130
  "@shikijs/langs": "^4.5.0",