Skip to content

Commit

Permalink
enhanced exercise navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
mimiflynn committed Dec 27, 2023
1 parent 66a8f62 commit 6ca029c
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 49 deletions.
2 changes: 2 additions & 0 deletions site/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ exports.createPages = async ({ graphql, actions }) => {
pages.forEach((page) => {
const category = getCategory(page);
const path = page.internal.contentFilePath;
const level = page.frontmatter.level;
createPage({
path: page.fields.slug,
component: `${getTemplate(path)}?__contentFilePath=${path}`,
context: {
id: page.id,
categoryRegEx: `/${category}//`,
levelRegEx: `/${level}/`,
},
});
});
Expand Down
19 changes: 0 additions & 19 deletions site/src/components/excercises.jsx

This file was deleted.

65 changes: 65 additions & 0 deletions site/src/components/exercise-nav.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import React from 'react';
import { Link } from 'gatsby';

function getLevels(nodes) {
const potentialValues = [];
nodes.forEach((node) => {
if (!potentialValues.includes(node.frontmatter.level)) {
potentialValues.push(node.frontmatter.level);
}
});
return potentialValues;
}

const ExerciseNav = ({ location, nodes, toc }) => {
const levels = getLevels(nodes);

const ExerciseListItems = ({ location, nodes, toc }) => {
return nodes.map((node) => {
const current = location.pathname.includes(node.fields.slug);
const title = node.frontmatter.title;
return (
<li className={current ? 'current' : ''}>
<Link to={node.fields.slug}>
{node.frontmatter.exercise} ) {title}
</Link>
{current && toc && (
<nav className="nav exercise-content-nav">
<ul>
{toc.map((item) => (
<li>
<Link to={item.url}>{item.title}</Link>
</li>
))}
</ul>
</nav>
)}
</li>
);
});
};

return (
<nav className="nav exercise-nav">
{levels.map((level) => {
const currentLevel = nodes.filter(
(node) => node.frontmatter.level === level
);
return (
<>
{levels.length > 1 && <h3>Level {level}</h3>}
<ul>
<ExerciseListItems
location={location}
nodes={currentLevel}
toc={toc}
/>
</ul>
</>
);
})}
</nav>
);
};

export default ExerciseNav;
26 changes: 26 additions & 0 deletions site/src/components/exercises.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';
import { Link } from 'gatsby';

const ExerciseList = ({ nodes }) => {
let currentLevel;
return nodes.map((node) => {
currentLevel =
currentLevel === node.frontmatter.level
? currentLevel
: node.frontmatter.level;
return (
<article key={node.fields.slug} className="content">
<header>
<h3>
<Link to={node.fields.slug}>
Exercise {node.frontmatter.exercise} | {node.frontmatter.title}
</Link>
</h3>
<p>{node.frontmatter.description}</p>
</header>
</article>
);
});
};

export default ExerciseList;
2 changes: 1 addition & 1 deletion site/src/pages/exercises/circuitpython/level-1/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { Link, graphql } from 'gatsby';

import ExerciseList from '../../../../components/excercises';
import ExerciseList from '../../../../components/exercises';
import Hero from '../../../../components/hero';
import Layout from '../../../../components/layout';
import Seo from '../../../../components/seo';
Expand Down
2 changes: 1 addition & 1 deletion site/src/pages/exercises/circuitpython/level-2/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { Link, graphql } from 'gatsby';

import ExerciseList from '../../../../components/excercises';
import ExerciseList from '../../../../components/exercises';
import Hero from '../../../../components/hero';
import Layout from '../../../../components/layout';
import Seo from '../../../../components/seo';
Expand Down
2 changes: 1 addition & 1 deletion site/src/pages/exercises/makecode/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { graphql } from 'gatsby';

import ExerciseList from '../../../components/excercises';
import ExerciseList from '../../../components/exercises';
import Hero from '../../../components/hero';
import Layout from '../../../components/layout';

Expand Down
32 changes: 5 additions & 27 deletions site/src/templates/exercise.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { Link, graphql } from 'gatsby';

import ExerciseNav from '../components/exercise-nav';
import Layout from '../components/layout';
import Seo from '../components/seo';

Expand All @@ -24,33 +25,7 @@ const ExerciseTemplate = ({ children, data, pageContext, location }) => {
<Layout location={location}>
<Seo title={pageTitle} description={pageContext.description} />
<article className="exercise-main content">
<nav className="nav exercise-nav">
<ul>
{nodes.map((node) => {
const current = location.pathname.includes(node.fields.slug);
const title = node.frontmatter.title;
return (
<li className={current ? 'current' : ''}>
<Link to={node.fields.slug}>
{node.frontmatter.exercise} ) {title}
</Link>
{current && (
<nav className="nav exercise-content-nav">
<ul>
{toc &&
toc.map((item) => (
<li>
<Link to={item.url}>{item.title}</Link>
</li>
))}
</ul>
</nav>
)}
</li>
);
})}
</ul>
</nav>
<ExerciseNav location={location} nodes={nodes} toc={toc} />
<div className="exercise-content">
<header>
<h2>{pageTitle}</h2>
Expand Down Expand Up @@ -95,6 +70,8 @@ export const pageQuery = graphql`
mdx(id: { eq: $id }) {
frontmatter {
title
level
exercise
}
internal {
contentFilePath
Expand All @@ -117,6 +94,7 @@ export const pageQuery = graphql`
frontmatter {
title
exercise
level
}
internal {
contentFilePath
Expand Down

0 comments on commit 6ca029c

Please sign in to comment.