-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dynamically creating the sidebar
- v1.97.5
- v1.97.4
- v1.97.3
- v1.97.2
- v1.97.1
- v1.97.0
- v1.96.6
- v1.96.5
- v1.96.4
- v1.96.3
- v1.96.2
- v1.96.1
- v1.96.0
- v1.95.2
- v1.95.1
- v1.95.0
- v1.94.0
- v1.93.3
- v1.93.2
- v1.93.1
- v1.93.0
- v1.92.3
- v1.92.2
- v1.92.1
- v1.92.0
- v1.91.2
- v1.91.1
- v1.91.0
- v1.90.0
- v1.89.3
- v1.89.2
- v1.89.1
- v1.89.0
- v1.88.5
- v1.88.4
- v1.88.3
- v1.88.2
- v1.88.1
- v1.88.0
- v1.87.2
- v1.87.1
- v1.87.0
- v1.86.2
- v1.86.1
- v1.86.0
- v1.85.0
- v1.84.1
- v1.84.0
- v1.83.0
- v1.82.3
- v1.82.2
- v1.82.1
- v1.82.0
- v1.81.3
- v1.81.2
- v1.81.1
- v1.81.0
- v1.80.0
- v1.79.0
- v1.78.0
- v1.77.0
- v1.76.3
- v1.76.2
- v1.76.1
- v1.76.0
- v1.75.0
- v1.74.0
- v1.73.0
- v1.72.1
- v1.72.0
- v1.71.2
- v1.71.1
- v1.71.0
- v1.70.0
- v1.69.2
- v1.69.1
- v1.69.0
- v1.68.3
- v1.68.2
- v1.68.1
- v1.68.0
- v1.67.5
- v1.67.4
- v1.67.3
- v1.67.2
- v1.67.1
- v1.67.0
- v1.66.1
- v1.66.0
- v1.65.0
- v1.64.0
- v1.63.0
- v1.62.0
- v1.61.0
- v1.60.0
- v1.59.2
- v1.59.1
- v1.59.0
- v1.58.0
- v1.57.2
- v1.57.1
- v1.57.0
- v1.56.0
- v1.55.0
- v1.54.0
- v1.53.1
- v1.53.0
- v1.52.3
- v1.52.2
- v1.52.1
- v1.52.0
- v1.51.0
- v1.50.2
- v1.50.1
- v1.50.0
- v1.49.0
- v1.48.1
- v1.48.0
- v1.47.3
- v1.47.2
- v1.47.1
- v1.47.0
- v1.46.0
- v1.45.0
- v1.44.3
- v1.44.2
- v1.44.1
- v1.44.0
- v1.43.0
- v1.42.3
- v1.42.2
- v1.42.1
- v1.42.0
- v1.41.0
- v1.40.2
- v1.40.1
- v1.40.0
- v1.39.0
- v1.38.0
- v1.37.0
- v1.36.0
- v1.35.7
- v1.35.6
- v1.35.5
- v1.35.4
- v1.35.3
- v1.35.2
- v1.35.1
- v1.35.0
- v1.34.0
- v1.33.0
- v1.32.1
- v1.32.0
- v1.31.8
- v1.31.7
- v1.31.6
- v1.31.5
- v1.31.4
- v1.31.3
- v1.31.2
- v1.31.1
- v1.31.0
- v1.30.1
- v1.30.0
- v1.29.1
- v1.29.0
- v1.28.1
- v1.28.0
- v1.27.1
- v1.27.0
- v1.26.0
- v1.25.0
- v1.24.0
- v1.23.0
- v1.22.0
- v1.21.2
- v1.21.1
- v1.21.0
- v1.20.5
- v1.20.4
- v1.20.3
- v1.20.2
- v1.20.1
- v1.20.0
- v1.19.0
- v1.18.0
- v1.17.1
- v1.17.0
- v1.16.0
- v1.15.3
- v1.15.2
- v1.15.1
- v1.15.0
- v1.14.2
- v1.14.1
- v1.14.0
- v1.13.11
- v1.13.10
- v1.13.9
- v1.13.8
- v1.13.7
- v1.13.6
- v1.13.5
- v1.13.4
- v1.13.3
- v1.13.2
- v1.13.1
- v1.13.0
- v1.12.5
- v1.12.4
- v1.12.3
- v1.12.2
- v1.12.1
- v1.12.0
- v1.11.0
- v1.10.6
- v1.10.5
- v1.10.4
- v1.10.3
- v1.10.2
- v1.10.1
- v1.10.0
- v1.9.3
- v1.9.2
- v1.9.1
- v1.9.0
- v1.8.1
- v1.8.0
- v1.7.2
- v1.7.1
- v1.7.0
- v1.6.1
- v1.6.0
- v1.5.3
- v1.5.2
- v1.5.1
- v1.5.0
- v1.4.0
- v1.3.0
- v1.2.1
- v1.2.0
- v1.1.0
- v1.0.0
Showing
3 changed files
with
125 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/** | ||
* Convert an object used for generating the tree into an object | ||
* to be used in the UI. | ||
* | ||
* @param {Object} link - A link object that includes directories. | ||
* @param {string} link.url - The local path to the page. | ||
* @param {string} link.displayName - The title for the page. | ||
* @return {Object} A link object with just a url and displayName. | ||
*/ | ||
const linkWithoutDirs = (link) => ({ | ||
url: link.url, | ||
displayName: link.displayName, | ||
}); | ||
|
||
/** | ||
* Creates a link object (including a list of directories) from an | ||
* edge (generated by GraphQL). | ||
* | ||
* @param {Object} edge - A markdown edge. | ||
* @return {Object} A link object that includes directories. | ||
*/ | ||
const linkFromEdge = (edge) => { | ||
const { path, title } = edge?.node?.frontmatter; | ||
return { | ||
url: path, | ||
displayName: title, | ||
dirs: path.split('/').slice(1), | ||
}; | ||
}; | ||
|
||
/** | ||
* Converts a slug into a displayName. | ||
* | ||
* @param {string} str - A slug-like string. | ||
* @return {string} A displayName for use in the UI. | ||
*/ | ||
const makeDisplayName = (str) => | ||
str | ||
.split('-') | ||
.map((word) => word.replace(/\b\w/g, (l) => l.toUpperCase())) | ||
.join(' '); | ||
|
||
/** | ||
* Generates an array of links for the UI. Each link can have an | ||
* array of links below that. This function can call itself. | ||
* | ||
* TODO: do we need result? | ||
* | ||
* @param {Object[]} links - An array of links that are >= this level. | ||
* @param {Object[]} [result] - The links to be returned. | ||
* @param {number} [level=0] The nested level. | ||
*/ | ||
const genTree = (links, result = [], level = 0) => { | ||
const linksAtLevel = links.filter((link) => level === link.dirs.length - 1); | ||
|
||
// if we have nothing below this, just return a flat list of links | ||
if (linksAtLevel.length === links.length) { | ||
return linksAtLevel.map(linkWithoutDirs); | ||
} | ||
|
||
// get all the directories at this level | ||
const linksBelowLevel = links.filter((link) => level < link.dirs.length - 1); | ||
const dirsAtLevel = linksBelowLevel.map((link) => link.dirs[level]); | ||
const uniqueDirsAtLevel = [...new Set(dirsAtLevel)]; | ||
|
||
return uniqueDirsAtLevel.reduce((acc, dir) => { | ||
const linksUnderDir = links.filter((link) => link.dirs[level] === dir); | ||
|
||
// find the index page, or make a non-link item for this | ||
const index = linksUnderDir.find( | ||
(link) => link.dirs[level + 1] === 'index' | ||
); | ||
|
||
// get the children for this node | ||
const childLinks = linksUnderDir.filter((link) => link !== index); | ||
|
||
return [ | ||
...acc, | ||
{ | ||
...(index | ||
? linkWithoutDirs(index) | ||
: { displayName: makeDisplayName(dir) }), | ||
children: genTree(childLinks, result, level + 1), | ||
}, | ||
]; | ||
}, []); | ||
}; | ||
|
||
/** | ||
* Given a list of edges, generates the navigation to be used in the UI. | ||
* | ||
* @param {Object[]} edges - An array of edge objects. | ||
* @return {Object[]} An array of link objects. | ||
*/ | ||
const getNavFromEdges = (edges) => { | ||
const links = edges.map(linkFromEdge); | ||
return genTree(links); | ||
}; | ||
|
||
export default getNavFromEdges; |