Add interface for DiagramDb and other minor changes

This commit is contained in:
Mason Malone
2022-11-19 12:48:40 -08:00
parent a11ab3d5ea
commit 3316aa5f4f
2 changed files with 15 additions and 6 deletions

View File

@@ -1,3 +1,4 @@
import { DiagramDb } from './types';
// The "* as yaml" part is necessary for tree-shaking // The "* as yaml" part is necessary for tree-shaking
import * as yaml from 'js-yaml'; import * as yaml from 'js-yaml';
@@ -15,11 +16,11 @@ type FrontMatterMetadata = {
/** /**
* Extract and parse frontmatter from text, if present, and sets appropriate * Extract and parse frontmatter from text, if present, and sets appropriate
* properties in the provided db. * properties in the provided db.
* @param text - * @param text - The text that may have a YAML frontmatter.
* @param db - * @param db - Diagram database, could be of any diagram.
* @returns text with frontmatter stripped out * @returns text with frontmatter stripped out
*/ */
export function extractFrontMatter(text: string, db: any): string { export function extractFrontMatter(text: string, db: DiagramDb): string {
const matches = text.match(frontMatterRegex); const matches = text.match(frontMatterRegex);
if (matches) { if (matches) {
const parsed: FrontMatterMetadata = yaml.load(matches[1], { const parsed: FrontMatterMetadata = yaml.load(matches[1], {
@@ -28,8 +29,8 @@ export function extractFrontMatter(text: string, db: any): string {
schema: yaml.FAILSAFE_SCHEMA, schema: yaml.FAILSAFE_SCHEMA,
}) as FrontMatterMetadata; }) as FrontMatterMetadata;
if (parsed && parsed.title) { if (parsed?.title) {
db?.setDiagramTitle(parsed.title); db.setDiagramTitle?.(parsed.title);
} }
return text.slice(matches[0].length); return text.slice(matches[0].length);

View File

@@ -8,8 +8,16 @@ export interface InjectUtils {
_setupGraphViewbox: any; _setupGraphViewbox: any;
} }
/**
* Generic Diagram DB that may apply to any diagram type.
*/
export interface DiagramDb {
clear?: () => void;
setDiagramTitle?: (title: string) => void;
}
export interface DiagramDefinition { export interface DiagramDefinition {
db: any; db: DiagramDb;
renderer: any; renderer: any;
parser: any; parser: any;
styles: any; styles: any;