Skip to content

Commit

Permalink
feat(yfmHtmlBlock): updated @diplodoc/html-extension (#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
makhnatkin authored Aug 21, 2024
1 parent 7a0bdcb commit 4a6df56
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 19 deletions.
15 changes: 9 additions & 6 deletions demo/Playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,15 @@ export const Playground = React.memo<PlaygroundProps>((props) => {
.use(YfmHtmlBlock, {
useConfig: useYfmHtmlBlockStyles,
sanitize: getSanitizeYfmHtmlBlock({options: defaultOptions}),
baseTarget: '_blank',
styles: {
body: {
margin: 0,
},
},
head: `
<base target="_blank" />
<style>
html, body {
margin: 0;
padding: 0;
}
</style
`,
})
.use(FoldingHeading),
});
Expand Down
10 changes: 9 additions & 1 deletion demo/md-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,15 @@ const extendedPlugins = defaultPlugins.concat(
yfmHtmlBlock({
bundle: false,
runtimeJsPath: YFM_HTML_BLOCK_RUNTIME,
styles: 'https://meyerweb.com/eric/tools/css/reset/reset200802.css',
head: `
<base target="_blank" />
<style>
html, body {
margin: 0;
padding: 0;
}
</style
`,
}),
foldingHeadings({bundle: false}),
);
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@
},
"devDependencies": {
"@diplodoc/folding-headings-extension": "0.1.0",
"@diplodoc/html-extension": "1.3.3",
"@diplodoc/html-extension": "1.5.0",
"@diplodoc/latex-extension": "1.0.3",
"@diplodoc/mermaid-extension": "1.2.1",
"@diplodoc/transform": "4.22.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,13 @@ export const YfmHtmlBlockView: React.FC<{
onChange: (attrs: {[YfmHtmlBlockConsts.NodeAttrs.srcdoc]: string}) => void;
options: YfmHtmlBlockOptions;
view: EditorView;
}> = ({onChange, node, getPos, view, options: {useConfig, sanitize, styles, baseTarget = '_'}}) => {
}> = ({
onChange,
node,
getPos,
view,
options: {useConfig, sanitize, styles, baseTarget = '_parent', head: headContent = ''},
}) => {
const [editing, setEditing, unsetEditing, toggleEditing] = useBooleanState(
Boolean(node.attrs[YfmHtmlBlockConsts.NodeAttrs.newCreated]),
);
Expand All @@ -233,25 +239,27 @@ export const YfmHtmlBlockView: React.FC<{
);
}

let dirtyHtml =
`<base target="${baseTarget}">` + node.attrs[YfmHtmlBlockConsts.NodeAttrs.srcdoc];

let additional = baseTarget ? `<base target="${baseTarget}">` : '';
if (styles) {
const stylesContent =
typeof styles === 'string'
? `<link rel="stylesheet" href="${styles}" />`
: `<style>${getStyles(styles)}</style>`;
dirtyHtml = stylesContent + dirtyHtml;
additional += stylesContent;
}

const html = sanitize ? sanitize(dirtyHtml) : dirtyHtml;
const head = `<head>${headContent || additional}</head>`;
const body = `<body>${node.attrs[YfmHtmlBlockConsts.NodeAttrs.srcdoc] ?? ''}</body>`;
const html = `<!DOCTYPE html><html>${head}${body}</html>`;

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

return (
<div className={b()} onDoubleClick={setEditing}>
<Label className={b('label')} icon={<Icon size={16} data={Eye} />}>
{i18n('preview')}
</Label>
<YfmHtmlBlockPreview html={html} onСlick={handleClick} config={config} />
<YfmHtmlBlockPreview html={resultHtml} onСlick={handleClick} config={config} />

<div className={b('menu')}>
<Button
Expand Down
1 change: 1 addition & 0 deletions src/extensions/yfm/YfmHtmlBlock/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type YfmHtmlBlockOptions = {
sanitize?: (dirtyHtml: string) => string;
styles?: string | StylesObject;
baseTarget?: BaseTarget;
head?: string;
};

export const YfmHtmlBlock: ExtensionAuto<YfmHtmlBlockOptions> = (
Expand Down

0 comments on commit 4a6df56

Please sign in to comment.