Added some additional safe parse element stuff

This commit is contained in:
Nate Kelley 2025-08-07 17:16:41 -06:00
parent 75950ae108
commit 216e610aa8
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
4 changed files with 24 additions and 16 deletions

View File

@ -1,5 +1,5 @@
import { getReport } from '@buster/database'; import { getReport } from '@buster/database';
import type { GetReportIndividualResponse } from '@buster/server-shared/reports'; import type { GetReportIndividualResponse, ReportElements } from '@buster/server-shared/reports';
import { markdownToPlatejs } from '@buster/server-utils/report'; import { markdownToPlatejs } from '@buster/server-utils/report';
import { Hono } from 'hono'; import { Hono } from 'hono';
import { HTTPException } from 'hono/http-exception'; import { HTTPException } from 'hono/http-exception';
@ -14,10 +14,10 @@ export async function getReportHandler(
const platejsResult = await markdownToPlatejs(report.content); const platejsResult = await markdownToPlatejs(report.content);
if (platejsResult.error) { if (platejsResult.error) {
throw platejsResult.error; console.error('Error converting markdown to PlateJS:', platejsResult.error);
} }
const content = platejsResult.elements ?? []; const content: ReportElements = platejsResult.elements as unknown as ReportElements; //why do I have to do this?
const response: GetReportIndividualResponse = { const response: GetReportIndividualResponse = {
...report, ...report,

View File

@ -323,7 +323,7 @@ const ListTypeEnum = z.enum(['ul', 'ol']);
// Nested list item for complex lists // Nested list item for complex lists
const NestedListElementSchema = z.object({ const NestedListElementSchema = z.object({
type: z.enum(['li', 'lic', 'lii', 'ul', 'ol']), type: z.enum(['li', 'lic', 'lii', 'ul', 'ol']),
children: z.array(z.any()).default([]), children: z.array(z.any()).default([]), //not super happy about this... but... ce la vie
}); });
// List container (unordered or ordered) // List container (unordered or ordered)

View File

@ -1,4 +1,4 @@
import type { ReportElements } from '@buster/database'; import { type ReportElements, ReportElementsSchema } from '@buster/database';
import { describe, expect, it } from 'vitest'; import { describe, expect, it } from 'vitest';
import { markdownToPlatejs, platejsToMarkdown } from './platejs-conversions'; import { markdownToPlatejs, platejsToMarkdown } from './platejs-conversions';
@ -73,7 +73,7 @@ Here's an unordered list:
}); });
it('real world markdown', async () => { it('real world markdown', async () => {
const markdown = `Our most popular mountain bike over the last 12 months is Mountain-200 Black, 38 with 825 units sold. const markdownOG = `Our most popular mountain bike over the last 12 months is Mountain-200 Black, 38 with 825 units sold.
## Key Findings ## Key Findings
- The top-selling mountain bike model is **Mountain-200 Black, 38**. - The top-selling mountain bike model is **Mountain-200 Black, 38**.
- It sold **825 units** in the last 12 months. - It sold **825 units** in the last 12 months.
@ -99,8 +99,14 @@ Here's an unordered list:
- Using revenue-based popularity could favor higher-priced bikes; I chose units to avoid price bias. - Using revenue-based popularity could favor higher-priced bikes; I chose units to avoid price bias.
- Using the riding discipline filter (e.g., Mountain) was considered, but I used the explicit Mountain Bikes subcategory to exclude components. - Using the riding discipline filter (e.g., Mountain) was considered, but I used the explicit Mountain Bikes subcategory to exclude components.
`; `;
const markdown = `
- Filters:
- Product Category = "Bikes"
`;
const platejs = await markdownToPlatejs(markdown); const platejs = await markdownToPlatejs(markdown);
expect(platejs).toBeDefined(); expect(platejs.elements).toBeDefined();
}); });
}); });
@ -564,5 +570,9 @@ describe('platejsToMarkdown', () => {
type: 'ul', type: 'ul',
}, },
]; ];
const markdownFromPlatejs = await platejsToMarkdown(elements);
expect(markdownFromPlatejs).toBeDefined();
}); });
}); });

View File

@ -1,21 +1,19 @@
import { type ReportElements, ReportElementsSchema } from '@buster/database'; import { type ReportElements, ReportElementsSchema } from '@buster/database';
import type { Descendant } from 'platejs'; import type { Descendant } from 'platejs';
import type { ZodError } from 'zod';
import { SERVER_EDITOR } from './server-editor'; import { SERVER_EDITOR } from './server-editor';
export const markdownToPlatejs = async (markdown: string) => { export const markdownToPlatejs = async (
markdown: string
): Promise<{ error: ZodError | Error | null; elements: ReportElements }> => {
try { try {
const descendants = SERVER_EDITOR.api.markdown.deserialize(markdown); const descendants: ReportElements = SERVER_EDITOR.api.markdown.deserialize(markdown);
console.log('descendants', descendants);
console.log('descendants.json', JSON.stringify(descendants, null, 2));
const safeParsedElements = ReportElementsSchema.safeParse(descendants); const safeParsedElements = ReportElementsSchema.safeParse(descendants);
console.log('safeParsedElements', safeParsedElements);
return { return {
error: safeParsedElements.error, error: safeParsedElements.error as ZodError,
elements: safeParsedElements.data, elements: descendants,
}; };
} catch (error) { } catch (error) {
console.error('Error converting markdown to PlateJS:', error); console.error('Error converting markdown to PlateJS:', error);