-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🧑💻 Add dynamic tutorial lists (#9)
- Loading branch information
Showing
4 changed files
with
69 additions
and
5 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,16 @@ | ||
import React from 'react' | ||
import { usePluginData } from '@docusaurus/useGlobalData' | ||
|
||
export default function TutorialsList({ category }) { | ||
const { categories } = usePluginData('dynamic-tutorials-list'); | ||
|
||
return ( | ||
<ul> | ||
{ categories[category].map(tutorial => ( | ||
<li key={tutorial.name}> | ||
<a href={tutorial.href}>{ tutorial.title }</a> | ||
</li> | ||
)) } | ||
</ul> | ||
) | ||
} |
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,43 @@ | ||
const path = require('path') | ||
const fs = require('fs') | ||
|
||
function makeTitle(name) { | ||
return name.split(/\W+/) | ||
.map(word => word.length > 3 ? word.charAt(0).toUpperCase() + word.substring(1) : word) | ||
.join(" ") | ||
} | ||
|
||
function readCategories(dir) { | ||
return Object.fromEntries( | ||
fs.readdirSync(dir, { withFileTypes: true }) | ||
.filter(dirent => dirent.isDirectory()) | ||
.map(dirent => [dirent.name, readCategory(dirent)]) | ||
); | ||
} | ||
|
||
function readCategory(dirent) { | ||
const dir = path.join(dirent.path, dirent.name); | ||
|
||
return fs.readdirSync(dir) | ||
.filter(file => /\.mdx?/.test(file)) | ||
.map(file => { | ||
const name = file.substring(0, file.lastIndexOf('.')); | ||
const filePath = path.join(dir, file); | ||
const contents = fs.readFileSync(filePath).toString(); | ||
const title = contents.match(/\#[^\r\n]+/)?.[0]?.substring(1)?.trim() | ||
?? makeTitle(name); | ||
const href = `/docs/docs/tutorials/${dirent.name}/${name}` | ||
return { name, title, href }; | ||
}) | ||
} | ||
|
||
module.exports = async function dynamicTutorialsPlugin({ siteDir }) { | ||
return { | ||
name: 'dynamic-tutorials-list', | ||
async contentLoaded({ actions }) { | ||
const tutorialsDir = path.join(siteDir, '/docs/tutorials'); | ||
const categories = readCategories(tutorialsDir); | ||
actions.setGlobalData({ categories }); | ||
} | ||
} | ||
} |