Skip to content

Instantly share code, notes, and snippets.

@liamcain
Created November 8, 2020 01:04
  • Star 33 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save liamcain/81679412643690a7cdd13dff4dc909ce to your computer and use it in GitHub Desktop.
Obsidian Pagebreaks
/**
Create pagebreaks in exported Obsidian PDFs.
Example:
# Heading 1
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a type
## Heading 2 (pagebreak before this)
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a type
---
--- ( <-- linebreak )
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a type
*/
@media print {
/* pagebreak before all ## headings */
h2 {
page-break-before: always;
}
h3,
h4 {
page-break-after: avoid;
}
pre,
blockquote {
page-break-inside: avoid;
}
/* use double <hr> ('---') to indicate a page break */
hr + hr {
page-break-before: always;
visibility: hidden;
}
:not(hr) + hr {
visibility: hidden;
}
:not(hr) + hr + :not(hr):not(h2) {
border-top: 1px solid black;
padding-top: 2em;
}
}
@luckman212
Copy link

Whoa man, this is freakin' awesome! 🤩

Thanks @liamcain !

@DominikDoom
Copy link

Very useful snippet!
One suggestion:

I changed the first block to

:not(h1) + h2 {
    page-break-before: always;
}

to prevent h2 from creating a page break if no text other than its parent heading comes before it. Useful if you have chapters without introduction text.

@BoxThatBeat
Copy link

This is great thank you!

@PaulMndn
Copy link

It doesn't avoid page breaks after h3, h4. I've been looking for a solution everywhere with no luck. Do you have an idea?

h3,
h4 {
  page-break-after: avoid;
}

@matyasmatta
Copy link

I have the same problem as @PaulMndn, it doesn't avoid page breaks after headings.

@jrossfeld
Copy link

jrossfeld commented Jul 23, 2023

This was really useful. Thanks! I added one to mine for H1, but having it skip the first one:

h1:not(:first-of-type) {
    page-break-before: always;
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment