Skip to content

Commit

Permalink
refactor(parser): remove unused plugins and convert to sync
Browse files Browse the repository at this point in the history
Signed-off-by: mateonunez <[email protected]>
  • Loading branch information
mateonunez committed Jan 25, 2024
1 parent b5be8a3 commit 6a2011f
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions lib/articles/parser.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
import { promises } from 'fs';
import path from 'path';

import { readdirSync, readFileSync } from 'node:fs';
import path from 'node:path';
import matter from 'gray-matter';
import removeMarkdown from 'remove-markdown';
import readingTime from 'reading-time';

import config from 'lib/config';

import { serialize } from 'next-mdx-remote/serialize';

// remark/rehype markdown plugins
import remarkGfm from 'remark-gfm';
import rehypeSlug from 'rehype-slug';
import rehypePrism from 'rehype-prism-plus';

export async function getArticleSlugs() {
export function getArticleSlugs() {
const fullPath = path.join(process.cwd(), './articles');
const articles = await promises.readdir(fullPath);
const articles = readdirSync(fullPath);
const slugs = articles.filter((file) => /\.mdx?$/.test(file)).map((file) => file.replace(/\.mdx?$/, ''));
return slugs;
}

export async function getAllArticles() {
const slugs = await getArticleSlugs();
const slugs = getArticleSlugs();
const articles = [];
for (const slug of slugs) {
const { frontMatter } = await getArticleData({ slug });
Expand All @@ -37,13 +31,13 @@ export async function getLastArticle() {
}

const articlesCache = new Map();
async function getArticleData({ slug }) {
function getArticleData({ slug }) {
if (articlesCache.has(slug)) {
return articlesCache.get(slug);
}

const fullPath = path.join(process.cwd(), './articles', `${slug}.mdx`);
const raw = await promises.readFile(fullPath, 'utf8');
const raw = readFileSync(fullPath, 'utf8');
const parsedData = parseArticleData({ raw, slug });

articlesCache.set(slug, parsedData);
Expand Down Expand Up @@ -72,7 +66,7 @@ function parseArticleData({ raw, slug = '' }) {
}

export async function getArticle({ slug }) {
const { frontMatter, content } = await getArticleData({ slug });
const { frontMatter, content } = getArticleData({ slug });
const compiledSource = await compileSource({ content });
const article = {
frontMatter,
Expand All @@ -82,12 +76,11 @@ export async function getArticle({ slug }) {
return article;
}

const remarkPlugins = [[remarkGfm]];
const rehypePlugins = [[rehypeSlug], [rehypePrism, { ignoreMissing: true }]];
const serializeOptions = {
parserFormatter: false,
mdxOptions: {
remarkPlugins,
// remarkPlugins,
rehypePlugins,
},
}
Expand Down

0 comments on commit 6a2011f

Please sign in to comment.