Skip to content

Commit a156ac9

Browse files
committed
refactor(markdown): move default table configuration with filename pattern support
1 parent ab993bf commit a156ac9

File tree

1 file changed

+43
-43
lines changed
  • packages/epicenter/src/providers/markdown

1 file changed

+43
-43
lines changed

packages/epicenter/src/providers/markdown/configs.ts

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,48 @@ export type WithBodyFieldOptions<
392392
filenameField?: keyof TTableSchema & string;
393393
};
394394

395+
/**
396+
* Default table configuration using the `{id}.md` filename pattern.
397+
*
398+
* Default behavior:
399+
* - Serialize: All fields except id → frontmatter, empty body, filename "{id}.md"
400+
* - ParseFilename: Strip .md extension, return { id }
401+
* - Deserialize: Validate frontmatter against schema with id from parsed
402+
*
403+
* Use this when your table doesn't have a dedicated content/body field.
404+
*/
405+
export const DEFAULT_TABLE_CONFIG = defineTableConfig<TableSchema>()
406+
.withParser((filename: `${string}.md`) => {
407+
const id = path.basename(filename, '.md');
408+
return { id };
409+
})
410+
.withSerializers({
411+
serialize: ({ row: { id, ...rest } }) => ({
412+
frontmatter: rest,
413+
body: '',
414+
filename: `${id}.md`,
415+
}),
416+
deserialize: ({ frontmatter, parsed, table }) => {
417+
const { id } = parsed;
418+
419+
// Combine id with frontmatter
420+
const data = { id, ...frontmatter };
421+
422+
// Validate using direct arktype pattern
423+
const validator = table.validators.toArktype();
424+
const result = validator(data);
425+
426+
if (result instanceof type.errors) {
427+
return MarkdownProviderErr({
428+
message: `Failed to validate row ${id}`,
429+
context: { fileName: `${id}.md`, id, reason: result.summary },
430+
});
431+
}
432+
433+
return Ok(result as SerializedRow<TableSchema>);
434+
},
435+
});
436+
395437
/**
396438
* Factory function to create a table config with human-readable filenames.
397439
*
@@ -519,7 +561,7 @@ export function withBodyField<TTableSchema extends TableSchema>(
519561
} = options;
520562

521563
return defineTableConfig<TTableSchema>()
522-
.withParser((filename: string) => {
564+
.withParser((filename: `${string}.md`) => {
523565
const id = path.basename(filename, '.md');
524566
return { id };
525567
})
@@ -576,45 +618,3 @@ export function withBodyField<TTableSchema extends TableSchema>(
576618
},
577619
});
578620
}
579-
580-
/**
581-
* Default table configuration using the `{id}.md` filename pattern.
582-
*
583-
* Default behavior:
584-
* - Serialize: All fields except id → frontmatter, empty body, filename "{id}.md"
585-
* - ParseFilename: Strip .md extension, return { id }
586-
* - Deserialize: Validate frontmatter against schema with id from parsed
587-
*
588-
* Use this when your table doesn't have a dedicated content/body field.
589-
*/
590-
export const DEFAULT_TABLE_CONFIG = defineTableConfig<TableSchema>()
591-
.withParser((filename: `${string}.md`) => {
592-
const id = path.basename(filename, '.md');
593-
return { id };
594-
})
595-
.withSerializers({
596-
serialize: ({ row: { id, ...rest } }) => ({
597-
frontmatter: rest,
598-
body: '',
599-
filename: `${id}.md`,
600-
}),
601-
deserialize: ({ frontmatter, parsed, table }) => {
602-
const { id } = parsed;
603-
604-
// Combine id with frontmatter
605-
const data = { id, ...frontmatter };
606-
607-
// Validate using direct arktype pattern
608-
const validator = table.validators.toArktype();
609-
const result = validator(data);
610-
611-
if (result instanceof type.errors) {
612-
return MarkdownProviderErr({
613-
message: `Failed to validate row ${id}`,
614-
context: { fileName: `${id}.md`, id, reason: result.summary },
615-
});
616-
}
617-
618-
return Ok(result as SerializedRow<TableSchema>);
619-
},
620-
});

0 commit comments

Comments
 (0)