-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[partial] cci: section name conflict handling
- Loading branch information
1 parent
57af6cc
commit 266f0c8
Showing
6 changed files
with
99 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import last from '../../util/last'; | ||
|
||
/** | ||
* Checks the n of a given element, that is to say the `n`th occurrence of a section | ||
* with this exact heading name in the entire page. | ||
* | ||
* This is purely string- and element-based, with no additional metadata or parsing | ||
* information required. | ||
* | ||
* This function detects the `n` using the following conditions: | ||
* - If the heading ID does not have an n suffix, the n is always 1. | ||
* - If the heading ID does have an n suffix, and the detected heading name does not end | ||
* with a number, the n is always the last number on the ID. | ||
* - If the heading ID and heading name both end with a number, | ||
* - The n is 1 if the ID has an equal number of ending number patterns (sequences of "_n", | ||
* e.g. "_20_30_40" has three) with the heading name. | ||
* - Otherwise, the n is the last number on the ID if the ID than the heading name. | ||
* | ||
* @param heading The heading to check | ||
* @param headingName The name of the heading to check | ||
* @return The n, a number | ||
*/ | ||
export default function ( heading: HTMLHeadingElement, headingName: string ): number { | ||
const headingNameEndPattern = /(?:\s|_)*(\d+)$/g; | ||
const headingIdEndPattern = /_(\d+)$/g; | ||
|
||
const headingId = heading.getAttribute( 'id' ) ?? | ||
heading.querySelector( '.mw-headline' ).getAttribute( 'id' ); | ||
const headingIdMatches = headingId.match( headingIdEndPattern ); | ||
const headingNameMatches = headingName.match( headingNameEndPattern ); | ||
|
||
if ( headingIdMatches == null ) { | ||
return 1; | ||
} else if ( headingNameMatches == null ) { | ||
// Last number of the ID | ||
return +( headingIdEndPattern.exec( last( headingIdMatches ) )[ 1 ] ); | ||
} else if ( headingIdMatches.length === headingNameMatches.length ) { | ||
return 1; | ||
} else { | ||
// Last number of the ID | ||
return +( headingIdEndPattern.exec( last( headingIdMatches ) )[ 1 ] ); | ||
} | ||
} |