facelessuser / pymdown-extensions

Extensions for Python Markdown
https://facelessuser.github.io/pymdown-extensions/
Other
949 stars 253 forks source link

Fixing tabbed-set label placement for cases where alternative renderers keep the HTML element order #2420

Closed nbanyan closed 1 month ago

nbanyan commented 1 month ago

Adding hidden alt-style tabbed-set labels to the top of tabbed-blocks

Scenario: I'm working with Material for MkDocs using the alternate tabbed style. I also use domWalter's continuation of the mkdocs-with-pdf plugin using RelaxedJS. When exporting to PDF, tabbed-sets would print all the tab labels at the top followed by the tabbed-contents, just like it is in HTML despite the web browser's print view showing differently. So this is my fix for this - essentially creating something closer to the standard tabbed style for use only with @media print.

facelessuser commented 1 month ago

This addition will break documentation for people, even MkDocs Material, without them adding some new CSS.

Additionally, I don't think you've really made clear what you are "fixing" either. Can you demonstrate with some images or something showing what the change is?

nbanyan commented 1 month ago

Using a new mkdocs project with only the following YAML and no additional CSS:

site_name: My Docs
theme:
  name:        material

plugins: 
  - search
  # Uncomment this block and use `PDF_GENERATE=1 mkdocs build`
  # to export this MkDocs to HTML and PDF
  - with-pdf:
      enabled_if_env: PDF_GENERATE
      relaxedjs_path: "/opt/homebrew/bin/relaxed"
      render_js: True
      headless_chrome_path: "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome" #"/opt/homebrew/bin/chromium"

markdown_extensions:
  - pymdownx.superfences
  - pymdownx.tabbed:
      alternate_style: true
Renderer Web Browser Print View PDF Export
Current Screenshot 2024-07-25 at 10 04 58 Screenshot 2024-07-25 at 10 05 13 Screenshot 2024-07-25 at 10 05 37
With Changes Screenshot 2024-07-25 at 10 12 19 Screenshot 2024-07-25 at 10 12 35 Screenshot 2024-07-25 at 10 13 41
Changes with CSS Screenshot 2024-07-25 at 11 13 49 Screenshot 2024-07-25 at 11 14 11 Screenshot 2024-07-25 at 11 14 32

The added CSS is:

<style>
  /* @media print { */
  .md-typeset .tabbed-set .tabbed-label-print {
    display: none;
  }
  /* } */
  .md-typeset .tabbed-set .tabbed-label-print > label,
  .md-typeset .tabbed-set .tabbed-labels > label {
    border-bottom: .1rem solid #0000;
    border-radius: .1rem .1rem 0 0;
    color: var(--md-default-fg-color--light);
    cursor: pointer;
    flex-shrink: 0;
    font-size: .64rem;
    font-weight: 700;
    padding: .78125em 1.25em .625em;
    scroll-margin-inline-start: 1rem;
    transition: background-color .25s,color .25s;
    white-space: nowrap;
    width: auto;
  }
  .md-typeset .tabbed-set .tabbed-label-print > label {
    padding-left: 0;
  }
  @media print {
    .md-typeset .tabbed-set .tabbed-labels {
      display: none;
    }
    .md-typeset .tabbed-set .tabbed-label-print {
      display: block;
    }
  }
</style>
nbanyan commented 1 month ago

Noticing the extra space the hidden div adds. It shouldn't do that and I can't find a way to make it not add that space, so I added a alternate_style_for_pdf config option to enable these additional labels when needed.

facelessuser commented 1 month ago

Unfortunately, you are claiming that export to PDF messes up tabs when printing, but I am not finding that true. I exported a site to PDF with absolutely no issues here.

pdf

Maybe you are claiming a specific PDF exporter is having trouble? But I'm not sure we should break documents because one exporter has trouble.

nbanyan commented 1 month ago

What PDF export tools/plugins did you use for that? The reason I'm using with-pdf with the RelaxedJS option is that it's the only method I've found so far that can export pages with Mermaid diagrams and Draw.io SVGs to PDF.

facelessuser commented 1 month ago

Browsers seem to print to PDF just fine.

nbanyan commented 1 month ago

Right, that's the Print View. But that doesn't work well when you need to print multiple pages with a cover page and table of contents. That's what Weasyprint and RelaxedJS can do, but some things end up rendered differently even when using headless Chrome as a generator.

facelessuser commented 1 month ago

Is that an "pymdownx" problem or a "Weasyprint" problem? This injects unnecessary HTML elements that will break other documentation. Yes, they can add additional CSS to hide them, but what you are suggesting is a hack for this specific PDF exporter.

facelessuser commented 1 month ago

I will add, if there is a specific need for this, a workaround mkdocs plugin could be crafted to post process the content and add such elements. I would recommend going this route as a work around. If/when this is resolved in the exporter, the required mkdocs plugin could be retired.

nbanyan commented 1 month ago

For anyone else who happens to needs this: