documentation/lib/github.js
Tom MacWright 25152edeb9 style(prettier): Use prettier for code formatting (#710)
* style(prettier): Use prettier for code formatting

This saves us style issues. Also adds husky and lint-staged for pre-commit testing

Refs https://github.com/documentationjs/documentation/issues/709
2017-04-10 14:25:45 -04:00

38 lines
930 B
JavaScript

'use strict';
/* @flow */
var path = require('path');
var findGit = require('../lib/git/find_git');
var getGithubURLPrefix = require('../lib/git/url_prefix');
/**
* Attempts to link code to its place on GitHub.
*
* @name linkGitHub
* @param {Object} comment parsed comment
* @return {Object} comment with github inferred
*/
module.exports = function(comment /*: Comment*/) {
var repoPath = findGit(comment.context.file);
var root = repoPath ? path.dirname(repoPath) : '.';
var urlPrefix = getGithubURLPrefix(root);
var fileRelativePath = comment.context.file
.replace(root + path.sep, '')
.split(path.sep)
.join('/');
if (urlPrefix) {
comment.context.github = {
url: urlPrefix +
fileRelativePath +
'#L' +
comment.context.loc.start.line +
'-' +
'L' +
comment.context.loc.end.line,
path: fileRelativePath
};
}
return comment;
};