remove browser header and footer from rendered doc
This commit is contained in:
parent
0811f1c396
commit
ea30f91e99
@ -15,6 +15,13 @@ window.addEventListener('message', (evt) => {
|
||||
});
|
||||
`;
|
||||
|
||||
const removePrintMarginsStyle = `
|
||||
@page {
|
||||
margin-bottom: 0;
|
||||
margin-top: 0;
|
||||
}
|
||||
`;
|
||||
|
||||
const injectPrintScript: Plugin<[], HastRoot> = function () {
|
||||
return (tree) => {
|
||||
const printScriptNode = h('script', printScript);
|
||||
@ -28,6 +35,19 @@ const injectPrintScript: Plugin<[], HastRoot> = function () {
|
||||
};
|
||||
};
|
||||
|
||||
const injectRemovePrintMarginsStyle: Plugin<[], HastRoot> = function () {
|
||||
return (tree) => {
|
||||
const removePrintMarginsStyleNode = h('style', removePrintMarginsStyle);
|
||||
const htmlNode = tree.children.find(
|
||||
(node) => node.type === 'element' && node.tagName === 'html',
|
||||
) as HastElement;
|
||||
const headNode = htmlNode.children.find(
|
||||
(node) => node.type === 'element' && node.tagName === 'head',
|
||||
) as HastElement;
|
||||
headNode.children.push(removePrintMarginsStyleNode);
|
||||
};
|
||||
};
|
||||
|
||||
const renderPreview = async (markdown: string, stylesheet: string): Promise<string> => {
|
||||
let allowedTags: string[] = ['body', 'head', 'html', 'style'];
|
||||
if (defaultSchema.tagNames) {
|
||||
@ -43,6 +63,7 @@ const renderPreview = async (markdown: string, stylesheet: string): Promise<stri
|
||||
allowDoctypes: true,
|
||||
tagNames: allowedTags,
|
||||
})
|
||||
.use(injectRemovePrintMarginsStyle)
|
||||
.use(injectPrintScript)
|
||||
.use(rehypeStringify)
|
||||
.process(markdown),
|
||||
|
Loading…
x
Reference in New Issue
Block a user