Skip to content

Commit 90378c0

Browse files
committed
Add preliminary support for section listing
1 parent 8d8dac3 commit 90378c0

15 files changed

+152
-27
lines changed

.babelrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
]
1111
],
1212
"plugins": [
13-
"@babel/plugin-proposal-optional-chaining"
13+
"@babel/plugin-proposal-optional-chaining",
1414
]
1515
}

data/osa-1/index.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ path: "/osa-1"
33
title: "Osa 1"
44
---
55

6-
Tässä osassa käyt läpi
6+
Tässä osassa opit kirjoittamaan ohjelmia, jotka lukevat käyttäjältä syötettä ja tekevät laskentaa syötteen perusteella. Opit käsitteet muuttuja, ehtolause ja toistolause, ja opit käyttämään näitä ohjelmissasi.
77

8-
* Tulostus
9-
* Muuttujat
10-
* Laskentaa
8+
<pages-in-this-section></pages-in-this-section>
9+
10+
Osa on tarkoitus suorittaa viikon aikana.

data/osa-2/1-ratkaisumalleja.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
---
22
path: "/osa-2/1-ratkaisumalleja"
3-
title: "Osa 2"
3+
title: "Ratkaisumalleja"
44
---
55

6-
76
<% partial 'partials/hint', locals: { name: 'Ensimmäisen osan tavoitteet' } do %>
87

98
<p>

data/osa-2/2-jotakin-muuta.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
path: "/osa-2/2-muuta"
3+
title: "Jotakin muuta"
4+
---
5+
6+
Lolled

data/osa-2/index.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ path: "/osa-2"
33
title: "Osa 2"
44
---
55

6-
Osa 2 on paras
6+
Tässä osassa opit paljon.
7+
8+
<pages-in-this-section>

package-lock.json

+28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"react-helmet": "^5.2.0",
1818
"react-motion": "^0.5.2",
1919
"react-scrollspy": "^3.3.5",
20+
"rehype-react": "^3.1.0",
2021
"styled-components": "^4.1.1",
2122
"typeface-open-sans-condensed": "0.0.54"
2223
},
@@ -33,6 +34,7 @@
3334
"devDependencies": {
3435
"@babel/plugin-proposal-optional-chaining": "^7.0.0",
3536
"babel-preset-gatsby": "^0.1.4",
37+
"import-all.macro": "^2.0.3",
3638
"prettier": "^1.15.2"
3739
}
3840
}

src/components/Sidebar.js

-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ class Sidebar extends React.Component {
4949
const edges =
5050
this.props.data?.allMarkdownRemark?.edges.map(o => o.node?.frontmatter) || []
5151
const content = content2.concat(edges)
52-
console.log(JSON.stringify(edges))
5352
return (
5453
<SidebarContainer>
5554
<TopContainer>

src/contexes/PagesContext.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import React from "react";
2+
3+
export default React.createContext()

src/partials/PagesInThisSection.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React from 'react'
2+
import PagesContext from '../contexes/PagesContext'
3+
import { nthIndex } from '../util/strings'
4+
import { Link } from 'gatsby'
5+
6+
export default () => (
7+
<PagesContext.Consumer>
8+
{value => {
9+
const currentPath = value.current.path
10+
let sectionPath = currentPath
11+
const sectionSeparator = nthIndex(currentPath, '/', 2)
12+
if (sectionSeparator !== -1) {
13+
sectionPath = currentPath.substr(0, sectionSeparator)
14+
}
15+
16+
const sectionPages = value.all.filter(o =>
17+
o.path.startsWith(`${sectionPath}/`)
18+
)
19+
20+
return (
21+
<div>
22+
<b>Tässä osassa:</b>
23+
{sectionPages.map((page, i) => (
24+
<div key={page.path}>
25+
{i + 1}. <Link to={page.path}>{page.title}</Link>
26+
</div>
27+
))}
28+
</div>
29+
)
30+
}}
31+
</PagesContext.Consumer>
32+
)

src/partials/Test.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import React from 'react'
2+
import PagesContext from '../contexes/PagesContext'
3+
4+
export default () => (
5+
<PagesContext.Consumer>{value => <div>{JSON.stringify(value)}</div>}</PagesContext.Consumer>
6+
)

src/partials/Test2.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import React from 'react'
2+
3+
export default () => <div>This is test 2.</div>

src/partials/index.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import importAll from 'import-all.macro'
2+
const partials = importAll.sync('./*.js')
3+
4+
const namedPartials = {}
5+
6+
Object.entries(partials)
7+
.filter(([k, _]) => k !== './index.js')
8+
.forEach(([k, v]) => {
9+
const newKey = toKebabCase(k.replace('./', '').replace('.js', ''))
10+
namedPartials[newKey] = v.default
11+
})
12+
13+
function toKebabCase(string) {
14+
return string.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase()
15+
}
16+
17+
export default namedPartials
+38-18
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import React, { Fragment } from 'react'
22
import { graphql } from 'gatsby'
33
import styled from 'styled-components'
4+
import rehypeReact from 'rehype-react'
45

56
import Layout from '../components/layout'
67
import Sidebar from '../components/Sidebar'
78
import ContentArea from '../components/ContentArea'
9+
import Partials from '../partials'
10+
import PagesContext from '../contexes/PagesContext'
811

912
const ContentWrapper = styled.div`
1013
margin-top: 1rem;
@@ -13,34 +16,51 @@ const ContentWrapper = styled.div`
1316
export default function CourseContentTemplate({
1417
data, // this prop will be injected by the GraphQL query below.
1518
}) {
16-
const { markdownRemark } = data // data.markdownRemark holds our post data
17-
const { frontmatter, html } = markdownRemark
19+
const { frontmatter, htmlAst } = data.page
20+
const allPages = data.allPages.edges.map(o => o.node?.frontmatter)
21+
const renderAst = new rehypeReact({
22+
createElement: React.createElement,
23+
components: Partials,
24+
}).Compiler
1825
return (
19-
<Fragment>
20-
<Sidebar />
21-
<ContentArea>
22-
<Layout>
23-
<ContentWrapper>
24-
<h1>{frontmatter.title}</h1>
25-
<div
26-
className="blog-post-content"
27-
dangerouslySetInnerHTML={{ __html: html }}
28-
/>
29-
</ContentWrapper>
30-
</Layout>
31-
</ContentArea>
32-
</Fragment>
26+
<PagesContext.Provider value={{
27+
all: allPages,
28+
current: frontmatter
29+
}}>
30+
<Fragment>
31+
<Sidebar />
32+
<ContentArea>
33+
<Layout>
34+
<ContentWrapper>
35+
<h1>{frontmatter.title}</h1>
36+
{renderAst(htmlAst)}
37+
</ContentWrapper>
38+
</Layout>
39+
</ContentArea>
40+
</Fragment>
41+
</PagesContext.Provider>
3342
)
3443
}
3544

3645
export const pageQuery = graphql`
3746
query($path: String!) {
38-
markdownRemark(frontmatter: { path: { eq: $path } }) {
39-
html
47+
page: markdownRemark(frontmatter: { path: { eq: $path } }) {
48+
htmlAst
4049
frontmatter {
4150
path
4251
title
4352
}
4453
}
54+
allPages: allMarkdownRemark {
55+
edges {
56+
node {
57+
id
58+
frontmatter {
59+
path
60+
title
61+
}
62+
}
63+
}
64+
}
4565
}
4666
`

src/util/strings.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function nthIndex(str, pat, n){
2+
var L= str.length, i= -1;
3+
while(n-- && i++<L){
4+
i= str.indexOf(pat, i);
5+
if (i < 0) break;
6+
}
7+
return i;
8+
}

0 commit comments

Comments
 (0)