Skip to content

Commit 4a6df56

Browse files
authored
feat(yfmHtmlBlock): updated @diplodoc/html-extension (#343)
1 parent 7a0bdcb commit 4a6df56

File tree

6 files changed

+39
-19
lines changed

6 files changed

+39
-19
lines changed

demo/Playground.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,15 @@ export const Playground = React.memo<PlaygroundProps>((props) => {
169169
.use(YfmHtmlBlock, {
170170
useConfig: useYfmHtmlBlockStyles,
171171
sanitize: getSanitizeYfmHtmlBlock({options: defaultOptions}),
172-
baseTarget: '_blank',
173-
styles: {
174-
body: {
175-
margin: 0,
176-
},
177-
},
172+
head: `
173+
<base target="_blank" />
174+
<style>
175+
html, body {
176+
margin: 0;
177+
padding: 0;
178+
}
179+
</style
180+
`,
178181
})
179182
.use(FoldingHeading),
180183
});

demo/md-plugins.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,15 @@ const extendedPlugins = defaultPlugins.concat(
5858
yfmHtmlBlock({
5959
bundle: false,
6060
runtimeJsPath: YFM_HTML_BLOCK_RUNTIME,
61-
styles: 'https://meyerweb.com/eric/tools/css/reset/reset200802.css',
61+
head: `
62+
<base target="_blank" />
63+
<style>
64+
html, body {
65+
margin: 0;
66+
padding: 0;
67+
}
68+
</style
69+
`,
6270
}),
6371
foldingHeadings({bundle: false}),
6472
);

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@
200200
},
201201
"devDependencies": {
202202
"@diplodoc/folding-headings-extension": "0.1.0",
203-
"@diplodoc/html-extension": "1.3.3",
203+
"@diplodoc/html-extension": "1.5.0",
204204
"@diplodoc/latex-extension": "1.0.3",
205205
"@diplodoc/mermaid-extension": "1.2.1",
206206
"@diplodoc/transform": "4.22.0",

src/extensions/yfm/YfmHtmlBlock/YfmHtmlBlockNodeView/YfmHtmlBlockView.tsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,13 @@ export const YfmHtmlBlockView: React.FC<{
206206
onChange: (attrs: {[YfmHtmlBlockConsts.NodeAttrs.srcdoc]: string}) => void;
207207
options: YfmHtmlBlockOptions;
208208
view: EditorView;
209-
}> = ({onChange, node, getPos, view, options: {useConfig, sanitize, styles, baseTarget = '_'}}) => {
209+
}> = ({
210+
onChange,
211+
node,
212+
getPos,
213+
view,
214+
options: {useConfig, sanitize, styles, baseTarget = '_parent', head: headContent = ''},
215+
}) => {
210216
const [editing, setEditing, unsetEditing, toggleEditing] = useBooleanState(
211217
Boolean(node.attrs[YfmHtmlBlockConsts.NodeAttrs.newCreated]),
212218
);
@@ -233,25 +239,27 @@ export const YfmHtmlBlockView: React.FC<{
233239
);
234240
}
235241

236-
let dirtyHtml =
237-
`<base target="${baseTarget}">` + node.attrs[YfmHtmlBlockConsts.NodeAttrs.srcdoc];
238-
242+
let additional = baseTarget ? `<base target="${baseTarget}">` : '';
239243
if (styles) {
240244
const stylesContent =
241245
typeof styles === 'string'
242246
? `<link rel="stylesheet" href="${styles}" />`
243247
: `<style>${getStyles(styles)}</style>`;
244-
dirtyHtml = stylesContent + dirtyHtml;
248+
additional += stylesContent;
245249
}
246250

247-
const html = sanitize ? sanitize(dirtyHtml) : dirtyHtml;
251+
const head = `<head>${headContent || additional}</head>`;
252+
const body = `<body>${node.attrs[YfmHtmlBlockConsts.NodeAttrs.srcdoc] ?? ''}</body>`;
253+
const html = `<!DOCTYPE html><html>${head}${body}</html>`;
254+
255+
const resultHtml = sanitize ? sanitize(html) : html;
248256

249257
return (
250258
<div className={b()} onDoubleClick={setEditing}>
251259
<Label className={b('label')} icon={<Icon size={16} data={Eye} />}>
252260
{i18n('preview')}
253261
</Label>
254-
<YfmHtmlBlockPreview html={html} onСlick={handleClick} config={config} />
262+
<YfmHtmlBlockPreview html={resultHtml} onСlick={handleClick} config={config} />
255263

256264
<div className={b('menu')}>
257265
<Button

src/extensions/yfm/YfmHtmlBlock/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type YfmHtmlBlockOptions = {
1313
sanitize?: (dirtyHtml: string) => string;
1414
styles?: string | StylesObject;
1515
baseTarget?: BaseTarget;
16+
head?: string;
1617
};
1718

1819
export const YfmHtmlBlock: ExtensionAuto<YfmHtmlBlockOptions> = (

0 commit comments

Comments
 (0)