Keep header connected to its paragraph when printed— Markdown to HTML to PDF page break before headings, CSS for widows & orphans

The CSS-obvious perfect answer is:

h2, h3, h4, h5 {
  page-break-after: avoid;

p {
  widows: 2;
  orphans: 2;

except that it does not seem to work, is not honored, either in Firefox or whatever mdpdf (Markdown to PDF command line converter) is using.

The terrible solution of adding divs around headings and paragraphs:

… which is completely unworkable in Markdown anyway?

… though i suppose we could write our own processor to do that step.

But the CSS is really supposed to work:

css Page break after avoid ignored print

firefox web developer tools print preview

has been a bug: 5097 – CSS2: page-break-after does not work