Skip to content

Commit 2260505

Browse files
committed
Add support for mkdocs-material instant loading feature
Required large rewrite of how to handle injection of CSS and JS files
1 parent 73785d0 commit 2260505

11 files changed

+171
-141
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
- Allow visitors to create PDFs from MkDocs sites
1616
- Support for pagination in PDFs
17-
- Support for generic and [mkdocs-material](https://github.com/squidfunk/mkdocs-material) themes, but works on all themes
17+
- Works on all MkDocs themes
18+
- Support for [mkdocs-material](https://github.com/squidfunk/mkdocs-material) features like instant loading and dark color themes.
1819
- Lightweight, no dependencies
1920

2021
If you need to create PDFs programmatically, have a look at alternatives like [mkdocs-pdf-export-plugin](https://github.com/zhaoterryy/mkdocs-pdf-export-plugin) and [mkdocs-pdf-with-js-plugin](https://github.com/smaxtec/mkdocs-pdf-with-js-plugin).

mkdocs_print_site_plugin/css/print-site-material.css

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ https://github.com/squidfunk/mkdocs-material */
1313
margin-bottom: 0;
1414
list-style-position: inside;
1515
}
16-
#print-page-toc ul.print-site-toc-level-2,ul.print-site-toc-level-3,ul.print-site-toc-level-4,ul.print-site-toc-level-5,ul.print-site-toc-level-6 {
16+
#print-page-toc ul.print-site-toc-level-2,
17+
#print-page-toc ul.print-site-toc-level-3,
18+
#print-page-toc ul.print-site-toc-level-4,
19+
#print-page-toc ul.print-site-toc-level-5,
20+
#print-page-toc ul.print-site-toc-level-6 {
1721
margin-left: 1.6em;
1822
}
1923
#print-page-toc ul li {
@@ -35,20 +39,24 @@ ul.print-site-toc-level-2 li a {
3539

3640

3741
/* Box shadows don't do well in PDFs */
38-
table {
42+
#print-site-page table {
3943
border: 1px solid hsla(200, 18%, 26%, 1); /* #EFEFEF */
4044
box-shadow: none !important;
4145
}
4246

4347
@media print {
44-
td {
48+
#print-site-page td {
4549
word-wrap: break-word;
4650
}
47-
4851
}
4952

5053
@page {
5154

55+
/*
56+
Note this CSS file is added to all MkDocs pages
57+
So this @page logic will affect print of all pages
58+
*/
59+
5260
size: a4 portrait;
5361
margin: 15mm 10mm 25mm 10mm;
5462
counter-increment: page;

mkdocs_print_site_plugin/css/print-site-mkdocs.css

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@
55
}
66

77
@media print {
8-
.col-md-3 { display: none !important; }
8+
9+
/* Hide side TOC banner */
10+
#print-site-page .col-md-3 { display: none !important; }
911
}
1012

1113
@page {
1214

15+
/*
16+
Note this CSS file is added to all MkDocs pages
17+
So this @page logic will affect print of all pages
18+
*/
19+
1320
size: a4 portrait;
1421
margin: 15mm 10mm 15mm 10mm;
1522
counter-increment: page;

mkdocs_print_site_plugin/css/print_site.css renamed to mkdocs_print_site_plugin/css/print-site.css

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111
}
1212

1313
/* Print site table of contents styling */
14-
h1:target, h2:target, h3:target, h4:target, h5:target, h6:target {
14+
#print-site-page h1:target,
15+
#print-site-page h2:target,
16+
#print-site-page h3:target,
17+
#print-site-page h4:target,
18+
#print-site-page h5:target,
19+
#print-site-page h6:target {
1520
animation: highlight 1s ease;
1621
}
1722
@keyframes highlight {
@@ -24,56 +29,64 @@ h1:target, h2:target, h3:target, h4:target, h5:target, h6:target {
2429
/* included bookmarks on h1 and h2
2530
Doesn't work, but included In case Chrome gets support
2631
for these experimental CSS features that define PDF bookmarks */
27-
h1 {
32+
#print-site-page h1 {
2833
bookmark-level: 1;
2934
bookmark-label: content();
3035
-ah-bookmark-level: 1;
3136
-ro-pdf-bookmark-level: 1;
3237
}
33-
h2 {
38+
#print-site-page h2 {
3439
bookmark-level: 2;
3540
bookmark-label: content();
3641
-ah-bookmark-level: 2;
3742
-ro-pdf-bookmark-level: 2;
3843
}
3944

45+
/* Remove print site banner */
4046
#print-site-banner { display: none; }
4147

4248
/* Ensure all tabbed content is displayed and printed
4349
https://squidfunk.github.io/mkdocs-material/reference/content-tabs/ */
44-
div.tabbed-content { display: block !important; }
45-
50+
#print-site-page div.tabbed-content { display: block !important; }
4651

4752
/* PDF page breaks on each MkDocs page, except the first one */
48-
section.print-page {
53+
#print-site-page section.print-page {
4954
page-break-before: always;
5055
}
51-
section.print-page:first-of-type {
56+
#print-site-page section.print-page:first-of-type {
5257
page-break-before: avoid;
5358
}
5459

55-
p, pre, blockquote, .tabbed-set {
60+
#print-site-page p,
61+
#print-site-page pre,
62+
#print-site-page blockquote,
63+
#print-site-page .tabbed-set {
5664
page-break-inside: avoid;
5765
}
5866

5967
/* Avoid a page break immediately after a heading */
6068
/* Credits https://stackoverflow.com/a/9238898/5525118 */
61-
h1 {
69+
#print-site-page h1 {
6270
page-break-inside: avoid;
6371
}
64-
h1::after {
72+
#print-site-page h1::after {
6573
content: "";
6674
display: block;
6775
height: 100px;
6876
margin-bottom: -100px;
6977
}
7078

71-
footer { display : none; }
79+
#print-site-page footer { display : none; }
7280

7381
}
7482

7583
@page {
7684

85+
/*
86+
Note this CSS file is added to all MkDocs pages
87+
So this @page logic will affect print of all pages
88+
*/
89+
7790
/* Prevent image page overflow */
7891
img { max-width: 500px !important; }
7992

mkdocs_print_site_plugin/js/print-site-material.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

mkdocs_print_site_plugin/js/print-site-toc.js renamed to mkdocs_print_site_plugin/js/print-site.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
11
/*
2-
Generates a table of contents for the print site page.
3-
4-
Only included when print-site-plugin option 'add_table_of_contents' is set to true
2+
Javascript functions to help make the print page more PDF friendly
53
*/
64

5+
76
/*
8-
TODO: Look at https://www.cssscript.com/easy-table-of-contents/ and just change the list style.
7+
mkdocs-material compatibility
8+
Change theme to default mode, when printing
9+
10+
Only called when theme 'material' is specified in the mkdocs.yml file
911
*/
12+
function change_material_theme(to="default") {
13+
14+
body = document.getElementsByTagName('body')[0];
15+
body.setAttribute("data-md-color-scheme", to)
16+
}
17+
1018

19+
/*
20+
Generates a table of contents for the print site page.
21+
Only called when print-site-plugin option 'add_table_of_contents' is set to true
22+
*/
1123
function generate_toc() {
1224

1325
var ToC = "<nav role='navigation' class='print-page-toc-nav'>" +
14-
"<h1>Table of Contents</h1>"
26+
"<h1 class='print-page-toc-title'>Table of Contents</h1>"
1527

1628
var newLine, el, title, link;
1729

@@ -22,6 +34,12 @@ function generate_toc() {
2234
for (var i = 0; i < toc_elements.length; i++) {
2335

2436
el = toc_elements[i]
37+
38+
// Don't put the toc h1 in the toc
39+
if ( el.classList.contains('print-page-toc-title') ) {
40+
continue;
41+
}
42+
2543
title = el.innerHTML;
2644
link = "#" + el.id;
2745
tag = el.tagName
@@ -53,6 +71,4 @@ function generate_toc() {
5371

5472
document.querySelectorAll("#print-page-toc")[0].innerHTML = ToC;
5573

56-
}
57-
58-
generate_toc();
74+
}

0 commit comments

Comments
 (0)