@@ -25,7 +25,7 @@ $(function(){
25
25
"<span style='font-size:11px;'><a href=" + commitUrl + ">" + commit + "</a></span>" ,
26
26
"</div>" ,
27
27
"<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;'>",
29
29
content ,
30
30
"</pre>" ,
31
31
"</div>" ,
@@ -57,6 +57,10 @@ $(function(){
57
57
return xmlHttp . responseText ;
58
58
}
59
59
60
+ function escapeHtml ( text ) {
61
+ return text . replace ( / & / g, "&" ) . replace ( / < / g, "<" ) . replace ( / > / g, ">" ) . replace ( / " / g, """ ) . replace ( / ' / g, "'" ) ;
62
+ }
63
+
60
64
function convertLinks ( ) {
61
65
const elements = $ ( '.markdown-body p' ) ;
62
66
let element ;
@@ -80,16 +84,30 @@ $(function(){
80
84
}
81
85
let commitUrl = getCommitUrl ( url , filepath ) ;
82
86
try {
87
+ filepath = decodeURIComponent ( filepath ) ;
88
+ filepath = escapeHtml ( filepath ) ;
83
89
let content = getContent ( url ) ;
84
- let linesAll = content . split ( "\n" ) ;
90
+ let linesAll = content . split ( / \n | \r \n ? / ) ;
85
91
let lines = linesAll . slice ( startLine - 1 , endLine ) . join ( "\n" ) ;
92
+ lines = escapeHtml ( lines ) ;
86
93
let snippetElement = generateSnippetElement ( repo , filepath , commit , startLine , endLine , lines , url , commitUrl ) ;
87
94
element . insertAdjacentHTML ( 'afterend' , snippetElement ) ;
88
95
element . remove ( ) ;
89
96
} catch ( e ) { }
90
97
}
91
98
}
92
- prettyPrint ( ) ;
93
99
}
100
+
101
+ // Initial processing
94
102
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
+ } ) ;
95
113
} )
0 commit comments