Skip to content

Commit 78c4284

Browse files
committed
refactor: Pangu finder APIs and unify render interfaces
1 parent 663aff5 commit 78c4284

13 files changed

Lines changed: 398 additions & 411 deletions

File tree

README.md

Lines changed: 58 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
- [HTML 内容字数统计 API](#html-内容字数统计-api)
7676
- [渲染 API](#渲染-api)
7777
- [中英文混排格式化 API](#中英文混排格式化-api)
78+
- [渲染 API(需要 JS 运行时)](#渲染-api需要-js-运行时)
7879
- [代码高亮 API](#代码高亮-api)
7980
- [处理器文档](#处理器文档)
8081
- [中英文混排格式化处理器](#中英文混排格式化处理器)
@@ -402,9 +403,11 @@ extraApiStatsFinder.getContentWordCount({
402403

403404
### 渲染 API
404405

405-
#### 中英文混排格式化 API
406+
此功能在轻量版和全量版中均可用。
407+
408+
**Finder 名称:** `extraApiRenderFinder`
406409

407-
**Finder 名称:** `extraApiPanguFinder`
410+
#### 中英文混排格式化 API
408411

409412
**描述**
410413

@@ -413,46 +416,42 @@ extraApiStatsFinder.getContentWordCount({
413416
**API 方法**
414417

415418
```javascript
416-
// 对 HTML 内容中的指定标签应用 Pangu 空格处理
417-
extraApiPanguFinder.spacingElementByTagName(htmlContent, tagName)
418-
419-
// 对 HTML 内容中具有指定 ID 的元素应用 Pangu 空格处理
420-
extraApiPanguFinder.spacingElementById(htmlContent, id)
419+
// 对整个 HTML 内容应用 Pangu 空格处理
420+
extraApiRenderFinder.applySpacingInHtml(htmlContent)
421421

422-
// 对 HTML 内容中具有指定 class 的元素应用 Pangu 空格处理
423-
extraApiPanguFinder.spacingElementByClassName(htmlContent, className)
422+
// 使用灵活参数对 HTML 内容应用 Pangu 空格处理
423+
extraApiRenderFinder.applySpacingInHtml({
424+
htmlContent: '<p>HTML 内容</p>', // 必需
425+
selector: 'p' // 可选,CSS 选择器
426+
})
424427

425428
// 对纯文本应用 Pangu 空格处理
426-
extraApiPanguFinder.spacingText(text)
429+
extraApiRenderFinder.applySpacingInText(text)
427430
```
428431

429432
**参数**
430433

431-
- `spacingElementByTagName(htmlContent, tagName)`
434+
- `applySpacingInHtml(htmlContent)`
432435
- `htmlContent`
433436
- 类型:`string`
434437
- 解释:包含 HTML 标签的内容
435-
- `tagName`
436-
- 类型:`string`
437-
- 解释:要处理的 HTML 标签名称(如 "p"、"div"、"span" 等)
438-
439-
- `spacingElementById(htmlContent, id)`
440-
- `htmlContent`
441-
- 类型:`string`
442-
- 解释:包含 HTML 标签的内容
443-
- `id`
444-
- 类型:`string`
445-
- 解释:要处理的元素 ID(如 "main"、"content" 等)
446-
447-
- `spacingElementByClassName(htmlContent, className)`
448-
- `htmlContent`
449-
- 类型:`string`
450-
- 解释:包含 HTML 标签的内容
451-
- `className`
452-
- 类型:`string`
453-
- 解释:要处理的 class 名称(如 "comment"、"article" 等)
454-
455-
- `spacingText(text)`
438+
- `applySpacingInHtml(params)`
439+
- `params` - 映射形式参数
440+
- `htmlContent`
441+
- 类型:`string`
442+
- 解释:包含 HTML 标签的内容(必需)
443+
- `selector`
444+
- 类型:`string`
445+
- 解释:CSS 选择器,用于定位特定元素(可选)
446+
- 支持所有 JSoup CSS 选择器,包括:
447+
- 标签选择器:`p`, `div`, `span`
448+
- ID 选择器:`#main`, `#content`
449+
- Class 选择器:`.article`, `.comment`
450+
- 属性选择器:`[attr]`, `[attr=value]`
451+
- 后代选择器:`div p`, `.article .content`
452+
- 子选择器:`div > p`
453+
- 伪类选择器:`:first-child`, `:nth-child(n)`
454+
- `applySpacingInText(text)`
456455
- `text`
457456
- 类型:`string`
458457
- 解释:要处理的纯文本内容
@@ -472,24 +471,32 @@ extraApiPanguFinder.spacingText(text)
472471
**使用示例**
473472

474473
```html
475-
<!--/* 对文章内容中的段落标签应用 Pangu 处理,下面这段代码可直接用于 /templates/post.html */-->
476-
<div th:utext="${extraApiPanguFinder.spacingElementByTagName(post.content?.content, 'p')}"></div>
474+
<!--/* 处理整个 HTML 内容(自动跳过 code、pre 等标签) */-->
475+
<div th:utext="${extraApiRenderFinder.applySpacingInHtml(post.content?.content)}"></div>
477476

478-
<!--/* 对整个 HTML 内容的所有 div 标签应用 Pangu 处理 */-->
479-
<div th:utext="${extraApiPanguFinder.spacingElementByTagName(content, 'div')}"></div>
477+
<!--/* 使用 CSS 选择器仅处理段落标签 */-->
478+
<div th:utext="${extraApiRenderFinder.applySpacingInHtml({htmlContent: post.content?.content, selector: 'p'})}"></div>
480479

481-
<!--/* 对指定 ID 的元素应用 Pangu 处理 */-->
482-
<div th:utext="${extraApiPanguFinder.spacingElementById(content, 'main')}"></div>
480+
<!--/* 使用 class 选择器处理指定 class 的元素 */-->
481+
<div th:utext="${extraApiRenderFinder.applySpacingInHtml({htmlContent: content, selector: '.article-content'})}"></div>
483482

484-
<!--/* 对指定 class 的元素应用 Pangu 处理 */-->
485-
<div th:utext="${extraApiPanguFinder.spacingElementByClassName(content, 'comment')}"></div>
483+
<!--/* 使用 ID 选择器处理指定 ID 的元素 */-->
484+
<div th:utext="${extraApiRenderFinder.applySpacingInHtml({htmlContent: content, selector: '#main'})}"></div>
485+
486+
<!--/* 使用复合选择器处理 */-->
487+
<div th:utext="${extraApiRenderFinder.applySpacingInHtml({htmlContent: content, selector: 'div.article > p'})}"></div>
486488

487489
<!--/* 对纯文本应用 Pangu 处理 */-->
488-
<span th:text="${extraApiPanguFinder.spacingText('请问Jackie的鼻子有几个?123个!')}"></span>
490+
<span th:text="${extraApiRenderFinder.applySpacingInText('请问Jackie的鼻子有几个?123个!')}"></span>
489491
<!--/* 输出:请问 Jackie 的鼻子有几个?123 个! */-->
490492

491493
<!--/* 在变量中使用 */-->
492-
<div th:with="processedContent=${extraApiPanguFinder.spacingElementByTagName(moment.spec.content?.html, 'p')}">
494+
<div th:with="processedContent=${extraApiRenderFinder.applySpacingInHtml(moment.spec.content?.html)}">
495+
<div th:utext="${processedContent}"></div>
496+
</div>
497+
498+
<!--/* 处理瞬间内容中的特定元素 */-->
499+
<div th:with="processedContent=${extraApiRenderFinder.applySpacingInHtml({htmlContent: moment.spec.content?.html, selector: '.moment-text'})}">
493500
<div th:utext="${processedContent}"></div>
494501
</div>
495502
```
@@ -506,12 +513,16 @@ extraApiPanguFinder.spacingText(text)
506513
- HTML 解析失败时返回原始内容
507514
- 不会抛出异常,保证页面渲染稳定性
508515

509-
#### 代码高亮 API
516+
### 渲染 API(需要 JS 运行时)
510517

511-
**Finder 名称:** `extraApiRenderFinder`
518+
此功能仅在全量版中可用。
519+
520+
**Finder 名称:** `extraApiJsRenderFinder`
521+
522+
#### 代码高亮 API
512523

513524
```javascript
514-
extraApiRenderFinder.renderCodeHtml(htmlContent)
525+
extraApiJsRenderFinder.highlightCodeInHtml(htmlContent)
515526
```
516527

517528
**参数**
@@ -534,15 +545,14 @@ extraApiRenderFinder.renderCodeHtml(htmlContent)
534545

535546
```html
536547
<!--/* 渲染文章内容中的代码块,下面这段代码可直接用于 /templates/post.html */-->
537-
<div th:utext="${extraApiRenderFinder.renderCodeHtml(post.content?.content)}"></div>
548+
<div th:utext="${extraApiJsRenderFinder.highlightCodeInHtml(post.content?.content)}"></div>
538549

539550
<!--/* 在模板中使用,下面这段代码可直接用于 /templates/moment.html */-->
540-
<div th:with="renderedContent=${extraApiRenderFinder.renderCodeHtml(moment.spec.content?.html)}">
551+
<div th:with="renderedContent=${extraApiJsRenderFinder.highlightCodeInHtml(moment.spec.content?.html)}">
541552
<div th:utext="${renderedContent}"></div>
542553
</div>
543554
```
544555

545-
546556
## 处理器文档
547557

548558
### 中英文混排格式化处理器

src/main/java/top/howiehz/halo/plugin/extra/api/finder/basic/ExtraApiPanguFinder.java

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package top.howiehz.halo.plugin.extra.api.finder.basic;
2+
3+
import java.util.Map;
4+
import reactor.core.publisher.Mono;
5+
6+
/**
7+
* Finder for render operations (Pangu spacing).
8+
* 渲染操作的 Finder(Pangu 空格)。
9+
*
10+
* <p>This finder provides template-accessible methods for applying Pangu spacing
11+
* to text content, improving readability by inserting whitespace between CJK
12+
* and Latin characters.</p>
13+
* <p>此 Finder 提供模板可访问的方法,用于对文本内容应用 Pangu 空格,
14+
* 通过在中日韩字符和拉丁字符之间插入空格来提高可读性。</p>
15+
*
16+
* @author HowieXie
17+
* @since 1.0.0
18+
*/
19+
public interface ExtraApiRenderFinder {
20+
21+
/**
22+
* Apply Pangu spacing to entire HTML content.
23+
* 对整个 HTML 内容应用 Pangu 空格。
24+
*
25+
* <p>This method processes the entire HTML document, automatically skipping
26+
* certain tags like code, pre, script, style, and textarea to preserve their
27+
* original formatting.</p>
28+
* <p>此方法处理整个 HTML 文档,自动跳过某些标签(如 code、pre、script、style 和 textarea),
29+
* 以保留其原始格式。</p>
30+
*
31+
* @param htmlContent the HTML content to process / 要处理的 HTML 内容
32+
* @return Mono emitting processed HTML content / 发出处理后的 HTML 内容的 Mono
33+
*/
34+
Mono<String> applySpacingInHtml(String htmlContent);
35+
36+
/**
37+
* Apply Pangu spacing to HTML content with flexible parameters.
38+
* 使用灵活参数对 HTML 内容应用 Pangu 空格。
39+
*
40+
* <p>Supported parameters:</p>
41+
* <ul>
42+
* <li>htmlContent (String, required): The HTML content to process</li>
43+
* <li>selector (String, optional): CSS selector to target specific elements.
44+
* Supports all JSoup CSS selectors including element, class, ID, attribute,
45+
* pseudo-class selectors, and combinators.</li>
46+
* </ul>
47+
*
48+
* <p>支持的参数:</p>
49+
* <ul>
50+
* <li>htmlContent (String, 必需):要处理的 HTML 内容</li>
51+
* <li>selector (String, 可选):用于定位特定元素的 CSS 选择器。
52+
* 支持所有 JSoup CSS 选择器,包括元素、class、ID、属性、伪类选择器和组合器。</li>
53+
* </ul>
54+
*
55+
* <p>Example usage:</p>
56+
* <pre>
57+
* // Process entire HTML
58+
* applySpacingInHtml(Map.of("htmlContent", html))
59+
*
60+
* // Process only paragraphs
61+
* applySpacingInHtml(Map.of("htmlContent", html, "selector", "p"))
62+
*
63+
* // Process elements with specific class
64+
* applySpacingInHtml(Map.of("htmlContent", html, "selector", ".article-content"))
65+
*
66+
* // Process using complex selector
67+
* applySpacingInHtml(Map.of("htmlContent", html, "selector", "div.content > p"))
68+
* </pre>
69+
*
70+
* @param params map containing htmlContent (required) and optional selector
71+
* 包含 htmlContent(必需)和可选 selector 的映射
72+
* @return Mono emitting processed HTML content / 发出处理后的 HTML 内容的 Mono
73+
*/
74+
Mono<String> applySpacingInHtml(Map<String, Object> params);
75+
76+
/**
77+
* Apply Pangu spacing to plain text.
78+
* 对纯文本应用 Pangu 空格。
79+
*
80+
* @param text the plain text to process / 要处理的纯文本
81+
* @return Mono emitting processed text / 发出处理后的文本的 Mono
82+
*/
83+
Mono<String> applySpacingInText(String text);
84+
}

src/main/java/top/howiehz/halo/plugin/extra/api/finder/basic/impl/ExtraApiPanguFinderImpl.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)