MaxMelcher / AzureDevOps.WikiPDFExport

Export Azure DevOps Wiki to PDF
MIT License
191 stars 65 forks source link

Internal Links are not working #97

Closed Diom closed 2 years ago

Diom commented 2 years ago

Issue:

Links to other sections of the wiki (not deep-linked) are not clickable in the generated PDF

image

To Reproduce:

Add a link to a wiki MD page:

I want to test if this [link](/Admin-Layout-and-Customization) works

Run the build (ctrl+f5)

Expected

Link to internal section of the PDF will work.

Actual

The text is styled as a link but does not work.

Example

Added the above to the test wiki in the repo and built the project. This was output in the console:

<div style='page-break-after: always;'><h1>Another Page</h1><a id="another-page">&nbsp;</a><h2 id="another-page-here">Another Page Here</h2>
<p>I want to test if this <a href="/Admin-Layout-and-Customization">link</a> works</p>
</div>
  html:
<h1>Admin Layout and Customization</h1><a id="admin-layout-and-customization">&nbsp;</a><h2 id="smileys">Smileys</h2>

Attached PDF the link does not work (I took out the images at the top of the page for clarity in the console

export.pdf

Diom commented 2 years ago

Looks like this section is failing:

                        fileInfo = new FileInfo($"{absPath}.md");
                        if (fileInfo.Exists && fileInfo.Extension.Equals(".md", StringComparison.InvariantCultureIgnoreCase))
                        {
                            isMarkdown = true;
                        }

                        //only markdown files get a pdf internal link
                        if (isMarkdown)
                        {

For some reason, in the absPath there is a ".wiki" being added to the parent folder? This causes the file to "not exist" and not get treated as an internal link.

Diom commented 2 years ago
                    var split = _path.Split(".wiki");
                    _rootWikiPath = split[0].IsNullOrEmpty() ? _path : split[0] + ".wiki";

added there.

Changing to this makes it work:

                    var split = _path.Split(".wiki");
                    _rootWikiPath = _path; //split[0].IsNullOrEmpty() ? _path : split[0] + ".wiki";

It looks like the work added to make the internal links work when exporting a sub-section broke the links. It made a bad assumption that ALL wiki have '.wiki' in the path, which is only true of the default wikis. A better mechanism to detect the root would be to use the path to the .attachments page.

I will look into that once I have my customer doc updates done!

Diom commented 2 years ago

Solved it properly by doing this:

    public static DirectoryInfo FindNearestParentAttachmentsDirectory(DirectoryInfo exportBase)
    {
        DirectoryInfo[] attDirs = exportBase.GetDirectories("./.attachments");
        if(attDirs != null && attDirs.Length > 0) {
            return attDirs[0].Parent;
        }
        if (null != exportBase.Parent) {
            return FindNearestParentAttachmentsDirectory(exportBase.Parent);
        }
        throw new WikiPdfExportException("Failed to locate the base of the Wiki using attachment detection");
    }

Went for .attachments instead because maybe for some reason there is no git?? will try to find time to test it PRed soon

Diom commented 2 years ago

PR #98 opened

MaxMelcher commented 2 years ago

wow - intensive review and addition - THANK YOU @Diom