mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-08-30 21:56:43 +02:00
Code refactor and updated docs
This commit is contained in:
@@ -2,4 +2,4 @@
|
|||||||
'mermaid': patch
|
'mermaid': patch
|
||||||
---
|
---
|
||||||
|
|
||||||
Registered icons are now embedded as SVGs inside diagram. If an icon is not available in the registered icons it will still use <i> tag
|
FontAwesome icons can now be embedded as SVGs in flowcharts if they are registered via `mermaid.registerIconPacks`.
|
||||||
|
@@ -18,6 +18,9 @@
|
|||||||
.exClass {
|
.exClass {
|
||||||
fill: greenyellow !important;
|
fill: greenyellow !important;
|
||||||
}
|
}
|
||||||
|
.label-icon {
|
||||||
|
border: none;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body></body>
|
<body></body>
|
||||||
|
@@ -1916,13 +1916,9 @@ If a class is named default it will be assigned to all classes without specific
|
|||||||
|
|
||||||
## Basic support for fontawesome
|
## Basic support for fontawesome
|
||||||
|
|
||||||
It is possible to add icons from fontawesome and registered icon pack.
|
It is possible to add icons from fontawesome.
|
||||||
|
|
||||||
Mermaid supports icons from registered icon packs. Follow the instructions provided [here](../config/icons.md) to register your icon packs.
|
The icons are accessed via the syntax fa:#icon class name#.
|
||||||
|
|
||||||
The registered icons can be accessed via the syntax #registered icon pack name#:#icon name#.
|
|
||||||
|
|
||||||
The fontawesome icons are accessed via the syntax fa:#icon class name#.
|
|
||||||
|
|
||||||
```mermaid-example
|
```mermaid-example
|
||||||
flowchart TD
|
flowchart TD
|
||||||
@@ -1940,6 +1936,17 @@ flowchart TD
|
|||||||
B-->E(A fa:fa-camera-retro perhaps?)
|
B-->E(A fa:fa-camera-retro perhaps?)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
There are two ways to display these FontAwesome icons:
|
||||||
|
|
||||||
|
### Register FontAwesome icon packs (v11.4.2+)
|
||||||
|
|
||||||
|
You can register your own FontAwesome icon pack, to register follow the instructions provided [here](../config/icons.md).
|
||||||
|
|
||||||
|
> **Note**
|
||||||
|
> Note that it will fall back to FontAwesome CSS if FontAwesome packs are not registered.
|
||||||
|
|
||||||
|
### Register FontAwesome CSS
|
||||||
|
|
||||||
Mermaid supports Font Awesome if the CSS is included on the website.
|
Mermaid supports Font Awesome if the CSS is included on the website.
|
||||||
Mermaid does not have any restriction on the version of Font Awesome that can be used.
|
Mermaid does not have any restriction on the version of Font Awesome that can be used.
|
||||||
|
|
||||||
|
@@ -1231,13 +1231,9 @@ If a class is named default it will be assigned to all classes without specific
|
|||||||
|
|
||||||
## Basic support for fontawesome
|
## Basic support for fontawesome
|
||||||
|
|
||||||
It is possible to add icons from fontawesome and registered icon pack.
|
It is possible to add icons from fontawesome.
|
||||||
|
|
||||||
Mermaid supports icons from registered icon packs. Follow the instructions provided [here](../config/icons.md) to register your icon packs.
|
The icons are accessed via the syntax fa:#icon class name#.
|
||||||
|
|
||||||
The registered icons can be accessed via the syntax #registered icon pack name#:#icon name#.
|
|
||||||
|
|
||||||
The fontawesome icons are accessed via the syntax fa:#icon class name#.
|
|
||||||
|
|
||||||
```mermaid-example
|
```mermaid-example
|
||||||
flowchart TD
|
flowchart TD
|
||||||
@@ -1247,6 +1243,18 @@ flowchart TD
|
|||||||
B-->E(A fa:fa-camera-retro perhaps?)
|
B-->E(A fa:fa-camera-retro perhaps?)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
There are two ways to display these FontAwesome icons:
|
||||||
|
|
||||||
|
### Register FontAwesome icon packs (v11.4.2+)
|
||||||
|
|
||||||
|
You can register your own FontAwesome icon pack, to register follow the instructions provided [here](../config/icons.md).
|
||||||
|
|
||||||
|
```note
|
||||||
|
Note that it will fall back to FontAwesome CSS if FontAwesome packs are not registered.
|
||||||
|
```
|
||||||
|
|
||||||
|
### Register FontAwesome CSS
|
||||||
|
|
||||||
Mermaid supports Font Awesome if the CSS is included on the website.
|
Mermaid supports Font Awesome if the CSS is included on the website.
|
||||||
Mermaid does not have any restriction on the version of Font Awesome that can be used.
|
Mermaid does not have any restriction on the version of Font Awesome that can be used.
|
||||||
|
|
||||||
|
@@ -184,34 +184,25 @@ function updateTextContentAndStyles(tspan: any, wrappedLine: MarkdownWord[]) {
|
|||||||
* @returns string with fontawesome icons as svg if the icon is registered otherwise as i tags
|
* @returns string with fontawesome icons as svg if the icon is registered otherwise as i tags
|
||||||
*/
|
*/
|
||||||
export async function replaceIconSubstring(text: string) {
|
export async function replaceIconSubstring(text: string) {
|
||||||
// The letters 'bklrs' stand for possible endings of the fontawesome prefix (e.g. 'fab' for brands, 'fak' for fa-kit) // cspell: disable-line
|
const pendingReplacements: Promise<string>[] = [];
|
||||||
const iconRegex = /(fa[bklrs]?):fa-([\w-]+)/g; // cspell: disable-line
|
// cspell: disable-next-line
|
||||||
|
text.replace(/(fa[bklrs]?):fa-([\w-]+)/g, (fullMatch, prefix, iconName) => {
|
||||||
const matches = [...text.matchAll(iconRegex)];
|
pendingReplacements.push(
|
||||||
if (matches.length === 0) {
|
(async () => {
|
||||||
return text;
|
|
||||||
}
|
|
||||||
|
|
||||||
const replacements = await Promise.all(
|
|
||||||
matches.map(async ([fullMatch, prefix, iconName]) => {
|
|
||||||
const registeredIconName = `${prefix}:${iconName}`;
|
const registeredIconName = `${prefix}:${iconName}`;
|
||||||
try {
|
if (await isIconAvailable(registeredIconName)) {
|
||||||
const isIconRegistered = await isIconAvailable(registeredIconName);
|
return await getIconSVG(registeredIconName, undefined, { class: 'label-icon' });
|
||||||
const replacement = isIconRegistered
|
} else {
|
||||||
? await getIconSVG(registeredIconName, undefined, { class: 'label-icon' })
|
return `<i class='${sanitizeText(fullMatch).replace(':', ' ')}'></i>`;
|
||||||
: `<i class='${sanitizeText(fullMatch).replace(':', ' ')}'></i>`;
|
|
||||||
return { fullMatch, replacement };
|
|
||||||
} catch (error) {
|
|
||||||
log.error(`Error processing ${registeredIconName}:`, error);
|
|
||||||
return { fullMatch, replacement: fullMatch };
|
|
||||||
}
|
}
|
||||||
})
|
})()
|
||||||
);
|
);
|
||||||
|
return fullMatch;
|
||||||
|
});
|
||||||
|
|
||||||
return replacements.reduce(
|
const replacements = await Promise.all(pendingReplacements);
|
||||||
(text, { fullMatch, replacement }) => text.replace(fullMatch, replacement),
|
// cspell: disable-next-line
|
||||||
text
|
return text.replace(/(fa[bklrs]?):fa-([\w-]+)/g, () => replacements.shift() ?? '');
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note when using from flowcharts converting the API isNode means classes should be set accordingly. When using htmlLabels => to sett classes to'nodeLabel' when isNode=true otherwise 'edgeLabel'
|
// Note when using from flowcharts converting the API isNode means classes should be set accordingly. When using htmlLabels => to sett classes to'nodeLabel' when isNode=true otherwise 'edgeLabel'
|
||||||
|
Reference in New Issue
Block a user