feat: Add title to parseResult

This commit is contained in:
Sidharth Vinod
2024-09-13 19:49:24 +05:30
parent 71889fd135
commit 19e79fda73
2 changed files with 17 additions and 6 deletions

View File

@@ -68,17 +68,23 @@ function processAndSetConfigs(text: string) {
async function parse( async function parse(
text: string, text: string,
parseOptions: ParseOptions & { suppressErrors: true } parseOptions: ParseOptions & { suppressErrors: true }
): Promise<ParseResult | false>; ): Promise<ParseResult & { error?: unknown }>;
async function parse(text: string, parseOptions?: ParseOptions): Promise<ParseResult>; async function parse(text: string, parseOptions?: ParseOptions): Promise<ParseResult>;
async function parse(text: string, parseOptions?: ParseOptions): Promise<ParseResult | false> { async function parse(
text: string,
parseOptions?: ParseOptions
): Promise<ParseResult & { error?: unknown }> {
addDiagrams(); addDiagrams();
let code = '';
let title = undefined;
let config: MermaidConfig = {};
try { try {
const { code, config } = processAndSetConfigs(text); ({ code, config, title } = processAndSetConfigs(text));
const diagram = await getDiagramFromText(code); const diagram = await getDiagramFromText(code);
return { diagram, config }; return { diagram, code, config, title, success: true };
} catch (error) { } catch (error) {
if (parseOptions?.suppressErrors) { if (parseOptions?.suppressErrors) {
return false; return { code, config, title, success: false, error };
} }
throw error; throw error;
} }

View File

@@ -58,16 +58,21 @@ export interface ParseOptions {
} }
export interface ParseResult { export interface ParseResult {
success: boolean;
code: string;
/** /**
* The config passed as YAML frontmatter or directives * The config passed as YAML frontmatter or directives
*/ */
config: MermaidConfig; config: MermaidConfig;
title?: string;
/** /**
* The diagram AST * The diagram AST
* *
*/ */
diagram: Diagram; diagram?: Diagram;
} }
// This makes it clear that we're working with a d3 selected element of some kind, even though it's hard to specify the exact type. // This makes it clear that we're working with a d3 selected element of some kind, even though it's hard to specify the exact type.