Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle invalid front-matter errors #243

Merged
merged 4 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ async function readPages(root: string): Promise<Page[]> {
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);
Expand Down
14 changes: 13 additions & 1 deletion src/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,19 @@ function toParseCells(pieces: RenderPiece[]): CellPiece[] {
}

export async function parseMarkdown(source: string, root: string, sourcePath: string): Promise<ParseResult> {
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!();
Copy link
Contributor

@cinxmo cinxmo Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this an undocumented method? I don't see it in the types... (but I see the code in your comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is! I'd prefer for them to fix that bug, but in the meantime it's the best I can do

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really want to throw instead of just logging the error? The rest of the page should render 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can bypass the cache by passing in empty options:

https://github.com/jonschlinkert/gray-matter/blob/ce67a86dba419381db0dd01cc84e2d30a1d1e6a5/index.js#L44-L47

So let’s do that. Then you won’t need the try catch or clearCache call.

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});
Expand Down