From 8e749b364ff8a3284c5d1dc0b398a4f598710df3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Rivi=C3=A8re?= Date: Mon, 27 Nov 2023 12:23:55 +0100 Subject: [PATCH 1/3] closes #215 --- src/config.ts | 2 +- src/markdown.ts | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/config.ts b/src/config.ts index e8d7777c7..24672ac6f 100644 --- a/src/config.ts +++ b/src/config.ts @@ -46,8 +46,8 @@ async function readPages(root: string): Promise { const pages: Page[] = []; for await (const file of visitFiles(root)) { if (file === "404.md" || extname(file) !== ".md") continue; - const parsed = await parseMarkdown(await readFile(join(root, file), "utf-8"), root, file); const name = basename(file, ".md"); + const parsed = await parseMarkdown(await readFile(join(root, file), "utf-8"), root, `/${name}`); const page = {path: join("/", dirname(file), name), name: parsed.title ?? "Untitled"}; if (name === "index") pages.unshift(page); else pages.push(page); diff --git a/src/markdown.ts b/src/markdown.ts index c582277b8..43391806c 100644 --- a/src/markdown.ts +++ b/src/markdown.ts @@ -381,7 +381,19 @@ function toParseCells(pieces: RenderPiece[]): CellPiece[] { } export async function parseMarkdown(source: string, root: string, sourcePath: string): Promise { - const parts = matter(source); + let parts; + try { + parts = matter(source); + } catch (error) { + // If the front-matter is invalid, flush gray-matter’s (private) cache, + // ensuring an error is also thrown the next time we evaluate it. + // https://github.com/jonschlinkert/gray-matter/issues/166 + // https://github.com/jonschlinkert/gray-matter/blob/ce67a86dba419381db0dd01cc84e2d30a1d1e6a5/index.js#L225 + // eslint-disable-next-line + // @ts-ignore + matter.clearCache!(); + throw new Error(`invalid front matter in ${sourcePath}`); + } // TODO: We need to know what line in the source the markdown starts on and pass that // as startLine in the parse context below. const md = MarkdownIt({html: true}); From 58bbba1aabc4374a256b99e55079d4b789941f35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Rivi=C3=A8re?= Date: Thu, 30 Nov 2023 16:18:49 +0100 Subject: [PATCH 2/3] Use an object config to skip gray-matter's caching. Revert unrelated change. --- src/config.ts | 2 +- src/markdown.ts | 15 ++------------- 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/src/config.ts b/src/config.ts index 8c25519b3..cf05662f8 100644 --- a/src/config.ts +++ b/src/config.ts @@ -47,8 +47,8 @@ async function readPages(root: string): Promise { const pages: Page[] = []; for await (const file of visitFiles(root)) { if (file === "404.md" || extname(file) !== ".md") continue; + const parsed = await parseMarkdown(await readFile(join(root, file), "utf-8"), root, file); const name = basename(file, ".md"); - const parsed = await parseMarkdown(await readFile(join(root, file), "utf-8"), root, `/${name}`); const page = {path: join("/", dirname(file), name), name: parsed.title ?? "Untitled"}; if (name === "index") pages.unshift(page); else pages.push(page); diff --git a/src/markdown.ts b/src/markdown.ts index 61253d232..232533e34 100644 --- a/src/markdown.ts +++ b/src/markdown.ts @@ -422,19 +422,8 @@ function toParseCells(pieces: RenderPiece[]): CellPiece[] { } export async function parseMarkdown(source: string, root: string, sourcePath: string): Promise { - let parts; - try { - parts = matter(source); - } catch (error) { - // If the front-matter is invalid, flush gray-matter’s (private) cache, - // ensuring an error is also thrown the next time we evaluate it. - // https://github.com/jonschlinkert/gray-matter/issues/166 - // https://github.com/jonschlinkert/gray-matter/blob/ce67a86dba419381db0dd01cc84e2d30a1d1e6a5/index.js#L225 - // eslint-disable-next-line - // @ts-ignore - matter.clearCache!(); - throw new Error(`invalid front matter in ${sourcePath}`); - } + const parts = matter(source, {}); // Using empty options to avoid caching. + // TODO: We need to know what line in the source the markdown starts on and pass that // as startLine in the parse context below. const md = MarkdownIt({html: true}); From 2aa9e1b523aef8e4e795852686881b3fe555e3d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Rivi=C3=A8re?= Date: Thu, 30 Nov 2023 16:19:55 +0100 Subject: [PATCH 3/3] Remove unnecessary comment --- src/markdown.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/markdown.ts b/src/markdown.ts index 232533e34..59dc5ee8d 100644 --- a/src/markdown.ts +++ b/src/markdown.ts @@ -422,7 +422,7 @@ function toParseCells(pieces: RenderPiece[]): CellPiece[] { } export async function parseMarkdown(source: string, root: string, sourcePath: string): Promise { - const parts = matter(source, {}); // Using empty options to avoid caching. + const parts = matter(source, {}); // TODO: We need to know what line in the source the markdown starts on and pass that // as startLine in the parse context below.