Skip to content

Commit

Permalink
[partial#2] cci: section name conflict handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ChlodAlejandro committed Nov 23, 2022
1 parent c525164 commit 5e2b60f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/ui/root/DeputyContributionSurveySection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default class DeputyContributionSurveySection implements DeputyUIElement
private _section: ContributionSurveySection;
heading: HTMLHeadingElement;
headingName: string;
headingN: number;
sectionElements: HTMLElement[];
originalList: HTMLElement;
/**
Expand Down Expand Up @@ -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 );
}

Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
21 changes: 18 additions & 3 deletions src/wiki/util/getSectionId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/wiki/util/sectionHeadingN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down

0 comments on commit 5e2b60f

Please sign in to comment.