Skip to content

Commit c849fff

Browse files
Improve error handling and type safety in DOM manipulation.
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 84f1cd1 commit c849fff

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

js/table-of-contents.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,19 @@ const initialize = (): void => {
9292
nav.setAttribute('role', 'navigation');
9393
nav.setAttribute('aria-label', 'Table of Contents');
9494

95-
for (const x of Array.from(document.getElementById('article')?.querySelectorAll('a.header') ?? [])) {
95+
const article = document.getElementById('article');
96+
if (!article) {
97+
console.error('Article element not found');
98+
return;
99+
}
100+
101+
const headers = article.querySelectorAll('a.header');
102+
for (const x of Array.from(headers)) {
96103
const el = x as HTMLAnchorElement;
104+
if (!el.parentElement) {
105+
console.error('Header element has no parent');
106+
continue;
107+
}
97108

98109
observer.observe(el);
99110

0 commit comments

Comments
 (0)