Skip to content

Commit

Permalink
Handle HTML inside headings for ToC
Browse files Browse the repository at this point in the history
Previously, the HTML tag would break the `content` match,
resulting in an incorrect navigation item. By excluding the
opening tag from the link pattern, inlide code can now be
parsed correctly inside of headings.
  • Loading branch information
Chalarangelo committed Jun 9, 2024
1 parent 9a2826d commit 36c0e0a
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/blocks/utilities/tocReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
*/
export class TocReader {
static matcher =
/<h(?<level>[2-4])><a.*id="(?<href>.*?)".*>(?<content>.*?)<\/a><\/h\1>/g;
/<h(?<level>[2-4])><a.*id="(?<href>.*?)"[^<]*>(?<content>.*?)<\/a><\/h\1>/g;

static readToC = html => {
if (!html) return undefined;
const headings = [...html.matchAll(TocReader.matcher)].map(({ groups }) => {
return {
level: Number.parseInt(groups.level, 10),
href: groups.href,
content: groups.content.trim(),
content: groups.content.replace(/<[^>]*>/g, '').trim(),
};
});
if (headings.length === 0) return undefined;
Expand Down

0 comments on commit 36c0e0a

Please sign in to comment.