From 5b55589e3d184bbf81fbd653644f4a5959f1c1db Mon Sep 17 00:00:00 2001 From: Roman Gafurov Date: Fri, 26 Aug 2022 15:21:17 +0300 Subject: [PATCH] Form post title from file name if it isn't specified --- next-app/api/index.js | 19 +++++++++++++++---- next-app/components/PostPageNav.jsx | 26 +++++++++++--------------- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/next-app/api/index.js b/next-app/api/index.js index 32ec5d6f3..88f17c0b8 100644 --- a/next-app/api/index.js +++ b/next-app/api/index.js @@ -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, }; }; @@ -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); @@ -50,7 +60,8 @@ export const getPostsList = async (locale) => { header, summary, name, - })); + })) + .reverse(); }; export const findPost = async (name, locale) => { diff --git a/next-app/components/PostPageNav.jsx b/next-app/components/PostPageNav.jsx index 37eec7e30..fccfd1894 100644 --- a/next-app/components/PostPageNav.jsx +++ b/next-app/components/PostPageNav.jsx @@ -14,26 +14,22 @@ const PostPageNav = ({ nextPost, prevPost }) => { const prevHref = makeHref(prevPost.name); const nextHref = makeHref(nextPost.name); - + return (
- {prevPost.header && ( - - - « {prevPost.header} - - - )} + + + « {prevPost.header} + +
- {nextPost.header && ( - - - {nextPost.header} » - - - )} + + + {nextPost.header} » + +
);