Skip to content

Commit 3ff7e54

Browse files
committed
1 parent 3f5808b commit 3ff7e54

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ $ npm install hexo-tag-github-code --save
2626
- URL links to raw file as below
2727
- https://raw.githubusercontent.com/nkmk/hexo-tag-github-code/master/index.js
2828
- https://gist.githubusercontent.com/nkmk/d60cdbcffdb60d624ac01871543f79a2/raw/e7528c00addcdd0b4d00cb9bbd8d225cc132fbfc/sample.py
29+
- URL of github permalink
30+
- https://github.com/nkmk/hexo-tag-github-code/blob/master/index.js#L5-L16
31+
- https://github.com/nkmk/hexo-tag-github-code/blob/master/index.js#L116
2932

3033
### Other site
3134
If URL links to raw file, it will work.

index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,21 @@ function ghcode(args){
6262
var url = args[0]
6363
var options = {}
6464
var start, stop
65+
66+
var groups = url.match(/#(L(\d+))(-L(\d+))?/)
67+
if(groups && groups.length != -1){
68+
if(groups.length = 5){
69+
start = parseInt(groups[2], 10)
70+
stop = parseInt(groups[4], 10)
71+
}else if(groups.length = 3){
72+
start = parseInt(groups[2], 10)
73+
stop = parseInt(groups[2], 10)
74+
}
75+
}
76+
6577
if(typeof(args[1]) == 'string' && args[1].charAt(0) == '{'){
6678
options = str2obj(args[1])
67-
}else{
79+
}else if(start == undefined){
6880
start = args[1]
6981
stop = args[2]
7082
}
@@ -110,7 +122,7 @@ function ghcode(args){
110122
})
111123
})
112124
}else if(url.search(/github.com/) != -1){
113-
raw_url = url.replace(/github.com/, 'raw.githubusercontent.com').replace(/blob\//, '')
125+
raw_url = url.replace(/github.com/, 'raw.githubusercontent.com').replace(/blob\//, '').replace(/#(L(\d+))(-L(\d+))?/, '')
114126
get_code(raw_url, function(data){
115127
resolve(get_result(data, url, raw_url, start, stop, options, codeTag))
116128
})

0 commit comments

Comments
 (0)