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

wip: Add optInMarkdownLabels config setting #6277

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
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
13 changes: 13 additions & 0 deletions docs/config/setup/interfaces/mermaid.MermaidConfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,19 @@ The maximum allowed size of the users text diagram

---

### optInMarkdownLabels

• `Optional` **optInMarkdownLabels**: `boolean`

Only process markdown for labels enclosed in double-quote-backtick delimiters, e.g. "`_markdown label_`".
This can be useful when upgrading from mermaid 10 to 11, as version 11 started interpreting labels as markdown by default.

#### Defined in

[packages/mermaid/src/config.type.ts:217](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/config.type.ts#L217)

---

### packet

• `Optional` **packet**: `PacketDiagramConfig`
Expand Down
6 changes: 6 additions & 0 deletions packages/mermaid/src/config.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ export interface MermaidConfig {
*
*/
suppressErrorRendering?: boolean;
/**
* Only process markdown for labels enclosed in double-quote-backtick delimiters, e.g. "`_markdown label_`".
* This can be useful when upgrading from mermaid 10 to 11, as version 11 started interpreting labels as markdown by default.
*
*/
optInMarkdownLabels?: boolean;
}
/**
* The object containing configurations specific for flowcharts
Expand Down
19 changes: 16 additions & 3 deletions packages/mermaid/src/rendering-util/createText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ export const createText = async (
isTitle = false,
classes = '',
useHtmlLabels = true,
optInMarkdownLabels = false,
isNode = true,
width = 200,
addSvgBackground = false,
Expand All @@ -215,12 +216,24 @@ export const createText = async (
useHtmlLabels,
isNode,
'addSvgBackground: ',
addSvgBackground
addSvgBackground,
'optInMarkdownLabels: ',
optInMarkdownLabels
);
if (useHtmlLabels) {
// TODO: addHtmlLabel accepts a labelStyle. Do we possibly have that?

const htmlText = markdownToHTML(text, config);
let labelMaker = (text, config) => {
const match = text.match(/^"`(.*)`"$/);
if (match) {
return markdownToHTML(match[1], config);
} else {
return `<span class="${classes}">${text}</span>`;
}
};
if (!optInMarkdownLabels) {
labelMaker = markdownToHTML;
}
const htmlText = labelMaker(text, config);
const decodedReplacedText = replaceIconSubstring(decodeEntities(htmlText));

//for Katex the text could contain escaped characters, \\relax that should be transformed to \relax
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ export const labelHelper = async <T extends SVGGraphicsElement>(
_classes?: string
) => {
let cssClasses;
const useHtmlLabels = node.useHtmlLabels || evaluate(getConfig()?.htmlLabels);
const config = getConfig();
const useHtmlLabels = node.useHtmlLabels || evaluate(config?.htmlLabels);
const optInMarkdownLabels = evaluate(config?.optInMarkdownLabels);

if (!_classes) {
cssClasses = 'node default';
} else {
Expand Down Expand Up @@ -42,6 +45,7 @@ export const labelHelper = async <T extends SVGGraphicsElement>(

const text = await createText(labelEl, sanitizeText(decodeEntities(label), getConfig()), {
useHtmlLabels,
optInMarkdownLabels,
width: node.width || getConfig().flowchart?.wrappingWidth,
// @ts-expect-error -- This is currently not used. Should this be `classes` instead?
cssClasses: 'markdown-node-label',
Expand Down
6 changes: 6 additions & 0 deletions packages/mermaid/src/schemas/config.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,12 @@ properties:
description: |
Suppresses inserting 'Syntax error' diagram in the DOM.
This is useful when you want to control how to handle syntax errors in your application.
optInMarkdownLabels:
type: boolean
default: false
description: |
Only process markdown for labels enclosed in double-quote-backtick delimiters, e.g. "`_markdown label_`".
This can be useful when upgrading from mermaid 10 to 11, as version 11 started interpreting labels as markdown by default.

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