How to convert link targets into links themselves pointing back to the ToC in Writer using a macro?

I am exporting a very large document (1400+ pages) to ebook, and have already worked out the Table of Contents (ToC) as hyperlinks to all the chapters and sections, and the hyperlinks work fine after being exported to the target ebook format so that part is done.

Now I want to make all the link targets in the ToC into links themselves pointing back to the appropriate page of the ToC.

I do not wish to attempt this manually one by one, as there are hundreds of entries in the ToC, so I am looking for help to figure out how to take the link target information from the ToC, find said target in the text body, and then convert that target into a link pointing back to the correct ToC page automatically.

Thus my question is how to convert link targets into links themselves pointing back to the ToC in Writer using a macro?


To explain further, the steps I imagine the macro taking would look something like this:

  1. Start at beginning of Table of Contents (ToC).
  2. Scan for next hyperlink.
  3. Get the location of the ToC hyperlink itself.
  4. Get the display text of the hyperlink.
  5. Get the hyperlink target destination.
  6. Jump to hyperlink target destination.
  7. Use the display text previously retrieved from the ToC in step 4 to find the hyperlink target text on the target destination page (It will definitely be an H1-H4 heading in all cases).
  8. Convert the found target text into a hyperlink pointing back to the original ToC hyperlink using the location information of the original hyperlink retrieved in step 3.
  9. Return to the ToC at a location after the ToC hyperlink found in step 2 and location found in step 3.
  10. Scan for next hyperlink after the previously found hyperlink.
  11. Repeat for every link in the ToC until the end of the ToC is reached, at which point, Stop.

Inserting bookmarks in the TOC field may be a problem. Maybe it will be easier to change the plan. If you want to generate the hyperlinks from the headings back to the TOC using a macro, you might as well generate the TOC using a macro, using cross-references for the content in the TOC lines, using the modified heading target names as bookmark names. You can still use the Contents paragraph styles, but beware if you made any formatting changes in the TOC - changes there will override the paragraph styles.

1 Like

I haven’t used bookmarks yet, so I am not sure what you are intending to reference by mentioning bookmarks.

Though you have given me a simpler idea. Since all the Headings are in the ToC, I suppose the macro could simply search the entire document for Headings, and then find the matching text in the ToC and set the Heading to be a hyperlink to said part of the ToC?

You use the macro to build the TOC as a list of cross-references to the headings in the document, with number, text and page number as separate fields, separators as desired, and with TOC paragraph styles applied, each item preceded by a bookmark (anchor for hyperlinks). Then you turn the headings themselves into hyperlinks to the bookmarks in the TOC.
In pseudo-code:

Move the insertion point to the TOC.
Delete the existing TOC.
Enter the TOC title heading, don’t apply the Heading 1 style yet.
For each heading in the headings in the document

  • Insert hyperlink anchor (bookmark in LO jargon) at start of line.
  • Insert cross-reference to heading number, separator, cross-reference to heading text, separator, cross-reference to heading page number.
  • Apply appropriate Contents paragraph style.

End repeat

For each heading in the headings in the document

  • Move insertion point to the heading.
  • Make it a hyperlink to the bookmark in the TOC.

End repeat

Apply Heading 1 style to the TOC title.

End code

Modify the Contents paragraph styles to suit your needs (right aligned tab with fill character, etc.)

Do you rather mean Contents Heading? Heading 1 is intended for chapter headings. If you apply it to TOC title, it includes the TOC heading into the TOC. Though this is possible, it is usually not wanted (and it is unstable)

@ ajlittoz the TOC is generated by the macro only once, so applying Heading 1 afterwards shouldn’t lead to problems. But it should be applied as the last step instead of in the middle.

@anon87010807: OK. However this removes one degree of freedom: TOC heading formatting can no longer be controlled independently from chapter appearance. So it all boils down to the graphical charter chosen for the final document.

Too bad the Heading Styles do not include a way to incorporate a hyperlink in the settings…
Then I could at least point the Heading back to the first page of the ToC… better than nothing.

But I would really prefer each of all the Heading One through Four to point back to the specific line entry of the ToC that they appear on.

The work in question is a reference book, there are thousands of Headings.