-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
ec889f3
commit 2cd7642
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} |