Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TOC generation improvement #323

Closed
jvdvegt opened this issue Mar 9, 2019 · 4 comments
Closed

TOC generation improvement #323

jvdvegt opened this issue Mar 9, 2019 · 4 comments

Comments

@jvdvegt
Copy link
Contributor

jvdvegt commented Mar 9, 2019

The TOC extention could use some improvements:

First: If I create options this way:
PegdownOptionsAdapter .flexmarkOptions(Extensions.ALL & ~(Extensions.ANCHORLINKS | Extensions.EXTANCHORLINKS_WRAP), TocExtension.create()) .toMutable();
Then in ParserEmulationProfile.java:403, HtmlRenderer.RENDER_HEADER_ID is set to false. Then on TocExtention.java:68 it is not forced to false, leading to non-working links in the TOC.

I need to set the RENDER_HEADER_ID to true manually. I see I should/could use the Multimarkdown profile, but how to use that?

Additionally, the generated PDF looks much better with some CSS like this (after adding OPTIONS.set(TocExtension.LIST_CLASS, "toc")). Please feel free to add this somewhere to the extension:

/* Table of contents */
@media print {
    .toc a::after {
      content: leader('. ') target-counter(attr(href), page) '.';
    }
}

.toc li { display: block }
ol.toc li:first-child { counter-reset: l1 }
ol.toc li li:first-child { counter-reset: l2; }
ol.toc li li li:first-child { counter-reset: l3 }
ol.toc li li li li:first-child { counter-reset: l4 }

ol.toc li:before { counter-increment: l1; content: counter(l1) ". " }
ol.toc li li:before { counter-increment: l2; content: counter(l1) "." counter(l2) ". " }
ol.toc li li li:before { counter-increment: l3; content: counter(l1) "." counter(l2) "." counter(l3) ". " }
ol.toc li li li li:before { counter-increment: l4; content: counter(l1) "." counter(l2) "." counter(l3) "." counter(l4) ". " }

/* Heading numbers */
body {counter-reset: h2}
h2 {counter-reset: h3}
h3 {counter-reset: h4}
h4 {counter-reset: h5}
h5 {counter-reset: h6}

h2:before {counter-increment: h2; content: counter(h2) ". "}
h3:before {counter-increment: h3; content: counter(h2) "." counter(h3) ". "}
h4:before {counter-increment: h4; content: counter(h2) "." counter(h3) "." counter(h4) ". "}
h5:before {counter-increment: h5; content: counter(h2) "." counter(h3) "." counter(h4) "." counter(h5) ". "}
h6:before {counter-increment: h6; content: counter(h2) "." counter(h3) "." counter(h4) "." counter(h5) "." counter(h6) ". "}

.nocount:before { content: ""; counter-increment: none } 
@vsch
Copy link
Owner

vsch commented Mar 9, 2019

@jvdvegt, thank you for the PDF css, I will incorporate it into the extension and add an option to override the default CSS.

First: If I create options this way:
PegdownOptionsAdapter .flexmarkOptions(Extensions.ALL & ~(Extensions.ANCHORLINKS | Extensions.EXTANCHORLINKS_WRAP), TocExtension.create()) .toMutable();
Then in ParserEmulationProfile.java:403, HtmlRenderer.RENDER_HEADER_ID is set to false.

The reason Pegdown profile turns off header id generation is because pegdown uses ANCHORLINKS extension to create an anchor link with the header id. When you turn off this extension there is no place for the header id anchor.

Then on TocExtension.java:68 it is not forced to false, leading to non-working links in the TOC.

It is not set to true by the TocExtension because it was already set to false explicitly in the options. Otherwise, it would always override the given setting.

The only workable improvement that I can see is to add logic to the pegdown emulation profile to check which pegdown extensions are enabled and change the some settings based on it. I will do it for next release.

Is there a reason, other than convenience and inertia to use PegdownOptions adapter instead of providing your settings using flexmark-java native options API?

I ask because I still use a heavily modified PegdownOptionsAdapter in my plugin because of reluctance to rework all code using it. It is on my to do list for things to get rid off.

@vsch
Copy link
Owner

vsch commented Mar 9, 2019

@jvdvegt, added separate issues for two raised here.

vsch added a commit that referenced this issue Mar 14, 2019
@vsch
Copy link
Owner

vsch commented Mar 14, 2019

Fix for this is available. Repo updated, maven updated but may take a while to show up in maven central.

@vsch
Copy link
Owner

vsch commented Mar 14, 2019

Updated Sample: PdfConverter.java.

Updated wiki:

Details

Use class PdfConverterExtension from artifact flexmark-pdf-converter.

The following options are available:

Defined in PdfConverterExtension class:

Static Field Default Value Description
DEFAULT_TEXT_DIRECTION (PdfRendererBuilder.TextDirection) null default text direction for document
PROTECTION_POLICY (ProtectionPolicy) null document protection policy PDF Box Documentation, Related OpenHtmlToPdf
DEFAULT_CSS default embedded CSS for improved TOC generation default.css

ℹ️ Default CSS is only added if PdfConverterExtension.exportToPdf(OutputStream, String, String, DataHolder) is used to provide options. For all other calls you need to embed the default css into your HTML string before exporting to PDF. You can use PdfConverterExtension.embedCss(String html, String css) for this purpose and get the contents of the default css using PdfConverterExtension.DEFAULT_CSS.getFrom(null)

ℹ️ If you are using the TocExtension, you will need to set TocExtension.LIST_CLASS to PdfConverterExtension.DEFAULT_TOC_LIST_CLASS

@vsch vsch closed this as completed Aug 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants