Skip to content

Commit

Permalink
DOC-721 Add template for autogenerated index pages from filtered docs (
Browse files Browse the repository at this point in the history
…#236)

* Add template for autogenerated index pages from filtered docs

* Support filtering by component and version
  • Loading branch information
JakeSCahill authored Dec 12, 2024
1 parent ec889f3 commit 2cd7642
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/helpers/get-index-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module.exports = ({ data: { root } }) => {
const { page } = root
const indexKey = page.attributes['index-data']
if (!indexKey) return null

const indexData = page.component.asciidoc.attributes[indexKey]
if (!indexData) return null
const matchComponentVersion = page.attributes['match-component-version']

try {
const parsedData = JSON.parse(indexData)
const currentPageUrl = page.url
const currentComponent = page.component.name
const currentVersion = page.version

const filteredData = parsedData.reduce((acc, item) => {
if (item.pages && Array.isArray(item.pages)) {
const filteredPages = item.pages.filter((page) => {
const isNotCurrentPage = page.url !== currentPageUrl

// Optional filtering by component and version
const matchesComponentVersion = !matchComponentVersion || (
item.component === currentComponent && item.version === currentVersion
)

return isNotCurrentPage && matchesComponentVersion
})
acc.push(...filteredPages)
}
return acc
}, [])

return filteredData
} catch (error) {
console.log(
`Error parsing JSON attribute "${indexKey}" in ${page.url} component. Index page will be empty. Make sure the generate-index-data extension is correctly configured.\n\n ${error}`
)
return null
}
}
2 changes: 2 additions & 0 deletions src/partials/article.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
{{> component-home-v2}}
{{else if (eq page.attributes.role 'bloblang-playground')}}
{{> bloblang-playground}}
{{else if (eq page.attributes.role 'index-list')}}
{{> index-list}}
{{else if (eq page.layout 'index')}}
{{> index}}
{{else if (eq page.attributes.role 'related-labs')}}
Expand Down
18 changes: 18 additions & 0 deletions src/partials/index-list.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{{{page.contents}}}

{{#with (get-index-data) as |indexData|}}
{{#if indexData}}
<ul class="index">
{{#each indexData}}
<li class="index">
<a href="{{{relativize this.url}}}">{{{this.title}}}</a>
{{#if (ne this.title this.description)}}
<p class="index">{{{this.description}}}</p>
{{/if}}
</li>
{{/each}}
</ul>
{{else}}
<p>No index data found.</p>
{{/if}}
{{/with}}

0 comments on commit 2cd7642

Please sign in to comment.