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

Bug/4391 make markdown auto wrapping optional #4856

Merged
Show file tree
Hide file tree
Changes from 10 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 docs/syntax/flowchart.md
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,11 @@ Formatting:

This feature is applicable to node labels, edge labels, and subgraph labels.

The auto wrapping can be disabled by using

%%{init: {"markdownAutoWrap": false} }%%
graph LR

## Interaction

It is possible to bind a click event to a node, the click can lead to either a javascript callback or to a link which will be opened in a new browser tab.
Expand Down
1 change: 1 addition & 0 deletions packages/mermaid/src/config.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ export interface MermaidConfig {
dompurifyConfig?: DOMPurifyConfiguration;
wrap?: boolean;
fontSize?: number;
markdownAutoWrap?: boolean;
}
/**
* This interface was referenced by `MermaidConfig`'s JSON-Schema
Expand Down
7 changes: 7 additions & 0 deletions packages/mermaid/src/docs/syntax/flowchart.md
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,13 @@ Formatting:

This feature is applicable to node labels, edge labels, and subgraph labels.

The auto wrapping can be disabled by using

```
%%{init: {"markdownAutoWrap": false} }%%
sidharthv96 marked this conversation as resolved.
Show resolved Hide resolved
graph LR
```

## Interaction

It is possible to bind a click event to a node, the click can lead to either a javascript callback or to a link which will be opened in a new browser tab.
Expand Down
4 changes: 2 additions & 2 deletions packages/mermaid/src/rendering-util/createText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ export const createText = (
log.info('createText', text, style, isTitle, classes, useHtmlLabels, isNode, addSvgBackground);
if (useHtmlLabels) {
// TODO: addHtmlLabel accepts a labelStyle. Do we possibly have that?
// text = text.replace(/\\n|\n/g, '<br />');

const htmlText = markdownToHTML(text);
// log.info('markdo wnToHTML' + text, markdownToHTML(text));

const node = {
isNode,
label: decodeEntities(htmlText).replace(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { markdownToLines, markdownToHTML } from './handle-markdown-text.js';
import { test, expect } from 'vitest';
import { setConfig } from '../config.js';

test('markdownToLines - Basic test', () => {
const input = `This is regular text
Expand Down Expand Up @@ -262,3 +263,11 @@ test('markdownToHTML - Unsupported formatting', () => {
- l3`)
).toMatchInlineSnapshot('"<p>Hello</p>Unsupported markdown: list"');
});

test('markdownToHTML - no auto wrapping', () => {
setConfig({ markdownAutoWrap: false });
expect(
markdownToHTML(`Hello, how do
you do?`)
).toMatchInlineSnapshot('"<p>Hello,&nbsp;how&nbsp;do<br/>you&nbsp;do?</p>"');
});
12 changes: 11 additions & 1 deletion packages/mermaid/src/rendering-util/handle-markdown-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@
import { fromMarkdown } from 'mdast-util-from-markdown';
import { dedent } from 'ts-dedent';
import type { MarkdownLine, MarkdownWordType } from './types.js';
import { getConfig } from '../config.js';

/**
* @param markdown - markdown to process
* @returns processed markdown
*/
function preprocessMarkdown(markdown: string): string {
const markdownAutoWrap = getConfig().markdownAutoWrap;
sidharthv96 marked this conversation as resolved.
Show resolved Hide resolved
// Replace multiple newlines with a single newline
const withoutMultipleNewlines = markdown.replace(/\n{2,}/g, '\n');
// Remove extra spaces at the beginning of each line
const withoutExtraSpaces = dedent(withoutMultipleNewlines);
if (markdownAutoWrap === false) {
return withoutExtraSpaces.replace(/ /g, '&nbsp;');

Check warning on line 18 in packages/mermaid/src/rendering-util/handle-markdown-text.ts

View check run for this annotation

Codecov / codecov/patch

packages/mermaid/src/rendering-util/handle-markdown-text.ts#L18

Added line #L18 was not covered by tests
}
return withoutExtraSpaces;
}

Expand Down Expand Up @@ -58,10 +63,15 @@

export function markdownToHTML(markdown: string) {
const { children } = fromMarkdown(markdown);
const markdownAutoWrap = getConfig().markdownAutoWrap;

function output(node: Content): string {
if (node.type === 'text') {
return node.value.replace(/\n/g, '<br/>');
if (markdownAutoWrap === false) {
return node.value.replace(/\n/g, '<br/>').replace(/ /g, '&nbsp;');

Check warning on line 71 in packages/mermaid/src/rendering-util/handle-markdown-text.ts

View check run for this annotation

Codecov / codecov/patch

packages/mermaid/src/rendering-util/handle-markdown-text.ts#L71

Added line #L71 was not covered by tests
} else {
return node.value.replace(/\n/g, '<br/>');
}
sidharthv96 marked this conversation as resolved.
Show resolved Hide resolved
} else if (node.type === 'strong') {
return `<strong>${node.children.map(output).join('')}</strong>`;
} else if (node.type === 'emphasis') {
Expand Down
2 changes: 2 additions & 0 deletions packages/mermaid/src/schemas/config.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ properties:
fontSize:
type: number
default: 16
markdownAutoWrap:
type: boolean
sidharthv96 marked this conversation as resolved.
Show resolved Hide resolved

$defs: # JSON Schema definition (maybe we should move these to a seperate file)
BaseDiagramConfig:
Expand Down
Loading