-
-
Notifications
You must be signed in to change notification settings - Fork 277
/
index.js
68 lines (54 loc) · 1.51 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { join, dirname } from 'path';
import { go } from '@octolinker/helper-grammar-regex-collection';
import liveResolverQuery from '@octolinker/resolver-live-query';
function goFile({ path, target }) {
const list = [];
const basePath = join(dirname(path), target);
const filename = target.slice(target.lastIndexOf('/') + 1);
list.push(`/${filename}.go`);
list.push('.go');
list.push('');
return list.map((file) => `{BASE_URL}${basePath}${file}`);
}
function githubUrls(url) {
const [, user, repo, ...path] = url.split('/');
if (!path.length) {
return [
`{BASE_URL}/${user}/${repo}/blob/master/${repo}.go`,
`{BASE_URL}/${user}/${repo}`,
];
}
const fullPath = path.join('/');
const last = path.slice(-1);
return [
`{BASE_URL}/${user}/${repo}/blob/master/${fullPath}/${last}.go`,
`{BASE_URL}/${user}/${repo}/tree/master/${fullPath}`,
`{BASE_URL}/${user}/${repo}`,
];
}
export default {
name: 'Go',
resolve(path, [target]) {
const isPath = !!target.match(/^\.\.?[\\|\/]?/);
if (isPath) {
return goFile({ path, target });
}
if (target.startsWith('github.com')) {
return githubUrls(target);
}
return [
`https://${target}`,
`https://golang.org/pkg/${target}`,
liveResolverQuery({ type: 'go', target }),
];
},
getPattern() {
return {
pathRegexes: [/\.go$/, /go\.mod$/],
githubClasses: ['type-go', 'highlight-source-go'],
};
},
getLinkRegexes(blob) {
return go(blob.toString());
},
};