Skip to content

Commit

Permalink
add series navigator STILL WRITE TESTS
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanfiller committed Mar 7, 2021
1 parent e939c36 commit d58c989
Show file tree
Hide file tree
Showing 18 changed files with 217 additions and 31 deletions.
34 changes: 34 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"devDependencies": {
"@cypress/snapshot": "^2.1.7",
"@rollup/plugin-commonjs": "^12.0.0",
"@rollup/plugin-dynamic-import-vars": "^1.1.1",
"@rollup/plugin-node-resolve": "^8.0.0",
"@rollup/plugin-replace": "^2.2.0",
"color-contrast-table-svelte": "^3.0.8",
Expand Down
7 changes: 7 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { terser } from 'rollup-plugin-terser'
import config from 'sapper/config/rollup.js'
import pkg from './package.json'
import { mdsvex } from 'mdsvex'
import dynamicImportVars from '@rollup/plugin-dynamic-import-vars'
import svelteSVG from 'rollup-plugin-svelte-svg'
import { globalStyle, scss } from 'svelte-preprocess'
import copy from 'rollup-plugin-copy'
Expand Down Expand Up @@ -43,6 +44,10 @@ const envVars = {
exclude: 'src/routes/**/*.md'
}

const dynamicImportVarsOptions = {
include: `src/routes/**/*.svelte`
}

const preprocess = [
mdsvex({
extension: '.md',
Expand Down Expand Up @@ -104,6 +109,7 @@ export default {
dedupe: ['svelte']
}),
commonjs(),
dynamicImportVars(dynamicImportVarsOptions),
svelteSVG({ dev }),
copy({
targets: [
Expand Down Expand Up @@ -139,6 +145,7 @@ export default {
dedupe: ['svelte']
}),
commonjs(),
dynamicImportVars(dynamicImportVarsOptions),
svelteSVG({ generate: 'ssr', dev }),
copy({
targets: [
Expand Down
86 changes: 86 additions & 0 deletions src/components/blog/series-navigator.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<script>
import { getContext } from 'svelte'
const series = getContext('series')
let postIndex
let previous
let next
$: if (series) {
postIndex = series.posts.findIndex(post => post.title === $$props.title)
if (series.posts[postIndex - 1]) {
previous = series.posts[postIndex - 1]
}
if (series.posts[postIndex + 1]) {
next = series.posts[postIndex + 1]
}
}
</script>

<style global type='text/scss'>
.series-navigator {
&__title {
color: var(--colorWhite);
background: var(--colorHighlight);
text-align: center;
padding: var(--padding);
a {
color: inherit;
&:not(:hover) {
text-decoration: none;
}
}
}
&__buttons {
background: var(--colorHighlight);
display: flex;
}
&__previous,
&__next {
flex-wrap: wrap;
width: 50%;
&:before {
font-size: .8em;
margin-bottom: calc(.5 * var(--padding));
display: block;
}
}
&__previous {
text-align: right;
margin-right: auto;
&:before {
content: '« previous';
}
}
&__next {
text-align: left;
margin-left: auto;
&:before {
content: 'next »';
}
}
}
</style>

{#if series}
<aside class='series-navigator__title'>
This is post {postIndex + 1} of {series.posts.length} in the <a href={series.slug}>{series.title}</a> series.
</aside>

<slot />

<aside class='series-navigator__buttons'>
{#if previous}
<a href={previous.slug} class='series-navigator__previous button'>
{previous.title}
</a>
{/if}
{#if next}
<a href={next.slug} class='series-navigator__next button'>
{next.title}
</a>
{/if}
</aside>

{:else}
<slot />
{/if}
2 changes: 1 addition & 1 deletion src/components/layout/seo.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
imageCredit: banner.attribution ? banner.attribution : '',
url: pageUrl.replace('https://www.', '')
})
local && console.log('imageFunctionUrl', `${host}/.netlify/functions/generate-image?${imageParams}`)
// local && console.log('imageFunctionUrl', `${host}/.netlify/functions/generate-image?${imageParams}`)
socialImageUrl = `https://res.cloudinary.com/${process.env.CLOUDINARY_CLOUD}/image/upload/social-images/${slugify(title)}.png`
}
</script>
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/get-pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ const getPosts = ({

return ({
...frontmatter,
slug: `${directory}/${post}`,
// TODO remove _content from this helper method all together
slug: `/${directory.replace('/_content', '')}/${post}`,
html: html
})
})
Expand Down
9 changes: 6 additions & 3 deletions src/layouts/blog.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<script>
import Page from './page.svelte'
import Markdown from '../components/layout/markdown.svelte'
import SeriesNavigator from '../components/blog/series-navigator.svelte'
</script>

<Page {...$$props} >
<Markdown>
<slot />
</Markdown>
<SeriesNavigator {...$$props}>
<Markdown>
<slot />
</Markdown>
</SeriesNavigator>
</Page>
5 changes: 4 additions & 1 deletion src/layouts/page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
#content {
grid-area: content;
width: 100%;
max-height: 100%;
min-height: 100%;
display: flex;
flex-direction: column;
justify-content: space-between;
}
#site-footer {
Expand Down
32 changes: 32 additions & 0 deletions src/routes/blog/[slug].svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<script context='module'>
export async function preload(page) {
const { slug } = page.params
const component = await import(`./_content/${slug}/index.md`)
const series = await this.fetch(`/blog/series.json`)
.then(response => response.json())
.then(allSeries => allSeries.find(series => {
const postSlugs = series.posts.map(series => series.slug.replace('/blog/', ''))
if (postSlugs.includes(slug)) {
return series
}
}))
return {
page: component.default,
metadata: component.metadata,
series: series
}
}
</script>

<script>
export let page
export let series
import { setContext } from 'svelte'
setContext('series', series)
</script>

<svelte:component this={page} />
2 changes: 1 addition & 1 deletion src/routes/blog/index.json.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export function get(_req, res) {
'Content-Type': 'application/json'
})

res.end(JSON.stringify(getPages({directory: 'blog'})))
res.end(JSON.stringify(getPages({directory: 'blog/_content'})))
}
16 changes: 16 additions & 0 deletions src/routes/blog/series/[slug].json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

import fetch from 'node-fetch'

export async function get(req, res) {
res.writeHead(200, {
'Content-Type': 'application/json'
})

const slug = req.path.match(/(.*)\.json/)[1]

const series = await fetch('http://localhost:3000/blog/series.json')
.then(response => response.json())
.then(series => series.find(series => series.slug === slug))

res.end(JSON.stringify(series))
}
8 changes: 2 additions & 6 deletions src/routes/blog/series/[slug].svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script context='module'>
export function preload({ params, query }) {
return this.fetch(`/blog/series.json`)
return this.fetch(`/blog/series/${params.slug}.json`)
.then(response => response.json())
.then(series => {
return { series }
Expand All @@ -10,10 +10,6 @@

<script>
export let series
import { stores } from '@sapper/app'
const { page } = stores()
series = series.filter(series => series.slug = $page.params.slug)[0]
const preview = {...series, title: ''}
import Page from '../../../layouts/page.svelte'
Expand All @@ -23,4 +19,4 @@

<Page {...series}>
<SeriesPreview {...preview} />
</Page>
</Page>
15 changes: 8 additions & 7 deletions src/routes/blog/series/index.json.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fetch from 'node-fetch'
import series from './_series.js'
import { slugify } from '../../../helpers'
import { compareAsc, compareDesc } from 'date-fns'

export async function get(_req, res) {
res.writeHead(200, {
Expand All @@ -10,17 +11,17 @@ export async function get(_req, res) {
let list = {}
// create a list of keys that can have posts pushed to them
series.map(series => {
series.slug = `blog/series/${slugify(series.title)}`
series.slug = `/blog/series/${slugify(series.title)}`
list[series.title] = series
})

await fetch('http://localhost:3000/blog.json')
.then(response => response.json())
// get only the posts that have series
.then(posts => posts.filter(post => !!post.meta.series && Object.keys(list).includes(post.meta.series)))
.then(posts => posts.filter(post => !!post.series && Object.keys(list).includes(post.series)))
// format the series with the posts
.then(posts => posts.map(post => {
const seriesTitle = post.meta.series
const seriesTitle = post.series
const postTitle = post.title
if(list[seriesTitle].posts) {
// stop duplication that's happening for some reason
Expand All @@ -35,14 +36,14 @@ export async function get(_req, res) {
// get rid of keys, make an array
list = Object.values(list)

if (list.length > 1) {
if (list.length) {
// sort the posts with oldest first
list.forEach(item => item.posts.sort((a, b) => (a.meta.date < b.meta.date) ? -1 : 1))
list.forEach(item => item.posts.sort((a, b) => compareAsc(new Date(a.meta.date), new Date(b.meta.date))))

// sort the series with most recent last post first
const getLast = array => array[array.length - 1]
list.sort((a, b) => (getLast(a.posts).meta.date < getLast(b.posts).meta.date) ? 1 : -1)
list.sort((a, b) => compareDesc(new Date(getLast(a.posts).meta.date), new Date(getLast(b.posts).meta.date)))
}

res.end(JSON.stringify(list))
}
}
4 changes: 0 additions & 4 deletions src/routes/blog/series/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@

<script>
export let series
import { setContext } from 'svelte'
import Page from '../../../layouts/page.svelte'
import List from '../../../components/content/list.svelte'
import SeriesPreview from '../../../components/blog/series-preview.svelte'
setContext('segment', 'series')
</script>

<Page>
Expand Down
Loading

0 comments on commit d58c989

Please sign in to comment.