@difizen/libro-markdown 0.3.4 → 0.3.5
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.map +1 -1
- package/es/markdown-render.js +10 -0
- package/package.json +6 -2
- package/src/markdown-render.ts +59 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"markdown-render.d.ts","sourceRoot":"","sources":["../src/markdown-render.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EAIrB,MAAM,mBAAmB,CAAC;AAE3B,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,mBAAmB,CAAC;AAE3B,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;IAgEJ,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/mana-app';
|
|
12
12
|
import latexPlugin from '@traptitech/markdown-it-katex';
|
|
13
13
|
import MarkdownIt from 'markdown-it';
|
|
14
|
+
import MarkdownItSanitizer from 'markdown-it-sanitizer';
|
|
14
15
|
import { libroAnchor, linkInsideHeader, slugify } from "./anchor.js";
|
|
15
16
|
import { LibroMarkdownConfiguration } from "./config.js";
|
|
16
17
|
import { MarkdownParser } from "./markdown-protocol.js";
|
|
@@ -44,6 +45,15 @@ export var MarkdownRender = (_dec = singleton({
|
|
|
44
45
|
slugify: this.slugify
|
|
45
46
|
});
|
|
46
47
|
this.mkt.use(latexPlugin);
|
|
48
|
+
this.mkt.use(MarkdownItSanitizer, {
|
|
49
|
+
allowedTags: ['p', 'a', 'img', 'strong', 'em'],
|
|
50
|
+
// 允许的标签
|
|
51
|
+
allowedAttributes: {
|
|
52
|
+
a: ['href', 'target'],
|
|
53
|
+
// 允许的属性
|
|
54
|
+
img: ['src', 'alt']
|
|
55
|
+
}
|
|
56
|
+
});
|
|
47
57
|
this.configurationService.get(LibroMarkdownConfiguration.TargetToBlank).then(function (value) {
|
|
48
58
|
if (value) {
|
|
49
59
|
_this.mkt.use(function (md) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@difizen/libro-markdown",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.5",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"libro",
|
|
@@ -39,7 +39,11 @@
|
|
|
39
39
|
"@types/markdown-it": "^12.2.3",
|
|
40
40
|
"katex": "^0.16.10",
|
|
41
41
|
"markdown-it": "^13.0.1",
|
|
42
|
-
"markdown-it-anchor": "^8.6.5"
|
|
42
|
+
"markdown-it-anchor": "^8.6.5",
|
|
43
|
+
"sanitize-html": "^2.14.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/sanitize-html": "^2.13.0"
|
|
43
47
|
},
|
|
44
48
|
"scripts": {
|
|
45
49
|
"setup": "father build",
|
package/src/markdown-render.ts
CHANGED
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
} from '@difizen/mana-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
|
}
|