-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Navigation for modification within a content/ directory. (#165)
* Refactor Navigation for modification within a content/ directory. * Add filter to weed out non-mdx files. * Pare down content pages for easy duplication.
- Loading branch information
1 parent
61630de
commit e670c4c
Showing
15 changed files
with
177 additions
and
170 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,14 @@ | ||
[ | ||
{ | ||
"path": "/works", | ||
"text": "Works" | ||
}, | ||
{ | ||
"path": "/metadata", | ||
"text": "Metadata" | ||
}, | ||
{ | ||
"path": "/about", | ||
"text": "About" | ||
} | ||
] |
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,22 @@ | ||
[ | ||
{ | ||
"path": "/about", | ||
"text": "About" | ||
}, | ||
{ | ||
"path": "/about/documentation", | ||
"text": "Documentation" | ||
}, | ||
{ | ||
"path": "/about/example", | ||
"text": "Markdown Example" | ||
}, | ||
{ | ||
"path": "/about/another-example", | ||
"text": "Another Example" | ||
}, | ||
{ | ||
"path": "/about/history", | ||
"text": "Project History" | ||
} | ||
] |
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
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,56 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
import { NavigationItem } from "@src/customTypes/navigation"; | ||
|
||
const useNavigation = ({ relativePath }: { relativePath?: string }) => { | ||
const [navigation, setNavigation] = useState<NavigationItem[]>(); | ||
|
||
useEffect(() => { | ||
(async () => { | ||
try { | ||
if (typeof relativePath === "undefined") return; | ||
|
||
const contentDirectory = "content/"; | ||
const directory = relativePath | ||
? `${contentDirectory}${relativePath}` | ||
: contentDirectory; | ||
|
||
const data = await import(`../../${directory}_meta.json`); | ||
const json: NavigationItem[] = transformObjectWithGetters( | ||
data?.default | ||
); | ||
|
||
setNavigation(json); | ||
} catch (error) { | ||
console.warn(error); | ||
console.warn("The relative _meta.json file could not be found."); | ||
} | ||
})(); | ||
}, []); | ||
|
||
return { navigation }; | ||
}; | ||
|
||
function transformObjectWithGetters(obj: any) { | ||
const result = Array.isArray(obj) ? [] : {}; | ||
|
||
for (const key in obj) { | ||
if (Object.prototype.hasOwnProperty.call(obj, key)) { | ||
const desc = Object.getOwnPropertyDescriptor(obj, key); | ||
if (desc?.get) { | ||
// @ts-ignore | ||
result[key] = desc.get.call(obj); | ||
} else { | ||
// @ts-ignore | ||
result[key] = | ||
typeof obj[key] === "object" && obj[key] !== null | ||
? transformObjectWithGetters(obj[key]) | ||
: obj[key]; | ||
} | ||
} | ||
} | ||
|
||
return result as NavigationItem[]; | ||
} | ||
|
||
export default useNavigation; |
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
Oops, something went wrong.