Skip to content

Commit

Permalink
Add JSdocs
Browse files Browse the repository at this point in the history
  • Loading branch information
eGavr committed Dec 13, 2014
1 parent d968f80 commit 5aad4dc
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 7 deletions.
6 changes: 6 additions & 0 deletions lib/defaults.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
var _ = require('lodash');

/**
* Sets options
* @param {Object} [options]
* @param {Number} [options.maxDepth]
* returns {Object}
*/
module.exports = function (options) {
return _.defaults(options || {}, {
maxDepth: 6
Expand Down
19 changes: 19 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
var getToc = require('./toc'),
defaults = require('./defaults');

/**
* Inserts a TOC object into a source
* @param {String} source
* @param {Object} [toc]
* @param {Number} [toc.index]
* @param {String} [toc.data]
* @param {Array} [toc.headers]
* @param {Object} [toc.headers[i]]
* @param {Number} [toc.headers[i].index]
* @param {String} [toc.headers[i].href]
* @returns {String}
*/
function insertToc(source, toc) {
if (toc.index === -1) return source;

Expand All @@ -18,6 +30,13 @@ function insertToc(source, toc) {
return source;
}

/**
* Generates a TOC (Table Of Contents) for a given source from a place where the HTML comment <!-- TOC --> was found
* @param {String} source
* @param {Object} [opts]
* @param {Number} [opts.maxDepth]
* @param {Function} cb
*/
module.exports = function (source, opts, cb) {
var callback,
options;
Expand Down
4 changes: 4 additions & 0 deletions lib/log.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
require('colors');

/**
* Logs which are used in 'cli.js'
* @param {String} source
*/
module.exports = {
successMsg: function (source) {
console.log('The TOC was ' + 'successfully'.bold.green + ' generated for the file ' + source.bold);
Expand Down
12 changes: 12 additions & 0 deletions lib/toc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
var marked = require('marked'),
utils = require('./utils');

/**
* Generates a TOC object
* @param {String} source
* @param {Object} [options]
* @param {Number} [options.maxDepth]
* @returns {Object} [toc]
* @returns {Number} [toc.index]
* @returns {String} [toc.data]
* @returns {Array} [toc.headers]
* @returns {Number} [toc.headers[i].index]
* @returns {String} [toc.headers[i].href]
*/
module.exports = function (source, options) {
var toc = {
index: -1,
Expand Down
35 changes: 28 additions & 7 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
var specialSymbols = new RegExp('[^A-Za-zА-Яа-я0-9_\\- ]', 'g');

/**
* Screens backslashes for special symbols
* @param {String} text
* @returns {String}
*/
function _screenBackSlashes(text) {
text = text.split('');

Expand All @@ -12,10 +17,15 @@ function _screenBackSlashes(text) {
return text.join('');
}

function getHref(text) {
var invalidSymbols = text.match(specialSymbols) || [];
/**
* Returns a href for a given header which should be added in a TOC element
* @param {String} headerText
* @returns {String}
*/
function getHref(headerText) {
var invalidSymbols = headerText.match(specialSymbols) || [];

var href = text.toLowerCase().replace(/( )/g, '-').split('');
var href = headerText.toLowerCase().replace(/( )/g, '-').split('');

for (var i = 0; i < invalidSymbols.length; i++) {
href.splice(href.indexOf(invalidSymbols[i]), 1);
Expand All @@ -26,18 +36,29 @@ function getHref(text) {
return href ? href : 'href';
}

function getIndent(usedHeaders, depth) {
/**
* Returns an indent of a given depth in a TOC
* @param {Array} usedHeaders
* @param {Number} currentDepth
* @return {String}
*/
function getIndent(usedHeaders, currentDepth) {
for (var i = 0; i < usedHeaders.length; i++) {
if (usedHeaders[i].depth < depth) {
if (usedHeaders[i].depth < currentDepth) {
return usedHeaders[i].indent + ' ';
}
}

return '';
}

function getHeaderRegExp(header) {
var screenedHeader = _screenBackSlashes(header);
/**
* Returns a regular expression which matches a given header
* @param {String} headerText
* @returns {RegExp}
*/
function getHeaderRegExp(headerText) {
var screenedHeader = _screenBackSlashes(headerText);

return new RegExp('\\n(#+( )*' + screenedHeader + '( )*#*)|(( )*' + screenedHeader + '\\n(-|=)+' + ')\\n');
}
Expand Down

0 comments on commit 5aad4dc

Please sign in to comment.