Skip to content

Commit

Permalink
font size fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ad1992 committed Jul 10, 2024
1 parent a1fe898 commit c28a563
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Currently `mermaid-to-excalidraw` only supports the :point_down: config params
};
/**
* Theme variables
* @default { fontSize: "25px" }
* @default { fontSize: "20px" }
*/
themeVariables?: {
fontSize?: string;
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const MERMAID_CONFIG = {
startOnLoad: false,
flowchart: { curve: "linear" },
themeVariables: {
// Multiplying by 1.25 to increase the font size by 25% and render correctly in Excalidraw
fontSize: `${DEFAULT_FONT_SIZE * 1.25}px`,
},
};
11 changes: 7 additions & 4 deletions src/converter/GraphConverter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MermaidConfig } from "../index.js";
import { ExcalidrawConfig } from "../index.js";
import { MermaidToExcalidrawResult } from "../interfaces.js";
import { Flowchart } from "../parser/flowchart.js";
import { Sequence } from "../parser/sequence.js";
Expand All @@ -8,11 +8,14 @@ export class GraphConverter<T = Flowchart | Sequence> {
constructor({
converter,
}: {
converter: (graph: T, options: MermaidConfig) => MermaidToExcalidrawResult;
converter: (
graph: T,
config: ExcalidrawConfig
) => MermaidToExcalidrawResult;
}) {
this.converter = converter;
}
convert = (graph: T, options: MermaidConfig) => {
return this.converter(graph, options);
convert = (graph: T, config: ExcalidrawConfig) => {
return this.converter(graph, config);
};
}
3 changes: 1 addition & 2 deletions src/converter/types/flowchart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ const computeGroupIds = (
export const FlowchartToExcalidrawSkeletonConverter = new GraphConverter({
converter: (graph: Flowchart, options) => {
const elements: ExcalidrawElementSkeleton[] = [];
const fontSize =
parseInt(options.themeVariables?.fontSize ?? "") || DEFAULT_FONT_SIZE;
const fontSize = options.fontSize || DEFAULT_FONT_SIZE;
const { getGroupIds, getParentId } = computeGroupIds(graph);

// SubGraphs
Expand Down
4 changes: 2 additions & 2 deletions src/graphToExcalidraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { Sequence } from "./parser/sequence.js";
import { Flowchart } from "./parser/flowchart.js";
import { Class } from "./parser/class.js";
import { classToExcalidrawSkeletonConvertor } from "./converter/types/class.js";
import { MermaidConfig } from "./index.js";
import { ExcalidrawConfig } from "./index.js";

export const graphToExcalidraw = (
graph: Flowchart | GraphImage | Sequence | Class,
options: MermaidConfig = {}
options: ExcalidrawConfig = {}
): MermaidToExcalidrawResult => {
switch (graph.type) {
case "graphImage": {
Expand Down
20 changes: 14 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,29 @@ export interface MermaidConfig {
maxTextSize?: number;
}

export interface ExcalidrawConfig {
fontSize?: number;
}

const parseMermaidToExcalidraw = async (
definition: string,
config?: MermaidConfig
) => {
const mermaidConfig = config || {};
const parsedMermaidData = await parseMermaid(definition, config);

// Only font size supported for excalidraw elements
const excalidrawElements = graphToExcalidraw(parsedMermaidData, {
const fontSize =
parseInt(mermaidConfig.themeVariables?.fontSize ?? "") || DEFAULT_FONT_SIZE;
const parsedMermaidData = await parseMermaid(definition, {
...config,
themeVariables: {
...mermaidConfig.themeVariables,
fontSize:
mermaidConfig.themeVariables?.fontSize || `${DEFAULT_FONT_SIZE}px`,
// Multiplying by 1.25 to increase the font size by 25% and render correctly in Excalidraw
fontSize: `${fontSize * 1.25}px`,
},
});
// Only font size supported for excalidraw elements
const excalidrawElements = graphToExcalidraw(parsedMermaidData, {
fontSize,
});
return excalidrawElements;
};

Expand Down
1 change: 0 additions & 1 deletion src/parseMermaid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export const parseMermaid = async (
config: MermaidConfig = MERMAID_CONFIG
): Promise<Flowchart | GraphImage | Sequence | Class> => {
mermaid.initialize(config);

// Parse the diagram
const diagram = await mermaid.mermaidAPI.getDiagramFromText(
encodeEntities(definition)
Expand Down

0 comments on commit c28a563

Please sign in to comment.