English
@mmd-toolkit/no-invalid-init-directive
Disallow a directive meant to configure the diagram that is not written validly, so that mermaid.js ignores it.
✅ The recommended configuration enables this rule.
🔧 This rule is automatically fixable by the --fix CLI option.
An init directive gives diagram settings as JSON. Invalid JSON can make mermaid.js drop the settings of every init directive without an error. A name ending in init or initialize with different letter case gives no settings at all.
Rule Details
This rule applies to every diagram type. It reports invalid JSON in an init directive and a directive name ending in init or initialize with different letter case, such as INIT or myINIT (a lowercase suffix such as myinit gives settings).
mermaid.js turns every ' into " before reading the JSON, so {'theme': 'dark'} is valid, as is a string opened with one quote and closed with the other, but a ' inside a "-quoted string, an unescaped " inside a '-quoted string, and \' are read as something else. A \' is always reported; the other two are reported when they are all that keeps mermaid.js from reading the JSON. Each report comes with a fix that writes \u0027 or \"; a trailing comma is removed too. Any other error is reported once per directive, without a fix.
A word argument, such as %%{init: dark}%%, or no argument gives no settings and is not reported.
The examples are flowcharts.
Examples of incorrect code for this rule:
mmd
%% eslint @mmd-toolkit/no-invalid-init-directive: error
%%{init: {"theme": "dark",}}%%
flowchart LR
A --> Bmmd
%% eslint @mmd-toolkit/no-invalid-init-directive: error
%%{INIT: {"theme": "dark"}}%%
flowchart LR
A --> Bmmd
%% eslint @mmd-toolkit/no-invalid-init-directive: error
%%{init: {"fontFamily": "O'Brien"}}%%
flowchart LR
A --> BExamples of correct code for this rule:
mmd
%% eslint @mmd-toolkit/no-invalid-init-directive: error
%%{init: {"theme": "dark"}}%%
flowchart LR
A --> BOptions
This rule has no options.
Related Rules
Further Reading
Version
This rule was introduced in @mmd-toolkit/eslint-plugin v0.1.0.