1 | 1 | var format = require('string-template'); |
|
2 | 1 | var handlebars = require('handlebars'); |
|
3 | 1 | var hljs = require('highlight.js'); |
|
4 | |||
5 | /** |
||
6 | * Formats a mixin using a SassDoc mixin object to look like this: |
||
7 | * ```scss |
||
8 | * @include mixinName($param, $param) { } |
||
9 | * ``` |
||
10 | * |
||
11 | * @param {object} mixin - Mixin data, taken from a SassDoc object. |
||
12 | * @returns A formatted code sample. |
||
13 | */ |
||
14 | 1 | handlebars.registerHelper('writeSassMixin', function(mixin) { |
|
15 | 3 | var name = mixin['context']['name']; |
|
16 | 3 | var params = mixin['parameter']; |
|
17 | |||
18 | 3 | var str = '@include '; |
|
19 | 3 | str += name; |
|
20 | |||
21 | 3 | if (params) str += '('; |
|
22 | |||
23 | 3 | for (var i in params) { |
|
0 ignored issues
–
show
|
|||
24 | 2 | str += '$' + params[i]['name'] + ', '; |
|
25 | } |
||
26 | |||
27 | 3 | if (params) str = str.slice(0, -2) + ')'; |
|
28 | |||
29 | 3 | if (typeof mixin.content === 'string') { |
|
30 | 1 | str += ' { }'; |
|
31 | } |
||
32 | else { |
||
33 | 2 | str += ';' |
|
34 | } |
||
35 | |||
36 | 3 | str = hljs.highlight('scss', str).value; |
|
37 | |||
38 | 3 | return new handlebars.SafeString(str); |
|
39 | }); |
||
40 | |||
41 | /** |
||
42 | * Formats a function using a SassDoc function object to look like this: |
||
43 | * ```scss |
||
44 | * function($param, $param) |
||
45 | * ``` |
||
46 | * |
||
47 | * @param {object} func - Function data, taken from a SassDoc object. |
||
48 | * @returns {string} A formatted code sample. |
||
49 | */ |
||
50 | 1 | handlebars.registerHelper('writeSassFunction', function(func) { |
|
51 | 2 | var name = func['context']['name']; |
|
52 | 2 | var params = func['parameter']; |
|
53 | |||
54 | 2 | var str = ''; |
|
55 | 2 | str += name + '('; |
|
56 | |||
57 | 2 | for (var i in params) { |
|
0 ignored issues
–
show
A for in loop automatically includes the property of any prototype object, consider checking the key using
hasOwnProperty .
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: var someObject;
for (var key in someObject) {
if ( ! someObject.hasOwnProperty(key)) {
continue; // Skip keys from the prototype.
}
doSomethingWith(key);
}
Loading history...
|
|||
58 | 2 | str += '$' + params[i]['name'] + ', '; |
|
59 | } |
||
60 | 2 | if (params) str = str.slice(0, -2); |
|
61 | 2 | str += ')'; |
|
62 | |||
63 | 2 | str = hljs.highlight('scss', str).value; |
|
64 | |||
65 | 2 | return new handlebars.SafeString(str); |
|
66 | }); |
||
67 | |||
68 | /** |
||
69 | * Formats a variable declaration using a SassDoc variable object to look like this: |
||
70 | * ```scss |
||
71 | * $name: $value; |
||
72 | * ``` |
||
73 | * |
||
74 | * @param {object} variable - Variable data, taken from a SassDoc object. |
||
75 | * @returns {string} A formatted code sample. |
||
76 | */ |
||
77 | 1 | handlebars.registerHelper('writeSassVariable', function(variable) { |
|
78 | 1 | var name = variable['context']['name']; |
|
79 | 1 | var value = variable['context']['value']; |
|
80 | 1 | var str = '$' + name + ': ' + value + ';'; |
|
81 | 1 | str = hljs.highlight('scss', str).value; |
|
82 | |||
83 | 1 | return new handlebars.SafeString(str); |
|
84 | }); |
||
85 | |||
86 | /** |
||
87 | * Formats SassDoc "type" definitions to read "x or y or z" |
||
88 | * @param {string[]} types - Array of types. |
||
89 | * @returns {string} A formatted string. |
||
90 | */ |
||
91 | 1 | handlebars.registerHelper('formatSassTypes', function(types) { |
|
92 | 3 | if (typeof types === 'undefined') return ''; |
|
93 | |||
94 | 2 | var types = types.replace(' ', '').split('|'); |
|
95 | 2 | var output = ''; |
|
96 | |||
97 | 2 | for (var i in types) { |
|
0 ignored issues
–
show
A for in loop automatically includes the property of any prototype object, consider checking the key using
hasOwnProperty .
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: var someObject;
for (var key in someObject) {
if ( ! someObject.hasOwnProperty(key)) {
continue; // Skip keys from the prototype.
}
doSomethingWith(key);
}
Loading history...
|
|||
98 | 3 | output += types[i] + ' or '; |
|
99 | } |
||
100 | |||
101 | 2 | return output.slice(0, -4); |
|
102 | }); |
||
103 | |||
104 | /** |
||
105 | * Format a Sass value: |
||
106 | * - For basic values, return as-is. |
||
107 | * - For maps, render each item in the map on its own line. |
||
108 | * - For undefined values, return "None" |
||
109 | * @param {string} value - Sass value to format. |
||
110 | * @returns {string} A formatted value. |
||
111 | */ |
||
112 | 1 | handlebars.registerHelper('formatSassValue', function(value) { |
|
113 | 3 | if (typeof value === 'undefined') return '<span style="color: #999;">None</span>'; |
|
114 | |||
115 | 4 | if (value[0] === '(' && value[value.length - 1] === ')') { |
|
116 | 1 | value = value.slice(1, -1).split(',').join('<br>'); |
|
117 | } |
||
118 | |||
119 | 2 | return value; |
|
120 | }); |
||
121 | |||
122 | // Adds an external link, pulled from a SassDock @link annotation. |
||
123 | 1 | handlebars.registerHelper('writeSassLink', function(link) { |
|
124 | 2 | if (!link) return ''; |
|
125 | |||
126 | 1 | var output = format('<p><strong>Learn more:</strong> <a href="{0}">{1}</a></p>', [link[0].url, link[0].caption]); |
|
127 | |||
128 | 1 | return new handlebars.SafeString(output); |
|
129 | }); |
||
130 |
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: