Skip to content

Commit

Permalink
Fixes #94 - Removing code sections when searching for table of conten…
Browse files Browse the repository at this point in the history
…t headers
  • Loading branch information
chrPetry authored and MaxMelcher committed Feb 11, 2022
1 parent f726d3d commit a5e65e1
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ public void CreateGlobalTableOfContent_ShouldReturnTOCandMultipleHeaderLines()
{
// Arrange
var wikiPDFExporter = new WikiPDFExporter(new Options());
var mdContent1 = "\n# SomeHeader\n"
+ "SomeText";
var mdContent2 = " ## SomeOtherHeader \n"
+ " []() #Some very interesting text in wrong header format #";
var mdContent1 = @"
# SomeHeader
SomeText";
var mdContent2 = @" ## SomeOtherHeader
[]() #Some very interesting text in wrong header format #";

// Act
var result = wikiPDFExporter.CreateGlobalTableOfContent(new List<string> { mdContent1, mdContent2 });
Expand All @@ -54,6 +55,81 @@ public void CreateGlobalTableOfContent_ShouldReturnTOCandMultipleHeaderLines()
Assert.Equal("## SomeOtherHeader", result[2]);
}

[Fact]
public void CreateGlobalTableOfContent_ShouldIgnoreCodeSectionsSingle()
{
// Arrange
var wikiPDFExporter = new WikiPDFExporter(new Options());
var mdContent1 = @"
``` code section
# SomeHeader
```";

// Act
var result = wikiPDFExporter.CreateGlobalTableOfContent(new List<string> { mdContent1 });

Assert.False(result.Any());
}

[Fact]
public void CreateGlobalTableOfContent_ShouldIgnoreCodeSectionsMultiple()
{
// Arrange
var wikiPDFExporter = new WikiPDFExporter(new Options());
var mdContent1 = @"
``` code section
## SomeHeader
Some other text
```
```
## Another header ```
# A valid header";

// Act
var result = wikiPDFExporter.CreateGlobalTableOfContent(new List<string> { mdContent1 });

Assert.Equal(2, result.Count);
Assert.Equal("[TOC]", result[0]);
Assert.Equal("# A valid header", result[1]);
}

[Fact]
public void CreateGlobalTableOfContent_ShouldNotIgnoreInvalidCodeSections()
{
// Arrange
var wikiPDFExporter = new WikiPDFExporter(new Options());
var mdContent1 = @"
Not at the beginning ```
# A valid header
```
";

// Act
var result = wikiPDFExporter.CreateGlobalTableOfContent(new List<string> { mdContent1 });

Assert.Equal(2, result.Count);
Assert.Equal("[TOC]", result[0]);
Assert.Equal("# A valid header", result[1]);
}

[Fact]
public void CreateGlobalTableOfContent_ShouldNotIgnoreUnclosedCodeSections()
{
// Arrange
var wikiPDFExporter = new WikiPDFExporter(new Options());
var mdContent1 = @"
``` unclosed comment
# A valid header
";

// Act
var result = wikiPDFExporter.CreateGlobalTableOfContent(new List<string> { mdContent1 });

Assert.Equal(2, result.Count);
Assert.Equal("[TOC]", result[0]);
Assert.Equal("# A valid header", result[1]);
}

[Fact]
public void RemoveDuplicatedHeadersFromGlobalTOC()
{
Expand Down
15 changes: 14 additions & 1 deletion AzureDevOps.WikiPDFExport/WikiPDFExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,9 @@ internal string RemoveDuplicatedHeadersFromGlobalTOC(string html)
internal List<string> CreateGlobalTableOfContent(List<string> contents)
{
var headers = new List<string>();
foreach (var content in contents)
var filteredContentList = RemoveCodeSections(contents);

foreach (var content in filteredContentList)
{
var headerMatches = Regex.Matches(content, "^ *#+ ?.*$", RegexOptions.Multiline);
headers.AddRange(headerMatches.Select(x => x.Value.Trim()));
Expand All @@ -648,6 +650,17 @@ internal List<string> CreateGlobalTableOfContent(List<string> contents)
return tocContent;
}

private List<string> RemoveCodeSections(List<string> contents)
{
var contentWithoutCode = new List<string>();
for(var i=0; i < contents.Count; i++)
{
var contentWithoutCodeSection = Regex.Replace(contents[i], "^[ \t]*```[^`]*```", "", RegexOptions.Multiline);
contentWithoutCode.Add(contentWithoutCodeSection);
}
return contentWithoutCode;
}

private MarkdownDocument RemoveFrontmatter(MarkdownDocument document)
{
var frontmatter = document.Descendants<YamlFrontMatterBlock>().FirstOrDefault();
Expand Down

0 comments on commit a5e65e1

Please sign in to comment.