Skip to content

Commit

Permalink
util: + findSectionHeading
Browse files Browse the repository at this point in the history
  • Loading branch information
ChlodAlejandro committed May 28, 2024
1 parent dbd2a08 commit 8a2fc4b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/wiki/util/findSectionHeading.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Finds a MediaWiki section heading from the current DOM using its title.
*
* @param sectionHeadingName The name of the section to find.
* @param n The `n` of the section. Starts at 1.
* @return The found section heading. `null` if not found.
*/
export default function findSectionHeading(
sectionHeadingName: string,
n = 1
): HTMLElement | null {
let currentN = 1;

const headlines = Array.from( document.querySelectorAll( 'h2 > .mw-headline' ) );
for ( const el of headlines ) {
if ( el instanceof HTMLElement && el.innerText === sectionHeadingName ) {
if ( currentN >= n ) {
return el.parentElement;
} else {
currentN++;
}
}
}

return null;
}
2 changes: 2 additions & 0 deletions src/wiki/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import decorateEditSummary from './decorateEditSummary';
import delink from './delink';
import errorToOO from './errorToOO';
import findSectionHeading from './findSectionHeading';
import getApiErrorText from './getApiErrorText';
import getNativeRange from './getNativeRange';
import getPageContent from './getPageContent';
Expand Down Expand Up @@ -32,6 +33,7 @@ export default {
decorateEditSummary: decorateEditSummary,
delink: delink,
errorToOO: errorToOO,
findSectionHeading: findSectionHeading,
getApiErrorText: getApiErrorText,
getNativeRange: getNativeRange,
getPageContent: getPageContent,
Expand Down

0 comments on commit 8a2fc4b

Please sign in to comment.