diff --git a/src/utils/api-faqs.js b/src/utils/api-faqs.js
index a73cd9cffc..29dc7e451e 100644
--- a/src/utils/api-faqs.js
+++ b/src/utils/api-faqs.js
@@ -1,13 +1,44 @@
+const fs = require('fs');
+
+const matter = require('gray-matter');
+
const { FAQS_DIR_PATH } = require('../constants/content');
const { getPostSlugs, getPostBySlug } = require('./api-content');
+const isProduction = process.env.NEXT_PUBLIC_VERCEL_ENV === 'production';
+
+const getFaqFrontmatter = (slug) => {
+ try {
+ const source = fs.readFileSync(`${process.cwd()}/${FAQS_DIR_PATH}/${slug}.md`, 'utf-8');
+ const { data } = matter(source);
+ return data;
+ } catch (_e) {
+ return null;
+ }
+};
+
+const getAllFaqSlugs = async () => {
+ const slugs = await getPostSlugs(FAQS_DIR_PATH);
+ if (!isProduction) {
+ return slugs.map((slug) => slug.slice(1));
+ }
+
+ return slugs
+ .map((slug) => {
+ const data = getFaqFrontmatter(slug);
+ if (!data || data.isDraft) return;
+ return slug.slice(1);
+ })
+ .filter(Boolean);
+};
+
const getAllFaqs = async () => {
const slugs = await getPostSlugs(FAQS_DIR_PATH);
return slugs
.map((slug) => {
- if (!getPostBySlug(slug, FAQS_DIR_PATH)) return;
const data = getPostBySlug(slug, FAQS_DIR_PATH);
+ if (!data) return;
const slugWithoutFirstSlash = slug.slice(1);
const {
@@ -28,20 +59,8 @@ const getAllFaqs = async () => {
redirectFrom,
};
})
- .filter((item) => process.env.NEXT_PUBLIC_VERCEL_ENV !== 'production' || !item.isDraft)
+ .filter((item) => !isProduction || !item.isDraft)
.sort((a, b) => (new Date(a.createdAt).getTime() < new Date(b.createdAt).getTime() ? 1 : -1));
};
-const getNavigationLinks = (slug, posts) => {
- const currentItemIndex = posts.findIndex((item) => item.slug === slug);
-
- const previousItem = posts[currentItemIndex - 1];
- const nextItem = posts[currentItemIndex + 1];
-
- return {
- previousLink: { title: previousItem?.title, slug: previousItem?.slug },
- nextLink: { title: nextItem?.title, slug: nextItem?.slug },
- };
-};
-
-export { getAllFaqs, getNavigationLinks };
+export { getAllFaqSlugs, getAllFaqs };
diff --git a/src/utils/api-guides.js b/src/utils/api-guides.js
index ad812c64ed..c32d9cf898 100644
--- a/src/utils/api-guides.js
+++ b/src/utils/api-guides.js
@@ -1,5 +1,7 @@
import fs from 'fs';
+import matter from 'gray-matter';
+
const { GUIDES_DIR_PATH } = require('../constants/content');
const { getPostSlugs, getPostBySlug } = require('./api-content');
@@ -30,6 +32,36 @@ const getAuthor = (id) => {
}
};
+const getGuideFrontmatter = (slug) => {
+ try {
+ const source = fs.readFileSync(`${process.cwd()}/${GUIDES_DIR_PATH}/${slug}.md`, 'utf-8');
+ const { data } = matter(source);
+ return data;
+ } catch (_e) {
+ return null;
+ }
+};
+
+const getGuideNavigationItems = async () => {
+ const slugs = await getPostSlugs(GUIDES_DIR_PATH);
+ return slugs
+ .map((slug) => {
+ const data = getGuideFrontmatter(slug);
+ if (!data) return;
+
+ const { title, createdAt, isDraft } = data;
+
+ return {
+ title,
+ slug: slug.slice(1),
+ createdAt,
+ isDraft,
+ };
+ })
+ .filter((item) => process.env.NEXT_PUBLIC_VERCEL_ENV !== 'production' || !item.isDraft)
+ .sort((a, b) => (new Date(a.createdAt).getTime() < new Date(b.createdAt).getTime() ? 1 : -1));
+};
+
const getAllGuides = async () => {
const slugs = await getPostSlugs(GUIDES_DIR_PATH);
return slugs
@@ -84,4 +116,4 @@ const getNavigationLinks = (slug, posts) => {
};
};
-export { getAllGuides, getAuthor, getAuthors, getNavigationLinks };
+export { getAllGuides, getAuthor, getAuthors, getGuideNavigationItems, getNavigationLinks };