Skip to content

Commit

Permalink
perf(parser): add compiled source cache
Browse files Browse the repository at this point in the history
Signed-off-by: mateonunez <mateonunez95@gmail.com>
  • Loading branch information
mateonunez committed Jan 25, 2024
1 parent 318f6ae commit 446d5a3
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions lib/articles/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import removeMarkdown from 'remove-markdown';
import readingTime from 'reading-time';
import config from 'lib/config';
import { serialize } from 'next-mdx-remote/serialize';
// import remarkGfm from 'remark-gfm';
import rehypeSlug from 'rehype-slug';
import rehypePrism from 'rehype-prism-plus';


export function getArticleSlugs() {
const fullPath = path.join(process.cwd(), './articles');
Expand Down Expand Up @@ -65,21 +69,33 @@ function parseArticleData({ raw, slug = '' }) {

export async function getArticle({ slug }) {
const { frontMatter, content } = getArticleData({ slug });
const compiledSource = await compileSource({ content });
const compiledSource = await compileSource({ content, slug });
const article = {
frontMatter,
source: compiledSource,
};

return article;
}

// const remarkPlugins = [[remarkGfm]];
const rehypePlugins = [[rehypeSlug], [rehypePrism, { ignoreMissing: true }]];
const serializeOptions = {
parserFormatter: false,
mdxOptions: {
// remarkPlugins, is not compatible with the current mdx-js version
rehypePlugins,
},
}

async function compileSource({ content }) {
const { compiledSource } = await serialize(content, serializeOptions);
const compiledSourcesCached = new Map()
async function compileSource({ content, slug }) {
const isCompiledSourceCached = compiledSourcesCached.has(slug);
if (isCompiledSourceCached) {
console.log('hit!');
return compiledSourcesCached.get(slug);
}

const { compiledSource } = await serialize(content, serializeOptions);
compiledSourcesCached.set(slug, compiledSource);
return compiledSource;
}

0 comments on commit 446d5a3

Please sign in to comment.