diff --git a/assets/plugin.js b/assets/plugin.js index 555d724..87173f1 100644 --- a/assets/plugin.js +++ b/assets/plugin.js @@ -47,9 +47,26 @@ require(['gitbook', 'jQuery'], function(gitbook, $) { } if (lines.length > 1) { - console.log(lines); - lines = lines.map(line => '' + line + ''); - console.log(lines); + var tagEnd = []; + var tagStart = []; + lines = lines.map(line => { + if(/^\s*<[a-zA-Z]+/.test(line)) { + if(!line.match(/<([a-zA-Z]+)(\s\w+=.+)*>.*?<\/\1>/)) { + var m = line.match(/<([a-zA-Z]+)?(\s\w+=.+)*>.*?/); + tagStart.push(m[0]); + tagEnd.push(''); + line = line + tagEnd[tagEnd.length - 1]; + } + } else { + if(/<\/[a-zA-Z]+>\s*$/.test(line)) { + tagEnd.pop(); + line = (tagStart.pop() || '') + line; + } else { + line = (tagStart[tagStart.length - 1] || '') + line + (tagEnd[tagEnd.length - 1] || ''); + } + } + return '' + line + ''; + }); code.html(lines.join('\n')); }