@ztl-uwu/nuxt-content
Version:
Write your content inside your Nuxt app
30 lines (29 loc) • 763 B
JavaScript
import { destr } from "destr";
import { defineTransformer } from "./utils.js";
export default defineTransformer({
name: "Json",
extensions: [".json", ".json5"],
parse: async (_id, content) => {
let parsed;
if (typeof content === "string") {
if (_id.endsWith("json5")) {
parsed = (await import("json5").then((m) => m.default || m)).parse(content);
} else if (_id.endsWith("json")) {
parsed = destr(content);
}
} else {
parsed = content;
}
if (Array.isArray(parsed)) {
console.warn(`JSON array is not supported in ${_id}, moving the array into the \`body\` key`);
parsed = {
body: parsed
};
}
return {
...parsed,
_id,
_type: "json"
};
}
});