1
- // const showdown = require("showdown")
2
- // const converter = new showdown.Converter();
3
- //
4
- // /**
5
- // * 将Markdown转换为HTML
6
- // * @param md Markdown
7
- // * @returns {* } HTML
8
- // */
9
- // export function mdToHtml(md: string) {
10
- // let html = converter.makeHtml(md);
11
- // return removeWidgetTag(html);
12
- // }
13
- // instdead by lute
14
-
15
1
import { renderHTML } from "./markdownUtil" ;
16
2
17
3
/**
@@ -33,7 +19,7 @@ export function removeTitleNumber(str: string) {
33
19
* @param str 原字符
34
20
* @returns {*|string } 删除后的字符
35
21
*/
36
- export function removeWidgetTag ( str : string ) {
22
+ function removeWidgetTag ( str : string ) {
37
23
let newstr = str
38
24
39
25
// 旧版发布挂件
@@ -54,24 +40,6 @@ export function removeWidgetTag(str: string) {
54
40
return newstr
55
41
}
56
42
57
- /**
58
- * 截取指定长度html
59
- * @param html html
60
- * @param length 长度
61
- * @param ignore 不要结尾省略号
62
- * @returns {string } 结果
63
- */
64
- export function parseHtml ( html : string , length : number , ignore ?: boolean ) {
65
- let allText = filterHtml ( html ) ;
66
- if ( allText . length < length ) {
67
- return allText ;
68
- }
69
- if ( ignore ) {
70
- return allText . substring ( 0 , length ) ;
71
- }
72
- return allText . substring ( 0 , length ) + "..." ;
73
- }
74
-
75
43
/**
76
44
* 去除html标签,残缺不全也可以
77
45
* @param str
@@ -115,13 +83,43 @@ function filterHtml(str: string) {
115
83
return str ;
116
84
}
117
85
86
+ /**
87
+ * 去除挂件占位符HTML、标题序号等
88
+ * @param html 原始HTML
89
+ */
90
+ export function prettyHtml ( html : string ) {
91
+ let newHtml = html
92
+ newHtml = removeWidgetTag ( newHtml )
93
+ newHtml = removeTitleNumber ( newHtml )
94
+ return newHtml
95
+ }
96
+
97
+ /**
98
+ * 截取指定长度字符
99
+ * @param text
100
+ * @param length 长度
101
+ * @param ignore 不要结尾省略号
102
+ * @returns {string } 结果
103
+ */
104
+ export function subText ( text : string , length : number , ignore ?: boolean ) {
105
+ let allText = text
106
+ if ( allText . length < length ) {
107
+ return allText ;
108
+ }
109
+ if ( ignore ) {
110
+ return allText . substring ( 0 , length ) ;
111
+ }
112
+ return allText . substring ( 0 , length ) + "..." ;
113
+ }
114
+
118
115
/**
119
116
* 将Markdown转换为纯文本
120
117
* @param md
121
118
* @returns {string }
122
119
*/
123
- export function mdToPlanText ( md : string ) {
120
+ export function mdToPlainText ( md : string ) {
124
121
let html = renderHTML ( md )
125
- html = removeWidgetTag ( html )
126
- return filterHtml ( html )
122
+ html = prettyHtml ( html )
123
+ html = filterHtml ( html )
124
+ return html
127
125
}
0 commit comments