Skip to content

Commit 4cd1bf8

Browse files
committed
support special character in url
1 parent 5802a2a commit 4cd1bf8

File tree

3 files changed

+42
-27
lines changed

3 files changed

+42
-27
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name := "gitbucket-embedding-plugin"
22
organization := "io.github.gitbucket"
3-
version := "0.1.2"
3+
version := "1.0.0"
44
scalaVersion := "2.13.1"
55
gitbucketVersion := "4.34.0"
Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,83 @@
11
$(function(){
2+
const patternElement1 = '[\\w!\\?\\+\\-_~=;\\.,\\*&@#\\$\\%\\(\\)\'\\[\\]]+';
3+
const patternElement2 = '[\\w!\\?\\+\\-_~=;\\.,\\*&@\\$\\%\\(\\)\'\\[\\]]+';
4+
const matchPattern = new RegExp(
5+
'^'+
6+
location.origin +
7+
'(/' + patternElement1 + ')?' +
8+
'/(' + patternElement1 + ')' +
9+
'/(' + patternElement1 + ')' +
10+
'/blob' +
11+
'/(' + patternElement1 + ')' +
12+
'/(' + patternElement2 + ')#L([\\d]+)-?L?([\\d]+)?$');
213

314
function generateSnippetElement(repo, filename, commit, lineStartNum, lineEndNum, content, url, commitUrl){
415
return [
5-
"<style>.prettyprint ol{word-wrap:normal;} .prettyprint ol.linenums > li {list-style-type:decimal;}</style>",
16+
"<style>",
17+
".prettyprint ol {word-wrap:normal;} ",
18+
".prettyprint ol.linenums > li {list-style-type:decimal; background-color: white;} ",
19+
".embedded-snippet {background-color: white;}",
20+
"</style>",
621
"<div class='panel panel-default'>",
7-
"<div class='panel-heading' style='font-size:12px; padding:5px; padding-left:15px;'>",
22+
"<div class='panel-heading' style='font-size:12px; padding: 5px 5px 5px 15px;'>",
823
"<span><b><a href="+ url + ">" + repo + "/" + filename + "</a></b></span><br>",
924
"<span style='color: gray'>Lines " + lineStartNum + " to " + lineEndNum + " in </span>",
1025
"<span style='font-size:11px;'><a href=" + commitUrl + ">" + commit + "</a></span>",
1126
"</div>",
1227
"<div class='panel-body' style='padding:0; background-color:white;'>",
13-
"<pre class='prettyprint linenums:" + lineStartNum + "' style='padding-left:15px; margin-bottom:0;'>",
28+
"<pre class='embedded-snippet prettyprint linenums:" + lineStartNum + "' style='padding-left:15px; margin-bottom:0;'>",
1429
content,
1530
"</pre>",
1631
"</div>",
1732
"</div>"
1833
].join("");
19-
};
34+
}
2035

2136
function fixUrl(url){
2237
return url.replace("/blob/", "/raw/");
23-
};
38+
}
2439

2540
function getCommitUrl(url, filepath){
26-
url = url.replace("/blob/", "/commit/")
27-
reg = new RegExp('/' + filepath + '#L\\d+(-L\\d+)?$')
28-
return url.replace(reg, "")
29-
};
41+
url = url.replace("/blob/", "/commit/");
42+
let reg = new RegExp('/' + filepath + '#L\\d+(-L\\d+)?$');
43+
return url.replace(reg, "");
44+
}
3045

3146
function getContent(url){
32-
var xmlHttp;
47+
let xmlHttp;
3348
xmlHttp = new XMLHttpRequest();
3449
xmlHttp.open("GET", fixUrl(url), false);
3550
xmlHttp.send(null);
3651
if(xmlHttp.responseURL.includes(location.host + "/signin?redirect")){
37-
throw new Error("XMLHttpRequest error")
52+
throw new Error("XMLHttpRequest error");
3853
}
39-
if(xmlHttp.status != 200){
40-
throw new Error("XMLHttpRequest error")
54+
if(xmlHttp.status !== 200){
55+
throw new Error("XMLHttpRequest error");
4156
}
42-
return xmlHttp.responseText
43-
};
57+
return xmlHttp.responseText;
58+
}
4459

4560
function convertLinks(){
46-
var matchPattern = new RegExp('https?://' + location.host + '(/[\\w-\\.]+)?/([\\w-\\.]+)/([\\w-\\.]+)/blob/([\\w-\\.]+)/([\\w-\\./]+)#L([0-9]+)-?L?([0-9]+)?$');
47-
var elements = $('.markdown-body p a');
48-
var element;
49-
var url;
50-
var mat;
61+
const elements = $('.markdown-body p a');
62+
let element;
63+
let url;
64+
let mat;
5165
for(let i = 0; i< elements.length; ++i ) {
5266
element = elements[i];
5367
url = element.innerText;
5468
mat = url.match(matchPattern);
5569
if(mat !== null){
56-
let urlPrefix = mat[1];
57-
let owner = mat[2];
70+
// let urlPrefix = mat[1];
71+
// let owner = mat[2];
5872
let repo = mat[3];
5973
let commit = mat[4];
6074
let filepath = mat[5];
6175
let startLine = Number(mat[6]);
6276
let endLine = startLine;
6377
if(typeof mat[7] !== "undefined"){
6478
endLine = Number(mat[7]);
65-
};
66-
let commitUrl = getCommitUrl(url, filepath)
79+
}
80+
let commitUrl = getCommitUrl(url, filepath);
6781
try{
6882
let content = getContent(url);
6983
let linesAll = content.split("\n");
@@ -75,6 +89,6 @@ $(function(){
7589
}
7690
}
7791
prettyPrint();
78-
};
92+
}
7993
convertLinks();
8094
})

src/main/scala/Plugin.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ class Plugin extends gitbucket.core.plugin.Plugin {
1010
override val versions: List[Version] = List(
1111
new Version("0.1.0"),
1212
new Version("0.1.1"),
13-
new Version("0.1.2")
13+
new Version("0.1.2"),
14+
new Version("1.0.0")
1415
)
1516

1617
override val assetsMappings: Seq[(String, String)] = Seq("/embedding" -> "/embedding/assets")

0 commit comments

Comments
 (0)