Skip to content

Commit

Permalink
tools: improve section tag additions in HTML doc generator
Browse files Browse the repository at this point in the history
There is an edge case involving GFM footnotes where our current code
adds an empty section which results in a warning (but not an error) in
HTML validators. This change causes the HTML generator to skip the
unnecessary addition of a section tag in that one edge case.

PR-URL: #41318
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Mohammed Keyvanzadeh <[email protected]>
  • Loading branch information
Trott authored and targos committed Jan 14, 2022
1 parent 5cfc547 commit 75ff8e6
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions tools/doc/html.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,14 @@ function processContent(content) {
}
// `++level` to convert the string to a number and increment it.
content = content.replace(/(?<=<\/?h)[1-5](?=[^<>]*>)/g, (level) => ++level);
// Wrap h3 tags in section tags.
// Wrap h3 tags in section tags unless they are immediately preceded by a
// section tag. The latter happens when GFM footnotes are generated. We don't
// want to add another section tag to the footnotes section at the end of the
// document because that will result in an empty section element. While not an
// HTML error, it's enough for validator.w3.org to print a warning.
let firstTime = true;
return content
.replace(/<h3/g, (heading) => {
.replace(/(?<!<section [^>]+>)<h3/g, (heading) => {
if (firstTime) {
firstTime = false;
return '<section>' + heading;
Expand Down

0 comments on commit 75ff8e6

Please sign in to comment.