Skip to content

Commit

Permalink
Add out-of-the-box support for mermaid (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulo-ferraz-oliveira authored Jul 22, 2023
1 parent 2415f08 commit 026741d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ _*
ebin
log
erl_crash.dump
rebar3.crashdump
.rebar
_rel
_deps
Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,35 @@ In case you get a warning for something that is working or would like further co

### Additional options

#### Support for Mermaid

Out-of-the-box support for [Mermaid](https://mermaid.js.org/) is provided via `rebar.config` option
`with_mermaid`, as follows:

```erlang
{ex_doc, [
{with_mermaid, true}
]}.
```

or

```erlang
{ex_doc, [
{with_mermaid, "10.2.4"}
]}.
```

Check out what this looks like in `doc/index.html`.

```mermaid
graph TD;
A-->B;
A-->C;
B-->D;
C-->D;
```

#### v prefix for source ref versions

By default rebar3_ex_doc adopts the convention of prefixing versions with `v`.
Expand Down
3 changes: 2 additions & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
{homepage_url, <<"https://github.com/starbelly/rebar3_ex_doc">>},
{source_url, <<"https://github.com/starbelly/rebar3_ex_doc">>},
{assets, <<"assets">>},
{api_reference, false}
{api_reference, false},
{with_mermaid, true}
]}.

{profiles, [
Expand Down
18 changes: 18 additions & 0 deletions src/rebar3_ex_doc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

-define(DEFAULT_DOC_DIR, "doc").
-define(DEFAULT_DOC_LANG, "en").
-define(DEFAULT_MERMAID_VSN, "10.2.4").

%% ===================================================================
%% Public API
Expand Down Expand Up @@ -77,6 +78,9 @@ format_error({write_config, Err}) ->
format_error({unsupported_otp, Ver}) ->
Str = "You are using erlang/OTP '~ts', but this plugin requires at least erlang/OTP 24.",
io_lib:format(Str, [integer_to_list(Ver)]);
format_error({mermaid_vsn_not_string, Vsn}) ->
Str = "ex_doc option 'with_mermaid' should be 'true', 'false', or string(). Got: ~p",
io_lib:format(Str, [Vsn]);
format_error({ex_doc, _}) ->
"";
format_error(Err) ->
Expand Down Expand Up @@ -307,6 +311,8 @@ to_ex_doc_format(ExDocOpts) ->
[{K, to_binary(OutputDir)} | Opts];
({prefix_ref_vsn_with_v, _}, Opts) ->
Opts;
({with_mermaid, Vsn}, Opts) ->
Opts ++ mermaid_add(Vsn);
(OtherOpt, Opts) ->
rebar_api:warn("unknown ex_doc option ~p", [OtherOpt]),
[OtherOpt | Opts]
Expand Down Expand Up @@ -490,3 +496,15 @@ help(language) ->
help(logo) ->
"Path to the image logo of the project (only PNG or JPEG accepted)."
"The image size will be 64x64 and copied to the assets directory.".

% Mermaid support
mermaid_add(false) ->
"";
mermaid_add(true) ->
mermaid_add(?DEFAULT_MERMAID_VSN);
mermaid_add(Vsn) when is_list(Vsn) ->
[{before_closing_head_tag, #{
html => "<script src=\"https://cdn.jsdelivr.net/npm/mermaid@" ++ Vsn ++ "/dist/mermaid.min.js\" />"
}}];
mermaid_add(Vsn) ->
?RAISE({mermaid_vsn_not_string, Vsn}).

0 comments on commit 026741d

Please sign in to comment.