Thursday, November 1, 2018

Sitecore PxM "Table of Contents" renderers

I just finished a PxM project where one of the requirements was to implement a "Table of Contents" based on the article heading tags. The content of the article comes from data sources of various renderings that assigned to an item presentation settings. The Sitecore data source structure that we had to work with have a separation between section titles and the body, so the task at hand was to parse HTML and find H2 and H3 tags that the "Table of Contents" can be built on.

Here is a screenshot of the Table of Contents in PDF:



To implement this functionality two renderers had to be implemented:

  • Renderer for "Table of Contents" headings;
  • A renderer that would inject the InserVar XML node into the heading ParagraphStyle nodes that would allow for InDesign to determine the page number where a particular heading is placed.

Table of Contents headings


The Table of Contents page displays the list of al H2 and H3 headings from the Article Content with the page number where those headings render in the content section. 

The PxM project structure for TOC looks like this:


The TOC page is of Flow type. It has a TextFrame that holds a Repeater that loops through all renderings in item Presentation settings that live in specified in "Placeholder" parameter placeholder and are based on the template name that is specified in "AllowedRenderingNames".

The "H Tags" renderer is responsible for parsing HTML from each "TextModule" rendering data source, extracting the value of the H2 or H3 tags and rendering a ParagraphStyle XML element with appropriate Style.


In Parameters section of this renderer item the following parameters are defined:
  • HTags – the list of HTML tags to search for to produce the Table of Contents
  • IndexName – the name of the index for the variable list that is used for the Table of Contents
  • ReplaceVar – the substring that would be replaced with the index element order number.
  • HParagraphStyles – the list of ParagraphStyles that should be used for the Table of Contents entries. This parameter goes together with the HTags one. In the example above the H2 tag value would render in PDF with “style 1 Level 1” ParagraphStyle, and H3 with “style 1 Level 2”.
The XML that this renderer produces look like this:


Dynamic Content Renderer with TOC

The Dynamic Content Renderer with TOC is a part of the following PxM project structure:


A similar Repeater creates a loop through the same renderings as the previous renderer. However, in this case, it is not extracting the value of the H tags, but injects an InserVar XML element with the index value.

XML output for this section looks like this:


Conclusion

In combination, these two renderers allow for the "Table of Contents" with page numbers to be rendered in PDF.

Source Code:

You can find the code for these renderers at:

No comments:

Post a Comment