Skip to content

Commit

Permalink
Autogen category links for Reddit nesting (#14)
Browse files Browse the repository at this point in the history
Customizes the result of the autogenerated sidebar to organize it the same way Reddit's wiki is organized.
This means linking the category created by folders to the "index" page for that folder located one level up with the same name as the folder. Also, removes the no longer needed sidebar item to the "index" page because it would be redundant (see method `updateAutogeneratedSiderbarForRedditWikiStructure`).
  • Loading branch information
shakuzen authored Sep 20, 2023
1 parent aa99426 commit 4b6280e
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/countries/us/_category_.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
label: US
link:
type: doc
id: countries/us
4 changes: 4 additions & 0 deletions docs/earning/business/_category_.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
label: Running a business
link:
type: doc
id: earning/business
5 changes: 5 additions & 0 deletions docs/tax/income/_category_.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
position: 1
label: Income Tax
link:
type: doc
id: tax/income
5 changes: 5 additions & 0 deletions docs/tax/residence/_category_.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
position: 2
label: Residence Tax
link:
type: doc
id: tax/residence
22 changes: 21 additions & 1 deletion docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ const config = {
docs: {
routeBasePath: '/',
sidebarPath: require.resolve('./sidebars.js'),
editUrl: ({docPath}) =>
editUrl: ({ docPath }) =>
`https://www.reddit.com/r/JapanFinance/wiki/edit/index/${docPath.includes("index") ? '' : docPath.slice(0, docPath.lastIndexOf('.'))}`,
async sidebarItemsGenerator({ defaultSidebarItemsGenerator, ...args }) {
const sidebarItems = await defaultSidebarItemsGenerator(args);
return updateAutogeneratedSiderbarForRedditWikiStructure(sidebarItems);
},
},
blog: false,
theme: {
Expand Down Expand Up @@ -125,3 +129,19 @@ const config = {
};

module.exports = config;

/**
* @param {import("@docusaurus/plugin-content-docs/src/sidebars/types.js").NormalizedSidebar} items
*/
function updateAutogeneratedSiderbarForRedditWikiStructure(items) {
/** @type {import("@docusaurus/plugin-content-docs/src/sidebars/types.js").SidebarItemDoc[]} */
const docs = items.filter(item => item.type === 'doc');
let docsToRemove = [];
items.filter(item => item.type === "category" && item.link !== undefined && item.link.type === 'doc' && docs.find(doc => doc.id === item.link.id) !== undefined)
.forEach(item => {
const matchingDoc = docs.find(doc => doc.id === item.link.id);
docsToRemove.push(matchingDoc);
});
const result = items.filter(item => !docsToRemove.includes(item));
return result;
}
2 changes: 1 addition & 1 deletion sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const sidebars = {
Other: [
{ type: 'autogenerated', dirName: 'other'},
]
}
},
],

};
Expand Down

0 comments on commit 4b6280e

Please sign in to comment.