Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC-721 Add template for autogenerated index pages from filtered docs #236

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}}