@jayfong/x-text-render 2.119.0 → 3.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.
Files changed (41) hide show
  1. package/lib/code_render.css +2 -2
  2. package/lib/code_render.d.ts +3 -3
  3. package/lib/code_render.js +13 -15
  4. package/lib/code_render.module.css.js +7 -0
  5. package/lib/douyin_emoji_render.css +17 -17
  6. package/lib/douyin_emoji_render.d.ts +11 -11
  7. package/lib/douyin_emoji_render.js +757 -304
  8. package/lib/douyin_emoji_render.module.css.js +22 -0
  9. package/lib/html_render.d.ts +4 -4
  10. package/lib/html_render.js +21 -32
  11. package/lib/index.d.ts +7 -7
  12. package/lib/index.js +11 -15
  13. package/lib/math_render.d.ts +3 -4
  14. package/lib/math_render.js +30 -42
  15. package/lib/text_parser.d.ts +81 -81
  16. package/lib/text_parser.js +158 -162
  17. package/lib/text_render.d.ts +16 -16
  18. package/lib/text_render.js +23 -27
  19. package/lib/unified_emoji_render.css +2 -2
  20. package/lib/unified_emoji_render.d.ts +11 -11
  21. package/lib/unified_emoji_render.js +2691 -107
  22. package/lib/unified_emoji_render.module.css.js +7 -0
  23. package/lib/weixin_emoji_render.css +2 -2
  24. package/lib/weixin_emoji_render.d.ts +9 -9
  25. package/lib/weixin_emoji_render.js +1092 -342
  26. package/lib/weixin_emoji_render.module.css.js +7 -0
  27. package/package.json +23 -12
  28. package/lib/_cjs/code_render.css +0 -15
  29. package/lib/_cjs/code_render.js +0 -19
  30. package/lib/_cjs/douyin_emoji_render.css +0 -73
  31. package/lib/_cjs/douyin_emoji_render.js +0 -309
  32. package/lib/_cjs/html_render.js +0 -36
  33. package/lib/_cjs/index.js +0 -20
  34. package/lib/_cjs/math_render.css +0 -3
  35. package/lib/_cjs/math_render.js +0 -48
  36. package/lib/_cjs/text_parser.js +0 -168
  37. package/lib/_cjs/text_render.js +0 -31
  38. package/lib/_cjs/unified_emoji_render.css +0 -13
  39. package/lib/_cjs/unified_emoji_render.js +0 -112
  40. package/lib/_cjs/weixin_emoji_render.css +0 -13
  41. package/lib/_cjs/weixin_emoji_render.js +0 -349
@@ -1,163 +1,159 @@
1
- import _createForOfIteratorHelperLoose from "@babel/runtime/helpers/esm/createForOfIteratorHelperLoose";
2
- import { Parser } from 'simple-text-parser';
3
- import { DouyinEmojiRender } from "./douyin_emoji_render";
4
- import { UnifiedEmojiRender } from "./unified_emoji_render";
5
- import { WeixinEmojiRender } from "./weixin_emoji_render";
6
- export var TextParserNodeType = function (TextParserNodeType) {
7
- TextParserNodeType[TextParserNodeType["PARAGRAPH"] = 1] = "PARAGRAPH";
8
- TextParserNodeType[TextParserNodeType["PLAIN_TEXT"] = 2] = "PLAIN_TEXT";
9
- TextParserNodeType[TextParserNodeType["WEB_LINK"] = 4] = "WEB_LINK";
10
- TextParserNodeType[TextParserNodeType["LINE_BREAK"] = 8] = "LINE_BREAK";
11
- TextParserNodeType[TextParserNodeType["WEIXIN_EMOJI"] = 16] = "WEIXIN_EMOJI";
12
- TextParserNodeType[TextParserNodeType["UNIFIED_EMOJI"] = 32] = "UNIFIED_EMOJI";
13
- TextParserNodeType[TextParserNodeType["DOUYIN_EMOJI"] = 64] = "DOUYIN_EMOJI";
14
- TextParserNodeType[TextParserNodeType["MINI_PROGRAM_SHORT_LINK"] = 128] = "MINI_PROGRAM_SHORT_LINK";
15
- TextParserNodeType[TextParserNodeType["BOLD"] = 256] = "BOLD";
16
- TextParserNodeType[TextParserNodeType["ELLIPSIS"] = 512] = "ELLIPSIS";
17
- TextParserNodeType[TextParserNodeType["ANY_EMOJI"] = TextParserNodeType.UNIFIED_EMOJI | TextParserNodeType.WEIXIN_EMOJI | TextParserNodeType.DOUYIN_EMOJI] = "ANY_EMOJI";
18
- TextParserNodeType[TextParserNodeType["ANY_LINK"] = TextParserNodeType.WEB_LINK | TextParserNodeType.MINI_PROGRAM_SHORT_LINK] = "ANY_LINK";
19
- return TextParserNodeType;
1
+ import { DouyinEmojiRender } from "./douyin_emoji_render.js";
2
+ import { UnifiedEmojiRender } from "./unified_emoji_render.js";
3
+ import { WeixinEmojiRender } from "./weixin_emoji_render.js";
4
+ import { Parser } from "simple-text-parser";
5
+ // src/text_parser.ts
6
+ let TextParserNodeType = /* @__PURE__ */ function(TextParserNodeType) {
7
+ /** 段落 */
8
+ TextParserNodeType[TextParserNodeType["PARAGRAPH"] = 1] = "PARAGRAPH";
9
+ /** 纯文本 */
10
+ TextParserNodeType[TextParserNodeType["PLAIN_TEXT"] = 2] = "PLAIN_TEXT";
11
+ /** 网页链接 */
12
+ TextParserNodeType[TextParserNodeType["WEB_LINK"] = 4] = "WEB_LINK";
13
+ /** 换行 */
14
+ TextParserNodeType[TextParserNodeType["LINE_BREAK"] = 8] = "LINE_BREAK";
15
+ /** 微信表情 */
16
+ TextParserNodeType[TextParserNodeType["WEIXIN_EMOJI"] = 16] = "WEIXIN_EMOJI";
17
+ /** Emoji */
18
+ TextParserNodeType[TextParserNodeType["UNIFIED_EMOJI"] = 32] = "UNIFIED_EMOJI";
19
+ /** 抖音表情 */
20
+ TextParserNodeType[TextParserNodeType["DOUYIN_EMOJI"] = 64] = "DOUYIN_EMOJI";
21
+ /** 小程序短链接 */
22
+ TextParserNodeType[TextParserNodeType["MINI_PROGRAM_SHORT_LINK"] = 128] = "MINI_PROGRAM_SHORT_LINK";
23
+ /** 加粗效果 */
24
+ TextParserNodeType[TextParserNodeType["BOLD"] = 256] = "BOLD";
25
+ /** 省略 */
26
+ TextParserNodeType[TextParserNodeType["ELLIPSIS"] = 512] = "ELLIPSIS";
27
+ /** 任意表情 */
28
+ TextParserNodeType[TextParserNodeType["ANY_EMOJI"] = 112] = "ANY_EMOJI";
29
+ /** 任意链接 */
30
+ TextParserNodeType[TextParserNodeType["ANY_LINK"] = 132] = "ANY_LINK";
31
+ return TextParserNodeType;
20
32
  }({});
21
- export var TextParser = function TextParser(options) {
22
- var _this = this;
23
- this.options = options;
24
- this.parser = void 0;
25
- this.parse = function (options) {
26
- if (!options.store) {
27
- var _store = {
28
- maxLength: options.maxLength || 0,
29
- currentLength: 0,
30
- get remainingLength() {
31
- return _store.maxLength - _store.currentLength;
32
- },
33
- get isReachMaxLength() {
34
- return _store.maxLength > 0 && _store.currentLength >= _store.maxLength;
35
- },
36
- get isOverMaxLength() {
37
- return _store.maxLength > 0 && _store.currentLength > _store.maxLength;
38
- },
39
- hasEllipsis: false
40
- };
41
- options.store = _store;
42
- }
43
- var text = options.text;
44
- var asFragment = options.asFragment;
45
- var store = options.store;
46
- var paragraphs = [];
47
- var paragraph = [];
48
- for (var _iterator = _createForOfIteratorHelperLoose(_this.parser.toTree(text)), _step; !(_step = _iterator()).done;) {
49
- var item = _step.value;
50
- if (store.isReachMaxLength) {
51
- store.hasEllipsis = true;
52
- break;
53
- }
54
- var nodeType = item.type === 'text' ? TextParserNodeType.PLAIN_TEXT : item.type === 'url' ? TextParserNodeType.WEB_LINK : item.type;
55
- if (nodeType & TextParserNodeType.LINE_BREAK) {
56
- if (paragraph.length > 0) {
57
- paragraphs.push({
58
- type: TextParserNodeType.PARAGRAPH,
59
- nodes: paragraph
60
- });
61
- paragraph = [];
62
- }
63
- } else {
64
- var node = item.continue ? {
65
- type: nodeType,
66
- nodes: _this.parse({
67
- text: item.text,
68
- asFragment: true,
69
- store: store
70
- }),
71
- info: item.info
72
- } : {
73
- type: nodeType,
74
- content: item.text,
75
- info: item.info
76
- };
77
- paragraph.push(node);
78
- if (!item.continue) {
79
- var isEmoji = nodeType & TextParserNodeType.ANY_EMOJI;
80
- var isPlainText = nodeType & TextParserNodeType.PLAIN_TEXT;
81
- var chars = isEmoji ? [item.text] : Array.from(item.text);
82
- store.currentLength += chars.length;
83
- if (store.isReachMaxLength) {
84
- if (isPlainText && store.isOverMaxLength) {
85
- node.content = chars.slice(0, store.remainingLength).join('');
86
- store.hasEllipsis = true;
87
- }
88
- }
89
- }
90
- }
91
- }
92
- if (asFragment) {
93
- return paragraph;
94
- }
95
- if (paragraph.length > 0) {
96
- if (store.hasEllipsis) {
97
- paragraph.push({
98
- type: TextParserNodeType.ELLIPSIS
99
- });
100
- }
101
- paragraphs.push({
102
- type: TextParserNodeType.PARAGRAPH,
103
- nodes: paragraph
104
- });
105
- }
106
- return paragraphs;
107
- };
108
- var parser = new Parser();
109
- if (this.options.webLink) {
110
- parser.addPreset('url');
111
- }
112
- parser.addRule(/\r?\n/g, function (lineBreak) {
113
- return {
114
- type: TextParserNodeType.LINE_BREAK,
115
- text: lineBreak
116
- };
117
- });
118
- if (this.options.bold) {
119
- parser.addRule(/\*\*(.*?)\*\*/g, function (_, text) {
120
- return {
121
- type: TextParserNodeType.BOLD,
122
- text: text,
123
- continue: true
124
- };
125
- });
126
- }
127
- if (this.options.weixinEmoji) {
128
- parser.addRule(WeixinEmojiRender.regExp, function (weixinEmoji) {
129
- return {
130
- type: TextParserNodeType.WEIXIN_EMOJI,
131
- text: weixinEmoji,
132
- info: _this.options.weixinEmojiInfo ? WeixinEmojiRender.getInfo(weixinEmoji) : undefined
133
- };
134
- });
135
- }
136
- if (this.options.unifiedEmoji) {
137
- parser.addRule(UnifiedEmojiRender.regExp, function (unifiedEmoji) {
138
- return {
139
- type: TextParserNodeType.UNIFIED_EMOJI,
140
- text: unifiedEmoji,
141
- info: _this.options.unifiedEmojiInfo ? UnifiedEmojiRender.getInfo(unifiedEmoji) : undefined
142
- };
143
- });
144
- }
145
- if (this.options.douyinEmoji) {
146
- parser.addRule(DouyinEmojiRender.regExp, function (douyinEmoji) {
147
- return {
148
- type: TextParserNodeType.DOUYIN_EMOJI,
149
- text: douyinEmoji,
150
- info: _this.options.douyinEmojiInfo ? DouyinEmojiRender.getInfo(douyinEmoji) : undefined
151
- };
152
- });
153
- }
154
- if (this.options.miniProgramShortLink) {
155
- parser.addRule(/#小程序:\/\/[^\/]+\/\w+/g, function (miniProgramShortLink) {
156
- return {
157
- type: TextParserNodeType.MINI_PROGRAM_SHORT_LINK,
158
- text: miniProgramShortLink
159
- };
160
- });
161
- }
162
- this.parser = parser;
163
- };
33
+ var TextParser = class {
34
+ options;
35
+ parser;
36
+ constructor(options) {
37
+ this.options = options;
38
+ const parser = new Parser();
39
+ if (this.options.webLink) parser.addPreset("url");
40
+ parser.addRule(/\r?\n/g, (lineBreak) => {
41
+ return {
42
+ type: 8,
43
+ text: lineBreak
44
+ };
45
+ });
46
+ if (this.options.bold) parser.addRule(/\*\*(.*?)\*\*/g, (_, text) => {
47
+ return {
48
+ type: 256,
49
+ text,
50
+ continue: true
51
+ };
52
+ });
53
+ if (this.options.weixinEmoji) parser.addRule(WeixinEmojiRender.regExp, (weixinEmoji) => {
54
+ return {
55
+ type: 16,
56
+ text: weixinEmoji,
57
+ info: this.options.weixinEmojiInfo ? WeixinEmojiRender.getInfo(weixinEmoji) : void 0
58
+ };
59
+ });
60
+ if (this.options.unifiedEmoji) parser.addRule(UnifiedEmojiRender.regExp, (unifiedEmoji) => {
61
+ return {
62
+ type: 32,
63
+ text: unifiedEmoji,
64
+ info: this.options.unifiedEmojiInfo ? UnifiedEmojiRender.getInfo(unifiedEmoji) : void 0
65
+ };
66
+ });
67
+ if (this.options.douyinEmoji) parser.addRule(DouyinEmojiRender.regExp, (douyinEmoji) => {
68
+ return {
69
+ type: 64,
70
+ text: douyinEmoji,
71
+ info: this.options.douyinEmojiInfo ? DouyinEmojiRender.getInfo(douyinEmoji) : void 0
72
+ };
73
+ });
74
+ if (this.options.miniProgramShortLink) parser.addRule(/#小程序:\/\/[^/]+\/\w+/g, (miniProgramShortLink) => {
75
+ return {
76
+ type: 128,
77
+ text: miniProgramShortLink
78
+ };
79
+ });
80
+ this.parser = parser;
81
+ }
82
+ parse = (options) => {
83
+ if (!options.store) {
84
+ const store = {
85
+ maxLength: options.maxLength || 0,
86
+ currentLength: 0,
87
+ get remainingLength() {
88
+ return store.maxLength - store.currentLength;
89
+ },
90
+ get isReachMaxLength() {
91
+ return store.maxLength > 0 && store.currentLength >= store.maxLength;
92
+ },
93
+ get isOverMaxLength() {
94
+ return store.maxLength > 0 && store.currentLength > store.maxLength;
95
+ },
96
+ hasEllipsis: false
97
+ };
98
+ options.store = store;
99
+ }
100
+ const text = options.text;
101
+ const asFragment = options.asFragment;
102
+ const store = options.store;
103
+ const paragraphs = [];
104
+ let paragraph = [];
105
+ for (const item of this.parser.toTree(text)) {
106
+ if (store.isReachMaxLength) {
107
+ store.hasEllipsis = true;
108
+ break;
109
+ }
110
+ const nodeType = item.type === "text" ? 2 : item.type === "url" ? 4 : item.type;
111
+ if (nodeType & 8) {
112
+ if (paragraph.length > 0) {
113
+ paragraphs.push({
114
+ type: 1,
115
+ nodes: paragraph
116
+ });
117
+ paragraph = [];
118
+ }
119
+ } else {
120
+ const node = item.continue ? {
121
+ type: nodeType,
122
+ nodes: this.parse({
123
+ text: item.text,
124
+ asFragment: true,
125
+ store
126
+ }),
127
+ info: item.info
128
+ } : {
129
+ type: nodeType,
130
+ content: item.text,
131
+ info: item.info
132
+ };
133
+ paragraph.push(node);
134
+ if (!item.continue) {
135
+ const isEmoji = nodeType & 112;
136
+ const isPlainText = nodeType & 2;
137
+ const chars = isEmoji ? [item.text] : Array.from(item.text);
138
+ store.currentLength += chars.length;
139
+ if (store.isReachMaxLength) {
140
+ if (isPlainText && store.isOverMaxLength) {
141
+ node.content = chars.slice(0, store.remainingLength).join("");
142
+ store.hasEllipsis = true;
143
+ }
144
+ }
145
+ }
146
+ }
147
+ }
148
+ if (asFragment) return paragraph;
149
+ if (paragraph.length > 0) {
150
+ if (store.hasEllipsis) paragraph.push({ type: 512 });
151
+ paragraphs.push({
152
+ type: 1,
153
+ nodes: paragraph
154
+ });
155
+ }
156
+ return paragraphs;
157
+ };
158
+ };
159
+ export { TextParser, TextParserNodeType };
@@ -1,20 +1,20 @@
1
1
  export type TextRenderEmojiRenderer = 'img' | 'background';
2
2
  export interface TextRenderOptions {
3
- /** @default false */
4
- douyinEmoji?: boolean;
5
- /** @default false */
6
- weixinEmoji?: boolean;
7
- /** @default false */
8
- unifiedEmoji?: boolean;
9
- /** @default false */
10
- code?: boolean;
11
- /** @default false */
12
- math?: boolean;
13
- /** @default true */
14
- html?: boolean;
15
- /** @default 'img' */
16
- emojiRenderer?: TextRenderEmojiRenderer;
3
+ /** @default false */
4
+ douyinEmoji?: boolean;
5
+ /** @default false */
6
+ weixinEmoji?: boolean;
7
+ /** @default false */
8
+ unifiedEmoji?: boolean;
9
+ /** @default false */
10
+ code?: boolean;
11
+ /** @default false */
12
+ math?: boolean;
13
+ /** @default true */
14
+ html?: boolean;
15
+ /** @default 'img' */
16
+ emojiRenderer?: TextRenderEmojiRenderer;
17
17
  }
18
18
  export declare class TextRender {
19
- static render(text: string, options?: TextRenderOptions): string | Promise<string>;
20
- }
19
+ static render(text: string, options?: TextRenderOptions): string | Promise<string>;
20
+ }
@@ -1,27 +1,23 @@
1
- import { isPromiseLike } from 'vtils';
2
- import { CodeRender } from "./code_render";
3
- import { DouyinEmojiRender } from "./douyin_emoji_render";
4
- import { HTMLRender } from "./html_render";
5
- import { MathRender } from "./math_render";
6
- import { UnifiedEmojiRender } from "./unified_emoji_render";
7
- import { WeixinEmojiRender } from "./weixin_emoji_render";
8
- export var TextRender = /*#__PURE__*/function () {
9
- function TextRender() {}
10
- TextRender.render = function render(text, options) {
11
- if (options === void 0) {
12
- options = {};
13
- }
14
- var result = text;
15
- var _loop = function _loop() {
16
- var render = _arr[_i];
17
- result = isPromiseLike(result) ? result.then(function (temp) {
18
- return render.render(temp, options);
19
- }) : render.render(result, options);
20
- };
21
- for (var _i = 0, _arr = [options.douyinEmoji && DouyinEmojiRender, options.weixinEmoji && WeixinEmojiRender, options.unifiedEmoji && UnifiedEmojiRender, options.code && CodeRender, options.math && MathRender, options.html !== false && HTMLRender].filter(Boolean); _i < _arr.length; _i++) {
22
- _loop();
23
- }
24
- return result;
25
- };
26
- return TextRender;
27
- }();
1
+ import { CodeRender } from "./code_render.js";
2
+ import { DouyinEmojiRender } from "./douyin_emoji_render.js";
3
+ import { HTMLRender } from "./html_render.js";
4
+ import { MathRender } from "./math_render.js";
5
+ import { UnifiedEmojiRender } from "./unified_emoji_render.js";
6
+ import { WeixinEmojiRender } from "./weixin_emoji_render.js";
7
+ import { isPromiseLike } from "vtils";
8
+ // src/text_render.ts
9
+ var TextRender = class {
10
+ static render(text, options = {}) {
11
+ let result = text;
12
+ for (const render of [
13
+ options.douyinEmoji && DouyinEmojiRender,
14
+ options.weixinEmoji && WeixinEmojiRender,
15
+ options.unifiedEmoji && UnifiedEmojiRender,
16
+ options.code && CodeRender,
17
+ options.math && MathRender,
18
+ options.html !== false && HTMLRender
19
+ ].filter(Boolean)) result = isPromiseLike(result) ? result.then((temp) => render.render(temp, options)) : render.render(result, options);
20
+ return result;
21
+ }
22
+ };
23
+ export { TextRender };
@@ -1,4 +1,4 @@
1
- .xtr_10330 {
1
+ .xtr_16516 {
2
2
  display: inline-block;
3
3
  font-size: 1.2em;
4
4
  width: 1em;
@@ -6,7 +6,7 @@
6
6
  vertical-align: -0.2em;
7
7
  }
8
8
 
9
- .xtr_14492 {
9
+ .xtr_776 {
10
10
  background-size: 100% 100%;
11
11
  background-repeat: no-repeat;
12
12
  background-position: center;
@@ -1,12 +1,12 @@
1
- import type { TextRenderOptions } from './text_render';
1
+ import { TextRenderOptions } from "./text_render.js";
2
2
  export declare class UnifiedEmojiRender {
3
- static regExp: RegExp;
4
- static getEmojiImageUrl(emoji: string): string;
5
- static tryParseEmojiImageUrl(url: string): string;
6
- static getInfo: (emoji: string) => {
7
- imgUrl: string;
8
- imgClass: any;
9
- imgBackgroundClass: any;
10
- };
11
- static render(text: string, options: TextRenderOptions): string;
12
- }
3
+ static regExp: RegExp;
4
+ static getEmojiImageUrl(emoji: string): string;
5
+ static tryParseEmojiImageUrl(url: string): string;
6
+ static getInfo: (emoji: string) => {
7
+ imgUrl: string;
8
+ imgClass: any;
9
+ imgBackgroundClass: any;
10
+ };
11
+ static render(text: string, options: TextRenderOptions): string;
12
+ }