Skip to content

Commit

Permalink
chore: Minor fixes #4856
Browse files Browse the repository at this point in the history
  • Loading branch information
sidharthv96 committed Mar 23, 2024
1 parent 60be701 commit f907ac3
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 18 deletions.
5 changes: 4 additions & 1 deletion docs/syntax/flowchart.md
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,10 @@ This feature is applicable to node labels, edge labels, and subgraph labels.

The auto wrapping can be disabled by using

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

## Interaction
Expand Down
5 changes: 4 additions & 1 deletion packages/mermaid/src/docs/syntax/flowchart.md
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,10 @@ This feature is applicable to node labels, edge labels, and subgraph labels.
The auto wrapping can be disabled by using

```
%%{init: {"markdownAutoWrap": false} }%%
---
config:
markdownAutoWrap: false
---
graph LR
```

Expand Down
8 changes: 5 additions & 3 deletions packages/mermaid/src/rendering-util/createText.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
// @ts-nocheck TODO: Fix types
import type { MermaidConfig } from '../config.type.js';
import type { Group } from '../diagram-api/types.js';
import type { D3TSpanElement, D3TextElement } from '../diagrams/common/commonTypes.js';
import { log } from '../logger.js';
Expand Down Expand Up @@ -181,13 +182,14 @@ export const createText = (
isNode = true,
width = 200,
addSvgBackground = false,
} = {}
} = {},
config: MermaidConfig = {}
) => {
log.info('createText', text, style, isTitle, classes, useHtmlLabels, isNode, addSvgBackground);
if (useHtmlLabels) {
// TODO: addHtmlLabel accepts a labelStyle. Do we possibly have that?

const htmlText = markdownToHTML(text);
const htmlText = markdownToHTML(text, config);
const node = {
isNode,
label: decodeEntities(htmlText).replace(
Expand All @@ -199,7 +201,7 @@ export const createText = (
const vertexNode = addHtmlSpan(el, node, width, classes, addSvgBackground);
return vertexNode;
} else {
const structuredText = markdownToLines(text);
const structuredText = markdownToLines(text, config);
const svgLabel = createFormattedText(width, el, structuredText, addSvgBackground);
return svgLabel;
}
Expand Down
35 changes: 31 additions & 4 deletions packages/mermaid/src/rendering-util/handle-markdown-text.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-irregular-whitespace */
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 @@ -204,6 +204,31 @@ Word!`;
expect(output).toEqual(expectedOutput);
});

test('markdownToLines - No auto wrapping', () => {
expect(
markdownToLines(
`Hello, how do
you do?`,
{ markdownAutoWrap: false }
)
).toMatchInlineSnapshot(`
[
[
{
"content": "Hello, how do",
"type": "normal",
},
],
[
{
"content": "you do?",
"type": "normal",
},
],
]
`);
});

test('markdownToHTML - Basic test', () => {
const input = `This is regular text
Here is a new line
Expand Down Expand Up @@ -265,9 +290,11 @@ test('markdownToHTML - Unsupported formatting', () => {
});

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

/**
* @param markdown - markdown to process
* @returns processed markdown
*/
function preprocessMarkdown(markdown: string): string {
const markdownAutoWrap = getConfig().markdownAutoWrap;
function preprocessMarkdown(markdown: string, { markdownAutoWrap }: MermaidConfig): string {
// Replace multiple newlines with a single newline
const withoutMultipleNewlines = markdown.replace(/\n{2,}/g, '\n');
// Remove extra spaces at the beginning of each line
Expand All @@ -23,8 +22,8 @@ function preprocessMarkdown(markdown: string): string {
/**
* @param markdown - markdown to split into lines
*/
export function markdownToLines(markdown: string): MarkdownLine[] {
const preprocessedMarkdown = preprocessMarkdown(markdown);
export function markdownToLines(markdown: string, config: MermaidConfig = {}): MarkdownLine[] {
const preprocessedMarkdown = preprocessMarkdown(markdown, config);
const { children } = fromMarkdown(preprocessedMarkdown);
const lines: MarkdownLine[] = [[]];
let currentLine = 0;
Expand Down Expand Up @@ -61,17 +60,15 @@ export function markdownToLines(markdown: string): MarkdownLine[] {
return lines;
}

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

function output(node: Content): string {
if (node.type === 'text') {
if (markdownAutoWrap === false) {
return node.value.replace(/\n/g, '<br/>').replace(/ /g, '&nbsp;');
} else {
return node.value.replace(/\n/g, '<br/>');
}
return node.value.replace(/\n/g, '<br/>');
} else if (node.type === 'strong') {
return `<strong>${node.children.map(output).join('')}</strong>`;
} else if (node.type === 'emphasis') {
Expand Down
1 change: 1 addition & 0 deletions packages/mermaid/src/schemas/config.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ properties:
default: 16
markdownAutoWrap:
type: boolean
default: true

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

0 comments on commit f907ac3

Please sign in to comment.