Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 11, 2018
1 parent 0f798ca commit 1689ec6
Show file tree
Hide file tree
Showing 23 changed files with 546 additions and 574 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
@@ -0,0 +1,3 @@
coverage/
remark-github.js
remark-github.min.js
4 changes: 2 additions & 2 deletions index.js
@@ -1,2 +1,2 @@
'use strict';
module.exports = require('./lib/index.js');
'use strict'
module.exports = require('./lib')
127 changes: 64 additions & 63 deletions lib/index.js
@@ -1,25 +1,25 @@
'use strict';
'use strict'

var visit = require('unist-util-visit');
var parse = require('./util/parse-link');
var abbr = require('./util/abbreviate');
var repo = require('./tokenizer/repo');
var mention = require('./tokenizer/mention');
var issue = require('./tokenizer/issue');
var hash = require('./tokenizer/hash');
var visit = require('unist-util-visit')
var parse = require('./util/parse-link')
var abbr = require('./util/abbreviate')
var repo = require('./tokenizer/repo')
var mention = require('./tokenizer/mention')
var issue = require('./tokenizer/issue')
var hash = require('./tokenizer/hash')

module.exports = github;
module.exports = github

/* Hide process use from browserify. */
var proc = typeof global !== 'undefined' && global.process;
var proc = typeof global !== 'undefined' && global.process

/* Load `fs` and `path` if available. */
var fs;
var path;
var fs
var path

try {
fs = require('fs');
path = require('path');
fs = require('fs')
path = require('path')
} catch (err) {}

/* Username may only contain alphanumeric characters or
Expand All @@ -30,113 +30,114 @@ try {
*
* https://github.com/blog/1121-introducing-team-mentions
*/
var NAME = '(?:[a-z0-9]{1,2}|[a-z0-9][a-z0-9-]{1,37}[a-z0-9])';
var USER = '(' + NAME + ')';
var PROJECT = '((?:[_A-Za-z0-9-]|\\.git[_A-Za-z0-9-]|\\.(?!git))+)';
var REPO = USER + '\\/' + PROJECT;
var NAME = '(?:[a-z0-9]{1,2}|[a-z0-9][a-z0-9-]{1,37}[a-z0-9])'
var USER = '(' + NAME + ')'
var PROJECT = '((?:[_A-Za-z0-9-]|\\.git[_A-Za-z0-9-]|\\.(?!git))+)'
var REPO = USER + '\\/' + PROJECT

/* Match a repo from a git / github URL. */
var REPOSITORY = new RegExp('(?:^|/(?:repos/)?)' + REPO + '(?=\\.git|[\\/#@]|$)', 'i');
var REPOSITORY = new RegExp(
'(?:^|/(?:repos/)?)' + REPO + '(?=\\.git|[\\/#@]|$)',
'i'
)

function github(options) {
var settings = options || {};
var repository = settings.repository;
var proto = this.Parser.prototype;
var scope = proto.inlineTokenizers;
var methods = proto.inlineMethods;
var pack;
var settings = options || {}
var repository = settings.repository
var proto = this.Parser.prototype
var scope = proto.inlineTokenizers
var methods = proto.inlineMethods
var pack

/* Get the repository from `package.json`. */
if (!repository) {
try {
pack = JSON.parse(fs.readFileSync(path.join(proc.cwd(), 'package.json')));
pack = JSON.parse(fs.readFileSync(path.join(proc.cwd(), 'package.json')))
} catch (err) {
pack = {};
pack = {}
}

if (pack.repository) {
repository = pack.repository.url || pack.repository;
repository = pack.repository.url || pack.repository
} else {
repository = '';
repository = ''
}
}

/* Parse the URL: See the tests for all possible kinds. */
repository = REPOSITORY.exec(repository);
repository = REPOSITORY.exec(repository)

REPOSITORY.lastIndex = 0;
REPOSITORY.lastIndex = 0

if (!repository) {
throw new Error('Missing `repository` field in `options`');
throw new Error('Missing `repository` field in `options`')
}

repository = {user: repository[1], project: repository[2]};
repository = {user: repository[1], project: repository[2]}

/* Add helpers. */
proto.githubRepo = repository;
proto.githubOptions = settings;
proto.githubRepo = repository
proto.githubOptions = settings

/* Add tokenizers to the `Parser`. */
scope.mention = mention;
scope.issue = issue;
scope.hash = hash;
scope.repoReference = repo;
scope.mention = mention
scope.issue = issue
scope.hash = hash
scope.repoReference = repo

/* Specify order (just before `inlineText`). */
methods.splice(methods.indexOf('inlineText'), 0,
methods.splice(
methods.indexOf('inlineText'),
0,
'mention',
'issue',
'hash',
'repoReference'
);
)

return transformer;
return transformer

function transformer(tree) {
visit(tree, 'link', visitor);
visit(tree, 'link', visitor)
}

function visitor(node) {
var link = parse(node);
var children;
var base;
var comment;
var link = parse(node)
var children
var base
var comment

if (!link) {
return;
return
}

comment = link.comment ? ' (comment)' : '';
comment = link.comment ? ' (comment)' : ''

if (link.project !== repository.project) {
base = link.user + '/' + link.project;
base = link.user + '/' + link.project
} else if (link.user === repository.user) {
base = '';
base = ''
} else {
base = link.user;
base = link.user
}

if (link.page === parse.COMMIT) {
children = [];
children = []

if (base) {
children.push({type: 'text', value: base + '@'});
children.push({type: 'text', value: base + '@'})
}

children.push({type: 'inlineCode', value: abbr(link.reference)});
children.push({type: 'inlineCode', value: abbr(link.reference)})

if (link.comment) {
children.push({type: 'text', value: comment});
children.push({type: 'text', value: comment})
}
} else {
base += '#';

children = [{
type: 'text',
value: base + abbr(link.reference) + comment
}];
base += '#'
children = [{type: 'text', value: base + abbr(link.reference) + comment}]
}

node.children = children;
node.children = children
}
}
12 changes: 6 additions & 6 deletions lib/locator/mention.js
@@ -1,16 +1,16 @@
'use strict';
'use strict'

module.exports = mention;
module.exports = mention

var repo = require('../util/repo-character');
var repo = require('../util/repo-character')

/* Find a possible mention. */
function mention(value, fromIndex) {
var index = value.indexOf('@', fromIndex);
var index = value.indexOf('@', fromIndex)

if (index !== -1 && repo(value.charCodeAt(index - 1))) {
return mention(value, index + 1);
return mention(value, index + 1)
}

return index;
return index
}
40 changes: 20 additions & 20 deletions lib/locator/repo.js
@@ -1,52 +1,52 @@
'use strict';
'use strict'

module.exports = locateRepoReference;
module.exports = locateRepoReference

var hexadecimal = require('is-hexadecimal');
var decimal = require('is-decimal');
var repoCharacter = require('../util/repo-character');
var hexadecimal = require('is-hexadecimal')
var decimal = require('is-decimal')
var repoCharacter = require('../util/repo-character')

/* Find a possible reference. */
function locateRepoReference(value, fromIndex) {
var hash = value.indexOf('@', fromIndex);
var issue = value.indexOf('#', fromIndex);
var index;
var start;
var test;
var hash = value.indexOf('@', fromIndex)
var issue = value.indexOf('#', fromIndex)
var index
var start
var test

if (hash === -1) {
index = issue;
index = issue
} else if (issue === -1) {
index = hash;
index = hash
} else {
index = (hash > issue ? issue : hash);
index = hash > issue ? issue : hash
}

start = index;
start = index

if (start === -1) {
return index;
return index
}

while (index >= fromIndex) {
if (!repoCharacter(value.charCodeAt(index - 1))) {
break;
break
}

index--;
index--
}

if (index < start && index >= fromIndex) {
test = start === hash ? hexadecimal : decimal;
test = start === hash ? hexadecimal : decimal

if (
test(value.charCodeAt(start + 1)) &&
!repoCharacter(value.charCodeAt(index - 1))
) {
return index;
return index
}
}

/* Find the next possible value. */
return locateRepoReference(value, start + 1);
return locateRepoReference(value, start + 1)
}

0 comments on commit 1689ec6

Please sign in to comment.