Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ $ npm install hexo-tag-github-code --save
- URL links to raw file as below
- https://raw.githubusercontent.com/nkmk/hexo-tag-github-code/master/index.js
- https://gist.githubusercontent.com/nkmk/d60cdbcffdb60d624ac01871543f79a2/raw/e7528c00addcdd0b4d00cb9bbd8d225cc132fbfc/sample.py
- URL of github permalink
- https://github.com/nkmk/hexo-tag-github-code/blob/master/index.js#L5-L16
- https://github.com/nkmk/hexo-tag-github-code/blob/master/index.js#L116

### Other site
If URL links to raw file, it will work.
Expand All @@ -36,6 +39,7 @@ Set like as `{cap:false,re:true}`. Please do __NOT__ insert any spaces.
| :--- | :--- | :--- |
| cap| `true`: show caption<br> `false`: no caption | `true` |
| re| `true`: the first line number restart from 1<br> `false`: the first line number start from original code number | `false` |
| link| link name of url | `link` |

### Config setting

Expand All @@ -45,6 +49,7 @@ You can change default settings in `_config.yml` as below.
github_code:
cap: false
re: true
link: 'link'
```


Expand Down
21 changes: 17 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function get_result(data, url, raw_url, start, stop, options, codeTag){
var basename = path.basename(raw_url)
var arg
if(options['cap']){
arg = [basename, 'lang:' + ext, url]
arg = [basename, 'lang:' + ext, url, options['link']]
}else{
arg = ['lang:' + ext]
}
Expand All @@ -52,9 +52,21 @@ function ghcode(args){
var url = args[0]
var options = {}
var start, stop

var groups = url.match(/#(L(\d+))(-L(\d+))?/)
if(groups && groups.length != -1){
if(groups.length = 5){
start = parseInt(groups[2], 10)
stop = parseInt(groups[4], 10)
}else if(groups.length = 3){
start = parseInt(groups[2], 10)
stop = parseInt(groups[2], 10)
}
}

if(typeof(args[1]) == 'string' && args[1].charAt(0) == '{'){
options = str2obj(args[1])
}else{
}else if(start == undefined){
start = args[1]
stop = args[2]
}
Expand All @@ -79,7 +91,8 @@ function ghcode(args){

options = assign({
cap: cap_default,
re: re_default
re: re_default,
link: 'link'
}, options);

var raw_url
Expand All @@ -100,7 +113,7 @@ function ghcode(args){
})
})
}else if(url.search(/github.com/) != -1){
raw_url = url.replace(/github.com/, 'raw.githubusercontent.com').replace(/blob\//, '')
raw_url = url.replace(/github.com/, 'raw.githubusercontent.com').replace(/blob\//, '').replace(/#(L(\d+))(-L(\d+))?/, '')
get_code(raw_url, function(data){
resolve(get_result(data, url, raw_url, start, stop, options, codeTag))
})
Expand Down