@difizen/libro-markdown 1.0.0 → 1.0.1
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/es/markdown-render.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ export declare class MarkdownRender implements MarkdownParser {
|
|
|
9
9
|
enablePermalink: boolean;
|
|
10
10
|
protected configurationService: ConfigurationService;
|
|
11
11
|
init(): void;
|
|
12
|
+
private sanitizeHTML;
|
|
12
13
|
render(markdownText: string, options?: MarkdownRenderOption): string;
|
|
13
14
|
}
|
|
14
15
|
//# sourceMappingURL=markdown-render.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"markdown-render.d.ts","sourceRoot":"","sources":["../src/markdown-render.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EAIrB,MAAM,2BAA2B,CAAC;AAEnC,OAAO,UAAU,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"markdown-render.d.ts","sourceRoot":"","sources":["../src/markdown-render.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EAIrB,MAAM,2BAA2B,CAAC;AAEnC,OAAO,UAAU,MAAM,aAAa,CAAC;AAKrC,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AACnE,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AACxD,OAAO,0BAA0B,CAAC;AAElC,qBACa,cAAe,YAAW,cAAc;IACnD,SAAS,CAAC,GAAG,EAAE,UAAU,CAAC;IAC1B,OAAO,wBAAW;IAClB,eAAe,UAAS;IACM,SAAS,CAAC,oBAAoB,EAAE,oBAAoB,CAAC;IAGnF,IAAI;IA4DJ,OAAO,CAAC,YAAY;IAsDpB,MAAM,CAAC,YAAY,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,oBAAoB,GAAG,MAAM;CAIrE"}
|
package/es/markdown-render.js
CHANGED
|
@@ -11,6 +11,7 @@ function _initializerWarningHelper(descriptor, context) { throw new Error('Decor
|
|
|
11
11
|
import { ConfigurationService, inject, postConstruct, singleton } from '@difizen/libro-common/app';
|
|
12
12
|
import latexPlugin from '@traptitech/markdown-it-katex';
|
|
13
13
|
import MarkdownIt from 'markdown-it';
|
|
14
|
+
import sanitizeHtml from 'sanitize-html';
|
|
14
15
|
import { libroAnchor, linkInsideHeader, slugify } from "./anchor.js";
|
|
15
16
|
import { LibroMarkdownConfiguration } from "./config.js";
|
|
16
17
|
import { MarkdownParser } from "./markdown-protocol.js";
|
|
@@ -75,11 +76,26 @@ export var MarkdownRender = (_dec = singleton({
|
|
|
75
76
|
//
|
|
76
77
|
});
|
|
77
78
|
}
|
|
79
|
+
|
|
80
|
+
// 使用 sanitize-html 清理 HTML
|
|
81
|
+
}, {
|
|
82
|
+
key: "sanitizeHTML",
|
|
83
|
+
value: function sanitizeHTML(html) {
|
|
84
|
+
return sanitizeHtml(html, {
|
|
85
|
+
allowedTags: sanitizeHtml.defaults.allowedTags.concat(['a', 'abbr', 'acronym', 'b', 'blockquote', 'br', 'code', 'col', 'colgroup', 'dd', 'del', 'div', 'em', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'img', 'li', 'ol', 'p', 'pre', 'q', 's', 'small', 'strong', 'sub', 'sup', 'table', 'tbody', 'td', 'th', 'tr', 'tt', 'u', 'ul', 'kbd', 'var']),
|
|
86
|
+
// 允许的标签
|
|
87
|
+
allowedAttributes: {
|
|
88
|
+
// 允许的属性
|
|
89
|
+
a: ['href', 'title'],
|
|
90
|
+
img: ['src', 'alt']
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
78
94
|
}, {
|
|
79
95
|
key: "render",
|
|
80
96
|
value: function render(markdownText, options) {
|
|
81
97
|
var unsanitizedRenderedMarkdown = this.mkt.render(markdownText, options);
|
|
82
|
-
return unsanitizedRenderedMarkdown;
|
|
98
|
+
return this.sanitizeHTML(unsanitizedRenderedMarkdown);
|
|
83
99
|
}
|
|
84
100
|
}]);
|
|
85
101
|
return MarkdownRender;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@difizen/libro-markdown",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"libro",
|
|
@@ -33,12 +33,16 @@
|
|
|
33
33
|
"src"
|
|
34
34
|
],
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@difizen/libro-common": "^1.0.
|
|
36
|
+
"@difizen/libro-common": "^1.0.1",
|
|
37
37
|
"@traptitech/markdown-it-katex": "^3.6.0",
|
|
38
38
|
"@types/markdown-it": "^12.2.3",
|
|
39
39
|
"katex": "^0.16.10",
|
|
40
40
|
"markdown-it": "^13.0.1",
|
|
41
|
-
"markdown-it-anchor": "^8.6.5"
|
|
41
|
+
"markdown-it-anchor": "^8.6.5",
|
|
42
|
+
"sanitize-html": "^2.14.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@types/sanitize-html": "^2.13.0"
|
|
42
46
|
},
|
|
43
47
|
"scripts": {
|
|
44
48
|
"setup": "father build",
|
package/src/markdown-render.ts
CHANGED
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
} from '@difizen/libro-common/app';
|
|
7
7
|
import latexPlugin from '@traptitech/markdown-it-katex';
|
|
8
8
|
import MarkdownIt from 'markdown-it';
|
|
9
|
+
import sanitizeHtml from 'sanitize-html';
|
|
9
10
|
|
|
10
11
|
import { libroAnchor, linkInsideHeader, slugify } from './anchor.js';
|
|
11
12
|
import { LibroMarkdownConfiguration } from './config.js';
|
|
@@ -32,7 +33,9 @@ export class MarkdownRender implements MarkdownParser {
|
|
|
32
33
|
permalink: this.enablePermalink ? linkInsideHeader : false,
|
|
33
34
|
slugify: this.slugify,
|
|
34
35
|
});
|
|
36
|
+
|
|
35
37
|
this.mkt.use(latexPlugin);
|
|
38
|
+
|
|
36
39
|
this.configurationService
|
|
37
40
|
.get(LibroMarkdownConfiguration.TargetToBlank)
|
|
38
41
|
.then((value) => {
|
|
@@ -78,8 +81,63 @@ export class MarkdownRender implements MarkdownParser {
|
|
|
78
81
|
});
|
|
79
82
|
}
|
|
80
83
|
|
|
84
|
+
// 使用 sanitize-html 清理 HTML
|
|
85
|
+
private sanitizeHTML(html: string): string {
|
|
86
|
+
return sanitizeHtml(html, {
|
|
87
|
+
allowedTags: sanitizeHtml.defaults.allowedTags.concat([
|
|
88
|
+
'a',
|
|
89
|
+
'abbr',
|
|
90
|
+
'acronym',
|
|
91
|
+
'b',
|
|
92
|
+
'blockquote',
|
|
93
|
+
'br',
|
|
94
|
+
'code',
|
|
95
|
+
'col',
|
|
96
|
+
'colgroup',
|
|
97
|
+
'dd',
|
|
98
|
+
'del',
|
|
99
|
+
'div',
|
|
100
|
+
'em',
|
|
101
|
+
'h1',
|
|
102
|
+
'h2',
|
|
103
|
+
'h3',
|
|
104
|
+
'h4',
|
|
105
|
+
'h5',
|
|
106
|
+
'h6',
|
|
107
|
+
'hr',
|
|
108
|
+
'i',
|
|
109
|
+
'img',
|
|
110
|
+
'li',
|
|
111
|
+
'ol',
|
|
112
|
+
'p',
|
|
113
|
+
'pre',
|
|
114
|
+
'q',
|
|
115
|
+
's',
|
|
116
|
+
'small',
|
|
117
|
+
'strong',
|
|
118
|
+
'sub',
|
|
119
|
+
'sup',
|
|
120
|
+
'table',
|
|
121
|
+
'tbody',
|
|
122
|
+
'td',
|
|
123
|
+
'th',
|
|
124
|
+
'tr',
|
|
125
|
+
'tt',
|
|
126
|
+
'u',
|
|
127
|
+
'ul',
|
|
128
|
+
'kbd',
|
|
129
|
+
'var',
|
|
130
|
+
]), // 允许的标签
|
|
131
|
+
allowedAttributes: {
|
|
132
|
+
// 允许的属性
|
|
133
|
+
a: ['href', 'title'],
|
|
134
|
+
img: ['src', 'alt'],
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
81
139
|
render(markdownText: string, options?: MarkdownRenderOption): string {
|
|
82
140
|
const unsanitizedRenderedMarkdown = this.mkt.render(markdownText, options);
|
|
83
|
-
return unsanitizedRenderedMarkdown;
|
|
141
|
+
return this.sanitizeHTML(unsanitizedRenderedMarkdown);
|
|
84
142
|
}
|
|
85
143
|
}
|