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

Fix YAML properties with hyphens cause Build error #643

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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
14 changes: 14 additions & 0 deletions lib/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ import { serialize } from "next-mdx-remote/serialize";

import siteConfig from "@/config/siteConfig";

function sanitizeKeys(obj: Record<string, any>): Record<string, any> {
const sanitizedObj: Record<string, any> = {};

for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
const sanitizedKey = key.replace(/[^a-zA-Z0-9_$]/g, '_'); // Removes characters that are not alphabets, numbers, underscore, or dollar sign
sanitizedObj[sanitizedKey] = obj[key];
}
}

return sanitizedObj;
}

/**
* Parse a markdown or MDX file to an MDX source form + front matter data
*
Expand Down Expand Up @@ -96,6 +109,7 @@ const parse = async function (source, format, scope) {
scope: { ...scope, ...data },
}
);
mdxSource.scope = sanitizeKeys(mdxSource.scope);

return {
mdxSource: mdxSource,
Expand Down
Loading