Skip to content

Commit

Permalink
Configurable position of TOC
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Szmigiel authored and MaxMelcher committed Nov 28, 2022
1 parent fdf05ad commit ea7d524
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
12 changes: 7 additions & 5 deletions AzureDevOps.WikiPDFExport/MarkdownConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ internal string ConvertToHTML(IList<MarkdownFile> files)

if (!string.IsNullOrEmpty(_options.GlobalTOC))
{
var firstMDFileInfo = files[0].FileInfo;
if (_options.GlobalTOCPosition > files.Count)
_options.GlobalTOCPosition = files.Count;
var firstMDFileInfo = files[_options.GlobalTOCPosition].FileInfo;
var directoryName = firstMDFileInfo.Directory.Name;
var tocName = _options.GlobalTOC == "" ? directoryName : _options.GlobalTOC;
var relativePath = "/" + tocName + ".md";
Expand All @@ -75,9 +77,9 @@ internal string ConvertToHTML(IList<MarkdownFile> files)
var contents = files.Select(x => x.Content).ToList();
var tocContent = CreateGlobalTableOfContent(contents);
var tocString = string.Join("\n", tocContent);

var tocMarkdownFile = new MarkdownFile(new FileInfo(tocMDFilePath), relativePath, 0, relativePath, tocString);
files.Insert(0, tocMarkdownFile);

files.Insert(_options.GlobalTOCPosition, tocMarkdownFile);
}

for (var i = 0; i < files.Count; i++)
Expand Down Expand Up @@ -155,7 +157,7 @@ internal string ConvertToHTML(IList<MarkdownFile> files)
}
html = builder.ToString();

if (!string.IsNullOrEmpty(_options.GlobalTOC) && i == 0)
if (!string.IsNullOrEmpty(_options.GlobalTOC) && i == _options.GlobalTOCPosition)
{
html = RemoveDuplicatedHeadersFromGlobalTOC(html);
Log($"Removed duplicated headers from toc html", LogLevel.Information, 1);
Expand Down Expand Up @@ -185,7 +187,7 @@ internal string ConvertToHTML(IList<MarkdownFile> files)
}


if (!string.IsNullOrEmpty(_options.GlobalTOC) && i == 0 && !_options.Heading)
if (!string.IsNullOrEmpty(_options.GlobalTOC) && i == _options.GlobalTOCPosition && !_options.Heading)
{
var heading = $"<h1>{_options.GlobalTOC}</h1>";
html = heading + html;
Expand Down
3 changes: 3 additions & 0 deletions AzureDevOps.WikiPDFExport/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ public class Options
[Option("globaltoc", Required = false, HelpText = "Title for a global table of content for all markdown files. When not specified each markdown creates its own toc if defined")]
public string GlobalTOC { get; set; }

[Option("globaltocposition", Required = false, HelpText = "Order of TOC within all files (starting from 0). When not specified TOC will be printed on the first page")]
public int GlobalTOCPosition { get; set; }

[Option("printbackground", Required = false, HelpText = "Enable print background when using no header/footer template")]
public bool PrintBackground { get; set; } = false;

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ Provide a path to html files that will be added as header and footer. See [examp
### --GlobalTOC
Title for a global table of content for all markdown files. When not specified each markdown creates its own toc if defined

### --GlobalTOCPosition
Order of TOC within all files (starting from 0). When not specified TOC will be printed on the first page

### --include-unlisted-pages
By default only pages listed in `.order` files are considered. Setting this option any Markdown file (`.md`) found will be included as-well.

Expand Down

0 comments on commit ea7d524

Please sign in to comment.