jgm / pandoc

Universal markup converter
https://pandoc.org
Other
34.15k stars 3.35k forks source link

Converting Markdown to RTF with working internal links #9097

Open altncsab opened 1 year ago

altncsab commented 1 year ago

I'm using Visual Studio Code with Markdown All in One extension to generate table of content into my ReadMe.md documentation files. I'm converting this Readme.md to .rtf format. (What I can load simply to my C# Windows form app for the users)

The problem what I'm facing is the converted .rtf document does not contains anchors for the internal references, therefore they are not working in viewer or in any document editor.

This is a sample .md:

# User guide

guide text

## Purpose of the tool

description text

## Table Of Content

- [User guide](#user-guide)
  - [Purpose of the tool](#purpose-of-the-tool)
  - [Table Of Content](#table-of-content)

The anchors should be inserted to the .rtf converted documents with proper naming.

jgm commented 1 year ago

Currently we do this for internal links:

{\field{\*\fldinst{HYPERLINK "#identifier"}}{\fldrslt{\ul
link text
}

Open to suggestions on what a better approach would be.

jgm commented 1 year ago

I tried using textutil on macos to convert an HTML file with an internal link to RTF. It simply used underlining and there was no link in the result.

\f1\b0\fs24 \cf3 \ul \ulc3 \strokec3 internal link\cf2 \ulnone \strokec2  ipsum dolor sit amet.\
jgm commented 1 year ago

Seems like there might be a way to do it with OLE - \linkobj, \linkself -- need to look into it.

altncsab commented 1 year ago

The part what is defining the hyper link itself is not OK. The converter .rtf should look like this:

{\rtf1\ansi\deff0{\fonttbl{\f0 \fswiss Helvetica;}{\f1 \fmodern Courier;}}
{\colortbl;\red255\green0\blue0;\red0\green0\blue255;}
\widowctrl\hyphauto

{\*\bkmkstart user_guide}{\*\bkmkend user_guide}{\pard \ql \f0 \sa180 \li0 \fi0 \outlinelevel0 \b \fs36 User guide\par}
{\pard \ql \f0 \sa180 \li0 \fi0 guide text\par}
{\*\bkmkstart purpose_of_the_tool}{\*\bkmkend purpose_of_the_tool}{\pard \ql \f0 \sa180 \li0 \fi0 \outlinelevel1 \b \fs32 Purpose of the tool\par}
{\pard \ql \f0 \sa180 \li0 \fi0 description text\par}
{\*\bkmkstart table_of_content}{\*\bkmkend table_of_content}{\pard \ql \f0 \sa180 \li0 \fi0 \outlinelevel1 \b \fs32 Table Of Content\par}
{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab {\field{\*\fldinst{HYPERLINK "#user_guide"}}{\fldrslt{\ul
User guide
}}}
\par}
{\pard \ql \f0 \sa0 \li720 \fi-360 \endash \tx360\tab {\field{\*\fldinst{HYPERLINK "#purpose_of_the_tool"}}{\fldrslt{\ul
Purpose of the tool
}}}
\par}
{\pard \ql \f0 \sa0 \li720 \fi-360 \endash \tx360\tab {\field{\*\fldinst{HYPERLINK "#table_of_content"}}{\fldrslt{\ul
Table Of Content
}}}
\sa180\sa180\par}
}

What means: only alphanumeric characters and no spaces in the identifier allowed. They must be changed to _ (underline) Need to insert the bookmark start and end markers (bkmkstart / bkmkend) to there, what have been referenced. In Markdown case those are the lines starting with # (hash).

If the converted rtf would look like this the internal links would work.

jgm commented 1 year ago

I tried the rtf you supply above with macos's TextEdit, and clicking on the links just raised an error. So I don't think that's enough.

altncsab commented 1 year ago

Interesting... I have used: Microsoft® Word for Microsoft 365 MSO (Version 2208 Build 16.0.15601.20526) 32-bit It did not complained. It just jumped to my bookmarks... I have tried use with C# and RichTextBox windows forms component: It is not capable to handle bookmarks and internal links. (There are condition when even the LinkClicked event does not firing. I have faced other issues related to this component when rtf contains bitmaps: Find() function throws exceptions. Document is rendered but the component itself is buggy for rtf documents)

jgm commented 1 year ago

I can confirm it works with Word. So maybe this is just a limitation of TextEdit.