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

[bug]时序图代码块内输入<tag>时会自动在尾部增加</tag> #12

Open
duquewu opened this issue Jun 28, 2019 · 0 comments
Open

Comments

@duquewu
Copy link

duquewu commented Jun 28, 2019

在 sequence 代码块内输入标签时,会自动在尾部闭合代码快内出现过的标签,如下所示。

Alice->Bob: 1. sendMessage(message:Message<T>)
Bob->Coco: 2. sendMessage(message:Message<T>)

WeChat59708446fbab25be7a8013a908a015f1

看了下 render.js 的源码,应该是 hexo 中标签插件 {% raw %}的默认行为。

data.content += sequences.map(function (code, index) {
  var seqId = getId(index);
  var codeId = seqId + '-code';
  var optionsId = seqId + '-options';
  return '' +
    '{% raw %}' +
    '<textarea id="' + codeId + '" style="display: none">' + code + '</textarea>' +
    '<textarea id="' + optionsId + '" style="display: none">' + JSON.stringify(config.options) + '</textarea>' +
    '<script>' +
    '  var code = document.getElementById("' + codeId + '").value;' +
    '  var options = JSON.parse(decodeURIComponent(document.getElementById("' + optionsId + '").value));' +
    '  var diagram = Diagram.parse(code);' +
    '  diagram.drawSVG("' + seqId + '", options);' +
    '</script>' +
    '{% endraw %}';
}).join('');

解决思路:提前将sequence 代码块中的 < >符号替换为 &lt&gt,代码如下。

 data.content += sequences.map(function (code, index) {
   var seqId = getId(index);
   var codeId = seqId + '-code';
   var optionsId = seqId + '-options';
+ code = code.replace(/</g,'&lt').replace(/>/g,'&gt');
   return '' +
     '{% raw %}' +
     '<textarea id="' + codeId + '" style="display: none">' + code + '</textarea>' +
     '<textarea id="' + optionsId + '" style="display: none">' + JSON.stringify(config.options) + '</textarea>' +
     '<script>' +
     '  var code = document.getElementById("' + codeId + '").value;' +
     '  var options = JSON.parse(decodeURIComponent(document.getElementById("' + optionsId + '").value));' +
     '  var diagram = Diagram.parse(code);' +
     '  diagram.drawSVG("' + seqId + '", options);' +
     '</script>' +
     '{% endraw %}';
 }).join('');

解决后的效果:
WeChat4f2801ae91af9846c558bbb731adc245

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant