Skip to content

Commit

Permalink
Merge pull request #662 from nature-of-code/dev/website-examples-page
Browse files Browse the repository at this point in the history
[Website] Example Page
  • Loading branch information
shiffman authored Jan 24, 2024
2 parents ffb6ba0 + 6146830 commit 6f0ed28
Show file tree
Hide file tree
Showing 15 changed files with 274 additions and 140 deletions.
1 change: 0 additions & 1 deletion content/chapters.json

This file was deleted.

1 change: 1 addition & 0 deletions content/content.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"title":"Dedication","type":"page","src":"./00_2_dedication.html","slug":"dedication"},{"title":"Acknowledgments","type":"page","src":"./00_4_acknowledgments.html","slug":"acknowledgements"},{"title":"Introduction","type":"page","src":"./00_5_introduction.html","slug":"introduction"},{"title":"0. Randomness","type":"chapter","src":"./00_randomness.html","slug":"random"},{"title":"1. Vectors","type":"chapter","src":"./01_vectors.html","slug":"vectors"},{"title":"2. Forces","type":"chapter","src":"./02_forces.html","slug":"force"},{"title":"3. Oscillation","type":"chapter","src":"./03_oscillation.html","slug":"oscillation"},{"title":"4. Particle Systems","type":"chapter","src":"./04_particles.html","slug":"particles"},{"title":"5. Autonomous Agents","type":"chapter","src":"./05_steering.html","slug":"autonomous-agents"},{"title":"6. Physics Libraries","type":"chapter","src":"./06_libraries.html","slug":"physics-libraries"},{"title":"7. Cellular Automata","type":"chapter","src":"./07_ca.html","slug":"cellular-automata"},{"title":"8. Fractals","type":"chapter","src":"./08_fractals.html","slug":"fractals"},{"title":"9. Evolutionary Computing","type":"chapter","src":"./09_ga.html","slug":"genetic-algorithms"},{"title":"10. Neural Networks","type":"chapter","src":"./10_nn.html","slug":"neural-networks"},{"title":"11. Neuroevolution","type":"chapter","src":"./11_nn_ga.html","slug":"neuroevolution"},{"title":"Appendix: Creature Design ","type":"page","src":"./xx_1_creature_design.html","slug":"appendix-creature"}]
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ function setup() {
function draw() {
background(255);

glow.update();
glow.show();

for (let creature of creatures) {
Expand All @@ -31,6 +30,7 @@ function draw() {
creature.seek(glow);
creature.update(glow);
}
glow.update();
lifeCounter++;
}

Expand Down
Binary file modified content/images/06_libraries/06_libraries_7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ module.exports = {
`gatsby-plugin-image`,
`gatsby-plugin-sharp`,
`gatsby-transformer-sharp`,
`gatsby-transformer-json`,
{
resolve: `gatsby-transformer-json`,
options: {
typeName: 'BookSection',
},
},
`gatsby-plugin-postcss`,
`gatsby-plugin-react-helmet`,
],
Expand Down
18 changes: 5 additions & 13 deletions gatsby/create-pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = async ({ graphql, actions, reporter }) => {

const result = await graphql(`
query {
allChaptersJson {
allBookSection {
edges {
previous {
id
Expand All @@ -26,11 +26,11 @@ module.exports = async ({ graphql, actions, reporter }) => {
reporter.panicOnBuild('🚨 ERROR: Loading "createPages" query');
}

// Create a page for each chapter
const chapters = result.data.allChaptersJson.edges;
// Create a page for each sections
const sections = result.data.allBookSection.edges;

if (chapters.length > 0) {
chapters.forEach(({ previous, node, next }) => {
if (sections.length > 0) {
sections.forEach(({ previous, node, next }) => {
const previousId = previous === null ? null : previous.id;
const nextId = next === null ? null : next.id;

Expand All @@ -45,12 +45,4 @@ module.exports = async ({ graphql, actions, reporter }) => {
});
});
}

// Create the example page (contains every embedded sketch with screenshot)
if (process.env.CREATE_EXAMPLES_PAGE === 'true') {
createPage({
path: '/examples',
component: path.resolve(`./src/layouts/ExamplesPageLayout.js`),
});
}
};
24 changes: 22 additions & 2 deletions gatsby/create-resolvers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Add images field to each ChaptersJson node
module.exports = ({ createResolvers }) => {
// Add images field
const resolvers = {
ChaptersJson: {
BookSection: {
images: {
type: ['File'],
resolve: async (source, args, context, info) => {
Expand All @@ -20,6 +20,26 @@ module.exports = ({ createResolvers }) => {
},
},
},

Example: {
screenshot: {
type: 'File',
resolve: async (source, args, context, info) => {
const screenshot = await context.nodeModel.findOne({
type: 'File',
query: {
filter: {
relativeDirectory: {
eq: source.relativeDirectory,
},
internal: { mediaType: { regex: '/^(image)/' } },
},
},
});
return screenshot;
},
},
},
};
createResolvers(resolvers);
};
47 changes: 38 additions & 9 deletions gatsby/lib/parse-content.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,6 @@ export function parseContent(html) {

const ast = unified().use(rehypeParse, { fragment: true }).parse(html);

const transformedAst = unified()
.use(replaceMedia)
.use(externalLinkInNewTab)
.use(rehypeCodesplit)
.use(rehypeHighlight)
.use(rehypeSlug)
.use(rehypeKatex)
.runSync(ast);

/**
* Generate Table of Content
*/
Expand All @@ -88,8 +79,46 @@ export function parseContent(html) {
});
});

/**
* List all examples
*/
const examples = [];
visit(ast, { tagName: 'div' }, (node) => {
if (node.properties.dataType !== 'example') {
return;
}

// Locate the h3 element within the current node's children
const titleNode = node.children.find((child) => child.tagName === 'h3');
const slug = titleNode && titleNode.properties.id;
const title =
titleNode && titleNode.children.length > 0
? titleNode.children[0].value
: '';

visit(node, { tagName: 'div' }, (embedDivNode) => {
if (embedDivNode.properties.dataType !== 'embed') return;
examples.push({
title,
slug,
relativeDirectory: embedDivNode.properties.dataExamplePath,
webEditorURL: embedDivNode.properties.dataP5Editor,
});
});
});

const transformedAst = unified()
.use(replaceMedia)
.use(externalLinkInNewTab)
.use(rehypeCodesplit)
.use(rehypeHighlight)
.use(rehypeSlug)
.use(rehypeKatex)
.runSync(ast);

return {
ast: transformedAst,
toc,
examples,
};
}
27 changes: 24 additions & 3 deletions gatsby/on-create-node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
module.exports = async ({ node, actions, loadNodeContent }) => {
const { createNodeField } = actions;
module.exports = async ({
node,
actions,
loadNodeContent,
createContentDigest,
createNodeId,
}) => {
const { createNodeField, createNode, createParentChildLink } = actions;

if (node.internal.mediaType !== `text/html`) {
return;
Expand All @@ -9,7 +15,7 @@ module.exports = async ({ node, actions, loadNodeContent }) => {

// load the html source to every HTML file node
const content = await loadNodeContent(node);
const { ast, toc } = parseContent(content);
const { ast, toc, examples } = parseContent(content);

createNodeField({
node,
Expand All @@ -22,4 +28,19 @@ module.exports = async ({ node, actions, loadNodeContent }) => {
name: 'toc',
value: JSON.stringify(toc),
});

for (let example of examples) {
const exampleNode = {
id: createNodeId(example.relativeDirectory),
parent: node.id,
internal: {
type: 'Example',
contentDigest: createContentDigest(example),
},
...example,
};

createNode(exampleNode);
createParentChildLink({ parent: node, child: exampleNode });
}
};
14 changes: 12 additions & 2 deletions src/components/ChapterNav.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useStaticQuery, graphql, Link } from 'gatsby';
const ChapterNav = () => {
const data = useStaticQuery(graphql`
query QueryChaptersLink {
allChaptersJson {
allBookSection {
edges {
node {
id
Expand All @@ -19,7 +19,7 @@ const ChapterNav = () => {
return (
<nav className="py-6">
<ul className="space-y-1">
{data.allChaptersJson.edges.map(({ node }) => {
{data.allBookSection.edges.map(({ node }) => {
return (
<li key={node.slug}>
<Link
Expand All @@ -32,6 +32,16 @@ const ChapterNav = () => {
</li>
);
})}

<li key="examples">
<Link
to={`/examples/`}
className="block font-semibold px-3 py-1 hover:underline"
activeClassName="bg-gray-100 rounded-l"
>
Examples
</Link>
</li>
</ul>
</nav>
);
Expand Down
58 changes: 58 additions & 0 deletions src/images/p5js_logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions src/layouts/ChapterLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default function ChapterLayout({ data }) {

export const query = graphql`
query ChapterById($id: String!, $previousId: String, $nextId: String) {
chapter: chaptersJson(id: { eq: $id }) {
chapter: bookSection(id: { eq: $id }) {
id
slug
title
Expand All @@ -93,11 +93,11 @@ export const query = graphql`
}
}
}
previous: chaptersJson(id: { eq: $previousId }) {
previous: bookSection(id: { eq: $previousId }) {
slug
title
}
next: chaptersJson(id: { eq: $nextId }) {
next: bookSection(id: { eq: $nextId }) {
slug
title
}
Expand Down
Loading

0 comments on commit 6f0ed28

Please sign in to comment.