Skip to content

Commit 634f7f8

Browse files
authored
feat: add html sanitizing [DOCSTOOLS-1350] (#177)
Adding utility, that sanitizing HTML. By default yfm-transform not sanitize HTML. Use needToSanitizeHtml: true option to enable sanitizing * feat: add html sanitizing * feat(sanitize): allow override default options * feat(sanitize): remove unnecessary schemes from default options * fix(package): move types of sanitize-html from devDependencies to dependencies
1 parent 4e0bc9b commit 634f7f8

File tree

6 files changed

+628
-4
lines changed

6 files changed

+628
-4
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
dist
2+
lib

package-lock.json

+97-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"prepublishOnly": "npm run lint && npm run test && npm run build"
3131
},
3232
"dependencies": {
33+
"@types/sanitize-html": "^2.6.2",
3334
"chalk": "4.1.2",
3435
"get-root-node-polyfill": "1.0.0",
3536
"github-slugger": "1.4.0",
@@ -42,6 +43,7 @@
4243
"markdownlint": "^0.25.1",
4344
"markdownlint-rule-helpers": "0.17.2",
4445
"postcss": "8.4.16",
46+
"sanitize-html": "^2.7.1",
4547
"slugify": "1.6.5"
4648
},
4749
"devDependencies": {

src/transform/index.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import makeHighlight from './highlight';
77
import extractTitle from './title';
88
import getHeadings from './headings';
99
import liquid from './liquid';
10+
import sanitizeHtml, {SanitizeOptions} from './sanitize';
1011

1112
import notes from './plugins/notes';
1213
import anchors from './plugins/anchors';
@@ -24,7 +25,7 @@ import monospace from './plugins/monospace';
2425
import yfmTable from './plugins/table';
2526
import {initMd} from './md';
2627
import {MarkdownItPluginCb} from './plugins/typings';
27-
import {HighlightLangMap, Heading} from './typings';
28+
import type {HighlightLangMap, Heading} from './typings';
2829

2930
interface OutputType {
3031
result: {
@@ -49,6 +50,8 @@ interface OptionsType {
4950
leftDelimiter?: string;
5051
rightDelimiter?: string;
5152
isLiquided?: boolean;
53+
needToSanitizeHtml?: boolean;
54+
sanitizeOptions?: SanitizeOptions;
5255
needFlatListHeadings?: boolean;
5356
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5457
plugins?: MarkdownItPluginCb<any>[];
@@ -67,6 +70,8 @@ function transform(originInput: string, opts: OptionsType = {}): OutputType {
6770
linkify = false,
6871
breaks = true,
6972
conditionsInCode = false,
73+
needToSanitizeHtml = false,
74+
sanitizeOptions,
7075
needFlatListHeadings = false,
7176
disableLiquid = false,
7277
leftDelimiter = '{',
@@ -137,7 +142,11 @@ function transform(originInput: string, opts: OptionsType = {}): OutputType {
137142

138143
// add all term template tokens to the end of the html
139144
const termTokens = (env.termTokens as Token[]) || [];
140-
const html = md.renderer.render([...tokens, ...termTokens], md.options, env);
145+
let html = md.renderer.render([...tokens, ...termTokens], md.options, env);
146+
if (needToSanitizeHtml) {
147+
html = sanitizeHtml(html, sanitizeOptions);
148+
}
149+
141150
const assets = md.assets;
142151
const meta = md.meta;
143152

0 commit comments

Comments
 (0)