Skip to content

Commit

Permalink
Merge pull request #51 from techfg/chore/cache-matter
Browse files Browse the repository at this point in the history
perf: cache frontmatter
  • Loading branch information
vernak2539 authored May 3, 2024
2 parents 646a743 + 7abdc5f commit 5d79921
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"pre-release": "yarn run changelog && yarn run prettier && yarn run generate-docs",
"generate-docs": "typedoc --readme none --gitRevision main --plugin typedoc-plugin-markdown src",
"prettier": "prettier ./src/** -w",
"test": "node --loader=esmock --test",
"test": "ARRML_MATTER_CACHE_DISABLE=true node --loader=esmock --test",
"type-check": "tsc --noEmit --emitDeclarationOnly false"
},
"dependencies": {
Expand Down
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;
20 changes: 19 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,20 @@ 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 = {};
const matterCacheEnabled = process.env.ARRML_MATTER_CACHE_DISABLE !== "true";
/** @type {import('./utils.d.ts').GetMatter} */
export function getMatter(npath) {
const readMatter = () => {
const content = readFileSync(npath);
const { data: frontmatter } = matter(content);
if (matterCacheEnabled) {
matterCache[npath] = frontmatter;
}
return frontmatter;
};

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

0 comments on commit 5d79921

Please sign in to comment.