Skip to content

Commit 2a04b3b

Browse files
committed
First
0 parents  commit 2a04b3b

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# documentation-theme-utils
2+
3+
Utilities that help in the process of building theme modules
4+
for documentation.js.

index.js

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
var Syntax = require('doctrine').Syntax,
2+
globalsDocs = require('globals-docs'),
3+
u = require('unist-builder');
4+
5+
function t(text) {
6+
return u('text', text);
7+
}
8+
9+
function link(text) {
10+
var docs = globalsDocs.getDoc(text);
11+
if (docs) {
12+
return u('link', { href: docs }, [u('text', text)]);
13+
}
14+
return u('text', text);
15+
}
16+
17+
function commaList(getNamedLink, items, start, end, sep) {
18+
var res = [];
19+
if (start) {
20+
res.push(t(start));
21+
}
22+
for (var i = 0, iz = items.length; i < iz; ++i) {
23+
res = res.concat(formatType(items[i], getNamedLink));
24+
if (i + 1 !== iz) {
25+
res.push(t(sep || ', '));
26+
}
27+
}
28+
if (end) {
29+
res.push(t(end));
30+
}
31+
return res;
32+
}
33+
34+
function decorate(formatted, str, prefix) {
35+
if (prefix) {
36+
return [t(str)].concat(formatted);
37+
}
38+
return formatted.concat(t(str));
39+
}
40+
41+
/**
42+
* Helper used to format JSDoc-style type definitions into HTML or Markdown.
43+
*
44+
* @name formatType
45+
* @param {Object} node type object in doctrine style
46+
* @param {function(text): text} getNamedLink a function that tries
47+
* to find a URL to point a named link to
48+
* @returns {string} string
49+
* @example
50+
* var x = { type: 'NameExpression', name: 'String' };
51+
* // in template
52+
* // {{ type x }}
53+
* // generates String
54+
*/
55+
function formatType(node, getNamedLink) {
56+
var result = [];
57+
58+
if (!node) {
59+
return [];
60+
}
61+
62+
switch (node.type) {
63+
case Syntax.NullableLiteral:
64+
return [t('?')];
65+
case Syntax.AllLiteral:
66+
return [t('Any')];
67+
case Syntax.NullLiteral:
68+
return [t('null')];
69+
case Syntax.VoidLiteral:
70+
return [t('void')];
71+
case Syntax.UndefinedLiteral:
72+
return [link('undefined')];
73+
case Syntax.NameExpression:
74+
return [link(node.name)];
75+
case Syntax.ParameterType:
76+
return [t(node.name + ': ')].concat(formatType(node.expression, getNamedLink));
77+
78+
case Syntax.TypeApplication:
79+
return formatType(node.expression, getNamedLink)
80+
.concat(commaList(getNamedLink, node.applications, '.&lt;', '&gt;'));
81+
case Syntax.UnionType:
82+
return commaList(getNamedLink, node.elements, '(', ')', '|');
83+
case Syntax.ArrayType:
84+
return commaList(getNamedLink, node.elements, '&#91;', '&#93;');
85+
case Syntax.RecordType:
86+
return commaList(getNamedLink, node.fields, '{', '}');
87+
88+
case Syntax.FieldType:
89+
if (node.value) {
90+
return [t(node.key + ': ')].concat(formatType(node.value, getNamedLink));
91+
}
92+
return [t(node.key)];
93+
94+
case Syntax.FunctionType:
95+
result = [t('function (')];
96+
97+
if (node['this']) {
98+
if (node['new']) {
99+
result.push(t('new: '));
100+
} else {
101+
result.push(t('this: '));
102+
}
103+
104+
result = result.concat(formatType(node['this'], getNamedLink));
105+
106+
if (node.params.length !== 0) {
107+
result.push(t(', '));
108+
}
109+
}
110+
111+
result = result.concat(commaList(getNamedLink, node.params, '', ')'));
112+
113+
if (node.result) {
114+
result = result.concat([t(': ')].concat(formatType(node.result, getNamedLink)));
115+
}
116+
return result;
117+
118+
case Syntax.RestType:
119+
// note that here we diverge from doctrine itself, which
120+
// lets the expression be omitted.
121+
return decorate(formatType(node.expression, getNamedLink), '...', true);
122+
case Syntax.OptionalType:
123+
return decorate(formatType(node.expression, getNamedLink), '=');
124+
case Syntax.NonNullableType:
125+
return decorate(formatType(node.expression, getNamedLink), '!', node.prefix);
126+
case Syntax.NullableType:
127+
return decorate(formatType(node.expression, getNamedLink), '?', node.prefix);
128+
129+
default:
130+
throw new Error('Unknown type ' + node.type);
131+
}
132+
}
133+
134+
module.exports.formatType = formatType;

package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "documentation-theme-utils",
3+
"version": "1.0.0",
4+
"description": "helpers for building themes with documentation.js",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [
10+
"documentation",
11+
"documentationjs",
12+
"theme",
13+
"utils"
14+
],
15+
"author": "Tom MacWright",
16+
"license": "ISC",
17+
"dependencies": {
18+
"doctrine": "^0.7.2",
19+
"globals-docs": "^2.1.0"
20+
}
21+
}

0 commit comments

Comments
 (0)