diff --git a/lib/index.js b/lib/index.js index f40458e..a68847c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -79,22 +79,17 @@ class Page { this.#index = index this.#entries = entries this.title = header.text - this.titleKey = this.title - .replace(/[^a-zA-Z0-9]+/g, ' ') - .trim() - .replace(/\s+/g, '-') - .toLowerCase() } async load () { // assigns unique key value for page - if (this.rawKey === 'page') { - this.pageKey = `page_${this.#index + 1}` + if (!this.rawPageKey) { + this.pageKey = `-${this.#index + 1}` } else { - const prior = this.documentation.pages.slice(0, this.#index) - const existing = prior.filter(({ rawKey }) => rawKey === this.rawKey) + const prior = this.#documentation.pages.slice(0, this.#index) + const existing = prior.filter(({ rawPageKey }) => rawPageKey === this.rawPageKey) const ending = existing.length ? `-${existing.length}` : '' - this.pageKey = `${this.rawKey}${ending}` + this.pageKey = `${this.rawPageKey}${ending}` } // process page content @@ -126,21 +121,26 @@ class Page { // load all sections existing under this page await Promise.all(this.sections.map(section => section.load())) + + this.titleKey = this.rawTitleKey || '-' + return this } // generates the raw un-deduped key value for page - get rawKey () { - return ( - this.#file - .replace(/[^a-zA-Z0-9]+/g, '') - .trim() || - 'page' - ) + get rawPageKey () { + return this.#file + .replace(/[^a-zA-Z0-9]+/g, '') + .trim() } - get documentation () { - return this.#documentation + // generates the raw un-deduped key value for title + get rawTitleKey () { + return this.title + .replace(/[^a-zA-Z0-9]+/g, ' ') + .trim() + .replace(/\s+/g, '-') + .toLowerCase() } } @@ -158,17 +158,6 @@ class Section { this.title = header.text // assigns unique key value for section - const prior = [ - this.page.titleKey, - ...this.page.sections - .map(section => [section, ...section.subsections]) - .flat() - .map(({ rawKey }) => rawKey) - ] - const existing = prior.filter(rawKey => rawKey === this.rawKey) - const ending = existing.length ? `-${existing.length}` : '' - - this.titleKey = `${this.rawKey}${ending}` } async load () { @@ -201,35 +190,42 @@ class Section { // load all subsections existing under this section await Promise.all(this.subsections.map(section => section.load())) + + const all = [ + this.page, + ...this.page.sections + .map(section => [section, ...section.subsections]) + .flat() + ] + const index = all.findIndex(section => section === this) + const prior = all.slice(0, index).map(({ rawTitleKey }) => rawTitleKey) + const existing = prior.filter(rawTitleKey => rawTitleKey === this.rawTitleKey) + const ending = existing.length ? `-${existing.length}` : '' + + this.titleKey = `${this.rawTitleKey}${ending}` || '-' + return this } // generates the raw un-deduped key value for section - get rawKey () { - const baseKey = this.title + get rawTitleKey () { + return this.title .replace(/[^a-zA-Z0-9]+/g, ' ') .trim() .replace(/\s+/g, '-') .toLowerCase() - if (baseKey) return baseKey - - return this.section ? 'subsection' : 'section' } get parent () { return this.#parent } - get documentation () { - return this.page.documentation - } - get page () { - return this.parent.parent || this.parent + return this.#parent.page || this.#parent } get section () { - return this.parent.parent ? this.parent : null + return this.#parent.page ? this.#parent : null } } diff --git a/package-lock.json b/package-lock.json index 41d70f6..5b251a0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -46,9 +46,9 @@ } }, "node_modules/@humanwhocodes/config-array": { - "version": "0.10.5", - "resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.10.5.tgz", - "integrity": "sha512-XVVDtp+dVvRxMoxSiSfasYaG02VEe1qH5cKgMQJWhol6HwzbcqoCMJi8dAGoYAO57jhUyhI6cWuRiTcRaDaYug==", + "version": "0.10.6", + "resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.10.6.tgz", + "integrity": "sha512-U/piU+VwXZsIgwnl+N+nRK12jCpHdc3s0UAc6zc1+HUgiESJxClpvYao/x9JwaN7onNeVb7kTlxlAvuEoaJ3ig==", "dev": true, "dependencies": { "@humanwhocodes/object-schema": "^1.2.1", @@ -2537,9 +2537,9 @@ } }, "@humanwhocodes/config-array": { - "version": "0.10.5", - "resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.10.5.tgz", - "integrity": "sha512-XVVDtp+dVvRxMoxSiSfasYaG02VEe1qH5cKgMQJWhol6HwzbcqoCMJi8dAGoYAO57jhUyhI6cWuRiTcRaDaYug==", + "version": "0.10.6", + "resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.10.6.tgz", + "integrity": "sha512-U/piU+VwXZsIgwnl+N+nRK12jCpHdc3s0UAc6zc1+HUgiESJxClpvYao/x9JwaN7onNeVb7kTlxlAvuEoaJ3ig==", "dev": true, "requires": { "@humanwhocodes/object-schema": "^1.2.1",