Skip to content

Commit e5e6ddd

Browse files
kohenkatzraghur
authored andcommitted
Include provided id on the generated object (#30)
* Include provided `id` on the generated object This allows for referencing mermaid figures using the `pandoc-crossref` filter. Example: ```{.mermaid caption="Example with id" #fig:example} sequenceDiagram Alice->>John: Hello John, how are you? John-->>Alice: Great! ``` This is an example paragraph with a reference to @fig:example. * Add README info about ID * Fix typo
1 parent a58b912 commit e5e6ddd

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ Admittedly, this is uglier than the earlier syntax on top - but that's how Pando
5555

5656
It's also possible to override global defaults by using environment variables. The name for these environment variables are the same as the attributes prefixed with a `MERMAID_FILTER_` so that `width` would be `MERMAID_FILTER_WIDTH`.
5757

58+
You can also specify an ID to be applied to the rendered image. This may be useful to use [`pandoc-crossref`](https://github.com/lierdakil/pandoc-crossref) or similar packages to reference your diagrams, for example:
59+
60+
```{.mermaid #fig:example}
61+
// Your diagram code here
62+
```
63+
64+
This text has a reference @fig:example which is automatically inserted.
65+
66+
(Note that `pandoc-crossref` will automatically find and use the `caption=` option. Also note that the order of applying the filters matters - you must apply `mermaid-filter` *before* `pandoc-crossref` so that `pandoc-crossref` can find the images.)
67+
5868
JSON and CSS configuration
5969
---------------------------
6070

index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ function mermaid(type, value, format, meta) {
1818
if (type != "CodeBlock") return null;
1919
var attrs = value[0],
2020
content = value[1];
21-
var classes = attrs[1];
21+
var id = attrs[0],
22+
classes = attrs[1];
2223
var options = {
2324
width: process.env.MERMAID_FILTER_WIDTH || '500',
2425
format: process.env.MERMAID_FILTER_FORMAT || 'png',
@@ -99,7 +100,7 @@ function mermaid(type, value, format, meta) {
99100
return pandoc.Para(
100101
[
101102
pandoc.Image(
102-
['', [], []],
103+
[id, [], []],
103104
[pandoc.Str(options.caption)],
104105
[newPath, fig]
105106
)

0 commit comments

Comments
 (0)