Kanji
・云架构工程师 / 自由职业 ・1993年出生 ・爱媛县出身 / 现居东京都涩谷区 ・5年 AWS 实战经验 个人资料详情
目录
在Mermaid中使用图像时,主要有两种方法:“①使用 img 标签指定图像URL”和“②使用图标包”。下面介绍如何使用旧的AWS架构图标。
img
参考: 如何在美人鱼流程图中更改节点形状并指定图像/图标#Beginner -奇塔
使用 Mermaid 的“img”标签直接指定 AWS 图标的 URL。
flowchart TB subgraph ACCOUNT[AWS Account] subgraph GRP1[" "] ELB@{ img: "https://api.iconify.design/logos/aws-elb.svg", label: "ELB", pos: "b", w: 60, h: 60, constraint: "on" } end subgraph GRP2[" "] EC2@{ img: "https://api.iconify.design/logos/aws-ec2.svg", label: "EC2", pos: "b", w: 60, h: 60, constraint: "on" } end subgraph GRP3[" "] RDS@{ img: "https://api.iconify.design/logos/aws-rds.svg", label: "RDS", pos: "b", w: 60, h: 60, constraint: "on" } end ELB --- EC2 --- RDS end classDef vpc fill:none,color:#0a0,stroke:#0a0 class ACCOUNT vpc classDef group fill:none,stroke:none class GRP1,GRP2,GRP3 group
当在 Mermaid 中渲染时,它看起来像这样:
另一种方法是使用美人鱼图标包。这样就无需为每个图标指定 URL。
参考:【在美人鱼中注册图标包|美人鱼](https://mermaid.js.org/config/icons.html)
flowchart TB subgraph ACCOUNT[AWS Account] subgraph GRP1[" "] ELB@{ icon: "logos:aws-elb", label: "ELB", pos: "b", h: 60} end subgraph GRP2[" "] EC2@{ icon: "logos:aws-ec2", label: "EC2", pos: "b", h: 60} end subgraph GRP3[" "] RDS@{ icon: "logos:aws-rds", label: "RDS", pos: "b", h: 60} end ELB --- EC2 --- RDS end classDef vpc fill:none,color:#0a0,stroke:#0a0 class ACCOUNT vpc classDef group fill:none,stroke:none class GRP1,GRP2,GRP3 group
但是,这种方法不能在 Qiita 或 GitHub 等 SaaS 平台上使用。 要使用图标包,您需要加载托管在 CDN 或服务器上的 JSON 文件,如下所示:
<script> mermaid.registerIconPacks([ { name: 'logos', loader: () => fetch('https://unpkg.com/@iconify-json/logos@1/icons.json').then((res) => res.json()), }, ]); mermaid.initialize({startOnLoad:true}); </script>
在此站点上,我们使用使用最新的 AWS 架构图标创建的自定义图标包。
要使用最新的 AWS 架构图标,您需要创建自定义 Mermaid 图标包。 本节介绍如何创建 Mermaid 图标包并使用最新的 AWS 架构图标。
首先,从以下站点下载最新的 AWS 架构图标。 点击【开始】->【图标包】开始下载。
AWS 简单图标 - AWS 架构中心 | AWS 简单图标AWS
下载的文件当前名为“Asset-Package_02072025.dee42cd0a6eaacc3da1ad9519579357fb546f803.zip”。 解压后,您将看到如下所示的文件夹结构。使用这些文件创建您的美人鱼图标包。
Asset-Package_02072025.dee42cd0a6eaacc3da1ad9519579357fb546f803/ ├── Architecture-Group-Icons_02072025 │ ├── _32.png │ ├── _32.svg │ └── ... ├── Architecture-Service-Icons_02072025 │ ├── Arch_Cloud-Financial-Management │ ├── Arch_Compute │ └── ... ├── Category-Icons_02072025 │ ├── Arch-Category_16 │ ├── Arch-Category_32 │ └── ... └── Resource-Icons_02072025 ├── Res_Analytics └── Resource-Icons_02072025 ├── Res_Analytics ├── Res_Application-Integration └── ...
接下来,将以下 Python 脚本保存在适当的目录中(例如“collect_mermaid_icons.py”)。 该脚本仅使用标准库:“re”、“os”、“json”和“argparse”。
import re import argparse FOLDER_FILE_PATTERNS = { "Architecture-Group-Icons": "_32.svg", "Architecture-Service-Icons": "_48.svg", "Category-Icons": "_48.svg", "Resource-Icons": "_48.svg", } def get_icon_folders(icon_root): return [name for name in os.listdir(icon_root) if os.path.isdir(os.path.join(icon_root, name))] def extract_svg_size(svg_content): m = re.search(r'<svg[^>]*width=["\']?(\d+)["\']?[^>]*height=["\']?(\d+)["\']?', svg_content) if m: return int(m.group(1)), int(m.group(2)) return 0, 0 def collect_svg_icons(icon_root): icon_folders = get_icon_folders(icon_root) icons = {} for folder in icon_folders: print(f"Processing folder: {folder}") folder_path = os.path.join(icon_root, folder) if not os.path.isdir(folder_path): continue pattern = None for key, value in FOLDER_FILE_PATTERNS.items(): if folder.startswith(key): pattern = value break for root, dirs, files in os.walk(folder_path): for file in files: icon_name = os.path.splitext(file)[0] normalized_name = icon_name.replace(pattern.replace(".svg", "").replace(".png", ""), "") if pattern else icon_name file_path = os.path.join(root, file) if pattern: if not file.endswith(pattern): continue svg_content = "" try: with open(file_path, "r", encoding="utf-8") as svg_file: svg_content = svg_file.read() fill_colors = re.findall(r'fill=["\'](#[0-9a-fA-F]{3,6})["\']', svg_content, flags=re.IGNORECASE) if fill_colors: main_color = fill_colors[0] if fill_colors else None svg_content = re.sub(r'fill=["\']#fff(?:fff)?["\']', f'fill="{main_color}"', svg_content, flags=re.IGNORECASE) except Exception: svg_content = "" width, height = extract_svg_size(svg_content) icons[normalized_name] = {"body": svg_content, "width": width, "height": height} continue return icons def main(): parser.add_argument("--icon-root", type=str, help="Root directory of AWS architecture icons") parser.add_argument("--output-json", type=str, default="./icons.json", help="Path to output JSON file (default: ./icons.json)") parser.add_argument("--icon-list-txt", type=str, default="./icon_list.txt", help="Path to icon name list TXT file (default: ./icon_list.txt)") args = parser.parse_args() error_flags = False error_messages = [] if not args.icon_root or not os.path.isdir(args.icon_root): error_flags = True error_messages.append("The icon root directory is not specified correctly. Please use the --icon-root option.") if not args.output_json: error_flags = True error_messages.append("The path to the output JSON file is not specified. Please use the --output-json option.") if not args.icon_list_txt: error_flags = True error_messages.append("The path to the icon name list TXT file is not specified. Please use the --icon-list-txt option.") if error_flags: for msg in error_messages: print(f"Error: {msg}") return icons = collect_svg_icons(args.icon_root) output = {"prefix": "logos", "icons": icons} with open(args.output_json, "w", encoding="utf-8") as f: json.dump(output, f, ensure_ascii=False, indent=2) icon_names = sorted(icons.keys()) with open(args.icon_list_txt, "w", encoding="utf-8") as f: for name in icon_names: f.write(name + "\n") if __name__ == "__main__": main()
By running this script, you can collect SVG icons from the specified AWS architecture icon root directory and generate a JSON file in the Mermaid icon pack format.
To execute the script, run the following command from the terminal. This will collect icons from the specified directory and generate both icons.json and icon_list.txt files.
icons.json
icon_list.txt
python collect_mermaid_icons.py \ --icon-root /path/to/Asset-Package_02072025.dee42cd0a6eaacc3da1ad9519579357fb546f803 \ # Optional --output-json ./icons.json \ --icon-list-txt ./icon_list.txt
The generated icons.json file can be used as a Mermaid icon pack. Below is an example of the actual icon pack JSON generated by the script.
{ "prefix": "logos", "icons": { "Arch-Category_Storage": { "body": "<svg width=\"74px\" height=\"74px\" viewBox=\"0 0 74 74\" xmlns=\"http://www.w3.org/2000/svg\">...</svg>", "width": 74, "height": 74 }, "Arch_Amazon-EC2": { "body": "<svg width=\"64px\" height=\"64px\" viewBox=\"0 0 64 64\" xmlns=\"http://www.w3.org/2000/svg\">...</svg>", "width": 64, "height": 64 }, "Arch_Amazon-RDS": { "body": "<svg width=\"64px\" height=\"64px\" viewBox=\"0 0 64 64\" xmlns=\"http://www.w3.org/2000/svg\">...</svg>", "width": 64, "height": 64 }, "Arch_Elastic-Load-Balancing": { "body": "<svg width=\"64px\" height=\"64px\" viewBox=\"0 0 64 64\" xmlns=\"http://www.w3.org/2000/svg\">...</svg>", "width": 64, "height": 64 }, "Arch_Amazon-Simple-Storage-Service": { "body": "<svg width=\"64px\" height=\"64px\" viewBox=\"0 0 64 64\" xmlns=\"http://www.w3.org/2000/svg\">...</svg>", "width": 64, "height": 64 }, "Arch_AWS-Lambda": { "body": "<svg width=\"64px\" height=\"64px\" viewBox=\"0 0 64 64\" xmlns=\"http://www.w3.org/2000/svg\">...</svg>", "width": 64, "height": 64 } } }
完整的 icons.json 文件(包含 780 多个 AWS 官方 SVG 图标定义)可通过运行上述第 2 步的 collect_mermaid_icons.py 在本地数秒内生成。为保证页面加载速度,上方仅展示核心服务(EC2、RDS、ELB、S3、Lambda)的代表性结构摘录。
collect_mermaid_icons.py
Finally, load the Mermaid icon pack. If you want to load the JSON file from your server, add the following code to your HTML file:
<script> mermaid.registerIconPacks([ { name: 'logos', loader: () => fetch('./icons.json').then((res) => res.json()), }, ]); mermaid.initialize({ startOnLoad: true }); </script>
On this site, we implement it by writing the contents of the JSON file into the mermaid-icons-json element according to the page.
mermaid-icons-json
<script type="application/json" id="mermaid-icons-json"> </script> <script> mermaid.registerIconPacks([ { name: 'logos', loader: () => { const jsonText = document.getElementById('mermaid-icons-json').textContent; return Promise.resolve(JSON.parse(jsonText)); }, }, ]); mermaid.initialize({ startOnLoad: true }); </script>
To specify an icon pack, use the icon name after logos: . The available icon names in the pack you created are listed in the icon_list.txt file.
logos:
flowchart TB subgraph ACCOUNT[AWS Account] subgraph GRP1[" "] ELB@{ icon: "logos:Arch_Elastic-Load-Balancing", label: "ELB", pos: "b", w: 60, h: 60, constraint: "on" } end subgraph GRP2[" "] EC2@{ icon: "logos:Res_Amazon-EC2_Instance", label: "EC2", pos: "b", w: 60, h: 60, constraint: "on" } end subgraph GRP3[" "] RDS@{ icon: "logos:Arch_Amazon-RDS", label: "RDS", pos: "b", w: 60, h: 60, constraint: "on" } end ELB --- EC2 --- RDS end classDef vpc fill:none,color:#0a0,stroke:#0a0 class ACCOUNT vpc classDef group fill:none,stroke:none class GRP1,GRP2,GRP3 group
When displayed in Mermaid, it looks like this: As mentioned at the beginning, the ELB and RDS icons have different colors from the latest AWS architecture icons. This is because the color conversion did not work well when processing the AWS architecture icon SVG files.
Real-time AI quant analysis of corporate earnings reports.
提供逼真 AI 模拟通话与多维反馈,结合 16 讲实战社交学院与工作室级 AI 肖像生成,全面提升社交互动质量。
拍小票即可完成高精度智能拆单,支持 LINE 登录无缝结算团队聚会支出,告别繁琐账单分摊。
占卜师、管家、调酒师等 AI 角色实时引导话题与聚会游戏,让线下与线上聚餐氛围瞬间升温。
人工甘味料や添加物のNG除外と有効成分あたりの実質単価比較データベース。