Skip to content

Commit

Permalink
Form post title from file name if it isn't specified
Browse files Browse the repository at this point in the history
  • Loading branch information
seth2810 committed Aug 26, 2022
1 parent 7f7956e commit 5b55589
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
19 changes: 15 additions & 4 deletions next-app/api/index.js
Expand Up @@ -8,19 +8,29 @@ import { serialize } from 'next-mdx-remote/serialize';

import config from '../data/config.js';

const capitalize = (str) => `${str.charAt(0).toUpperCase()}${str.slice(1)}`;

const formatNameToHeader = (name) => {
return name
.split('-')
.map(capitalize)
.join(' ');
};

const readPost = async (filePath, basePath) => {
const fileContent = await fsp.readFile(path.join(basePath, filePath), 'utf-8');
const { data, content } = matter(fileContent);
const { name } = path.parse(filePath);
const { title = null, header = title, description = null, summary = description, ...props } = data;
const sourceUrl = `${config.repositoryUrl}/tree/main/${filePath}`;
const shortName = name.slice(11);

return {
header,
summary,
content,
sourceUrl,
name: name.slice(11),
name: shortName,
header: header || formatNameToHeader(shortName),
...props,
};
};
Expand All @@ -35,7 +45,7 @@ export const getPublishedPosts = async (locale) => {
.map(({ name }) => name);

const promises = fileNames
.sort((a, b) => b.localeCompare(a))
.sort((a, b) => a.localeCompare(b))
.map(async (name) => readPost(path.join(postsPath, name), dir));

return await Promise.all(promises);
Expand All @@ -50,7 +60,8 @@ export const getPostsList = async (locale) => {
header,
summary,
name,
}));
}))
.reverse();
};

export const findPost = async (name, locale) => {
Expand Down
26 changes: 11 additions & 15 deletions next-app/components/PostPageNav.jsx
Expand Up @@ -14,26 +14,22 @@ const PostPageNav = ({ nextPost, prevPost }) => {

const prevHref = makeHref(prevPost.name);
const nextHref = makeHref(nextPost.name);

return (
<div className="row PageNavigation mt-4 prevnextlinks d-flex justify-content-between">
<div className="col-md-6 rightborder pl-0">
{prevPost.header && (
<Link href={prevHref}>
<a alt={t('page.prev_guide')} className="thepostlink">
« {prevPost.header}
</a>
</Link>
)}
<Link href={prevHref}>
<a alt={t('page.prev_guide')} className="thepostlink">
« {prevPost.header}
</a>
</Link>
</div>
<div className="col-md-6 text-end pr-0">
{nextPost.header && (
<Link href={nextHref}>
<a alt={t('page.next_guide')} className="thepostlink">
{nextPost.header} »
</a>
</Link>
)}
<Link href={nextHref}>
<a alt={t('page.next_guide')} className="thepostlink">
{nextPost.header} »
</a>
</Link>
</div>
</div>
);
Expand Down

0 comments on commit 5b55589

Please sign in to comment.