Skip to content

Commit

Permalink
adjust anchor tag generation to mirror github's assignment pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
derekwolpert committed Sep 28, 2022
1 parent 5ed6b10 commit 8dfad14
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class Page {
} else {
const prior = this.documentation.pages.slice(0, this.#index)
const existing = prior.filter(({ rawKey }) => rawKey === this.rawKey)
const ending = existing.length ? `_${existing.length + 1}` : ''
const ending = existing.length ? `-${existing.length}` : ''
this.key = `${this.rawKey}${ending}`
}

Expand Down Expand Up @@ -125,11 +125,9 @@ class Page {
// generates the raw un-deduped key value for page
get rawKey () {
return (
(this.title || this.#file)
.replace(/[^a-zA-Z0-9]+/g, ' ')
.trim()
.replace(/\s+/g, '-')
.toLowerCase() ||
this.#file
.replace(/[^a-zA-Z0-9]+/g, '')
.trim() ||
'page'
)
}
Expand All @@ -154,15 +152,18 @@ class Section {

// assigns unique key value for section
const prior = [
...this.documentation.pages,
this.page.title
.replace(/[^a-zA-Z0-9]+/g, ' ')
.trim()
.replace(/\s+/g, '-')
.toLowerCase(),
...this.page.sections
.map(section => [section, ...section.subsections])
.flat()
.map(({ rawKey }) => rawKey)
]
const existing = prior.filter(({ rawKey }) => rawKey === this.rawKey)
const ending = (existing.length || ['section', 'subsection'].includes(this.rawKey))
? `_${existing.length + 1}`
: ''
const existing = prior.filter(rawKey => rawKey === this.rawKey)
const ending = existing.length ? `-${existing.length}` : ''

this.key = `${this.rawKey}${ending}`
}
Expand Down Expand Up @@ -209,12 +210,6 @@ class Section {
.toLowerCase()
if (baseKey) return baseKey

const sectionKey = this.section?.rawKey
if (sectionKey && sectionKey !== 'section') return sectionKey

const pageKey = this.page?.rawKey
if (pageKey && pageKey !== 'page') return pageKey

return this.section ? 'subsection' : 'section'
}

Expand Down

0 comments on commit 8dfad14

Please sign in to comment.