Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions src/lib/getContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import path from 'path';
import fs from 'fs';
import matter from 'gray-matter';

const createSlugFromMd = (filename) => filename.replace('.md', '');

const formattedDate = (date) => {
// get day in format: Month day, Year. e.g. April 19, 2020
const options = { year: 'numeric', month: 'long', day: 'numeric' };
Expand All @@ -24,7 +26,7 @@ export const fileContent = (directory, file) => {

// use the filename as the slug and return the content
return {
slug: file.replace('.md', ''),
slug: createSlugFromMd(file),
frontmatter,
content,
};
Expand All @@ -49,24 +51,17 @@ export const directoryContent = (directory, fullPath = '') => {
);
return fileContents;
} catch (error) {
return null;
return [];
}
};

export const slugs = (directory) => {
export const directorySlugs = (directory) => {
const filesPath = path.resolve(`./public/content/${directory}`);

try {
const files = fs.readdirSync(filesPath);

const paths = files.map((file) => ({
params: {
slug: file.replace('.md', ''),
},
}));

return paths;
return files.map((file) => createSlugFromMd(file));
} catch (error) {
return null;
return [];
}
};
9 changes: 7 additions & 2 deletions src/pages/posts/[slug].jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import PropTypes from 'prop-types';

import { fileContent, slugs } from 'src/lib/getContent';
import { fileContent, directorySlugs } from 'src/lib/getContent';

import SiteLayout from 'src/components/layouts/SiteLayout';
import PostLayout from 'src/components/layouts/PostLayout';
Expand All @@ -17,7 +17,12 @@ const Slug = ({ post }) => (
);

export async function getStaticPaths() {
const paths = slugs('posts');
const postSlugs = directorySlugs('posts');
const paths = postSlugs.map((slug) => ({
params: {
slug,
},
}));

return {
paths,
Expand Down