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: nodejs#41318
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
  • Loading branch information
Trott authored and Linkgoron committed Jan 31, 2022
1 parent eb007ab commit 0451b27
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions tools/doc/html.mjs
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 0451b27

Please sign in to comment.