Skip to content

Commit 87e8c26

Browse files
authored
Merge pull request #5 from kaz-on/fixes
Support comment preview and some fixes
2 parents 1e36263 + f902188 commit 87e8c26

File tree

1 file changed

+21
-3
lines changed
  • src/main/resources/embedding/assets

1 file changed

+21
-3
lines changed

src/main/resources/embedding/assets/entry.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ $(function(){
2525
"<span style='font-size:11px;'><a href=" + commitUrl + ">" + commit + "</a></span>",
2626
"</div>",
2727
"<div class='panel-body' style='padding:0; background-color:white;'>",
28-
"<pre class='embedded-snippet prettyprint linenums:" + lineStartNum + "' style='padding-left:15px; margin-bottom:0;'>",
28+
"<pre class='embedded-snippet prettyprint linenums:" + lineStartNum + "' data-filename='" + filename + "' style='padding-left:15px; margin-bottom:0;'>",
2929
content,
3030
"</pre>",
3131
"</div>",
@@ -57,6 +57,10 @@ $(function(){
5757
return xmlHttp.responseText;
5858
}
5959

60+
function escapeHtml(text) {
61+
return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;");
62+
}
63+
6064
function convertLinks(){
6165
const elements = $('.markdown-body p');
6266
let element;
@@ -80,16 +84,30 @@ $(function(){
8084
}
8185
let commitUrl = getCommitUrl(url, filepath);
8286
try{
87+
filepath = decodeURIComponent(filepath);
88+
filepath = escapeHtml(filepath);
8389
let content = getContent(url);
84-
let linesAll = content.split("\n");
90+
let linesAll = content.split(/\n|\r\n?/);
8591
let lines = linesAll.slice(startLine-1, endLine).join("\n");
92+
lines = escapeHtml(lines);
8693
let snippetElement = generateSnippetElement(repo, filepath, commit, startLine, endLine, lines, url, commitUrl);
8794
element.insertAdjacentHTML('afterend', snippetElement);
8895
element.remove();
8996
}catch(e){}
9097
}
9198
}
92-
prettyPrint();
9399
}
100+
101+
// Initial processing
94102
convertLinks();
103+
prettyPrint();
104+
105+
setTimeout(function(){ // Delayed execution
106+
// Create a hook for prettyPrint function to support preview
107+
const original = prettyPrint;
108+
prettyPrint = function(){
109+
convertLinks();
110+
return original.apply(this, arguments);
111+
}
112+
});
95113
})

0 commit comments

Comments
 (0)