Skip to content

Commit

Permalink
perf: cache frontmatter data
Browse files Browse the repository at this point in the history
  • Loading branch information
techfg authored and vernak2539 committed May 3, 2024
1 parent 92c62e2 commit b76f496
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
7 changes: 2 additions & 5 deletions src/index.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { visit } from "unist-util-visit";
import * as path from "path";
import * as fs from "fs";
import { default as matter } from "gray-matter";
import { default as debugFn } from "debug";
import {
replaceExt,
Expand All @@ -15,6 +13,7 @@ import {
URL_PATH_SEPARATOR,
FILE_PATH_SEPARATOR,
shouldProcessFile,
getMatter,
} from "./utils.mjs";
import { validateOptions } from "./options.mjs";

Expand Down Expand Up @@ -63,9 +62,7 @@ function astroRehypeRelativeMarkdownLinks(opts = {}) {
}

// read gray matter from href file
const urlFileContent = fs.readFileSync(urlFilePath);
const { data: frontmatter } = matter(urlFileContent);
const frontmatterSlug = frontmatter.slug;
const { slug: frontmatterSlug } = getMatter(urlFilePath);
const contentDir = path.resolve(options.contentPath);
const collectionPathMode = options.collectionPathMode;
const trailingSlashMode = options.trailingSlash;
Expand Down
2 changes: 2 additions & 0 deletions src/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ export type NormaliseAstroOutputPath = (
export type Slash = (path: string, sep: string) => string;
export type NormalizePath = (path: string) => string;
export type ShouldProcessFile = (path: string) => boolean;
export type MatterData = { slug?: string };
export type GetMatter = (path: string) => MatterData;
17 changes: 16 additions & 1 deletion src/utils.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import path from "path";
import { statSync } from "fs";
import { readFileSync, statSync } from "fs";
import { slug as githubSlug } from "github-slugger";
import { z } from "zod";
import { asError } from "catch-unknown";
import isAbsoluteUrl from "is-absolute-url";
import matter from "gray-matter";

const validMarkdownExtensions = [".md", ".mdx"];
const isWindows =
Expand Down Expand Up @@ -167,3 +168,17 @@ export function shouldProcessFile(npath) {
// see https://github.com/withastro/astro/blob/0fec72b35cccf80b66a85664877ca9dcc94114aa/packages/astro/src/content/utils.ts#L253
return !npath.split(path.sep).some((p) => p && p.startsWith("_"));
}

/** @type {Record<string, import('./utils.d.ts').MatterData>} */
const matterCache = {};
/** @type {import('./utils.d.ts').GetMatter} */
export function getMatter(npath) {
const readMatter = () => {
const content = readFileSync(npath);
const { data: frontmatter } = matter(content);
matterCache[npath] = frontmatter;
return frontmatter;
};

return matterCache[npath] || readMatter();
}

0 comments on commit b76f496

Please sign in to comment.