UNPKG

1.08 kBJavaScriptView Raw
1const path = require('path');
2const findGit = require('./git/find_git');
3const getGithubURLPrefix = require('./git/url_prefix');
4
5/**
6 * Attempts to link code to its place on GitHub.
7 *
8 * @name linkGitHub
9 * @param {Object} comment parsed comment
10 * @returns {Object} comment with github inferred
11 */
12module.exports = function(comment) {
13 const repoPath = findGit(comment.context.file);
14 const root = repoPath ? path.dirname(repoPath) : '.';
15 const urlPrefix = getGithubURLPrefix(root);
16 const fileRelativePath = comment.context.file
17 .replace(root + path.sep, '')
18 .split(path.sep)
19 .join('/');
20
21 if (urlPrefix) {
22 let startLine;
23 let endLine;
24
25 if (comment.kind == 'typedef') {
26 startLine = comment.loc.start.line;
27 endLine = comment.loc.end.line;
28 } else {
29 startLine = comment.context.loc.start.line;
30 endLine = comment.context.loc.end.line;
31 }
32
33 comment.context.github = {
34 url:
35 urlPrefix + fileRelativePath + '#L' + startLine + '-' + 'L' + endLine,
36 path: fileRelativePath
37 };
38 }
39 return comment;
40};