chore: Minor fixes #4856

This commit is contained in:
Sidharth Vinod
2024-03-23 14:15:33 +05:30
parent 60be7012aa
commit f907ac30c6
6 changed files with 51 additions and 18 deletions

View File

@@ -852,7 +852,10 @@ This feature is applicable to node labels, edge labels, and subgraph labels.
The auto wrapping can be disabled by using The auto wrapping can be disabled by using
%%{init: {"markdownAutoWrap": false} }%% ---
config:
markdownAutoWrap: false
---
graph LR graph LR
## Interaction ## Interaction

View File

@@ -540,7 +540,10 @@ This feature is applicable to node labels, edge labels, and subgraph labels.
The auto wrapping can be disabled by using The auto wrapping can be disabled by using
``` ```
%%{init: {"markdownAutoWrap": false} }%% ---
config:
markdownAutoWrap: false
---
graph LR graph LR
``` ```

View File

@@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-explicit-any */
// @ts-nocheck TODO: Fix types // @ts-nocheck TODO: Fix types
import type { MermaidConfig } from '../config.type.js';
import type { Group } from '../diagram-api/types.js'; import type { Group } from '../diagram-api/types.js';
import type { D3TSpanElement, D3TextElement } from '../diagrams/common/commonTypes.js'; import type { D3TSpanElement, D3TextElement } from '../diagrams/common/commonTypes.js';
import { log } from '../logger.js'; import { log } from '../logger.js';
@@ -181,13 +182,14 @@ export const createText = (
isNode = true, isNode = true,
width = 200, width = 200,
addSvgBackground = false, addSvgBackground = false,
} = {} } = {},
config: MermaidConfig = {}
) => { ) => {
log.info('createText', text, style, isTitle, classes, useHtmlLabels, isNode, addSvgBackground); log.info('createText', text, style, isTitle, classes, useHtmlLabels, isNode, addSvgBackground);
if (useHtmlLabels) { if (useHtmlLabels) {
// TODO: addHtmlLabel accepts a labelStyle. Do we possibly have that? // TODO: addHtmlLabel accepts a labelStyle. Do we possibly have that?
const htmlText = markdownToHTML(text); const htmlText = markdownToHTML(text, config);
const node = { const node = {
isNode, isNode,
label: decodeEntities(htmlText).replace( label: decodeEntities(htmlText).replace(
@@ -199,7 +201,7 @@ export const createText = (
const vertexNode = addHtmlSpan(el, node, width, classes, addSvgBackground); const vertexNode = addHtmlSpan(el, node, width, classes, addSvgBackground);
return vertexNode; return vertexNode;
} else { } else {
const structuredText = markdownToLines(text); const structuredText = markdownToLines(text, config);
const svgLabel = createFormattedText(width, el, structuredText, addSvgBackground); const svgLabel = createFormattedText(width, el, structuredText, addSvgBackground);
return svgLabel; return svgLabel;
} }

View File

@@ -1,6 +1,6 @@
/* eslint-disable no-irregular-whitespace */
import { markdownToLines, markdownToHTML } from './handle-markdown-text.js'; import { markdownToLines, markdownToHTML } from './handle-markdown-text.js';
import { test, expect } from 'vitest'; import { test, expect } from 'vitest';
import { setConfig } from '../config.js';
test('markdownToLines - Basic test', () => { test('markdownToLines - Basic test', () => {
const input = `This is regular text const input = `This is regular text
@@ -204,6 +204,31 @@ Word!`;
expect(output).toEqual(expectedOutput); 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', () => { test('markdownToHTML - Basic test', () => {
const input = `This is regular text const input = `This is regular text
Here is a new line Here is a new line
@@ -265,9 +290,11 @@ test('markdownToHTML - Unsupported formatting', () => {
}); });
test('markdownToHTML - no auto wrapping', () => { test('markdownToHTML - no auto wrapping', () => {
setConfig({ markdownAutoWrap: false });
expect( expect(
markdownToHTML(`Hello, how do markdownToHTML(
you do?`) `Hello, how do
you do?`,
{ markdownAutoWrap: false }
)
).toMatchInlineSnapshot('"<p>Hello,&nbsp;how&nbsp;do<br/>you&nbsp;do?</p>"'); ).toMatchInlineSnapshot('"<p>Hello,&nbsp;how&nbsp;do<br/>you&nbsp;do?</p>"');
}); });

View File

@@ -2,14 +2,13 @@ import type { Content } from 'mdast';
import { fromMarkdown } from 'mdast-util-from-markdown'; import { fromMarkdown } from 'mdast-util-from-markdown';
import { dedent } from 'ts-dedent'; import { dedent } from 'ts-dedent';
import type { MarkdownLine, MarkdownWordType } from './types.js'; import type { MarkdownLine, MarkdownWordType } from './types.js';
import { getConfig } from '../config.js'; import type { MermaidConfig } from '../config.type.js';
/** /**
* @param markdown - markdown to process * @param markdown - markdown to process
* @returns processed markdown * @returns processed markdown
*/ */
function preprocessMarkdown(markdown: string): string { function preprocessMarkdown(markdown: string, { markdownAutoWrap }: MermaidConfig): string {
const markdownAutoWrap = getConfig().markdownAutoWrap;
// Replace multiple newlines with a single newline // Replace multiple newlines with a single newline
const withoutMultipleNewlines = markdown.replace(/\n{2,}/g, '\n'); const withoutMultipleNewlines = markdown.replace(/\n{2,}/g, '\n');
// Remove extra spaces at the beginning of each line // Remove extra spaces at the beginning of each line
@@ -23,8 +22,8 @@ function preprocessMarkdown(markdown: string): string {
/** /**
* @param markdown - markdown to split into lines * @param markdown - markdown to split into lines
*/ */
export function markdownToLines(markdown: string): MarkdownLine[] { export function markdownToLines(markdown: string, config: MermaidConfig = {}): MarkdownLine[] {
const preprocessedMarkdown = preprocessMarkdown(markdown); const preprocessedMarkdown = preprocessMarkdown(markdown, config);
const { children } = fromMarkdown(preprocessedMarkdown); const { children } = fromMarkdown(preprocessedMarkdown);
const lines: MarkdownLine[] = [[]]; const lines: MarkdownLine[] = [[]];
let currentLine = 0; let currentLine = 0;
@@ -61,17 +60,15 @@ export function markdownToLines(markdown: string): MarkdownLine[] {
return lines; return lines;
} }
export function markdownToHTML(markdown: string) { export function markdownToHTML(markdown: string, { markdownAutoWrap }: MermaidConfig = {}) {
const { children } = fromMarkdown(markdown); const { children } = fromMarkdown(markdown);
const markdownAutoWrap = getConfig().markdownAutoWrap;
function output(node: Content): string { function output(node: Content): string {
if (node.type === 'text') { if (node.type === 'text') {
if (markdownAutoWrap === false) { if (markdownAutoWrap === false) {
return node.value.replace(/\n/g, '<br/>').replace(/ /g, '&nbsp;'); 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') { } else if (node.type === 'strong') {
return `<strong>${node.children.map(output).join('')}</strong>`; return `<strong>${node.children.map(output).join('')}</strong>`;
} else if (node.type === 'emphasis') { } else if (node.type === 'emphasis') {

View File

@@ -237,6 +237,7 @@ properties:
default: 16 default: 16
markdownAutoWrap: markdownAutoWrap:
type: boolean type: boolean
default: true
$defs: # JSON Schema definition (maybe we should move these to a separate file) $defs: # JSON Schema definition (maybe we should move these to a separate file)
BaseDiagramConfig: BaseDiagramConfig: