# Interactive Sequence diagram using mermaid.js producing parse errors in Observable Notebooks

**URL:** <https://talk.observablehq.com/t/interactive-sequence-diagram-using-mermaid-js-producing-parse-errors-in-observable-notebooks/6690>\
**Category:** Help\
**Created:** [June 13, 2022, 6:12am UTC](https://talk.observablehq.com/t/interactive-sequence-diagram-using-mermaid-js-producing-parse-errors-in-observable-notebooks/6690 "2022-06-13T06:12:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pixel](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@pixel](https://talk.observablehq.com/u/pixel)\
**Post date:** [June 13, 2022, 6:12am UTC](https://talk.observablehq.com/t/interactive-sequence-diagram-using-mermaid-js-producing-parse-errors-in-observable-notebooks/6690/1 "2022-06-13T06:12:57Z")

</div>

I tried creating an interactive sequence diagram inspired by [https://observablehq.com/@tomlarkworthy/animated-sequence-diagrams](https://observablehq.com/@tomlarkworthy/animated-sequence-diagrams)  
`

The (required) diagram is as shown below:

 ![user login (2)](https://canada1.discourse-cdn.com/flex030/uploads/observablehq/original/2X/5/58fd4436107e60c36d0dcb78af25ea4c9ce2d26f.png)

However when I am trying to animate it(in the observable notebook) I am encountering parse errors.

```auto
mermaid`sequenceDiagram
    autonumber
    actor S as Student
    participant s as System
    participant ad as Admin(Librarian)
    participant b as Book database
    participant sdb as Student database
    ${includeIf(
      step > 0,
      `${includeIf(step === 1, `rect rgb(0,255,0)\n`)}
      S->>s: Login
      activate S
      activate s
      ${includeIf(step == 1, "\nend")}
      ${includeIf(
        step > 1,
        `${includeIf(step === 2, `rect rgb(0, 255, 0)\n`)}
            s->>sdb: Authenticate with login details
                        activate sdb
        ${includeIf(step == 2, "\nend")}
        ${includeIf(
          step > 2,`alt authenticated user
          ${includeIf(step === 3, `rect rgb(0, 255, 0)\n`)}
            sdb-->>s: user authenticated
            ${includeIf(step == 3, "\nend")}
          ${includeIf(
            step > 3,
            `${includeIf(step === 4, `rect rgb(0, 255, 0)\n`)}
              s-->>S: Login successful
            ${includeIf(step == 4, "\nend")}
            ${includeIf(
              step > 4,`else else
              ${includeIf(step === 5, `rect rgb(0, 255, 0)\n`)}
                sdb-->>s: authentication failed
              ${includeIf(step == 5, "\nend")}
              ${includeIf(
                step > 5,
                `${includeIf(step === 6, `rect rgb(0, 255,0)\n`)}
                s->>sdb: does the user exist?
              ${includeIf(step == 6, "\nend")}
              ${includeIf(step > 6,
                  `alt user doesn't exist
                ${includeIf(step === 7, `rect rgb(0, 255,0)\n`)}
                sdb-->>s: user does not exist
              ${includeIf(step == 7, "\nend")}
              ${includeIf(step > 7,
                `${includeIf(step === 8, `rect rgb(0, 255,0)\n`)}
                s->>S: Register?
              ${includeIf(step == 8, "\nend")}
              ${includeIf(step > 8,
                  `else else
                ${includeIf(step === 9, `rect rgb(0, 255,0)\n`)}
                sdb-->>s: user already exists
                                deactivate sdb
              ${includeIf(step == 9, "\nend")}
              ${includeIf(step > 9,
                `${includeIf(step === 10, `rect rgb(0,255,0)\n`)}
                s->>S: invalid credentials
              ${includeIf(step == 10, "\nend")}
                        end
                        end`
                      )}`
                    )}`
                  )}`
                )}`
              )}`
            )}`
          )}`
        )}`
      )}`
    )}
`

```

After observing closely I find that there needs to be an end statement immediately after an alt statement…but if I do so I would not get the required diagram.

---

<div class="post-metadata">

**Author:** ![tomlarkworthy](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/tomlarkworthy/32/5940_2.png) [@tomlarkworthy](https://talk.observablehq.com/u/tomlarkworthy)\
**Post date:** [June 13, 2022, 8:55am UTC](https://talk.observablehq.com/t/interactive-sequence-diagram-using-mermaid-js-producing-parse-errors-in-observable-notebooks/6690/2 "2022-06-13T08:55:47Z")

</div>

Start simple and just have 10 fully defined diagrams in a switch statement. The includeIf is just a hacky thing to reduce some repetition but it does not understand mermaid syntax so you have to balance your expressions manually. That’s hard to do unless you have a very firm grip on the diagram source you want as a goal.

I refactored my code after figuring out how I wanted the animation frames but I think you have jumped straight in at the optimized version.

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [June 13, 2022, 9:27am UTC](https://talk.observablehq.com/t/interactive-sequence-diagram-using-mermaid-js-producing-parse-errors-in-observable-notebooks/6690/3 "2022-06-13T09:27:47Z")

</div>

@pixel Can you share your notebook? I’d like to see if I can come up with a more maintainable authoring solution.

---

<div class="post-metadata">

**Author:** ![pixel](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@pixel](https://talk.observablehq.com/u/pixel)\
**Post date:** [June 14, 2022, 5:11am UTC](https://talk.observablehq.com/t/interactive-sequence-diagram-using-mermaid-js-producing-parse-errors-in-observable-notebooks/6690/4 "2022-06-14T05:11:23Z")

</div>

@mootari here’s the link to the notebook: [Interactive Sequence Diagram : User Login(Library Management System) / Parvathi R / Observable](https://observablehq.com/@parvathir/interactive-sequence-diagram-user-login-library-manageme)

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [June 14, 2022, 10:18am UTC](https://talk.observablehq.com/t/interactive-sequence-diagram-using-mermaid-js-producing-parse-errors-in-observable-notebooks/6690/5 "2022-06-14T10:18:29Z")

</div>

@pixel I’ve added a `steppedMermaid` helper to Tom’s notebook which lets you split your steps via `%% step %%` comments. Check the updated version of his notebook: [Interactive Sequence Diagrams / Tom Larkworthy / Observable](https://observablehq.com/@tomlarkworthy/animated-sequence-diagrams)

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [June 14, 2022, 6:59pm UTC](https://talk.observablehq.com/t/interactive-sequence-diagram-using-mermaid-js-producing-parse-errors-in-observable-notebooks/6690/6 "2022-06-14T18:59:41Z")

</div>

I just realized that your diagram contains nested blocks which also contain multiple steps. In this case I’d advise against trying to define the steps interpolated into the mermaid markup. Instead split your diagram into multiple blocks of text and recombine them according to the current step.

You’ll likely also want to add some helpers to wrap sections of your diagram. Here is a rough example to get you started:

```javascript
{
  const wrap = (prefix, body) => `\n${prefix}\n${body}\nend\n`;
  const mark = body => wrap('rect rgb(0,255,0)', body);
  const blockAuth = body => wrap(`alt authenticated user`, body);
  const blockFail = body => wrap(`alt user doesn't exist`, body);

  const head = `
    sequenceDiagram
    autonumber
  
    actor S as Student
    participant s as System
    participant ad as Admin(Librarian)
    participant b as Book database
    participant sdb as Student database
  `;

  return mermaid`
  ${head}

  ${blockAuth(`
    s->>sdb: does the user exist?
    ${blockFail(`
      sdb-->>s: authentication failed
      ${mark(`s->>S: Register?`)}
    `)}
  `)}
  `;
}

```

Note that mermaid does not care about indentation.

---

<div class="post-metadata">

**Author:** ![pixel](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@pixel](https://talk.observablehq.com/u/pixel)\
**Post date:** [June 17, 2022, 5:15am UTC](https://talk.observablehq.com/t/interactive-sequence-diagram-using-mermaid-js-producing-parse-errors-in-observable-notebooks/6690/7 "2022-06-17T05:15:25Z")

</div>

Thank you
