Skip to content

Commit 2cd7642

Browse files
authored
DOC-721 Add template for autogenerated index pages from filtered docs (#236)
* Add template for autogenerated index pages from filtered docs * Support filtering by component and version
1 parent ec889f3 commit 2cd7642

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

src/helpers/get-index-data.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
module.exports = ({ data: { root } }) => {
2+
const { page } = root
3+
const indexKey = page.attributes['index-data']
4+
if (!indexKey) return null
5+
6+
const indexData = page.component.asciidoc.attributes[indexKey]
7+
if (!indexData) return null
8+
const matchComponentVersion = page.attributes['match-component-version']
9+
10+
try {
11+
const parsedData = JSON.parse(indexData)
12+
const currentPageUrl = page.url
13+
const currentComponent = page.component.name
14+
const currentVersion = page.version
15+
16+
const filteredData = parsedData.reduce((acc, item) => {
17+
if (item.pages && Array.isArray(item.pages)) {
18+
const filteredPages = item.pages.filter((page) => {
19+
const isNotCurrentPage = page.url !== currentPageUrl
20+
21+
// Optional filtering by component and version
22+
const matchesComponentVersion = !matchComponentVersion || (
23+
item.component === currentComponent && item.version === currentVersion
24+
)
25+
26+
return isNotCurrentPage && matchesComponentVersion
27+
})
28+
acc.push(...filteredPages)
29+
}
30+
return acc
31+
}, [])
32+
33+
return filteredData
34+
} catch (error) {
35+
console.log(
36+
`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}`
37+
)
38+
return null
39+
}
40+
}

src/partials/article.hbs

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
{{> component-home-v2}}
3737
{{else if (eq page.attributes.role 'bloblang-playground')}}
3838
{{> bloblang-playground}}
39+
{{else if (eq page.attributes.role 'index-list')}}
40+
{{> index-list}}
3941
{{else if (eq page.layout 'index')}}
4042
{{> index}}
4143
{{else if (eq page.attributes.role 'related-labs')}}

src/partials/index-list.hbs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{{{page.contents}}}
2+
3+
{{#with (get-index-data) as |indexData|}}
4+
{{#if indexData}}
5+
<ul class="index">
6+
{{#each indexData}}
7+
<li class="index">
8+
<a href="{{{relativize this.url}}}">{{{this.title}}}</a>
9+
{{#if (ne this.title this.description)}}
10+
<p class="index">{{{this.description}}}</p>
11+
{{/if}}
12+
</li>
13+
{{/each}}
14+
</ul>
15+
{{else}}
16+
<p>No index data found.</p>
17+
{{/if}}
18+
{{/with}}

0 commit comments

Comments
 (0)