@difizen/libro-markdown 1.0.1 → 1.0.3
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 +21 -7
- package/package.json +2 -2
- package/src/markdown-render.ts +57 -48
|
@@ -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;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;
|
|
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;IA8DpB,MAAM,CAAC,YAAY,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,oBAAoB,GAAG,MAAM;CAKrE"}
|
package/es/markdown-render.js
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
2
|
var _dec, _dec2, _dec3, _class, _class2, _descriptor;
|
|
3
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
4
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
5
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
6
|
+
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
7
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
8
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
9
|
+
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
10
|
+
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
11
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
3
12
|
function _initializerDefineProperty(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); }
|
|
4
13
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
5
14
|
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
@@ -81,21 +90,26 @@ export var MarkdownRender = (_dec = singleton({
|
|
|
81
90
|
}, {
|
|
82
91
|
key: "sanitizeHTML",
|
|
83
92
|
value: function sanitizeHTML(html) {
|
|
93
|
+
var 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']);
|
|
94
|
+
// 构建新的 allowedAttributes,为所有允许的标签添加 'id'
|
|
95
|
+
var allowedAttributes = Object.fromEntries(allowedTags.map(function (tag) {
|
|
96
|
+
return [tag, [].concat(_toConsumableArray(sanitizeHtml.defaults.allowedAttributes[tag] || []), ['id'])];
|
|
97
|
+
}));
|
|
84
98
|
return sanitizeHtml(html, {
|
|
85
|
-
allowedTags:
|
|
99
|
+
allowedTags: allowedTags,
|
|
86
100
|
// 允许的标签
|
|
87
|
-
allowedAttributes: {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
}
|
|
101
|
+
allowedAttributes: _objectSpread(_objectSpread({}, allowedAttributes), {}, {
|
|
102
|
+
a: ['href', 'title', 'id'],
|
|
103
|
+
img: ['src', 'alt', 'id']
|
|
104
|
+
})
|
|
92
105
|
});
|
|
93
106
|
}
|
|
94
107
|
}, {
|
|
95
108
|
key: "render",
|
|
96
109
|
value: function render(markdownText, options) {
|
|
97
110
|
var unsanitizedRenderedMarkdown = this.mkt.render(markdownText, options);
|
|
98
|
-
|
|
111
|
+
var sanitizeHTML = this.sanitizeHTML(unsanitizedRenderedMarkdown);
|
|
112
|
+
return sanitizeHTML;
|
|
99
113
|
}
|
|
100
114
|
}]);
|
|
101
115
|
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.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"libro",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"src"
|
|
34
34
|
],
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@difizen/libro-common": "^1.0.
|
|
36
|
+
"@difizen/libro-common": "^1.0.3",
|
|
37
37
|
"@traptitech/markdown-it-katex": "^3.6.0",
|
|
38
38
|
"@types/markdown-it": "^12.2.3",
|
|
39
39
|
"katex": "^0.16.10",
|
package/src/markdown-render.ts
CHANGED
|
@@ -83,61 +83,70 @@ export class MarkdownRender implements MarkdownParser {
|
|
|
83
83
|
|
|
84
84
|
// 使用 sanitize-html 清理 HTML
|
|
85
85
|
private sanitizeHTML(html: string): string {
|
|
86
|
+
const allowedTags = sanitizeHtml.defaults.allowedTags.concat([
|
|
87
|
+
'a',
|
|
88
|
+
'abbr',
|
|
89
|
+
'acronym',
|
|
90
|
+
'b',
|
|
91
|
+
'blockquote',
|
|
92
|
+
'br',
|
|
93
|
+
'code',
|
|
94
|
+
'col',
|
|
95
|
+
'colgroup',
|
|
96
|
+
'dd',
|
|
97
|
+
'del',
|
|
98
|
+
'div',
|
|
99
|
+
'em',
|
|
100
|
+
'h1',
|
|
101
|
+
'h2',
|
|
102
|
+
'h3',
|
|
103
|
+
'h4',
|
|
104
|
+
'h5',
|
|
105
|
+
'h6',
|
|
106
|
+
'hr',
|
|
107
|
+
'i',
|
|
108
|
+
'img',
|
|
109
|
+
'li',
|
|
110
|
+
'ol',
|
|
111
|
+
'p',
|
|
112
|
+
'pre',
|
|
113
|
+
'q',
|
|
114
|
+
's',
|
|
115
|
+
'small',
|
|
116
|
+
'strong',
|
|
117
|
+
'sub',
|
|
118
|
+
'sup',
|
|
119
|
+
'table',
|
|
120
|
+
'tbody',
|
|
121
|
+
'td',
|
|
122
|
+
'th',
|
|
123
|
+
'tr',
|
|
124
|
+
'tt',
|
|
125
|
+
'u',
|
|
126
|
+
'ul',
|
|
127
|
+
'kbd',
|
|
128
|
+
'var',
|
|
129
|
+
]);
|
|
130
|
+
// 构建新的 allowedAttributes,为所有允许的标签添加 'id'
|
|
131
|
+
const allowedAttributes = Object.fromEntries(
|
|
132
|
+
allowedTags.map((tag) => [
|
|
133
|
+
tag,
|
|
134
|
+
[...(sanitizeHtml.defaults.allowedAttributes[tag] || []), 'id'],
|
|
135
|
+
]),
|
|
136
|
+
);
|
|
86
137
|
return sanitizeHtml(html, {
|
|
87
|
-
allowedTags
|
|
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
|
-
]), // 允许的标签
|
|
138
|
+
allowedTags, // 允许的标签
|
|
131
139
|
allowedAttributes: {
|
|
132
|
-
|
|
133
|
-
a: ['href', 'title'],
|
|
134
|
-
img: ['src', 'alt'],
|
|
140
|
+
...allowedAttributes,
|
|
141
|
+
a: ['href', 'title', 'id'],
|
|
142
|
+
img: ['src', 'alt', 'id'],
|
|
135
143
|
},
|
|
136
144
|
});
|
|
137
145
|
}
|
|
138
146
|
|
|
139
147
|
render(markdownText: string, options?: MarkdownRenderOption): string {
|
|
140
148
|
const unsanitizedRenderedMarkdown = this.mkt.render(markdownText, options);
|
|
141
|
-
|
|
149
|
+
const sanitizeHTML = this.sanitizeHTML(unsanitizedRenderedMarkdown);
|
|
150
|
+
return sanitizeHTML;
|
|
142
151
|
}
|
|
143
152
|
}
|