Skip to content

Instantly share code, notes, and snippets.

@frodo821
Last active November 20, 2023 03:47
Show Gist options
  • Save frodo821/32565e0b924ce6a6818d5763e0cdf819 to your computer and use it in GitHub Desktop.
Save frodo821/32565e0b924ce6a6818d5763e0cdf819 to your computer and use it in GitHub Desktop.
*{
background: white!important;
color: black;
}
.markdown-body code {
white-space: pre-wrap!important;
}
.markdown-body pre {
border: solid 2px;
}
.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
page-break-after: avoid;
}
.markdown-body img {
width: 80%;
display: block;
border: solid 2px;
border-radius: 6px;
margin: 1rem auto;
}
(async () => {
const fn = document.querySelector(".form-control.js-gist-filename.js-blob-filename")?.value;
if (!fn?.endsWith(".md")) {
console.log("Not a markdown");
return;
}
const edit = document.querySelector("button.code");
const preview = document.querySelector("button.preview");
if (edit === null || preview === null) {
console.log("unexpected page structure: tab switching button(s) not found.");
alert("Internal error occurred.");
return;
}
preview.click();
await new Promise((res) => setTimeout(res, 0));
const markdown = document.querySelector(".commit-preview.markdown-body");
if (markdown === null) {
console.log("unexpected page structure: preview body not found.");
alert("Internal error occurred.");
return;
}
const wnd = window.open(".");
if (wnd === null) {
console.log("failed to open new window.");
alert("Internal error occurred.");
return;
}
window.focus();
await new Promise((res) => setTimeout(res, 2500));
wnd.document.body.innerHTML = markdown.outerHTML;
wnd.document.body.firstElementChild.style.width = "210mm";
let sty = wnd.document.createElement("style");
sty.innerHTML = "*{background:white!important;color:#000}.markdown-body code{white-space:pre-wrap!important}.markdown-body pre{border:solid 2px;white-space: pre-wrap!important}.markdown-body h1,.markdown-body h2,.markdown-body h3,.markdown-body h4,.markdown-body h5,.markdown-body h6{page-break-after:avoid}.markdown-body img{width:80%;display:block;border:solid 2px;border-radius:6px;margin:1rem auto}";
wnd.document.head.append(sty);
wnd.document.title = fn.replace(/\.md$/, ".pdf");
try {
wnd.document.querySelector('link[rel="stylesheet"][href*=dark').href = wnd.document.querySelector('link[rel="stylesheet"][data-color-theme*=light]').getAttribute('data-href')
} catch {}
console.log("PDF is ready.");
wnd.focus();
edit.click();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment