Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle anchors correctly #520

Merged
merged 1 commit into from
Mar 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions tools/StandardAnchorTags/TocSectionNumberBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,7 @@ private SectionLink BuildSectionLink(SectionHeader header, string filename)
string newSectionNumber = isAnnexes
? string.Join('.', headings.Take(header.level).Select((n, index) => (index == 0) ? ((char)(n + 64)).ToString() : n.ToString()))
: string.Join('.', headings.Take(header.level).Select(n => n.ToString()));
string anchor = $"{newSectionNumber} {header.title}"
.Replace(' ', '-').Replace(".", "").Replace(",", "").Replace("`", "")
.Replace("/", "").Replace(":", "").Replace("?", "").Replace("&", "")
.Replace("|", "").Replace("!", "").Replace("\\<", "").Replace("\\>", "").Replace("\\#", "")
.Replace("…", "")
.ToLower();
string anchor = UrilizeAsGfm($"{newSectionNumber} {header.title}");

// Top-level annex references (e.g. just to "Annex D") need a leading "annex-" as that's
// in the title of the page.
Expand All @@ -167,6 +162,21 @@ private SectionLink BuildSectionLink(SectionHeader header, string filename)
return new SectionLink(header.sectionHeaderText, newSectionNumber, $"{filename}#{anchor}");
}

// Copy from https://github.com/xoofx/markdig/blob/0cfe6d7da48ea6621072eb50ade32141ea92bc35/src/Markdig/Helpers/LinkHelper.cs#L100-L113
private static string UrilizeAsGfm(string headingText)
{
// Following https://github.com/jch/html-pipeline/blob/master/lib/html/pipeline/toc_filter.rb
var headingBuffer = new StringBuilder();
for (int i = 0; i < headingText.Length; i++)
{
var c = headingText[i];
if (char.IsLetterOrDigit(c) || c == ' ' || c == '-' || c == '_')
{
headingBuffer.Append(c == ' ' ? '-' : char.ToLowerInvariant(c));
}
}
return headingBuffer.ToString();
}

// A line in the standard is either a paragraph of text or
// a header. this method determines which and returns one
Expand Down