1
0

remove browser header and footer from rendered doc

This commit is contained in:
Nicola Clark 2024-11-24 01:02:56 -06:00
parent 0811f1c396
commit ea30f91e99
Signed by: nicola
GPG Key ID: 3E1710E7FF08956C

View File

@ -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),