Skip to content

Commit

Permalink
tweaks to better match githubs anchor tag id assignment behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
derekwolpert committed Sep 29, 2022
1 parent c011cad commit c39f355
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 47 deletions.
78 changes: 37 additions & 41 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}

Expand All @@ -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 () {
Expand Down Expand Up @@ -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
}
}

Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c39f355

Please sign in to comment.