Skip to content

Commit

Permalink
Merge pull request #429 from seth2810/next-post-nav
Browse files Browse the repository at this point in the history
Form post title from file name if it isn't specified
  • Loading branch information
mikhaylov-ya committed Sep 1, 2022
2 parents 863066e + ab06ee9 commit 2edb16e
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 64 deletions.
21 changes: 16 additions & 5 deletions next-app/api/index.js
Expand Up @@ -5,22 +5,32 @@ import matter from 'gray-matter';
import remarkGfm from 'remark-gfm';
import rehypePrism from '@mapbox/rehype-prism';
import { serialize } from 'next-mdx-remote/serialize';
import findLastIndex from 'lodash.findlastindex';
import capitalize from 'lodash.capitalize';

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

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); // remove DD_MM_YYYY prefix from post file name

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,12 +60,13 @@ export const getPostsList = async (locale) => {
header,
summary,
name,
}));
}))
.reverse();
};

export const findPost = async (name, locale) => {
const posts = await getPublishedPosts(locale);
const postIndex = posts.findIndex((post) => post.name === name);
const postIndex = findLastIndex(posts, (post) => post.name === name);

if (postIndex === -1) {
return null;
Expand Down
6 changes: 3 additions & 3 deletions next-app/components/Footer.jsx
Expand Up @@ -37,9 +37,9 @@ const Footer = () => {
</Link>
</li>
<li>
<Link href={t('footer.PrivacyPolicy.url')}>
<a alt={t('footer.PrivacyPolicy.title')} target="_blank">
{t('footer.PrivacyPolicy.title')}
<Link href={t('footer.policy.url')}>
<a alt={t('footer.policy.title')} target="_blank">
{t('footer.policy.title')}
</a>
</Link>
</li>
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
22 changes: 22 additions & 0 deletions next-app/package-lock.json

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

2 changes: 2 additions & 0 deletions next-app/package.json
Expand Up @@ -22,6 +22,8 @@
"disqus-react": "^1.1.3",
"file-loader": "^6.2.0",
"gray-matter": "^4.0.3",
"lodash.capitalize": "^4.2.1",
"lodash.findlastindex": "^4.6.0",
"next": "^12.2.5",
"next-i18next": "^11.0.0",
"next-mdx-remote": "^4.1.0",
Expand Down
2 changes: 1 addition & 1 deletion next-app/public/locales/en/common.json
Expand Up @@ -25,7 +25,7 @@
"title": "Code Battle",
"url": "https://codebattle.hexlet.io/"
},
"PrivacyPolicy": {
"policy": {
"title": "Privacy Policy",
"url": "https://hexlet.io/pages/privacy"
}
Expand Down
4 changes: 2 additions & 2 deletions next-app/public/locales/ru/common.json
Expand Up @@ -28,9 +28,9 @@
"title": "Оценка резюме",
"url": "https://cv.hexlet.io/"
},
"PrivacyPolicy": {
"policy": {
"title": "Политика конфиденциальности.",
"url": "ru.https://hexlet.io/pages/privacy"
"url": "https://ru.hexlet.io/pages/privacy"
}
},
"disqus": {
Expand Down
38 changes: 0 additions & 38 deletions pages/privacy-policy.md

This file was deleted.

0 comments on commit 2edb16e

Please sign in to comment.