Skip to content

Commit a5e65e1

Browse files
chrPetryMaxMelcher
authored andcommitted
Fixes #94 - Removing code sections when searching for table of content headers
1 parent f726d3d commit a5e65e1

File tree

2 files changed

+94
-5
lines changed

2 files changed

+94
-5
lines changed

AzureDevOps.WikiPDFExport.Test/Tests/TableOfContent/TableOfContent_uTest.cs

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ public void CreateGlobalTableOfContent_ShouldReturnTOCandMultipleHeaderLines()
4141
{
4242
// Arrange
4343
var wikiPDFExporter = new WikiPDFExporter(new Options());
44-
var mdContent1 = "\n# SomeHeader\n"
45-
+ "SomeText";
46-
var mdContent2 = " ## SomeOtherHeader \n"
47-
+ " []() #Some very interesting text in wrong header format #";
44+
var mdContent1 = @"
45+
# SomeHeader
46+
SomeText";
47+
var mdContent2 = @" ## SomeOtherHeader
48+
[]() #Some very interesting text in wrong header format #";
4849

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

58+
[Fact]
59+
public void CreateGlobalTableOfContent_ShouldIgnoreCodeSectionsSingle()
60+
{
61+
// Arrange
62+
var wikiPDFExporter = new WikiPDFExporter(new Options());
63+
var mdContent1 = @"
64+
``` code section
65+
# SomeHeader
66+
```";
67+
68+
// Act
69+
var result = wikiPDFExporter.CreateGlobalTableOfContent(new List<string> { mdContent1 });
70+
71+
Assert.False(result.Any());
72+
}
73+
74+
[Fact]
75+
public void CreateGlobalTableOfContent_ShouldIgnoreCodeSectionsMultiple()
76+
{
77+
// Arrange
78+
var wikiPDFExporter = new WikiPDFExporter(new Options());
79+
var mdContent1 = @"
80+
``` code section
81+
## SomeHeader
82+
Some other text
83+
```
84+
```
85+
## Another header ```
86+
# A valid header";
87+
88+
// Act
89+
var result = wikiPDFExporter.CreateGlobalTableOfContent(new List<string> { mdContent1 });
90+
91+
Assert.Equal(2, result.Count);
92+
Assert.Equal("[TOC]", result[0]);
93+
Assert.Equal("# A valid header", result[1]);
94+
}
95+
96+
[Fact]
97+
public void CreateGlobalTableOfContent_ShouldNotIgnoreInvalidCodeSections()
98+
{
99+
// Arrange
100+
var wikiPDFExporter = new WikiPDFExporter(new Options());
101+
var mdContent1 = @"
102+
Not at the beginning ```
103+
# A valid header
104+
```
105+
";
106+
107+
// Act
108+
var result = wikiPDFExporter.CreateGlobalTableOfContent(new List<string> { mdContent1 });
109+
110+
Assert.Equal(2, result.Count);
111+
Assert.Equal("[TOC]", result[0]);
112+
Assert.Equal("# A valid header", result[1]);
113+
}
114+
115+
[Fact]
116+
public void CreateGlobalTableOfContent_ShouldNotIgnoreUnclosedCodeSections()
117+
{
118+
// Arrange
119+
var wikiPDFExporter = new WikiPDFExporter(new Options());
120+
var mdContent1 = @"
121+
``` unclosed comment
122+
# A valid header
123+
";
124+
125+
// Act
126+
var result = wikiPDFExporter.CreateGlobalTableOfContent(new List<string> { mdContent1 });
127+
128+
Assert.Equal(2, result.Count);
129+
Assert.Equal("[TOC]", result[0]);
130+
Assert.Equal("# A valid header", result[1]);
131+
}
132+
57133
[Fact]
58134
public void RemoveDuplicatedHeadersFromGlobalTOC()
59135
{

AzureDevOps.WikiPDFExport/WikiPDFExporter.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,9 @@ internal string RemoveDuplicatedHeadersFromGlobalTOC(string html)
634634
internal List<string> CreateGlobalTableOfContent(List<string> contents)
635635
{
636636
var headers = new List<string>();
637-
foreach (var content in contents)
637+
var filteredContentList = RemoveCodeSections(contents);
638+
639+
foreach (var content in filteredContentList)
638640
{
639641
var headerMatches = Regex.Matches(content, "^ *#+ ?.*$", RegexOptions.Multiline);
640642
headers.AddRange(headerMatches.Select(x => x.Value.Trim()));
@@ -648,6 +650,17 @@ internal List<string> CreateGlobalTableOfContent(List<string> contents)
648650
return tocContent;
649651
}
650652

653+
private List<string> RemoveCodeSections(List<string> contents)
654+
{
655+
var contentWithoutCode = new List<string>();
656+
for(var i=0; i < contents.Count; i++)
657+
{
658+
var contentWithoutCodeSection = Regex.Replace(contents[i], "^[ \t]*```[^`]*```", "", RegexOptions.Multiline);
659+
contentWithoutCode.Add(contentWithoutCodeSection);
660+
}
661+
return contentWithoutCode;
662+
}
663+
651664
private MarkdownDocument RemoveFrontmatter(MarkdownDocument document)
652665
{
653666
var frontmatter = document.Descendants<YamlFrontMatterBlock>().FirstOrDefault();

0 commit comments

Comments
 (0)