为了同步使用印象笔记流程图,所以这里选择了mermaid.js 语法来实现
mermaidjs 详细使用语法参见 https://mermaidjs.github.io/#/
使用方法:
参考:https://github.com/webappdevelp/hexo-filter-mermaid-diagrams
安装 package
1  | npm install hexo-filter-mermaid-diagrams  | 
在你的博客目录而不是项目根目录
配置 _config.yml
1  | # mermaid chart  | 
添加执行js
swig template engine:1
2
3
4
5
6
7
8{% if theme.mermaid.enable %}
  <script src='https://unpkg.com/mermaid@{{ theme.mermaid.version }}/dist/mermaid.min.js'></script>
  <script>
    if (window.mermaid) {
      mermaid.initialize({{ JSON.stringify(theme.mermaid.options) }});
    }
  </script>
{% endif %}
找到模板文件的公共footer文件,添加如上类似代码
问题和优化加载
问题:mermaid 8.2.1的版本流程图只能自动缩放,如果新版本不存在这个问题可以忽略
类似以下问题:https://github.com/knsv/mermaid/issues/838
可以修改源码:1
2
3
4
5
6// - 注释代码
// svg.attr('width', '100%');
// svg.attr('style', `max-width: ${width}px;`);
  
// + 添加代码
svg.attr('width', g.graph().width);
效果如下:超出长度不会强制缩放适应宽度
graph LR
1 --> 2
            2 -->  3
            3 -->  fasdf
            fasdf --> fasdfasdfasdf
            fasdfasdfasdf --> fasdfasdfasdffasdfasdfasdf
            fasdfasdfasdffasdfasdfasdf--> fasdfasdfasdfd
            fasdfasdfasdfd --> fasdfasdfasdfasdfasdf
            fasdfasdfasdfasdfasdf --> fasdfdd 
            fasdfdd --> 10
            10 --> 12
            12 --> 13
            13 --> 14
            14 --> 15
优化:mermaid.js 文件太大可以按需加载
先说下整个流程的实现方式
- 首先插件实现的效果是对 mermaid的元素添加了 mermaid 的class名
 - 然后导入了 mermaid.js 渲染对应的图表(实际上因为js的后执行你会看到一次明显的页面闪动)
 
优化点:
- 未加载的默认样式添加一个默认loading 刷新可见效果
 - 判断页面存在 mermaid 元素再加载js
 
效果如下:
代码如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28/* 修改你的模板css文件,添加全局样式 */
.mermaid {
  max-height: 200px;
  position: relative;
  overflow: hidden;
  &:after {
    content: 'Loading ...';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: #4b5154;
    color: #FFF;
    text-align: center;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
.mermaid[data-processed]{
  max-height: none;
  overflow: auto;
  &:after {
    content: none;
  }
}
1  | // 修改你的模板全局js文件  |