Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<div><a href="/">Markdown Live Preview</a></div>
<div id="reset-button"><a href="#">Reset</a></div>
<div id="copy-button"><a href="#">Copy</a></div>
<div id="print-button"><a href="#">Print/PDF</a></div>
<div id="sync-button"><input type="checkbox" id="sync-scroll-checkbox"><label for="sync-scroll-checkbox">Sync scroll</label></div>
</div>
<div id="github"><a href="https://github.com/tanabe/markdown-live-preview"><img src="image/GitHub-Mark-Light-32px.webp"></a></div>
Expand Down
4 changes: 4 additions & 0 deletions public/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ header a:hover {
margin-left: 16px;
}

#print-button {
margin-left: 16px;
}

#sync-button {
margin-left: 16px;
-webkit-user-select: none;
Expand Down
93 changes: 93 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,98 @@ This web site is using ${"`"}markedjs/marked${"`"}.
});
};

let openPrintWindow = () => {
// Get the content from the preview pane
let previewContent = document.querySelector('#output').innerHTML;

// Get the GitHub markdown CSS
let markdownStyles = '';
for (let sheet of document.styleSheets) {
if (sheet.href && sheet.href.includes('github-markdown')) {
try {
for (let rule of sheet.cssRules) {
markdownStyles += rule.cssText + '\n';
}
} catch (e) {
// Handle cross-origin CSS
markdownStyles = `@import url('${sheet.href}');`;
}
break;
}
}

// Create a new window with the content
let printWindow = window.open('', '_blank', 'width=800,height=600');

if (printWindow) {
printWindow.document.write(`
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Markdown Preview</title>
<style>
${markdownStyles}
body {
margin: 0;
padding: 20px;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
}
.markdown-body {
box-sizing: border-box;
min-width: 200px;
max-width: 980px;
margin: 0 auto;
padding: 45px;
}
@media print {
body {
margin: 0;
padding: 0;
}
.markdown-body {
padding: 20px;
}
/* Avoid page breaks inside these elements */
pre, blockquote, table {
page-break-inside: avoid;
}
/* Ensure headings stay with their content */
h1, h2, h3, h4, h5, h6 {
page-break-after: avoid;
}
/* Avoid breaking lists */
ul, ol {
page-break-inside: avoid;
}
}
</style>
</head>
<body>
<div class="markdown-body">
${previewContent}
</div>
</body>
</html>
`);
printWindow.document.close();

// Wait for content to load before printing
printWindow.onload = () => {
printWindow.focus();
printWindow.print();
};
}
};

let setupPrintButton = () => {
document.querySelector("#print-button").addEventListener('click', (event) => {
event.preventDefault();
openPrintWindow();
});
};


// ----- local state -----

let loadLastContent = () => {
Expand Down Expand Up @@ -346,6 +438,7 @@ This web site is using ${"`"}markedjs/marked${"`"}.
}
setupResetButton();
setupCopyButton(editor);
setupPrintButton();

let scrollBarSettings = loadScrollBarSettings() || false;
initScrollBarSync(scrollBarSettings);
Expand Down