In this topic, we will support hyperlinking in rtf files.
Create a hyperlink in the rtf file:
- Open
foo.rtf
byWord
. - Add a hyperlink in content
- Set the link target to an existing
bar.rtf
- Save the document.
An author can write any valid hyperlink in the document, and then needs to run DocFX build
to update file links.
- The hyperlink must be a relative path and not rooted.
- valid:
foo\bar.rtf
,../foobar.rtf
- invalid:
/foo.rtf
,c:\foo\bar.rtf
,http://foo.bar/
,mailto:[email protected]
- valid:
- The file must exist.
The story is:
- In
foo.rtf
, it has a file link tobar.rtf
. - In document build,
bar.rtf
generates a file with the namebar.html
. - But in
foo.rtf
, the link target is stillbar.rtf
, thus in the output folder we cannot find this file and we will get a broken link. - To resolve the broken link, we need to update the link target from
bar.rtf
tobar.html
.
File link is a relative path, but we cannot track the relative path easily. So we track the normalized file path instead.
- It always starts from the working folder (the folder that contains
docfx.json
), and we write it as~/
. - No
../
or./
or//
- Replace
\
with/
. - No url encoding. The path must be same as it in the file system.
- No anchor.
Finally, a valid normalized file path looks like: ~/foo/bar.rtf
.
-
Pros
-
Same form in different documents when the target is the same file.
When file structure is:
z:\a\b\foo.rtf z:\a\b\c\bar.rtf z:\a\b\c\foobar.rtf
Link target
c/foobar.rtf
infoo.rtf
and link targetfoobar.rtf
inbar.rtf
is the same file. When the working folder isz:\a\
, the link target is always~/b/c/foobar.rtf
. -
Avoids differences in style when referring to the same file.
For example, the following hyperlinks target the same file:
a/foo.rtf
,./a/foo.rtf
,a/b/../foo.rtf
,a//foo.rtf
,a\foo.rtf
-
-
Cons
- A folder with the name
~
is not supported.
- A folder with the name
-
Open the rtf plug-in library project in
Visual Studio
. -
Add nuget packages:
for plug-in:Docfx.Utility
-
Add framework assembly reference:
System.Core
,System.Web
,System.Xml.Linq
-
Following the rules for hyperlink, add a
FixLink
help method: [!Code-csharpFixLink]RelativePath
helps us generate the links correctly. -
Then add
CollectLinksAndFixDocument
method: [!Code-csharpCollectLinksAndFixDocument] -
Modify
Save
method with report links: [!Code-csharpSave]
View final RtfDocumentProcessor.cs
- Build project.
- Copy dll to
Plugins
folder. - Modify rtf file, create hyperlink, link to another rtf file, and save.
- Build with command
DocFX build
. - Verify output html file.