Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for markdown-style links #6158

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/witty-beds-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'mermaid': minor
---

feat:Added support for markdown-style links in line messages and notes
9 changes: 9 additions & 0 deletions cypress/integration/rendering/sequencediagram.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ describe('Sequence diagram', () => {
`
);
});
it('should render links', () => {
imgSnapshotTest(
`
sequenceDiagram
Alice->John: Look at [MermaidJS](https://mermaid.js.org/)
John->>Alice: Thanks for the link
`
);
});
it('should handle different line breaks', () => {
imgSnapshotTest(
`
Expand Down
22 changes: 22 additions & 0 deletions docs/syntax/sequenceDiagram.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ Messages can be of two displayed either solid or with a dotted line.
[Actor][Arrow][Actor]:Message text
```

### Arrow Types

There are ten types of arrows currently supported:

| Type | Description |
Expand All @@ -224,6 +226,26 @@ There are ten types of arrows currently supported:
| `-)` | Solid line with an open arrow at the end (async) |
| `--)` | Dotted line with a open arrow at the end (async) |

### Links in Messages (v\<MERMAID_RELEASE_VERSION>+)

You can add one or more markdown-style links to messages.

```
[Actor][Arrow][Actor]:[Label](URL)
```

Example:

```mermaid-example
sequenceDiagram
Alice->>John: Alice shared [MermaidJS](https://mermaid.js.org/) and [the Getting Started guide](https://mermaid.js.org/intro/getting-started.html) with John
```

```mermaid
sequenceDiagram
Alice->>John: Alice shared [MermaidJS](https://mermaid.js.org/) and [the Getting Started guide](https://mermaid.js.org/intro/getting-started.html) with John
```

## Activations

It is possible to activate and deactivate an actor. (de)activation can be dedicated declarations:
Expand Down
10 changes: 10 additions & 0 deletions packages/mermaid/src/diagrams/common/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,16 @@ export const katexRegex = /\$\$(.*)\$\$/g;
*/
export const hasKatex = (text: string): boolean => (text.match(katexRegex)?.length ?? 0) > 0;

export const markdownLinkRegex = /\[([^\]]+)]\(([^)]+)\)/;

/**
* Detect markdown links
*
* @param text - The text to test
* @returns Whether or not the text has markdown links
*/
export const hasMarkdownLink = (text: string): boolean => markdownLinkRegex.test(text);

/**
* Computes the minimum dimensions needed to display a div containing MathML
*
Expand Down
4 changes: 4 additions & 0 deletions packages/mermaid/src/diagrams/sequence/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ const getStyles = (options) =>
stroke: none;
}

.link {
fill: #0000EE;
}

.labelBox {
stroke: ${options.labelBoxBorderColor};
fill: ${options.labelBoxBkgColor};
Expand Down
83 changes: 78 additions & 5 deletions packages/mermaid/src/diagrams/sequence/svgDraw.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import common, { calculateMathMLDimensions, hasKatex, renderKatex } from '../common/common.js';
import common, {
calculateMathMLDimensions,
hasKatex,
renderKatex,
hasMarkdownLink,
markdownLinkRegex,
} from '../common/common.js';
import * as svgDrawCommon from '../common/svgDrawCommon.js';
import { ZERO_WIDTH_SPACE, parseFontSize } from '../../utils.js';
import { sanitizeUrl } from '@braintree/sanitize-url';
Expand Down Expand Up @@ -127,6 +133,67 @@ export const drawKatex = async function (elem, textData, msgModel = null) {
return [textElem];
};

export const drawLink = function (elem, label, url) {
const sanitizedUrl = sanitizeUrl(url);
elem
.append('a')
.attr('href', sanitizedUrl)
.attr('xlink:href', sanitizedUrl)
.attr('target', '_blank')
.attr('xlink:show', 'new')
.attr('rel', 'noopener noreferrer')
.append('tspan')
.attr('class', 'link')
.text(label);
};

export const drawMarkdownLinkText = function (elem, text) {
// Split text into segments - links and text between links
const segments = [];
let remainingText = text;
let match;

// Markdown link regex: [text](url)
const linkRegex = markdownLinkRegex;

while ((match = remainingText.match(linkRegex))) {
// Push text before link if exists
if (match.index > 0) {
segments.push({
type: 'text',
content: remainingText.substring(0, match.index),
});
}

// Push link
segments.push({
type: 'link',
text: match[1],
url: match[2],
});

// Update remaining text
remainingText = remainingText.substring(match.index + match[0].length);
}

// Push remaining text if any
if (remainingText) {
segments.push({
type: 'text',
content: remainingText,
});
}

// Append all segments
segments.forEach((segment) => {
if (segment.type === 'link') {
drawLink(elem, segment.text, segment.url);
} else {
elem.append('tspan').text(segment.content);
}
});
};

export const drawText = function (elem, textData) {
let prevTextHeight = 0;
let textHeight = 0;
Expand Down Expand Up @@ -234,13 +301,19 @@ export const drawText = function (elem, textData) {
}

const text = line || ZERO_WIDTH_SPACE;
let textSpan;
if (textData.tspan) {
const span = textElem.append('tspan');
span.attr('x', textData.x);
textSpan = textElem.append('tspan');
textSpan.attr('x', textData.x);
if (textData.fill !== undefined) {
span.attr('fill', textData.fill);
textSpan.attr('fill', textData.fill);
}
span.text(text);
} else {
textSpan = textElem;
}

if (hasMarkdownLink(text)) {
drawMarkdownLinkText(textSpan, text);
} else {
textElem.text(text);
}
Expand Down
17 changes: 17 additions & 0 deletions packages/mermaid/src/docs/syntax/sequenceDiagram.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ Messages can be of two displayed either solid or with a dotted line.
[Actor][Arrow][Actor]:Message text
```

### Arrow Types

There are ten types of arrows currently supported:

| Type | Description |
Expand All @@ -159,6 +161,21 @@ There are ten types of arrows currently supported:
| `-)` | Solid line with an open arrow at the end (async) |
| `--)` | Dotted line with a open arrow at the end (async) |

### Links in Messages (v<MERMAID_RELEASE_VERSION>+)

You can add one or more markdown-style links to messages.

```
[Actor][Arrow][Actor]:[Label](URL)
```

Example:

```mermaid-example
sequenceDiagram
Alice->>John: Alice shared [MermaidJS](https://mermaid.js.org/) and [the Getting Started guide](https://mermaid.js.org/intro/getting-started.html) with John
```

## Activations

It is possible to activate and deactivate an actor. (de)activation can be dedicated declarations:
Expand Down
Loading