Skip to content

Commit e32f8ab

Browse files
committed
feat: rewrote the recursive logic in mutate-sreialize mdx to avoid a bunch of errors, thanks https://github.com/MarcosNASA
1 parent 0924ba1 commit e32f8ab

1 file changed

Lines changed: 23 additions & 20 deletions

File tree

lib/mutateSerializeMdx.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,34 @@ export type RecursiveSerialize<T> = {
1313
: RecursiveSerialize<T[P]>;
1414
};
1515

16-
/**
17-
* @see {@link https://stackoverflow.com/questions/73835176/replace-all-the-nested-object-properties-to-particular-value}
18-
*/
1916
export async function mutateSerializeMdx<
2017
T extends object,
2118
R extends RecursiveSerialize<T>
2219
>(obj: T): Promise<R> {
23-
const keys = Object.keys(obj) as Array<keyof typeof obj>;
24-
25-
for (const key of keys) {
26-
const value = obj[key];
27-
28-
if (typeof value === "string")
29-
// @ts-ignore
30-
obj[key] = await serialize(value, {
31-
mdxOptions: {
32-
remarkPlugins: [remarkGfm],
33-
rehypePlugins: [],
34-
},
35-
});
20+
const entries = Object.entries(obj);
21+
22+
const temp = entries.flatMap(async ([key, value]) => {
23+
if (typeof value == "string") {
24+
return [
25+
key,
26+
await serialize(value, {
27+
mdxOptions: {
28+
remarkPlugins: [remarkGfm],
29+
rehypePlugins: [],
30+
},
31+
}),
32+
];
33+
}
34+
3635
if (typeof value === "object" && value !== null) {
37-
mutateSerializeMdx(value);
36+
return [key, await mutateSerializeMdx(value)];
3837
}
39-
}
4038

41-
// @ts-ignore
42-
return obj;
39+
console.error("Not a string or object", key, value);
40+
return [];
41+
});
42+
43+
const result = Object.fromEntries(await Promise.all(temp));
44+
45+
return result;
4346
}

0 commit comments

Comments
 (0)