Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions app/api/[owner]/[repo]/[branch]/entries/[path]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ const parseContent = (
schema: Record<string, any>,
config: Record<string, any>
) => {
const serializedTypes = ["yaml-frontmatter", "json-frontmatter", "toml-frontmatter", "yaml", "json", "toml"];

const serializedTypes = ["yaml-frontmatter", "json-frontmatter", "toml-frontmatter", "yaml", "json", "toml", "datagrid", "csv"];
let contentObject: Record<string, any> = {};

if (serializedTypes.includes(schema && schema.format) && schema.fields && schema.fields.length > 0) {
Expand Down
4 changes: 2 additions & 2 deletions lib/configSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ const ContentObjectSchema = z.object({
}).strict().optional().nullable(),
format: z.enum([
"yaml-frontmatter", "json-frontmatter", "toml-frontmatter",
"yaml", "json", "toml", "datagrid", "code", "raw"
"yaml", "json", "toml", "datagrid", "csv", "code", "raw"
], {
message: "'format' must be 'yaml-frontmatter', 'json-frontmatter', 'tom-frontmatter', 'yaml', 'json', 'toml', 'datagrid', 'code' or 'raw'."
message: "'format' must be 'yaml-frontmatter', 'json-frontmatter', 'tom-frontmatter', 'yaml', 'json', 'toml', 'datagrid', 'csv', 'code' or 'raw'."
}).optional().nullable(),
delimiters: z.union([
z.array(z.string({
Expand Down
25 changes: 21 additions & 4 deletions lib/serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@

import YAML from "yaml";
import * as TOML from "@ltd/j-toml"
import * as CSVS from "csv-stringify/sync";
import * as CSVP from "csv-parse/sync";

type FrontmatterFormat = "json-frontmatter" | "yaml-frontmatter" | "toml-frontmatter";
type SerialFormat = "json" | "yaml" | "toml";
type SerialFormat = "json" | "yaml" | "toml" | "csv";
type Format = FrontmatterFormat | SerialFormat;

// Parse straight YAML/JSON/TOML and YAML/JSON/TOML frontmatter strings into an object
const parse = (content: string = "", options: { delimiters?: string, format?: Format } = {}) => {
const format = options.format || "yaml-frontmatter";

// YAML/JSON/TOML without frontmatter
if (["yaml", "json", "toml"].includes(format)) return deserialize(content, format as SerialFormat);

if (["datagrid", "csv"].includes(format)) return deserialize(content, "csv" as SerialFormat);

const delimiters = setDelimiter(options.delimiters, format as FrontmatterFormat);
const startDelimiter = delimiters[0].replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const endDelimiter = delimiters[1].replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
Expand Down Expand Up @@ -51,6 +53,18 @@ const deserialize = (content: string = "", format: SerialFormat = "yaml") => {
switch (format) {
case "yaml":
return YAML.parse(content, { strict: false, uniqueKeys: false });
case "csv":
// Deserialise lists (and objects?). Ideally not naively (ie: using the schema).
return CSVP.parse(content, {
columns: true, relax_quotes: true,
cast: function (value, context) {
try {
return JSON.parse(value)
} catch (err) {
return value
}
},
});
case "json":
return JSON.parse(content);
case "toml":
Expand All @@ -64,9 +78,10 @@ const deserialize = (content: string = "", format: SerialFormat = "yaml") => {
// Convert an object into straight YAML/JSON/TOML or YAML/JSON/TOML frontmatter strings
const stringify = (contentObject: Record<string, any> = {}, options: { delimiters?: string, format?: Format } = {}) => {
const format = options.format || "yaml-frontmatter";

// YAML/JSON/TOML without frontmatter
if (["yaml", "json", "toml"].includes(format)) return serialize(contentObject, format as SerialFormat);
if (["datagrid", "csv"].includes(format)) return serialize(contentObject, "csv" as SerialFormat);

// Frontmatter
const delimiters = setDelimiter(options.delimiters, format as FrontmatterFormat);
Expand Down Expand Up @@ -96,6 +111,8 @@ const serialize = (contentObject: Record<string, any> = {}, format: SerialFormat
return JSON.stringify(contentObject, null, 2);
case "toml":
return TOML.stringify(contentObject, { newline: "\n"});
case "csv":
return CSVS.stringify(contentObject as any[], { header: true });
default:
return "";
}
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Define file types and provide Helper functions (get file info, get parent path, normalize paths...)
*/

const serializedTypes = ["yaml-frontmatter", "json-frontmatter", "toml-frontmatter", "yaml", "json", "toml"];
const serializedTypes = ["yaml-frontmatter", "json-frontmatter", "toml-frontmatter", "yaml", "json", "toml", "datagrid", "csv"];

const extensionCategories: Record<string, string[]> = {
image: ["jpg", "jpeg", "apng", "png", "gif", "svg", "ico", "avif", "bmp", "tif", "tiff", "webp"],
Expand Down Expand Up @@ -101,4 +101,4 @@ export {
sortFiles,
extensionCategories,
serializedTypes
};
};
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"cmdk": "^1.0.0",
"csv-parse": "^5.6.0",
"csv-stringify": "^6.5.2",
"date-fns": "^3.6.0",
"drizzle-orm": "^0.31.2",
"install": "^0.13.0",
Expand Down