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:
- Start at beginning of Table of Contents (ToC).
- Scan for next hyperlink.
- Get the location of the ToC hyperlink itself.
- Get the display text of the hyperlink.
- Get the hyperlink target destination.
- Jump to hyperlink target destination.
- 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).
- 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.
- Return to the ToC at a location after the ToC hyperlink found in step 2 and location found in step 3.
- Scan for next hyperlink after the previously found hyperlink.
- Repeat for every link in the ToC until the end of the ToC is reached, at which point, Stop.