-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(docs): finished adding dynamic pages and sidebar
- Loading branch information
1 parent
61b9fde
commit 6c7815f
Showing
7 changed files
with
250 additions
and
17 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
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
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,25 @@ | ||
import * as capitalize from "lodash.capitalize" | ||
|
||
export const fetchAndGenerateSidebarItems = async (networkName) => { | ||
try { | ||
const response = await fetch(`https://sot-network-methods.vercel.app/specs/${networkName}`); | ||
const data = await response.json(); | ||
const dynamicItems = data.methods.map((method) => ({ | ||
type: "link", | ||
label: method.name, | ||
href: `/services/reference/linea/json-rpc-methods-new/${method.name}`, | ||
})).sort((a, b) => a.label.localeCompare(b.label)); | ||
return [ | ||
{ | ||
type: "category", | ||
label: "JSON-RPC Methods NEW", | ||
collapsed: true, | ||
collapsible: true, | ||
items: dynamicItems, | ||
}, | ||
]; | ||
} catch (error) { | ||
console.error("Error fetching methods:", error); | ||
return []; | ||
} | ||
} |
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,125 @@ | ||
import Layout from "@theme/Layout"; | ||
import { NETWORK_NAMES } from "@site/src/plugins/plugin-json-rpc"; | ||
import ParserOpenRPC from "@site/src/components/ParserOpenRPC"; | ||
import React, { useEffect, useState } from "react"; | ||
import DocSidebar from '@theme/DocSidebar'; | ||
import styles from "@site/src/theme/Layout/styles.module.css" | ||
import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; | ||
import { fetchAndGenerateSidebarItems } from "@site/src/helpers"; | ||
import * as capitalize from "lodash.capitalize" | ||
|
||
function generateSidebarItems(docs) { | ||
const categories = {}; | ||
|
||
docs.forEach((doc) => { | ||
if (doc.id === 'index') { | ||
categories['Introduction'] = { | ||
type: 'link', | ||
href: '/services', | ||
label: capitalize(doc.frontMatter?.sidebar_label || doc.title), | ||
}; | ||
return; | ||
} | ||
|
||
const pathParts = doc.sourceDirName.split('/'); | ||
let currentCategory = categories; | ||
let isIndexPage = doc.id.endsWith('/index'); | ||
|
||
pathParts.forEach((part, index) => { | ||
if (!currentCategory[part]) { | ||
if (isIndexPage && index === pathParts.length - 2) { | ||
currentCategory[part] = { | ||
type: 'category', | ||
label: capitalize(doc.frontMatter?.sidebar_label || doc.frontMatter?.title || part), | ||
collapsed: true, | ||
collapsible: true, | ||
link: { | ||
type: 'generated-index', | ||
slug: pathParts.slice(0, index + 1).join('/') | ||
}, | ||
items: [] | ||
}; | ||
} else { | ||
currentCategory[part] = { | ||
type: 'category', | ||
label: capitalize(part), | ||
collapsed: true, | ||
collapsible: true, | ||
items: [] | ||
}; | ||
} | ||
} | ||
|
||
if (index === pathParts.length - 1 && !isIndexPage) { | ||
currentCategory[part].items.push({ | ||
type: 'link', | ||
label: capitalize(doc.frontMatter?.title || doc.title), | ||
href: `/services/${doc.id.replace(/\/index$/, '')}`, | ||
sidebar_position: doc.frontMatter?.sidebar_position || Number.MAX_SAFE_INTEGER | ||
}); | ||
} | ||
currentCategory = currentCategory[part].items; | ||
}); | ||
}); | ||
|
||
const convertToArray = (categoryObj) => { | ||
return Object.values(categoryObj).map((category) => { | ||
if (category.items && typeof category.items === 'object') { | ||
category.items = convertToArray(category.items); | ||
if (category.items.every(item => item.sidebar_position !== undefined)) { | ||
category.items.sort((a, b) => (a.sidebar_position || Number.MAX_SAFE_INTEGER) - (b.sidebar_position || Number.MAX_SAFE_INTEGER)); | ||
} | ||
} | ||
return category; | ||
}); | ||
}; | ||
return convertToArray(categories); | ||
} | ||
|
||
const sidebar_wrapper_classes = "theme-doc-sidebar-container docSidebarContainer_node_modules-@docusaurus-theme-classic-lib-theme-DocRoot-Layout-Sidebar-styles-module" | ||
|
||
const CustomPage = (props) => { | ||
const customData = props.route.customData; | ||
const { siteConfig } = useDocusaurusContext(); | ||
const [formattedData, setFormattedData] = useState([]); | ||
const networkName = "linea"; | ||
|
||
useEffect(() => { | ||
fetchAndGenerateSidebarItems(networkName).then(generatedItems => { | ||
setFormattedData(generateSidebarItems(siteConfig.customFields.sidebarData.docs).map(item => { | ||
if (item?.label === "Reference" && item?.items) { | ||
return { | ||
...item, | ||
items: item.items.map(referenceItem => { | ||
if (referenceItem?.label === capitalize(networkName) && referenceItem?.items) { | ||
return { ...referenceItem, items: [...referenceItem.items, ...generatedItems] }; | ||
} | ||
return referenceItem; | ||
}) | ||
} | ||
} | ||
return item; | ||
})); | ||
}); | ||
}, []); | ||
|
||
return formattedData ? ( | ||
<Layout> | ||
<div className={styles.pageWrapper}> | ||
<aside className={sidebar_wrapper_classes}> | ||
<DocSidebar sidebar={formattedData} path="" onCollapse={() => {}} isHidden={false} /> | ||
</aside> | ||
<div className={styles.mainContainer}> | ||
<div className={styles.contentWrapper}> | ||
<ParserOpenRPC | ||
network={NETWORK_NAMES.linea} | ||
method={customData.name} | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
</Layout> | ||
) : null; | ||
}; | ||
|
||
export default CustomPage; |
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