Skip to content

Commit 09f1cbf

Browse files
committed
chore: add forcedSanitizeWhiteList
1 parent 879e32d commit 09f1cbf

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

src/transform/constants.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import {CssWhiteList} from './typings';
2+
3+
export const forcedSanitizeWhiteList: CssWhiteList = {};

src/transform/plugins/imsize/plugin.ts

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import type MarkdownIt from 'markdown-it';
22
import type ParserInline from 'markdown-it/lib/parser_inline';
33
import type Token from 'markdown-it/lib/token';
44

5+
import {forcedSanitizeWhiteList} from '../../constants';
6+
57
import {ImsizeAttr} from './const';
68
import {parseImageSize} from './helpers';
79

@@ -225,6 +227,7 @@ export const imageWithSize = (md: MarkdownIt, opts?: ImsizeOptions): ParserInlin
225227
if (height !== '') {
226228
if (width !== '' && !heightWithPercent && !widthWithPercent) {
227229
style += `aspect-ratio: ${width} / ${height};height: auto;`;
230+
forcedSanitizeWhiteList['aspect-ratio'] = true;
228231
} else {
229232
const heightString = heightWithPercent ? height : `${height}px`;
230233
style += `height: ${heightString};`;

src/transform/sanitize.ts

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

7+
import {forcedSanitizeWhiteList} from './constants';
8+
import {CssWhiteList} from './typings';
9+
710
const htmlTags = [
811
'a',
912
'abbr',
@@ -492,8 +495,6 @@ const allowedTags = Array.from(
492495
);
493496
const allowedAttributes = Array.from(new Set([...htmlAttrs, ...svgAttrs, ...yfmHtmlAttrs]));
494497

495-
export type CssWhiteList = {[property: string]: boolean};
496-
497498
export interface SanitizeOptions extends sanitizeHtml.IOptions {
498499
cssWhiteList?: CssWhiteList;
499500
disableStyleSanitizer?: boolean;
@@ -584,7 +585,7 @@ function sanitizeStyleAttrs(dom: cheerio.CheerioAPI, cssWhiteList: CssWhiteList)
584585
}
585586

586587
function sanitizeStyles(html: string, options: SanitizeOptions) {
587-
const cssWhiteList = options.cssWhiteList || {};
588+
const cssWhiteList = {...forcedSanitizeWhiteList, ...(options.cssWhiteList || {})};
588589

589590
const $ = cheerio.load(html);
590591

src/transform/typings.ts

+2
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,5 @@ export type MarkdownItPluginCb<T extends {} = {}> = {
9898
export type MarkdownItPreprocessorCb<T extends unknown = {}> = {
9999
(input: string, opts: T & Partial<MarkdownItPluginOpts>, md?: MarkdownIt): string;
100100
};
101+
102+
export type CssWhiteList = {[property: string]: boolean};

0 commit comments

Comments
 (0)