Passed
Push — master ( 247477...5b3840 )
by Björn
02:52
created

lib/handlebars/formatting.js (3 issues)

1 1
var escape     = require('../util/escape');
2 1
var handlebars = require('handlebars');
3 1
var heading    = require('../util/writeHeading');
4 1
var markdown   = require('../vendor/markdown-it');
5
6
/**
7
 * Converts Markdown to HTML.
8
 * @param {string} text - Markdown string.
9
 * @returns {string} Converted HTML.
10
 */
11 1
handlebars.registerHelper('md', function(text) {
12 1
  return new handlebars.SafeString(markdown.render(text));
13
});
14
15
/**
16
 * Renders an HTML heading with a clickable anchor link.
17
 * @param {integer} level - The level of the heading, 1 through 6.
18
 * @param {string=} anchor - Anchor to use in the ID of the heading. If not specified, the anchor will be the escaped text of the heading.
19
 * @returns {string} HTML heading with an anchor link inside.
20
 */
21 1
handlebars.registerHelper('heading', function(level, anchor, options) {
22
  // Allow for optional second parameter
23 4
  if (typeof anchor === 'object') {
24 1
    options = anchor;
25 1
    anchor = options.fn(this);
26
  }
27
28 2
  return heading(options.fn(this), level, anchor);
29
});
30
31
/**
32
 * Escapes a string for use in a URL hash.
33
 * @param {string} text - String to escape.
34
 */
35 1
handlebars.registerHelper('escape', escape);
36
37
/**
38
 * Capitalizes the first letter of a string.
39
 * @param {string} text - Text to convert.
0 ignored issues
show
The parameter text does not exist. Did you maybe forget to remove this comment?
Loading history...
40
 * @returns {string} A capitalized string.
41
 */
42 1
handlebars.registerHelper('toUpper', function(str) {
43 2
  if (typeof str !== 'string') {
44 1
	  return '';
45
  }
46 1
  return str[0].toUpperCase() + str.slice(1);
47
});
48
49
/**
50
 * Converts a string to lowercase.
51
 * @param {string} text - Text to convert.
0 ignored issues
show
The parameter text does not exist. Did you maybe forget to remove this comment?
Loading history...
52
 * @returns {string} A lowercase string.
53
 */
54 1
handlebars.registerHelper('toLower', function(str) {
55 2
  if (typeof str !== 'string') {
56 1
	  return '';
57
  }
58 1
  return str.toLowerCase();
59
});
60
61
/**
62
 * Render a raw string which will not be parsed by Handlebars. This is used on any page that has actual Handlebars code samples. To use this helper, call it with a set of four curly braces instead of two, and no hash:
63
 * ```Handlebars
64
 * {{{{raw}}}}
65
 * {{{{/raw}}}}
66
 * ```
67
 *
68
 * @param {object} content - Handlebars context.
69
 * @returns {string} The content inside of the block helper, ignored by Handlebars.
70
 */
71 1
handlebars.registerHelper('raw', function(content) {
72 1
  return content.fn(this);
73
});
74
75
/**
76
 * Determine if a Sass or JavaScript item should be displayed. Private items, and items that are aliases, are not shown.
77
 * @param {object} item - Item to check the visibility of.
78
 * @returns {string} If the item can be shown, the content inside the block helper.
79
 */
80 1
handlebars.registerHelper('filter', function(item, options) {
81 4
  if (item.access === 'private' || item.alias) return '';
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
82 1
  return options.fn(this);
83
});
84
85
/**
86
 * Render a JSON string from an Object. Useful mostly for debug.
87
 * @param {object} value - Object to display in a JSON format.
88
 * @returns {string} JSON formatted string.
89
 */
90 1
handlebars.registerHelper('formatJson', function(value) {
91 1
    return JSON.stringify(value);
92
});
93