Skip to content

Commit 0ea3861

Browse files
committed
Create reactive menu
- Currently uses github API which has tight rate limit - All in one file
1 parent ca02050 commit 0ea3861

3 files changed

Lines changed: 71 additions & 13 deletions

File tree

‎components/Header.js‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function HeaderLink(props) {
55
const { link, title } = props;
66
return (
77
<li>
8-
<Link href={link}>
8+
<Link href={`/${link}`}>
99
<a>{title}</a>
1010
</Link>
1111
</li>
@@ -17,18 +17,30 @@ HeaderLink.propTypes = {
1717
link: PropTypes.string.isRequired,
1818
};
1919

20-
function Header() {
20+
function Header(props) {
21+
const { paths } = props;
22+
let pathElement = null;
23+
if (paths && Array.isArray(paths)) {
24+
pathElement = paths.map(path => <HeaderLink title={path} link={path} />);
25+
} else {
26+
pathElement = String(paths);
27+
}
28+
2129
return (
2230
<div>
2331
<h1>Title</h1>
2432
<nav>
2533
<ul>
26-
<HeaderLink title="home" link="/" />
34+
{pathElement}
2735
<HeaderLink title="about" link="/about.md" />
2836
</ul>
2937
</nav>
3038
</div>
3139
);
3240
}
3341

42+
Header.propTypes = {
43+
paths: PropTypes.arrayOf(PropTypes.string).isRequired,
44+
};
45+
3446
export default Header;

‎components/Layout.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Footer from './Footer';
55
import SiteStyle from './Style';
66

77
function Layout(props) {
8-
const { children } = props;
8+
const { children, paths } = props;
99
return (
1010
<div className="page">
1111
<div className="row page-inner">
@@ -15,7 +15,7 @@ function Layout(props) {
1515
</Head>
1616
<header className="col-sm-12 col-lg-2 outer">
1717
<div className="inner">
18-
<Header />
18+
<Header paths={paths} />
1919
</div>
2020
</header>
2121
<main className="col-sm-12 col-lg-10 outer">
@@ -36,6 +36,7 @@ function Layout(props) {
3636

3737
Layout.propTypes = {
3838
children: PropTypes.node.isRequired,
39+
paths: PropTypes.arrayOf(PropTypes.string).isRequired,
3940
};
4041

4142
export default Layout;

‎pages/index.js‎

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import Layout from '../components/Layout';
55
import Content from '../components/Content';
66

77
function Index(props) {
8-
const { content } = props;
8+
const { content, paths } = props;
99
return (
1010
<div>
11-
<Layout>
11+
<Layout paths={paths}>
1212
<Content content={content} />
1313
</Layout>
1414
</div>
@@ -17,23 +17,68 @@ function Index(props) {
1717

1818
Index.propTypes = {
1919
content: PropTypes.string.isRequired,
20+
paths: PropTypes.arrayOf(PropTypes.string).isRequired,
2021
};
2122

23+
async function getPage(path) {
24+
const uri = `https://api.github.com/repos/codebru/markdown-test-repo/contents/${path}`;
25+
const res = await Fetch(uri);
26+
const data = await res.json();
27+
if (!Object.prototype.hasOwnProperty.call(data, 'content')) {
28+
if (Object.prototype.hasOwnProperty.call(data, 'message')) {
29+
// Error possibly
30+
return data.message;
31+
}
32+
return false;
33+
}
34+
const contentMd = Buffer.from(data.content, data.encoding).toString();
35+
const contentHTML = Marked(contentMd);
36+
return contentHTML;
37+
}
38+
39+
async function getDirectory(path) {
40+
try {
41+
const uri = `https://api.github.com/repos/codebru/markdown-test-repo/contents/${path}`;
42+
const res = await Fetch(uri);
43+
const data = await res.json();
44+
if (!data || !Array.isArray(data) || !Object.prototype.hasOwnProperty.call(data[0], 'path')) {
45+
throw new Error('Failed to fetch tree.');
46+
}
47+
const pathArray = data.map(entry => entry.path);
48+
return pathArray;
49+
} catch (e) {
50+
return `Unable to get data: ${e}`;
51+
}
52+
}
53+
2254
Index.getInitialProps = async ({ query }) => {
55+
let pathArray = [];
56+
let contentHTML = null;
2357
let uri = null;
58+
let uri2 = null;
59+
2460
if (query.pathName && query.pathName !== '/') {
2561
uri = query.pathName;
2662
} else {
2763
uri = 'README.md';
2864
}
2965

30-
uri = `https://raw.githubusercontent.com/codebru/full-web-dev/master/${uri}`;
31-
const res = await Fetch(uri);
32-
const data = await res.text();
33-
const contentMd = data;
34-
const contentHTML = Marked(contentMd);
66+
contentHTML = await getPage(uri);
67+
68+
if (!contentHTML) {
69+
// This means that the content is a DIR
70+
contentHTML = 'This is a directory';
71+
// Now get directory info from the same URI
72+
pathArray = await getDirectory(uri);
73+
} else {
74+
// Remove the file from the path so we can get the DIR
75+
uri2 = uri.lastIndexOf('/') + 1;
76+
uri2 = uri.substring(0, uri2);
77+
78+
pathArray = await getDirectory(uri2);
79+
}
3580

36-
return { content: contentHTML };
81+
return { content: contentHTML, paths: pathArray };
3782
};
3883

3984
export default Index;

0 commit comments

Comments
 (0)