Skip to content

Commit

Permalink
Extended railroad config
Browse files Browse the repository at this point in the history
  • Loading branch information
nirname committed Sep 23, 2023
1 parent c386d49 commit be89ccb
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 43 deletions.
46 changes: 36 additions & 10 deletions packages/mermaid/src/config.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ export type SankeyLinkColor = 'source' | 'target' | 'gradient';
* via the `definition` "SankeyNodeAlignment".
*/
export type SankeyNodeAlignment = 'left' | 'right' | 'center' | 'justify';
/**
* Shapes of the first and the last state in the diagram
*
*
* This interface was referenced by `MermaidConfig`'s JSON-Schema
* via the `definition` "RailroadDiagramBoundaryShape".
*/
export type RailroadDiagramBoundaryShape = 'dot' | 'circle' | 'forward' | 'backward';
/**
* The font size to use
*/
Expand Down Expand Up @@ -1323,30 +1331,48 @@ export interface SankeyDiagramConfig extends BaseDiagramConfig {
*/
export interface RailroadDiagramConfig extends BaseDiagramConfig {
alignment?: 'left' | 'right' | 'center' | 'justify';
verticalAlignment?: 'top' | 'bottom' | 'center' | 'justify';
/**
* Wrap long grammars similarly to wrapping long lines
*
*/
wrap?: boolean;
syntax?: 'mermaid' | 'w3c' | 'iso';
wrapDiagram?: boolean;
/**
* Compress rules to get the smallest possible diagram
*
*/
compress?: boolean;
/**
* Draw grammar definitions inside diagram
*
*/
display_grammar?: boolean;
render?: ('railroad' | 'definition' | 'dfa' | 'nfa')[];
/**
* Print angular brackets around non-terminal symbols
*
*/
angle_brackets?: boolean;
draw_arrows?: boolean;
angleBrackets?: boolean;
drawArrows?: boolean;
inline?: boolean;
inline_items?: string[];
inlineItems?: string[];
/**
* Name of the initial rule in grammar
* First rule will be initial if it is null
*
*/
start?: string | null;
shapes?: {
terminal?: string;
non_terminal?: string;
/**
* Shape or list of shapes for the start element
* They will be applied in the order of occurrence
*
*/
start?: RailroadDiagramBoundaryShape[] | RailroadDiagramBoundaryShape;
/**
* Shape or list of shapes for the end element
* They will be applied in the order of occurrence
*
*/
end?: RailroadDiagramBoundaryShape[] | RailroadDiagramBoundaryShape;
};
}
/**
* This interface was referenced by `MermaidConfig`'s JSON-Schema
Expand Down
3 changes: 3 additions & 0 deletions packages/mermaid/src/defaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ const config: RequiredDeep<MermaidConfig> = {
// TODO: can we make this default to `true` instead?
useMaxWidth: false,
},
railroad: {
...defaultConfigJson.railroad,
}
};

const keyify = (obj: any, prefix = ''): string[] =>
Expand Down
3 changes: 3 additions & 0 deletions packages/mermaid/src/diagrams/railroad/railroad.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import railroad from './railroadGrammar.jison';
// import { prepareTextForParsing } from '../railroadUtils.js';
import { cleanupComments } from '../../diagram-api/comments.js';
import { db, Rules } from './railroadDB.js';
// import defaultConfigJson from '../../schemas/config.schema.yaml?only-defaults=true';

describe('Railroad diagram', function () {
beforeAll(() => {
// console.log(defaultConfigJson);

railroad.yy = db;
});

Expand Down
16 changes: 11 additions & 5 deletions packages/mermaid/src/diagrams/railroad/railroadDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,20 @@ const getConsole = () => console;
type Callback<T> = (item: Chunk, index: number, parent: Chunk | undefined, result: T[]) => T;
// type Traverse<T> = (callback: Callback<T>, index: number, parent?: Chunk) => T;

interface Traversible {
traverse<T>(callback: Callback<T>, index?: number, parent?: Chunk): T;
}
// interface Traversible {
// traverse<T>(callback: Callback<T>, index?: number, parent?: Chunk): T;
// }

// TODO: rewrite toString using traverse
//
interface Chunk extends Traversible {
toString(): string;
// interface Chunk extends Traversible {
// toString(): string;
// }

abstract class Chunk {
// static staticMethod(): void;
static display: ((instance: Chunk) => void) | undefined;
abstract traverse<T>(callback: Callback<T>, index?: number, parent?: Chunk): T;
}

class Leaf implements Chunk {
Expand Down
112 changes: 84 additions & 28 deletions packages/mermaid/src/schemas/config.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ required:
- gitGraph
- c4
- sankey
- railroad
properties:
theme:
description: |
Expand Down Expand Up @@ -1877,12 +1878,26 @@ $defs: # JSON Schema definition (maybe we should move these to a seperate file)
type: string
default: ''

RailroadDiagramBoundaryShape:
description: |
Shapes of the first and the last state in the diagram
oneOf:
- const: dot
description: Dot, filled
- const: circle
description: Small empty circle
- const: forward
description: Small triangle pointing from the left to the right
- const: backward
description: Small triangle pointing from the right to the left

RailroadDiagramConfig:
title: Railroad Diagram Config
allOf: [{ $ref: '#/$defs/BaseDiagramConfig' }]
description: The object containing configurations specific for railroad diagrams.
type: object
unevaluatedProperties: false
required: [alignment]
properties:
alignment:
type: string
Expand All @@ -1891,55 +1906,96 @@ $defs: # JSON Schema definition (maybe we should move these to a seperate file)
- right
- center
- justify
wrap:
verticalAlignment:
type: string
enum:
- top
- bottom
- center
- justify
wrapDiagram:
type: boolean
default: true
description: |
Wrap long grammars similarly to wrapping long lines
syntax:
type: string
enum:
- mermaid
- w3c
- iso
# syntax:
# type: string
# enum:
# - mermaid
# - w3c
# - iso
# description: |
# Specify which standart would be applied
compress:
type: boolean
default: true
description: |
Compress rules to get the smallest possible diagram
display_grammar:
type: boolean
default: true
description: |
Draw grammar definitions inside diagram
angle_brackets:
render:
type: array
default:
- ebnf
items:
oneOf:
- const: railroad
description: Railroad diagram
- const: definition
description: Rules definition
- const: dfa
description: Deterministic Finite Automaton
- const: nfa
description: Nondeterministic Finite Automata
angleBrackets:
type: boolean
default: true
description: |
Print angular brackets around non-terminal symbols
draw_arrows:
drawArrows:
type: boolean
default: false
inline:
type: boolean
default: false
inline_items:
inlineItems:
type: array
items:
type: string
# shapes:
# type: object
# unevaluatedProperties: false
# properties:
# terminal:
# type: string
# enum:
# - left
# - right
# - center
# - justify
# non_terminal:
# type: string
start:
description: |
Name of the initial rule in grammar
First rule will be initial if it is null
oneOf:
- type: string
- type: 'null'
default: 'null'
shapes:
type: object
unevaluatedProperties: false
properties:
terminal:
type: string
non_terminal:
type: string
start:
description: |
Shape or list of shapes for the start element
They will be applied in the order of occurrence
anyOf:
- type: array
items:
$ref: '#/$defs/RailroadDiagramBoundaryShape'
- $ref: '#/$defs/RailroadDiagramBoundaryShape'
default: 'dot'
end:
description: |
Shape or list of shapes for the end element
They will be applied in the order of occurrence
anyOf:
- type: array
items:
$ref: '#/$defs/RailroadDiagramBoundaryShape'
- $ref: '#/$defs/RailroadDiagramBoundaryShape'
default: 'circle'

FontCalculator:
title: Font Calculator
Expand Down

0 comments on commit be89ccb

Please sign in to comment.