From 421203013197532a1ce009a85dd39cee370a9e5c Mon Sep 17 00:00:00 2001 From: saxumcordis Date: Mon, 31 Mar 2025 18:16:47 +0300 Subject: [PATCH 1/4] fix sanitize --- src/extensions/markdown/Html/schema.ts | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/extensions/markdown/Html/schema.ts b/src/extensions/markdown/Html/schema.ts index 136eda4ef..15dc1d9c6 100644 --- a/src/extensions/markdown/Html/schema.ts +++ b/src/extensions/markdown/Html/schema.ts @@ -1,4 +1,5 @@ -import sanitize from '@diplodoc/transform/lib/sanitize.js'; +import type {SanitizeOptions} from '@diplodoc/transform/lib/sanitize.js'; +import * as sanitizeModule from '@diplodoc/transform/lib/sanitize.js'; import type {NodeSpec} from 'prosemirror-model'; import {HtmlAttr, HtmlNode} from './const'; @@ -8,6 +9,29 @@ enum DomAttr { HtmlRaw = 'data-html-raw', } +type SanitizeFn = ( + html: string, + options?: SanitizeOptions, + additionalOptions?: SanitizeOptions, +) => string; + +interface SanitizeModule { + sanitize?: SanitizeFn; + default?: SanitizeFn; +} + +const sanitizeAll = () => { + console.warn('[YfmHtmlBlock]: sanitize function not found'); + return ''; +}; +const getSanitizeFunction = (): SanitizeFn => { + const module = sanitizeModule as SanitizeModule; + const sanitize = 'sanitize' in module && module.sanitize ? module.sanitize : module.default; + return sanitize instanceof Function ? sanitize : sanitizeAll; +}; + +const sanitize = getSanitizeFunction(); + export const schemaSpecs: Record = { [HtmlNode.Block]: { atom: true, From 934068780fcfcf5839407c04f187ce1a48a90630 Mon Sep 17 00:00:00 2001 From: saxumcordis Date: Mon, 31 Mar 2025 18:28:01 +0300 Subject: [PATCH 2/4] fix warn title --- src/extensions/markdown/Html/schema.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/extensions/markdown/Html/schema.ts b/src/extensions/markdown/Html/schema.ts index 15dc1d9c6..7d4c8d537 100644 --- a/src/extensions/markdown/Html/schema.ts +++ b/src/extensions/markdown/Html/schema.ts @@ -21,7 +21,7 @@ interface SanitizeModule { } const sanitizeAll = () => { - console.warn('[YfmHtmlBlock]: sanitize function not found'); + console.warn('[Html]: sanitize function not found'); return ''; }; const getSanitizeFunction = (): SanitizeFn => { From 77be60dc990b3476e7343ec62ba82101c1ac7dda Mon Sep 17 00:00:00 2001 From: saxumcordis Date: Mon, 31 Mar 2025 18:33:34 +0300 Subject: [PATCH 3/4] not duplicate code --- .../additional/YfmHtmlBlock/utils.ts | 27 +++---------------- src/extensions/markdown/Html/schema.ts | 26 ++---------------- src/utils/getSanitize.ts | 23 ++++++++++++++++ 3 files changed, 28 insertions(+), 48 deletions(-) create mode 100644 src/utils/getSanitize.ts diff --git a/src/extensions/additional/YfmHtmlBlock/utils.ts b/src/extensions/additional/YfmHtmlBlock/utils.ts index dab8032c4..dbe978b7f 100644 --- a/src/extensions/additional/YfmHtmlBlock/utils.ts +++ b/src/extensions/additional/YfmHtmlBlock/utils.ts @@ -1,29 +1,8 @@ -import type {SanitizeOptions} from '@diplodoc/transform/lib/sanitize.js'; -import * as sanitizeModule from '@diplodoc/transform/lib/sanitize.js'; - -type SanitizeFn = ( - html: string, - options?: SanitizeOptions, - additionalOptions?: SanitizeOptions, -) => string; - -interface SanitizeModule { - sanitize?: SanitizeFn; - default?: SanitizeFn; -} - -const sanitizeAll = () => { - console.warn('[YfmHtmlBlock]: sanitize function not found'); - return ''; -}; -const getSanitizeFunction = (): SanitizeFn => { - const module = sanitizeModule as SanitizeModule; - const sanitize = 'sanitize' in module && module.sanitize ? module.sanitize : module.default; - return sanitize instanceof Function ? sanitize : sanitizeAll; -}; +import type { SanitizeOptions } from '@diplodoc/transform/lib/sanitize'; +import { getSanitize } from 'src/utils/getSanitize'; // MAJOR: use `import {sanitize} from '@diplodoc/transform/lib/sanitize.js'` -const diplodocSanitize = getSanitizeFunction(); +const diplodocSanitize = getSanitize('YfmHtmlBlock'); // yfmHtmlBlock additional css properties white list const getYfmHtmlBlockWhiteList = () => { diff --git a/src/extensions/markdown/Html/schema.ts b/src/extensions/markdown/Html/schema.ts index 7d4c8d537..e66911b5a 100644 --- a/src/extensions/markdown/Html/schema.ts +++ b/src/extensions/markdown/Html/schema.ts @@ -1,36 +1,14 @@ -import type {SanitizeOptions} from '@diplodoc/transform/lib/sanitize.js'; -import * as sanitizeModule from '@diplodoc/transform/lib/sanitize.js'; import type {NodeSpec} from 'prosemirror-model'; import {HtmlAttr, HtmlNode} from './const'; +import { getSanitize } from 'src/utils/getSanitize'; enum DomAttr { Html = 'data-html', HtmlRaw = 'data-html-raw', } -type SanitizeFn = ( - html: string, - options?: SanitizeOptions, - additionalOptions?: SanitizeOptions, -) => string; - -interface SanitizeModule { - sanitize?: SanitizeFn; - default?: SanitizeFn; -} - -const sanitizeAll = () => { - console.warn('[Html]: sanitize function not found'); - return ''; -}; -const getSanitizeFunction = (): SanitizeFn => { - const module = sanitizeModule as SanitizeModule; - const sanitize = 'sanitize' in module && module.sanitize ? module.sanitize : module.default; - return sanitize instanceof Function ? sanitize : sanitizeAll; -}; - -const sanitize = getSanitizeFunction(); +const sanitize = getSanitize('Html'); export const schemaSpecs: Record = { [HtmlNode.Block]: { diff --git a/src/utils/getSanitize.ts b/src/utils/getSanitize.ts new file mode 100644 index 000000000..86e88a51d --- /dev/null +++ b/src/utils/getSanitize.ts @@ -0,0 +1,23 @@ +import type {SanitizeOptions} from '@diplodoc/transform/lib/sanitize.js'; +import * as sanitizeModule from '@diplodoc/transform/lib/sanitize.js'; + +type SanitizeFn = ( + html: string, + options?: SanitizeOptions, + additionalOptions?: SanitizeOptions, +) => string; + +interface SanitizeModule { + sanitize?: SanitizeFn; + default?: SanitizeFn; +} + +const sanitizeAll = (blockName: string) => () => { + console.warn(`[${blockName}]: sanitize function not found`); + return ''; +}; +export const getSanitize = (blockName: string): SanitizeFn => { + const module = sanitizeModule as SanitizeModule; + const sanitize = 'sanitize' in module && module.sanitize ? module.sanitize : module.default; + return sanitize instanceof Function ? sanitize : sanitizeAll(blockName); +}; From b4cb4b147ed86f5892dc440e17e8320f01e82742 Mon Sep 17 00:00:00 2001 From: saxumcordis Date: Mon, 31 Mar 2025 18:35:22 +0300 Subject: [PATCH 4/4] not duplcate code --- src/extensions/additional/YfmHtmlBlock/utils.ts | 5 +++-- src/extensions/markdown/Html/schema.ts | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/extensions/additional/YfmHtmlBlock/utils.ts b/src/extensions/additional/YfmHtmlBlock/utils.ts index dbe978b7f..9b7d2ad6f 100644 --- a/src/extensions/additional/YfmHtmlBlock/utils.ts +++ b/src/extensions/additional/YfmHtmlBlock/utils.ts @@ -1,5 +1,6 @@ -import type { SanitizeOptions } from '@diplodoc/transform/lib/sanitize'; -import { getSanitize } from 'src/utils/getSanitize'; +import type {SanitizeOptions} from '@diplodoc/transform/lib/sanitize'; + +import {getSanitize} from 'src/utils/getSanitize'; // MAJOR: use `import {sanitize} from '@diplodoc/transform/lib/sanitize.js'` const diplodocSanitize = getSanitize('YfmHtmlBlock'); diff --git a/src/extensions/markdown/Html/schema.ts b/src/extensions/markdown/Html/schema.ts index e66911b5a..9dc1b87f5 100644 --- a/src/extensions/markdown/Html/schema.ts +++ b/src/extensions/markdown/Html/schema.ts @@ -1,7 +1,8 @@ import type {NodeSpec} from 'prosemirror-model'; +import {getSanitize} from 'src/utils/getSanitize'; + import {HtmlAttr, HtmlNode} from './const'; -import { getSanitize } from 'src/utils/getSanitize'; enum DomAttr { Html = 'data-html',