pinyin-ruby 1.0.0
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 +180 -0
- package/lib/cjs/index.js +164 -0
- package/lib/esm/index.js +156 -0
- package/lib/types/index.d.ts +80 -0
- package/package.json +42 -0
package/README.md
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
# pinyin-ruby
|
|
2
|
+
|
|
3
|
+
根据拼音规则生成带注音的Ruby标签的拼音工具(支持 HTML <ruby> 输出)
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install pinyin-ruby
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
或使用 pnpm:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pnpm add pinyin-ruby
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## 用法
|
|
18
|
+
|
|
19
|
+
### 基础用法
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { ruby } from 'pinyin-ruby';
|
|
23
|
+
|
|
24
|
+
// 生成 HTML ruby 标签(默认)
|
|
25
|
+
ruby('中文');
|
|
26
|
+
// 输出: <ruby>中<rt>zhōng</rt></ruby><ruby>文<rt>wén</rt></ruby>
|
|
27
|
+
|
|
28
|
+
// 生成纯文本格式
|
|
29
|
+
ruby('中文', { format: 'text' });
|
|
30
|
+
// 输出: 中(zhōng)文(wén)
|
|
31
|
+
|
|
32
|
+
// 生成 XML 格式
|
|
33
|
+
ruby('中文', { format: 'xml' });
|
|
34
|
+
// 输出: <ruby><base>中</base><rt>zhōng</rt></ruby><ruby><base>文</base><rt>wén</rt></ruby>
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### 自定义拼音风格
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
// 不带声调
|
|
41
|
+
ruby('中文', { style: 'normal' });
|
|
42
|
+
// 输出: <ruby>中<rt>zhong</rt></ruby><ruby>文<rt>wen</rt></ruby>
|
|
43
|
+
|
|
44
|
+
// 用数字标注声调
|
|
45
|
+
ruby('中文', { style: 'tone2' });
|
|
46
|
+
// 输出: <ruby>中<rt>zho1ng</rt></ruby><ruby>文<rt>we2n</rt></ruby>
|
|
47
|
+
|
|
48
|
+
// 只获取首字母
|
|
49
|
+
ruby('中文', { style: 'initials' });
|
|
50
|
+
// 输出: <ruby>中<rt>zh</rt></ruby><ruby>文<rt>w</rt></ruby>
|
|
51
|
+
|
|
52
|
+
// 只获取首字母(小写)
|
|
53
|
+
ruby('中文', { style: 'first_letter' });
|
|
54
|
+
// 输出: <ruby>中<rt>z</rt></ruby><ruby>文<rt>w</rt></ruby>
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### 处理非中文字符
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
// 默认跳过非中文字符
|
|
61
|
+
ruby('hello中文world');
|
|
62
|
+
// 输出: <ruby>中<rt>zhōng</rt></ruby><ruby>文<rt>wén</rt></ruby>
|
|
63
|
+
|
|
64
|
+
// 保留非中文字符
|
|
65
|
+
ruby('hello中文world', { skipNonChinese: false });
|
|
66
|
+
// 输出: hello<ruby>中<rt>zhōng</rt></ruby><ruby>文<rt>wén</rt></ruby>world
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### 处理多音字
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
// 默认只返回第一个拼音
|
|
73
|
+
ruby('行走', { format: 'text' });
|
|
74
|
+
// 输出: 行(xíng)走(zǒu)
|
|
75
|
+
|
|
76
|
+
// 返回所有拼音(用顿号分隔)
|
|
77
|
+
rubyHeteonym('行走', { format: 'text' });
|
|
78
|
+
// 输出: 行(xíng、háng)走(zǒu)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### 获取详细信息
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
import { rubyDetails } from 'pinyin-ruby';
|
|
85
|
+
|
|
86
|
+
const details = rubyDetails('中文');
|
|
87
|
+
// 返回:
|
|
88
|
+
// [
|
|
89
|
+
// {
|
|
90
|
+
// char: '中',
|
|
91
|
+
// pinyin: 'zhōng',
|
|
92
|
+
// isChinese: true,
|
|
93
|
+
// html: '<ruby>中<rt>zhōng</rt></ruby>',
|
|
94
|
+
// xml: '<ruby><base>中</base><rt>zhōng</rt></ruby>',
|
|
95
|
+
// text: '中(zhōng)'
|
|
96
|
+
// },
|
|
97
|
+
// {
|
|
98
|
+
// char: '文',
|
|
99
|
+
// pinyin: 'wén',
|
|
100
|
+
// isChinese: true,
|
|
101
|
+
// html: '<ruby>文<rt>wén</rt></ruby>',
|
|
102
|
+
// xml: '<ruby><base>文</base><rt>wén</rt></ruby>',
|
|
103
|
+
// text: '文(wén)'
|
|
104
|
+
// }
|
|
105
|
+
// ]
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## API 文档
|
|
109
|
+
|
|
110
|
+
### `ruby(text, options?): string`
|
|
111
|
+
|
|
112
|
+
将文本转换为带注音的 Ruby 标签。
|
|
113
|
+
|
|
114
|
+
**参数:**
|
|
115
|
+
- `text` - 输入的文本
|
|
116
|
+
- `options` - 配置选项
|
|
117
|
+
- `format?: 'html' | 'xml' | 'text'` - Ruby 标签的格式(默认:'html')
|
|
118
|
+
- `style?: string` - 拼音风格(详见 pinyin 文档)
|
|
119
|
+
- `heteronym?: boolean` - 是否返回多音字
|
|
120
|
+
- `skipNonChinese?: boolean` - 是否跳过非中文字符(默认:true)
|
|
121
|
+
- 其他选项参考 [pinyin](https://pinyin.js.org/) 库
|
|
122
|
+
|
|
123
|
+
**返回值:**
|
|
124
|
+
- 转换后的 Ruby 标签字符串
|
|
125
|
+
|
|
126
|
+
### `rubyDetails(text, options?): RubyItem[]`
|
|
127
|
+
|
|
128
|
+
获取文本的详细拼音和 Ruby 标签信息。
|
|
129
|
+
|
|
130
|
+
**参数:**
|
|
131
|
+
- `text` - 输入的文本
|
|
132
|
+
- `options` - 配置选项
|
|
133
|
+
|
|
134
|
+
**返回值:**
|
|
135
|
+
- Ruby 标签详细信息数组,每项包含:
|
|
136
|
+
- `char` - 原始字符
|
|
137
|
+
- `pinyin` - 拼音
|
|
138
|
+
- `isChinese` - 是否是中文字符
|
|
139
|
+
- `html` - HTML 格式的 Ruby 标签
|
|
140
|
+
- `xml` - XML 格式的 Ruby 标签
|
|
141
|
+
- `text` - 纯文本格式
|
|
142
|
+
|
|
143
|
+
### `rubyHeteonym(text, options?): string`
|
|
144
|
+
|
|
145
|
+
生成支持多拼音的 Ruby 标签(多音字用顿号分隔)。
|
|
146
|
+
|
|
147
|
+
**参数:**
|
|
148
|
+
- `text` - 输入的文本
|
|
149
|
+
- `options` - 配置选项
|
|
150
|
+
|
|
151
|
+
**返回值:**
|
|
152
|
+
- 支持多拼音的 Ruby 标签字符串
|
|
153
|
+
|
|
154
|
+
## 拼音风格对照表
|
|
155
|
+
|
|
156
|
+
| 风格 | 输入 | 输出示例 | 说明 |
|
|
157
|
+
|------|------|---------|------|
|
|
158
|
+
| `tone` | 中 | zhōng | 带声调(默认) |
|
|
159
|
+
| `tone2` | 中 | zho1ng | 声调用数字标注在拼音中间 |
|
|
160
|
+
| `to3ne` | 中 | zho3ng | 声调用数字标注在拼音末尾 |
|
|
161
|
+
| `normal` | 中 | zhong | 不带声调 |
|
|
162
|
+
| `initials` | 中 | ZH | 声母(大写) |
|
|
163
|
+
| `first_letter` | 中 | Z | 首字母(大写) |
|
|
164
|
+
| `passport` | 中 | ZHONG | 护照拼音(大写,无声调) |
|
|
165
|
+
|
|
166
|
+
## 应用场景
|
|
167
|
+
|
|
168
|
+
- **教学工具**:为汉字添加拼音注音,辅助儿童和外国学习者学习汉语
|
|
169
|
+
- **文本处理**:批量为文档添加拼音
|
|
170
|
+
- **Web 应用**:在网页上显示带拼音的汉字
|
|
171
|
+
- **电子书**:为电子书内容添加拼音注音
|
|
172
|
+
- **辅助阅读**:为生僻字和难字添加拼音辅助阅读
|
|
173
|
+
|
|
174
|
+
## 与拼音库的关系
|
|
175
|
+
|
|
176
|
+
`pinyin-ruby` 是基于 [pinyin](https://pinyin.js.org/) 库的扩展,提供了更便捷的 Ruby 标签生成功能。
|
|
177
|
+
|
|
178
|
+
## License
|
|
179
|
+
|
|
180
|
+
MIT
|
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
3
|
+
var t = {};
|
|
4
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
5
|
+
t[p] = s[p];
|
|
6
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
7
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
8
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
9
|
+
t[p[i]] = s[p[i]];
|
|
10
|
+
}
|
|
11
|
+
return t;
|
|
12
|
+
};
|
|
13
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.ruby = ruby;
|
|
18
|
+
exports.rubyDetails = rubyDetails;
|
|
19
|
+
exports.rubyHeteonym = rubyHeteonym;
|
|
20
|
+
const pinyin_1 = __importDefault(require("pinyin"));
|
|
21
|
+
/**
|
|
22
|
+
* 生成单个字符的 Ruby 标签
|
|
23
|
+
*/
|
|
24
|
+
function generateRubyTag(char, py, format) {
|
|
25
|
+
if (!py || py.length === 0) {
|
|
26
|
+
return char;
|
|
27
|
+
}
|
|
28
|
+
const pinyinStr = py[0];
|
|
29
|
+
switch (format) {
|
|
30
|
+
case 'html':
|
|
31
|
+
return `<ruby>${char}<rt>${pinyinStr}</rt></ruby>`;
|
|
32
|
+
case 'xml':
|
|
33
|
+
return `<ruby><base>${char}</base><rt>${pinyinStr}</rt></ruby>`;
|
|
34
|
+
case 'text':
|
|
35
|
+
return `${char}(${pinyinStr})`;
|
|
36
|
+
default:
|
|
37
|
+
return `<ruby>${char}<rt>${pinyinStr}</rt></ruby>`;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* 判断是否是中文字符
|
|
42
|
+
*/
|
|
43
|
+
function isChinese(char) {
|
|
44
|
+
const code = char.charCodeAt(0);
|
|
45
|
+
return code >= 0x4e00 && code <= 0x9fff;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* 将文本转换为带注音的 Ruby 标签
|
|
49
|
+
* @param text - 输入的文本
|
|
50
|
+
* @param options - 配置选项
|
|
51
|
+
* @returns 转换后的 Ruby 标签字符串
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* import { ruby } from 'pinyin-ruby';
|
|
56
|
+
*
|
|
57
|
+
* // 默认生成 HTML ruby 标签
|
|
58
|
+
* ruby('中文');
|
|
59
|
+
* // 输出: <ruby>中<rt>zhōng</rt></ruby><ruby>文<rt>wén</rt></ruby>
|
|
60
|
+
*
|
|
61
|
+
* // 生成纯文本格式
|
|
62
|
+
* ruby('中文', { format: 'text' });
|
|
63
|
+
* // 输出: 中(zhōng)文(wén)
|
|
64
|
+
*
|
|
65
|
+
* // 自定义拼音风格
|
|
66
|
+
* ruby('中文', { style: 'normal' });
|
|
67
|
+
* // 输出: <ruby>中<rt>zhong</rt></ruby><ruby>文<rt>wen</rt></ruby>
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
function ruby(text, options) {
|
|
71
|
+
if (!text) {
|
|
72
|
+
return '';
|
|
73
|
+
}
|
|
74
|
+
const _a = options || {}, { format = 'html', skipNonChinese = true } = _a, pinyinOptions = __rest(_a, ["format", "skipNonChinese"]);
|
|
75
|
+
const pinyinResult = (0, pinyin_1.default)(text, pinyinOptions);
|
|
76
|
+
const result = [];
|
|
77
|
+
for (let i = 0; i < text.length; i++) {
|
|
78
|
+
const char = text[i];
|
|
79
|
+
const py = pinyinResult[i];
|
|
80
|
+
if (!isChinese(char)) {
|
|
81
|
+
// 非中文字符的处理
|
|
82
|
+
if (!skipNonChinese) {
|
|
83
|
+
result.push(char);
|
|
84
|
+
}
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
result.push(generateRubyTag(char, py, format));
|
|
88
|
+
}
|
|
89
|
+
return result.join('');
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* 生成带注音的 Ruby 标签详细信息
|
|
93
|
+
* @param text - 输入的文本
|
|
94
|
+
* @param options - 配置选项
|
|
95
|
+
* @returns Ruby 标签详细信息数组
|
|
96
|
+
*/
|
|
97
|
+
function rubyDetails(text, options) {
|
|
98
|
+
if (!text) {
|
|
99
|
+
return [];
|
|
100
|
+
}
|
|
101
|
+
const pinyinOptions = Object.assign({}, options);
|
|
102
|
+
const pinyinResult = (0, pinyin_1.default)(text, pinyinOptions);
|
|
103
|
+
const result = [];
|
|
104
|
+
for (let i = 0; i < text.length; i++) {
|
|
105
|
+
const char = text[i];
|
|
106
|
+
const py = pinyinResult[i];
|
|
107
|
+
const chinese = isChinese(char);
|
|
108
|
+
const pinyinStr = chinese && py && py[0] ? py[0] : '';
|
|
109
|
+
result.push({
|
|
110
|
+
char,
|
|
111
|
+
pinyin: pinyinStr,
|
|
112
|
+
isChinese: chinese,
|
|
113
|
+
html: chinese ? generateRubyTag(char, py, 'html') : char,
|
|
114
|
+
xml: chinese ? generateRubyTag(char, py, 'xml') : char,
|
|
115
|
+
text: chinese ? generateRubyTag(char, py, 'text') : char,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
return result;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* 生成复杂的多拼音格式(支持多音字)
|
|
122
|
+
*/
|
|
123
|
+
function rubyHeteonym(text, options) {
|
|
124
|
+
if (!text) {
|
|
125
|
+
return '';
|
|
126
|
+
}
|
|
127
|
+
const _a = options || {}, { format = 'html', skipNonChinese = true } = _a, pinyinOptions = __rest(_a, ["format", "skipNonChinese"]);
|
|
128
|
+
const pinyinResult = (0, pinyin_1.default)(text, Object.assign(Object.assign({}, pinyinOptions), { heteronym: true }));
|
|
129
|
+
const result = [];
|
|
130
|
+
for (let i = 0; i < text.length; i++) {
|
|
131
|
+
const char = text[i];
|
|
132
|
+
const py = pinyinResult[i];
|
|
133
|
+
if (!isChinese(char)) {
|
|
134
|
+
if (!skipNonChinese) {
|
|
135
|
+
result.push(char);
|
|
136
|
+
}
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (!py || py.length === 0) {
|
|
140
|
+
result.push(char);
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
const pinyinStr = py.join('、');
|
|
144
|
+
switch (format) {
|
|
145
|
+
case 'html':
|
|
146
|
+
result.push(`<ruby>${char}<rt>${pinyinStr}</rt></ruby>`);
|
|
147
|
+
break;
|
|
148
|
+
case 'xml':
|
|
149
|
+
result.push(`<ruby><base>${char}</base><rt>${pinyinStr}</rt></ruby>`);
|
|
150
|
+
break;
|
|
151
|
+
case 'text':
|
|
152
|
+
result.push(`${char}(${pinyinStr})`);
|
|
153
|
+
break;
|
|
154
|
+
default:
|
|
155
|
+
result.push(`<ruby>${char}<rt>${pinyinStr}</rt></ruby>`);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return result.join('');
|
|
159
|
+
}
|
|
160
|
+
exports.default = {
|
|
161
|
+
ruby,
|
|
162
|
+
rubyDetails,
|
|
163
|
+
rubyHeteonym,
|
|
164
|
+
};
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
+
var t = {};
|
|
3
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
+
t[p] = s[p];
|
|
5
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
+
t[p[i]] = s[p[i]];
|
|
9
|
+
}
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
12
|
+
import pinyin from 'pinyin';
|
|
13
|
+
/**
|
|
14
|
+
* 生成单个字符的 Ruby 标签
|
|
15
|
+
*/
|
|
16
|
+
function generateRubyTag(char, py, format) {
|
|
17
|
+
if (!py || py.length === 0) {
|
|
18
|
+
return char;
|
|
19
|
+
}
|
|
20
|
+
const pinyinStr = py[0];
|
|
21
|
+
switch (format) {
|
|
22
|
+
case 'html':
|
|
23
|
+
return `<ruby>${char}<rt>${pinyinStr}</rt></ruby>`;
|
|
24
|
+
case 'xml':
|
|
25
|
+
return `<ruby><base>${char}</base><rt>${pinyinStr}</rt></ruby>`;
|
|
26
|
+
case 'text':
|
|
27
|
+
return `${char}(${pinyinStr})`;
|
|
28
|
+
default:
|
|
29
|
+
return `<ruby>${char}<rt>${pinyinStr}</rt></ruby>`;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* 判断是否是中文字符
|
|
34
|
+
*/
|
|
35
|
+
function isChinese(char) {
|
|
36
|
+
const code = char.charCodeAt(0);
|
|
37
|
+
return code >= 0x4e00 && code <= 0x9fff;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* 将文本转换为带注音的 Ruby 标签
|
|
41
|
+
* @param text - 输入的文本
|
|
42
|
+
* @param options - 配置选项
|
|
43
|
+
* @returns 转换后的 Ruby 标签字符串
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* import { ruby } from 'pinyin-ruby';
|
|
48
|
+
*
|
|
49
|
+
* // 默认生成 HTML ruby 标签
|
|
50
|
+
* ruby('中文');
|
|
51
|
+
* // 输出: <ruby>中<rt>zhōng</rt></ruby><ruby>文<rt>wén</rt></ruby>
|
|
52
|
+
*
|
|
53
|
+
* // 生成纯文本格式
|
|
54
|
+
* ruby('中文', { format: 'text' });
|
|
55
|
+
* // 输出: 中(zhōng)文(wén)
|
|
56
|
+
*
|
|
57
|
+
* // 自定义拼音风格
|
|
58
|
+
* ruby('中文', { style: 'normal' });
|
|
59
|
+
* // 输出: <ruby>中<rt>zhong</rt></ruby><ruby>文<rt>wen</rt></ruby>
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export function ruby(text, options) {
|
|
63
|
+
if (!text) {
|
|
64
|
+
return '';
|
|
65
|
+
}
|
|
66
|
+
const _a = options || {}, { format = 'html', skipNonChinese = true } = _a, pinyinOptions = __rest(_a, ["format", "skipNonChinese"]);
|
|
67
|
+
const pinyinResult = pinyin(text, pinyinOptions);
|
|
68
|
+
const result = [];
|
|
69
|
+
for (let i = 0; i < text.length; i++) {
|
|
70
|
+
const char = text[i];
|
|
71
|
+
const py = pinyinResult[i];
|
|
72
|
+
if (!isChinese(char)) {
|
|
73
|
+
// 非中文字符的处理
|
|
74
|
+
if (!skipNonChinese) {
|
|
75
|
+
result.push(char);
|
|
76
|
+
}
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
result.push(generateRubyTag(char, py, format));
|
|
80
|
+
}
|
|
81
|
+
return result.join('');
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* 生成带注音的 Ruby 标签详细信息
|
|
85
|
+
* @param text - 输入的文本
|
|
86
|
+
* @param options - 配置选项
|
|
87
|
+
* @returns Ruby 标签详细信息数组
|
|
88
|
+
*/
|
|
89
|
+
export function rubyDetails(text, options) {
|
|
90
|
+
if (!text) {
|
|
91
|
+
return [];
|
|
92
|
+
}
|
|
93
|
+
const pinyinOptions = Object.assign({}, options);
|
|
94
|
+
const pinyinResult = pinyin(text, pinyinOptions);
|
|
95
|
+
const result = [];
|
|
96
|
+
for (let i = 0; i < text.length; i++) {
|
|
97
|
+
const char = text[i];
|
|
98
|
+
const py = pinyinResult[i];
|
|
99
|
+
const chinese = isChinese(char);
|
|
100
|
+
const pinyinStr = chinese && py && py[0] ? py[0] : '';
|
|
101
|
+
result.push({
|
|
102
|
+
char,
|
|
103
|
+
pinyin: pinyinStr,
|
|
104
|
+
isChinese: chinese,
|
|
105
|
+
html: chinese ? generateRubyTag(char, py, 'html') : char,
|
|
106
|
+
xml: chinese ? generateRubyTag(char, py, 'xml') : char,
|
|
107
|
+
text: chinese ? generateRubyTag(char, py, 'text') : char,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
return result;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* 生成复杂的多拼音格式(支持多音字)
|
|
114
|
+
*/
|
|
115
|
+
export function rubyHeteonym(text, options) {
|
|
116
|
+
if (!text) {
|
|
117
|
+
return '';
|
|
118
|
+
}
|
|
119
|
+
const _a = options || {}, { format = 'html', skipNonChinese = true } = _a, pinyinOptions = __rest(_a, ["format", "skipNonChinese"]);
|
|
120
|
+
const pinyinResult = pinyin(text, Object.assign(Object.assign({}, pinyinOptions), { heteronym: true }));
|
|
121
|
+
const result = [];
|
|
122
|
+
for (let i = 0; i < text.length; i++) {
|
|
123
|
+
const char = text[i];
|
|
124
|
+
const py = pinyinResult[i];
|
|
125
|
+
if (!isChinese(char)) {
|
|
126
|
+
if (!skipNonChinese) {
|
|
127
|
+
result.push(char);
|
|
128
|
+
}
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
if (!py || py.length === 0) {
|
|
132
|
+
result.push(char);
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
const pinyinStr = py.join('、');
|
|
136
|
+
switch (format) {
|
|
137
|
+
case 'html':
|
|
138
|
+
result.push(`<ruby>${char}<rt>${pinyinStr}</rt></ruby>`);
|
|
139
|
+
break;
|
|
140
|
+
case 'xml':
|
|
141
|
+
result.push(`<ruby><base>${char}</base><rt>${pinyinStr}</rt></ruby>`);
|
|
142
|
+
break;
|
|
143
|
+
case 'text':
|
|
144
|
+
result.push(`${char}(${pinyinStr})`);
|
|
145
|
+
break;
|
|
146
|
+
default:
|
|
147
|
+
result.push(`<ruby>${char}<rt>${pinyinStr}</rt></ruby>`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return result.join('');
|
|
151
|
+
}
|
|
152
|
+
export default {
|
|
153
|
+
ruby,
|
|
154
|
+
rubyDetails,
|
|
155
|
+
rubyHeteonym,
|
|
156
|
+
};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { IPinyinOptions } from 'pinyin/lib/types/declare';
|
|
2
|
+
/**
|
|
3
|
+
* Ruby 标签的类型定义
|
|
4
|
+
*/
|
|
5
|
+
export interface RubyOptions extends IPinyinOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Ruby 标签的格式
|
|
8
|
+
* 'html': 生成 HTML ruby 标签
|
|
9
|
+
* 'xml': 生成 XML 格式
|
|
10
|
+
* 'text': 生成纯文本格式,用于控制台显示
|
|
11
|
+
*/
|
|
12
|
+
format?: 'html' | 'xml' | 'text';
|
|
13
|
+
/**
|
|
14
|
+
* 是否分组处理(将单个汉字的拼音合并)
|
|
15
|
+
*/
|
|
16
|
+
group?: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* 是否跳过非中文字符
|
|
19
|
+
*/
|
|
20
|
+
skipNonChinese?: boolean;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* 将文本转换为带注音的 Ruby 标签
|
|
24
|
+
* @param text - 输入的文本
|
|
25
|
+
* @param options - 配置选项
|
|
26
|
+
* @returns 转换后的 Ruby 标签字符串
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* import { ruby } from 'pinyin-ruby';
|
|
31
|
+
*
|
|
32
|
+
* // 默认生成 HTML ruby 标签
|
|
33
|
+
* ruby('中文');
|
|
34
|
+
* // 输出: <ruby>中<rt>zhōng</rt></ruby><ruby>文<rt>wén</rt></ruby>
|
|
35
|
+
*
|
|
36
|
+
* // 生成纯文本格式
|
|
37
|
+
* ruby('中文', { format: 'text' });
|
|
38
|
+
* // 输出: 中(zhōng)文(wén)
|
|
39
|
+
*
|
|
40
|
+
* // 自定义拼音风格
|
|
41
|
+
* ruby('中文', { style: 'normal' });
|
|
42
|
+
* // 输出: <ruby>中<rt>zhong</rt></ruby><ruby>文<rt>wen</rt></ruby>
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export declare function ruby(text: string, options?: RubyOptions): string;
|
|
46
|
+
/**
|
|
47
|
+
* 生成带注音的 Ruby 标签(仅处理中文字符)
|
|
48
|
+
* 返回数组格式,便于进一步处理
|
|
49
|
+
*/
|
|
50
|
+
export interface RubyItem {
|
|
51
|
+
/** 原始字符 */
|
|
52
|
+
char: string;
|
|
53
|
+
/** 拼音 */
|
|
54
|
+
pinyin: string;
|
|
55
|
+
/** 是否是中文字符 */
|
|
56
|
+
isChinese: boolean;
|
|
57
|
+
/** 生成的 Ruby HTML 标签 */
|
|
58
|
+
html: string;
|
|
59
|
+
/** 生成的 Ruby XML 格式 */
|
|
60
|
+
xml: string;
|
|
61
|
+
/** 生成的纯文本格式 */
|
|
62
|
+
text: string;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* 生成带注音的 Ruby 标签详细信息
|
|
66
|
+
* @param text - 输入的文本
|
|
67
|
+
* @param options - 配置选项
|
|
68
|
+
* @returns Ruby 标签详细信息数组
|
|
69
|
+
*/
|
|
70
|
+
export declare function rubyDetails(text: string, options?: RubyOptions): RubyItem[];
|
|
71
|
+
/**
|
|
72
|
+
* 生成复杂的多拼音格式(支持多音字)
|
|
73
|
+
*/
|
|
74
|
+
export declare function rubyHeteonym(text: string, options?: RubyOptions): string;
|
|
75
|
+
declare const _default: {
|
|
76
|
+
ruby: typeof ruby;
|
|
77
|
+
rubyDetails: typeof rubyDetails;
|
|
78
|
+
rubyHeteonym: typeof rubyHeteonym;
|
|
79
|
+
};
|
|
80
|
+
export default _default;
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "pinyin-ruby",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "生成带注音的Ruby标签的拼音工具(支持 HTML <ruby> 输出)",
|
|
5
|
+
"types": "./lib/types/index.d.ts",
|
|
6
|
+
"main": "./lib/cjs/index.js",
|
|
7
|
+
"module": "./lib/esm/index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./lib/types/index.d.ts",
|
|
11
|
+
"import": "./lib/esm/index.js",
|
|
12
|
+
"require": "./lib/cjs/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"lib"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsc -p tsconfig.json && tsc -p tsconfig.esm.json",
|
|
20
|
+
"test": "jest",
|
|
21
|
+
"dev": "tsc -p tsconfig.json --watch"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"pinyin": ">=4.0.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/node": "^20.0.0",
|
|
28
|
+
"typescript": "^5.0.0",
|
|
29
|
+
"pinyin": "^4.0.0"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"pinyin",
|
|
33
|
+
"ruby",
|
|
34
|
+
"annotation",
|
|
35
|
+
"汉字",
|
|
36
|
+
"拼音",
|
|
37
|
+
"注音"
|
|
38
|
+
],
|
|
39
|
+
"author": "mkoplqq2006",
|
|
40
|
+
"license": "MIT"
|
|
41
|
+
}
|
|
42
|
+
|