Skip to content

Commit 8a2fc4b

Browse files
util: + findSectionHeading
1 parent dbd2a08 commit 8a2fc4b

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/wiki/util/findSectionHeading.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Finds a MediaWiki section heading from the current DOM using its title.
3+
*
4+
* @param sectionHeadingName The name of the section to find.
5+
* @param n The `n` of the section. Starts at 1.
6+
* @return The found section heading. `null` if not found.
7+
*/
8+
export default function findSectionHeading(
9+
sectionHeadingName: string,
10+
n = 1
11+
): HTMLElement | null {
12+
let currentN = 1;
13+
14+
const headlines = Array.from( document.querySelectorAll( 'h2 > .mw-headline' ) );
15+
for ( const el of headlines ) {
16+
if ( el instanceof HTMLElement && el.innerText === sectionHeadingName ) {
17+
if ( currentN >= n ) {
18+
return el.parentElement;
19+
} else {
20+
currentN++;
21+
}
22+
}
23+
}
24+
25+
return null;
26+
}

src/wiki/util/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import decorateEditSummary from './decorateEditSummary';
22
import delink from './delink';
33
import errorToOO from './errorToOO';
4+
import findSectionHeading from './findSectionHeading';
45
import getApiErrorText from './getApiErrorText';
56
import getNativeRange from './getNativeRange';
67
import getPageContent from './getPageContent';
@@ -32,6 +33,7 @@ export default {
3233
decorateEditSummary: decorateEditSummary,
3334
delink: delink,
3435
errorToOO: errorToOO,
36+
findSectionHeading: findSectionHeading,
3537
getApiErrorText: getApiErrorText,
3638
getNativeRange: getNativeRange,
3739
getPageContent: getPageContent,

0 commit comments

Comments
 (0)