Skip to content

Commit

Permalink
mw: fix section detection (again)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChlodAlejandro committed Mar 3, 2024
1 parent 80a82f7 commit 742e90c
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/wiki/util/sectionHeadingName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@
*/
export default function sectionHeadingName( element: HTMLHeadingElement ): string {
try {
return element.querySelector<HTMLElement>( '.mw-headline' )?.innerText ??
element.innerText;
// Get only the direct text of .mw-headline
// Why? Because DiscussionTools inserts a [subscribe] link in the .mw-headline
// element, which we don't want to include in the section name.
const headlineElement = element.querySelector<HTMLElement>( '.mw-headline' );
const headlineDirectText = Array.from( headlineElement?.childNodes ?? [] )
.filter( n => n.nodeType === Node.TEXT_NODE )
.reduce( ( acc, n ) => acc + n.textContent, '' )
.trim();
return headlineDirectText || headlineElement?.innerText;
} catch ( e ) {
console.error( 'Error getting section name', e, element );
throw e;
Expand Down

0 comments on commit 742e90c

Please sign in to comment.