Markdown TOC exported to PDF?

Due to limitations of the frameworks used for the conversion no such plans currently, I’m afraid.

Fair enough

Would it be possible to (kind of) script this? I’m thinking along these lines:

  • convert the MD to PDF
  • grep all headlines from the MD document
  • for each of them, find the line in the PDF (i.e. the string in the text layer) corresponding to it
  • use the complete line (i.e. possible with leading numbers etc.) to find the PDFSelection in the document
  • using the properties from these PDFSelections, build a PDFOutline for the TOC by creating PDFOutline objects with PDFActionGoTo actions for each selection.
  • and finally adds thisPDFOutline to the document.

I can add an outline to the PDF alright (see the code below), but Preview doesn’t show me neither a TOC nor a bookmark. Instead, it gets really unhappy and finally crashes. Acrobat Reader shows me a bookmark, not no TOC. Is there anything special I have to do so that a PDFOutline is recognized as a TOC (also in Preview, preferably)? The Net is quite terse on that issue.

  const outline = $.PDFOutline.alloc.init;
    const firstChild = $.PDFOutline.alloc.init;
    firstChild.label = $("1st outline line");
    const pdfSelection = pdfDoc.findStringWithOptions($(headline),0);
    if (pdfSelection.js.length > 0) {
      const firstSelection = pdfSelection.js[0];
      const page = firstSel.pages.js[0]; // NSPage object!
      const bounds = firstSel.boundsForPage(page); //NSRect object
      const pt = $.NSPointFromCGPoint($.NSRectToCGRect(bounds).origin);
      const destination = $.PDFDestination.alloc.initWithPageAtPoint(page, pt);
      const action = $.PDFActionGoTo.alloc.initWithDestination(destination);
      firstChild.action = action;
      firstChild.isOpen = true;
      outline.insertChildAtIndex(firstChild, 0);
      pdfDoc.setOutlineRoot(outline);
      pdfDoc.writeToFile(r.path());
    }

And what does DEVONthink show in its inspector?

Wow. I didn’t think to look there, and the “TOC” in the inspector actually shows me the “Outline” as an entry. Clicking on it takes me to the point I defined previously. Same in PDFPen. So, the code is not as bad as I thought.

Thanks for pointing that out. Now I’m wondering: Should I perhaps not use an PDFActionGoTo for the PDFOutline items, but only a PDFDestination? I still have no idea how to generate a TOC that makes Preview and Acrobat Reader.

This should be sufficient already.

It is. The funny part is getting the correct nesting of headings.

This is actually the invisible root of the outline, therefore your ToC contains only one visible outline item without children and just a destination.

With “nesting of headings” I meant that the outline for a level 3 heading must be a child of the outline of the preceding level 2 heading. Like that:

On the left side, “Delving deeper into CSS” is marked as a top-level heading. On the right side, it’s expanded to show the three 2nd-level headings below it. I’ll clean up and comment the code, and then I’ll post it in a new thread, namely here.

1 Like