From 5e2b60f75e90bf3dd839caf825872b683109de6d Mon Sep 17 00:00:00 2001 From: Chlod Alejandro Date: Thu, 24 Nov 2022 00:23:47 +0800 Subject: [PATCH] [partial#2] cci: section name conflict handling --- .../root/DeputyContributionSurveySection.tsx | 10 +++++++-- src/wiki/util/getSectionId.ts | 21 ++++++++++++++++--- src/wiki/util/sectionHeadingN.ts | 4 ++-- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/ui/root/DeputyContributionSurveySection.tsx b/src/ui/root/DeputyContributionSurveySection.tsx index 2b994844..bd68886c 100644 --- a/src/ui/root/DeputyContributionSurveySection.tsx +++ b/src/ui/root/DeputyContributionSurveySection.tsx @@ -35,6 +35,7 @@ export default class DeputyContributionSurveySection implements DeputyUIElement private _section: ContributionSurveySection; heading: HTMLHeadingElement; headingName: string; + headingN: number; sectionElements: HTMLElement[]; originalList: HTMLElement; /** @@ -238,6 +239,7 @@ export default class DeputyContributionSurveySection implements DeputyUIElement this.casePage = casePage; this.heading = heading; this.headingName = sectionHeadingName( this.heading ); + this.headingN = sectionHeadingN( this.heading, this.headingName ); this.sectionElements = casePage.getContributionSurveySection( heading ); } @@ -253,7 +255,7 @@ export default class DeputyContributionSurveySection implements DeputyUIElement const sectionWikitext = await this.casePage.wikitext.getSectionWikitext( this.headingName, - sectionHeadingN( this.heading, this.headingName ) + this.headingN ); return this._section ?? ( this._section = new ContributionSurveySection( @@ -498,7 +500,11 @@ export default class DeputyContributionSurveySection implements DeputyUIElement this.setDisabled( true ); saveContainer.classList.add( 'active' ); - const sectionId = await getSectionId( this.casePage.title, this.headingName ); + const sectionId = await getSectionId( + this.casePage.title, + this.headingName, + this.headingN + ); await this.save( sectionId ).then( async ( result ) => { if ( result ) { mw.notify( diff --git a/src/wiki/util/getSectionId.ts b/src/wiki/util/getSectionId.ts index 19f7486e..d94f1961 100644 --- a/src/wiki/util/getSectionId.ts +++ b/src/wiki/util/getSectionId.ts @@ -6,8 +6,13 @@ import MwApi from '../../MwApi'; * * @param page The page to check for * @param sectionName The section name to get the ID of + * @param n The `n`th occurrence of a section with the same name */ -export default async function ( page: mw.Title | string, sectionName: string ) { +export default async function ( + page: mw.Title | string, + sectionName: string, + n = 1 +) { const parseRequest = await MwApi.action.get( { action: 'parse', page: normalizeTitle( page ).getPrefixedText(), @@ -18,8 +23,18 @@ export default async function ( page: mw.Title | string, sectionName: string ) { throw new Error( 'Error finding section ID: ' + parseRequest.error.info ); } - const indexSection = ( parseRequest.parse.sections as any[] ) - .find( ( section ) => section.line === sectionName ); + let indexSection; + let currentN = 1; + for ( const section of parseRequest.parse.sections as any[] ) { + if ( section.line === sectionName ) { + if ( currentN < n ) { + currentN++; + } else { + indexSection = section; + break; + } + } + } if ( indexSection ) { return isNaN( +indexSection.index ) ? null : +indexSection.index; diff --git a/src/wiki/util/sectionHeadingN.ts b/src/wiki/util/sectionHeadingN.ts index 8eafa679..0b52509e 100644 --- a/src/wiki/util/sectionHeadingN.ts +++ b/src/wiki/util/sectionHeadingN.ts @@ -24,8 +24,8 @@ import sectionHeadingId from './sectionHeadingId'; export default function ( heading: HTMLHeadingElement, headingName: string ): number { try { - const headingNameEndPattern = /(?:\s|_)*(\d+)$/g; - const headingIdEndPattern = /_(\d+)$/g; + const headingNameEndPattern = /(?:\s|_)*(\d+)/g; + const headingIdEndPattern = /_(\d+)/g; const headingId = sectionHeadingId( heading ); const headingIdMatches = headingId.match( headingIdEndPattern );