Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(view): added option to control HTML block runtimes #674

Merged
merged 2 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion demo/components/Playground.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {type CSSProperties, memo, useCallback, useEffect, useMemo, useState} from 'react';

import type {EmbeddingMode} from '@diplodoc/html-extension';
import {defaultOptions} from '@diplodoc/transform/lib/sanitize';
import {Button, DropdownMenu} from '@gravity-ui/uikit';

Expand Down Expand Up @@ -41,7 +42,7 @@
import {SplitModePreview} from './SplitModePreview';

const fileUploadHandler: FileUploadHandler = async (file) => {
console.info('[Playground] Uploading file: ' + file.name);

Check warning on line 45 in demo/components/Playground.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
await randomDelay(1000, 3000);
return {url: URL.createObjectURL(file)};
};
Expand Down Expand Up @@ -77,6 +78,7 @@
onChangeEditorType?: (mode: MarkdownEditorMode) => void;
onChangeSplitModeEnabled?: (splitModeEnabled: boolean) => void;
directiveSyntax?: DirectiveSyntaxValue;
disabledHTMLBlockModes?: EmbeddingMode[];
} & Pick<UseMarkdownEditorProps, 'experimental' | 'wysiwygConfig'> &
Pick<
MarkdownEditorViewProps,
Expand All @@ -89,12 +91,12 @@
>;

logger.setLogger({
log: (...data) => console.log('[Deprecated logger]', ...data),

Check warning on line 94 in demo/components/Playground.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
info: (...data) => console.info('[Deprecated logger]', ...data),

Check warning on line 95 in demo/components/Playground.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
warn: (...data) => console.warn('[Deprecated logger]', ...data),

Check warning on line 96 in demo/components/Playground.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
error: (...data) => console.error('[Deprecated logger]', ...data),

Check warning on line 97 in demo/components/Playground.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
metrics: (...data) => console.info('[Deprecated logger]', ...data),

Check warning on line 98 in demo/components/Playground.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
action: (data) => console.info(`[Deprecated logger] Action: ${data.action}`, data),

Check warning on line 99 in demo/components/Playground.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
});

export const Playground = memo<PlaygroundProps>((props) => {
Expand Down Expand Up @@ -125,6 +127,7 @@
hidePreviewAfterSubmit,
experimental,
directiveSyntax,
disabledHTMLBlockModes,
} = props;
const [editorMode, setEditorMode] = useState<MarkdownEditorMode>(initialEditor ?? 'wysiwyg');
const [mdRaw, setMdRaw] = useState<MarkupString>(initial || '');
Expand All @@ -134,7 +137,7 @@
}, [mdRaw]);

const renderPreview = useCallback<RenderPreview>(
({getValue, md, directiveSyntax}) => (

Check warning on line 140 in demo/components/Playground.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

'directiveSyntax' is already declared in the upper scope on line 129 column 9
<SplitModePreview
getValue={getValue}
allowHTML={md.html}
Expand All @@ -143,12 +146,13 @@
breaks={md.breaks}
needToSanitizeHtml={sanitizeHtml}
plugins={getPlugins({directiveSyntax})}
htmlRuntimeConfig={{disabledModes: disabledHTMLBlockModes}}
/>
),
[sanitizeHtml],
[sanitizeHtml, disabledHTMLBlockModes],
);

const logger = useMemo(() => new Logger2().nested({env: 'playground'}), []);

Check warning on line 155 in demo/components/Playground.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

'logger' is already declared in the upper scope on line 27 column 5
useLogs(logger);

const mdEditor = useMarkdownEditor(
Expand Down Expand Up @@ -266,7 +270,7 @@
setEditorMode(mode);
}
const onToolbarAction = ({id, editorMode: type}: ToolbarActionData) => {
console.info(`The '${id}' action is performed in the ${type}-editor.`);

Check warning on line 273 in demo/components/Playground.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
};
function onChangeSplitModeEnabled({splitModeEnabled}: {splitModeEnabled: boolean}) {
props.onChangeSplitModeEnabled?.(splitModeEnabled);
Expand Down
1 change: 1 addition & 0 deletions demo/components/PlaygroundMini.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type PlaygroundMiniProps = Pick<
| 'onChangeEditorType'
| 'onChangeSplitModeEnabled'
| 'directiveSyntax'
| 'disabledHTMLBlockModes'
> & {withDefaultInitialContent?: boolean};

export const PlaygroundMini = memo<PlaygroundMiniProps>(
Expand Down
14 changes: 13 additions & 1 deletion demo/components/SplitModePreview.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {useEffect, useMemo, useRef, useState} from 'react';

import type {HTMLRuntimeConfig} from '@diplodoc/html-extension';
import transform from '@diplodoc/transform';
import {useThemeValue} from '@gravity-ui/uikit';
import type MarkdownIt from 'markdown-it';
Expand Down Expand Up @@ -31,10 +32,20 @@ export type SplitModePreviewProps = {
linkify?: boolean;
linkifyTlds?: string | string[];
needToSanitizeHtml?: boolean;
htmlRuntimeConfig?: HTMLRuntimeConfig;
};

export const SplitModePreview: React.FC<SplitModePreviewProps> = (props) => {
const {plugins, getValue, allowHTML, breaks, linkify, linkifyTlds, needToSanitizeHtml} = props;
const {
plugins,
getValue,
allowHTML,
breaks,
linkify,
linkifyTlds,
needToSanitizeHtml,
htmlRuntimeConfig,
} = props;
const [html, setHtml] = useState('');
const [meta, setMeta] = useState<object | undefined>({});
const divRef = useRef<HTMLDivElement>(null);
Expand Down Expand Up @@ -74,6 +85,7 @@ export const SplitModePreview: React.FC<SplitModePreviewProps> = (props) => {
noListReset
mermaidConfig={mermaidConfig}
yfmHtmlBlockConfig={yfmHtmlBlockConfig}
htmlRuntimeConfig={htmlRuntimeConfig}
className="demo-preview"
/>
);
Expand Down
1 change: 1 addition & 0 deletions demo/defaults/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export const args: Meta<PlaygroundMiniProps>['args'] = {
renderPreviewDefined: true,
height: 'initial',
directiveSyntax: 'disabled',
disabledHTMLBlockModes: [],
};
2 changes: 2 additions & 0 deletions demo/stories/yfm/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ html, body {

Simple text again

<div data-yfm-sandbox-mode='shadow' data-yfm-sandbox-content='This is shadow html block content that injected in html by runtime'>This is initial shadow html block content</div>

## Next is another YFM HTML block with styles

::: html
Expand Down
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@
"devDependencies": {
"@diplodoc/cut-extension": "^0.6.1",
"@diplodoc/folding-headings-extension": "0.1.0",
"@diplodoc/html-extension": "^2.5.0",
"@diplodoc/html-extension": "2.7.1",
"@diplodoc/latex-extension": "1.0.3",
"@diplodoc/mermaid-extension": "1.2.1",
"@diplodoc/tabs-extension": "^3.5.1",
Expand Down Expand Up @@ -295,8 +295,8 @@
},
"peerDependencies": {
"@diplodoc/cut-extension": "^0.5.0 || ^0.6.1 || ^0.7.1",
"@diplodoc/folding-headings-extension": "^0.1.0",
"@diplodoc/file-extension": "^0.2.1",
"@diplodoc/folding-headings-extension": "^0.1.0",
"@diplodoc/html-extension": "^2.3.2",
"@diplodoc/latex-extension": "^1.0.3",
"@diplodoc/mermaid-extension": "^1.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,9 @@ export const YfmHtmlBlockView: React.FC<{
const body = `<body>${node.attrs[YfmHtmlBlockConsts.NodeAttrs.srcdoc] ?? ''}</body>`;
const html = `<!DOCTYPE html><html>${head}${body}</html>`;

const resultHtml = sanitize ? sanitize(html) : html;
const sanitizeFunction = typeof sanitize === 'function' ? sanitize : sanitize?.body;

const resultHtml = sanitizeFunction ? sanitizeFunction(html) : html;

return (
<div className={b()} onDoubleClick={setEditing}>
Expand Down
6 changes: 4 additions & 2 deletions src/view/hocs/withYfmHtml/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {type ComponentType, type RefAttributes, forwardRef, useEffect} from 'react';

import type {HTMLRuntimeConfig} from '@diplodoc/html-extension';
import {useDiplodocEmbeddedContentController} from '@diplodoc/html-extension/react';
import type {IHTMLIFrameElementConfig} from '@diplodoc/html-extension/runtime';

Expand All @@ -14,16 +15,17 @@ export type WithYfmHtmlBlockOptions = {
export type WithYfmHtmlBlockProps = {
meta: TransformMeta;
yfmHtmlBlockConfig?: IHTMLIFrameElementConfig;
htmlRuntimeConfig?: HTMLRuntimeConfig;
};

export function withYfmHtmlBlock(opts: WithYfmHtmlBlockOptions) {
return <T extends {html: string}>(
Component: ComponentType<T & RefAttributes<HTMLDivElement>>,
) =>
forwardRef<HTMLDivElement, T & WithYfmHtmlBlockProps>(function WithYfmHtml(props, ref) {
const {meta, html, yfmHtmlBlockConfig} = props;
const {meta, html, yfmHtmlBlockConfig, htmlRuntimeConfig} = props;

useYfmHtmlBlockRuntime(meta, opts.runtime);
useYfmHtmlBlockRuntime(meta, opts.runtime, htmlRuntimeConfig);

const yfmHtmlBlock = useDiplodocEmbeddedContentController();

Expand Down
5 changes: 5 additions & 0 deletions src/view/hocs/withYfmHtml/useYfmHtmlBlockRuntime.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import type {HTMLRuntimeConfig} from '@diplodoc/html-extension';
import {setupRuntimeConfig} from '@diplodoc/html-extension/utils';

import type {PluginRuntime, TransformMeta} from './types';

/** @internal */
export function useYfmHtmlBlockRuntime(
meta: TransformMeta,
runtime: PluginRuntime = '_assets/html-extension.js',
htmlRuntimeConfig: HTMLRuntimeConfig = {},
) {
if (meta?.script?.includes(runtime)) {
setupRuntimeConfig(htmlRuntimeConfig);
import(/* webpackChunkName: "yfm-html-runtime" */ '@diplodoc/html-extension/runtime');
}
}