From b76f496cfbb924d1417ff8c5d7c2712fb64a20d4 Mon Sep 17 00:00:00 2001 From: techfg Date: Mon, 22 Apr 2024 18:46:36 -0700 Subject: [PATCH 1/3] perf: cache frontmatter data --- src/index.mjs | 7 ++----- src/utils.d.ts | 2 ++ src/utils.mjs | 17 ++++++++++++++++- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/index.mjs b/src/index.mjs index e102014..5b594ca 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -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, @@ -15,6 +13,7 @@ import { URL_PATH_SEPARATOR, FILE_PATH_SEPARATOR, shouldProcessFile, + getMatter, } from "./utils.mjs"; import { validateOptions } from "./options.mjs"; @@ -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; diff --git a/src/utils.d.ts b/src/utils.d.ts index d47d051..f9b2d47 100644 --- a/src/utils.d.ts +++ b/src/utils.d.ts @@ -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; diff --git a/src/utils.mjs b/src/utils.mjs index 6781469..7a82743 100644 --- a/src/utils.mjs +++ b/src/utils.mjs @@ -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 = @@ -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} */ +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(); +} From 1bb5a4174956b261a0825454791a1fadec572c5d Mon Sep 17 00:00:00 2001 From: techfg Date: Tue, 23 Apr 2024 21:57:19 -0700 Subject: [PATCH 2/3] chore: disable cache during test --- package.json | 2 +- src/utils.mjs | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 92e40a9..522757f 100644 --- a/package.json +++ b/package.json @@ -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": "MATTER_CACHE_DISABLE=true node --loader=esmock --test", "type-check": "tsc --noEmit --emitDeclarationOnly false" }, "dependencies": { diff --git a/src/utils.mjs b/src/utils.mjs index 7a82743..704f4d1 100644 --- a/src/utils.mjs +++ b/src/utils.mjs @@ -171,12 +171,15 @@ export function shouldProcessFile(npath) { /** @type {Record} */ const matterCache = {}; +const matterCacheEnabled = process.env.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); - matterCache[npath] = frontmatter; + if (matterCacheEnabled) { + matterCache[npath] = frontmatter; + } return frontmatter; }; From 7abdc5f3a24a78d43f66a0d45c56713c5840aac5 Mon Sep 17 00:00:00 2001 From: techfg Date: Thu, 25 Apr 2024 01:29:20 -0700 Subject: [PATCH 3/3] chore: namespace env variable --- package.json | 2 +- src/utils.mjs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 522757f..2f5e6db 100644 --- a/package.json +++ b/package.json @@ -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": "MATTER_CACHE_DISABLE=true node --loader=esmock --test", + "test": "ARRML_MATTER_CACHE_DISABLE=true node --loader=esmock --test", "type-check": "tsc --noEmit --emitDeclarationOnly false" }, "dependencies": { diff --git a/src/utils.mjs b/src/utils.mjs index 704f4d1..17aa21e 100644 --- a/src/utils.mjs +++ b/src/utils.mjs @@ -171,7 +171,7 @@ export function shouldProcessFile(npath) { /** @type {Record} */ const matterCache = {}; -const matterCacheEnabled = process.env.MATTER_CACHE_DISABLE !== "true"; +const matterCacheEnabled = process.env.ARRML_MATTER_CACHE_DISABLE !== "true"; /** @type {import('./utils.d.ts').GetMatter} */ export function getMatter(npath) { const readMatter = () => {