Skip to content

Commit 36f3c4a

Browse files
committed
refactor(site): finish content-utils thus simplifying the Site component
1 parent f7bbce7 commit 36f3c4a

File tree

2 files changed

+37
-45
lines changed

2 files changed

+37
-45
lines changed

src/components/Site/Site.jsx

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import React from 'react';
33
import { Switch, Route } from 'react-router-dom';
44
import { hot as Hot } from 'react-hot-loader';
55

6+
// Import Utilities
7+
import { ExtractPages, ExtractSections } from '../../utilities/content-utils';
8+
69
// Import Components
710
import NotificationBar from '../NotificationBar/NotificationBar';
811
import Navigation from '../Navigation/Navigation';
@@ -31,8 +34,9 @@ class Site extends React.Component {
3134
render() {
3235
let { location } = this.props;
3336
let { mobileSidebarOpen } = this.state;
34-
let sections = this._sections;
37+
let sections = ExtractSections(Content);
3538
let section = sections.find(({ url }) => location.pathname.startsWith(url));
39+
let pages = ExtractPages(Content);
3640

3741
return (
3842
<div className="site">
@@ -70,7 +74,7 @@ class Site extends React.Component {
7074
render={ props => (
7175
<Container className="site__content">
7276
<Switch>
73-
{ this._pages.map(page => (
77+
{ pages.map(page => (
7478
<Route
7579
key={ page.url }
7680
exact={ true }
@@ -123,20 +127,6 @@ class Site extends React.Component {
123127
});
124128
}
125129

126-
/**
127-
* Flatten an array of `Content` items
128-
*
129-
* @param {array} array - ...
130-
* @return {array} - ...
131-
*/
132-
_flatten = array => {
133-
return array.reduce((flat, item) => {
134-
return flat.concat(
135-
Array.isArray(item.children) ? this._flatten(item.children) : item
136-
);
137-
}, []);
138-
}
139-
140130
/**
141131
* Strip any non-applicable properties
142132
*
@@ -154,28 +144,6 @@ class Site extends React.Component {
154144
children: children ? this._strip(children) : []
155145
}));
156146
}
157-
158-
/**
159-
* Get top-level sections
160-
*
161-
* @return {array} - ...
162-
*/
163-
get _sections() {
164-
return Content.children.filter(item => (
165-
item.type === 'directory'
166-
));
167-
}
168-
169-
/**
170-
* Get all markdown pages
171-
*
172-
* @return {array} - ...
173-
*/
174-
get _pages() {
175-
return this._flatten(Content.children).filter(item => {
176-
return item.extension === '.md';
177-
});
178-
}
179147
}
180148

181149
export default Hot(module)(Site);

src/utilities/content-utils.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* Walk the given tree of content
33
*
4-
* @param {object} tree - ...
5-
* @param {function} callback - ...
4+
* @param {object} tree - Any node in the content tree
5+
* @param {function} callback - Run on every descendant as well as the given `tree`
66
*/
77
export const WalkContent = (tree, callback) => {
88
callback(tree);
@@ -17,8 +17,8 @@ export const WalkContent = (tree, callback) => {
1717
/**
1818
* Deep flatten the given `tree`s child nodes
1919
*
20-
* @param {object} tree - ...
21-
* @return {array} - ...
20+
* @param {object} tree - Any node in the content tree
21+
* @return {array} - A flattened list of leaf node descendants
2222
*/
2323
export const FlattenContent = tree => {
2424
if ( tree.children ) {
@@ -34,12 +34,36 @@ export const FlattenContent = tree => {
3434
/**
3535
* Find an item within the given `tree`
3636
*
37-
* @param {object} tree - ...
38-
* @param {function} test - ...
39-
* @return {object} - ...
37+
* @param {object} tree - Any node in the content tree
38+
* @param {function} test - A callback to find any leaf node in the given `tree`
39+
* @return {object} - The first leaf node that passes the `test`
4040
*/
4141
export const FindInContent = (tree, test) => {
4242
let list = FlattenContent(tree);
4343

4444
return list.find(test);
4545
};
46+
47+
/**
48+
* Get top-level sections
49+
*
50+
* @param {object} tree - Any node in the content tree
51+
* @return {array} - Immediate children of the given `tree` that are directories
52+
*/
53+
export const ExtractSections = tree => {
54+
return tree.children.filter(item => (
55+
item.type === 'directory'
56+
));
57+
};
58+
59+
/**
60+
* Get all markdown pages
61+
*
62+
* @param {object} tree - Any node in the content tree
63+
* @return {array} - All markdown descendants of the given `tree`
64+
*/
65+
export const ExtractPages = tree => {
66+
return FlattenContent(tree).filter(item => {
67+
return item.extension === '.md';
68+
});
69+
};

0 commit comments

Comments
 (0)