Skip to content

Commit 648f539

Browse files
committed
chore: add forcedSanitizeWhiteList
1 parent 879e32d commit 648f539

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

src/transform/md.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ function initCompiler(md: MarkdownIt, options: OptionsType, env: EnvType) {
168168
const html = md.renderer.render(tokens, md.options, env);
169169

170170
// Sanitize the page
171-
return needToSanitizeHtml ? sanitizeHtml(html, sanitizeOptions) : html;
171+
return needToSanitizeHtml
172+
? sanitizeHtml(html, sanitizeOptions, env.forcedSanitizeCssWhiteList)
173+
: html;
172174
};
173175
}
174176

src/transform/plugins/imsize/plugin.ts

+2
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ export const imageWithSize = (md: MarkdownIt, opts?: ImsizeOptions): ParserInlin
225225
if (height !== '') {
226226
if (width !== '' && !heightWithPercent && !widthWithPercent) {
227227
style += `aspect-ratio: ${width} / ${height};height: auto;`;
228+
state.env.forcedSanitizeCssWhiteList ??= {};
229+
state.env.forcedSanitizeCssWhiteList['aspect-ratio'] = true;
228230
} else {
229231
const heightString = heightWithPercent ? height : `${height}px`;
230232
style += `height: ${heightString};`;

src/transform/sanitize.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import cssfilter from 'cssfilter';
44
import * as cheerio from 'cheerio';
55
import css from 'css';
66

7+
import {CssWhiteList} from './typings';
8+
79
const htmlTags = [
810
'a',
911
'abbr',
@@ -492,8 +494,6 @@ const allowedTags = Array.from(
492494
);
493495
const allowedAttributes = Array.from(new Set([...htmlAttrs, ...svgAttrs, ...yfmHtmlAttrs]));
494496

495-
export type CssWhiteList = {[property: string]: boolean};
496-
497497
export interface SanitizeOptions extends sanitizeHtml.IOptions {
498498
cssWhiteList?: CssWhiteList;
499499
disableStyleSanitizer?: boolean;
@@ -598,9 +598,20 @@ function sanitizeStyles(html: string, options: SanitizeOptions) {
598598
return styles + content;
599599
}
600600

601-
export default function sanitize(html: string, options?: SanitizeOptions) {
601+
export default function sanitize(
602+
html: string,
603+
options?: SanitizeOptions,
604+
forcedSanitizeCssWhiteList?: CssWhiteList,
605+
) {
602606
const sanitizeOptions = options || defaultOptions;
603607

608+
if (forcedSanitizeCssWhiteList) {
609+
sanitizeOptions.cssWhiteList = {
610+
...sanitizeOptions.cssWhiteList,
611+
...forcedSanitizeCssWhiteList,
612+
};
613+
}
614+
604615
const needToSanitizeStyles = !(sanitizeOptions.disableStyleSanitizer ?? false);
605616

606617
const modifiedHtml = needToSanitizeStyles ? sanitizeStyles(html, sanitizeOptions) : html;

src/transform/typings.ts

+3
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export type EnvType<Extras extends {} = {}> = {
7474
assets?: unknown[];
7575
meta?: object;
7676
changelogs?: ChangelogItem[];
77+
forcedSanitizeCssWhiteList?: CssWhiteList;
7778
} & Extras;
7879

7980
export interface MarkdownItPluginOpts {
@@ -98,3 +99,5 @@ export type MarkdownItPluginCb<T extends {} = {}> = {
9899
export type MarkdownItPreprocessorCb<T extends unknown = {}> = {
99100
(input: string, opts: T & Partial<MarkdownItPluginOpts>, md?: MarkdownIt): string;
100101
};
102+
103+
export type CssWhiteList = {[property: string]: boolean};

0 commit comments

Comments
 (0)