Skip to content

Commit b465902

Browse files
committed
Add support for example captions
Fixes #110
1 parent 224ade5 commit b465902

14 files changed

+61
-22
lines changed

lib/flatten.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ var flatteners = {
8080
if (!result.examples) {
8181
result.examples = [];
8282
}
83-
result.examples.push(tag.description);
83+
result.examples.push(tag);
8484
},
8585
'global': function (result) {
8686
result.scope = 'global';

lib/output/markdown_ast.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,13 @@ function commentsToAST(comments, opts, callback) {
8383

8484
function examplesSection(comment) {
8585
return !!comment.examples && [u('strong', [u('text', 'Examples')])]
86-
.concat(comment.examples.map(function (example) {
87-
language = hljsOptions.highlightAuto ? hljs.highlightAuto(example).language : 'javascript';
88-
return u('code', { lang: language }, example);
89-
}));
86+
.concat(comment.examples.reduce(function (memo, example) {
87+
language = hljsOptions.highlightAuto ?
88+
hljs.highlightAuto(example.description).language : 'javascript';
89+
return memo.concat(example.caption ?
90+
[u('paragraph', [u('emphasis', [u('text', example.caption)])])] :
91+
[]).concat([u('code', { lang: language }, example.description)]);
92+
}, []));
9093
}
9194

9295
function returnsSection(comment) {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
"concat-stream": "^1.5.0",
2121
"debounce": "^1.0.0",
2222
"disparity": "^2.0.0",
23-
"doctrine": "^0.7.1",
24-
"documentation-theme-default": "2.2.3",
23+
"doctrine": "^1.1.0",
24+
"documentation-theme-default": "3.0.0-beta",
2525
"documentation-theme-utils": "^1.2.1",
2626
"events": "^1.1.0",
2727
"extend": "^3.0.0",

test/fixture/_multi-file-input.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@
7575
}
7676
],
7777
"examples": [
78-
"var result = returnTwo(4);\n// result is 6"
78+
{
79+
"title": "example",
80+
"description": "var result = returnTwo(4);\n// result is 6",
81+
"lineNumber": 5
82+
}
7983
],
8084
"name": "returnTwo",
8185
"kind": "function",

test/fixture/empty-example.output.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"tags": [
55
{
66
"title": "example",
7-
"description": null,
7+
"description": "",
88
"lineNumber": 3
99
}
1010
],
@@ -33,7 +33,11 @@
3333
},
3434
"errors": [],
3535
"examples": [
36-
null
36+
{
37+
"title": "example",
38+
"description": "",
39+
"lineNumber": 3
40+
}
3741
],
3842
"name": "returnTwo",
3943
"kind": "function",

test/fixture/empty-example.output.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ This function returns the number plus two.
55
**Examples**
66

77
```javascript
8-
undefined
8+
99
```

test/fixture/empty-example.output.md.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
},
5454
{
5555
"lang": "javascript",
56-
"type": "code"
56+
"type": "code",
57+
"value": ""
5758
}
5859
]
5960
}

test/fixture/html/nested.input.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function Klass(foo) {
1111
/**
1212
* Get this Klass's foo
1313
* @returns {Number} foo
14-
* @example
14+
* @example <caption>this shows you how to getFoo</caption>
1515
* foo.getFoo();
1616
*/
1717
Klass.prototype.getFoo = function () {

test/fixture/html/nested.output.files

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,7 +1851,8 @@ like a <a href="#Klass">klass</a></p>
18511851

18521852
</div>
18531853
<h4>Examples</h4>
1854-
<pre class='overflow-auto'><span class="hljs-keyword">var</span> k = <span class="hljs-keyword">new</span> Klass();
1854+
1855+
<pre class='overflow-auto'><span class="hljs-keyword">var</span> k = <span class="hljs-keyword">new</span> Klass();
18551856
k.isArrayOfBuffers();</pre>
18561857
</section>
18571858
</div>
@@ -2069,7 +2070,9 @@ the referenced class type</p>
20692070

20702071
</div>
20712072
<h4>Examples</h4>
2072-
<pre class='overflow-auto'>foo.getFoo();</pre>
2073+
<p><p>this shows you how to getFoo</p>
2074+
</p>
2075+
<pre class='overflow-auto'>foo.getFoo();</pre>
20732076
</section>
20742077
</div>
20752078
</div>

test/fixture/multiexample.output.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,16 @@
9090
}
9191
],
9292
"examples": [
93-
"foo(1);",
94-
"foo(2);"
93+
{
94+
"title": "example",
95+
"description": "foo(1);",
96+
"lineNumber": 3
97+
},
98+
{
99+
"title": "example",
100+
"description": "foo(2);",
101+
"lineNumber": 5
102+
}
95103
],
96104
"throws": [
97105
{

test/fixture/params.output.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,11 @@
149149
}
150150
],
151151
"examples": [
152-
"var address = new Address6('2001::/32');"
152+
{
153+
"title": "example",
154+
"description": "var address = new Address6('2001::/32');",
155+
"lineNumber": 9
156+
}
153157
],
154158
"name": "Address6",
155159
"kind": "class",

test/fixture/simple-two.output.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@
7575
}
7676
],
7777
"examples": [
78-
"var result = returnTwo(4);\n// result is 6"
78+
{
79+
"title": "example",
80+
"description": "var result = returnTwo(4);\n// result is 6",
81+
"lineNumber": 5
82+
}
7983
],
8084
"name": "returnTwo",
8185
"kind": "function",

test/fixture/throws.output.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@
9595
}
9696
],
9797
"examples": [
98-
"var result = returnTwo(4);\n// result is 6"
98+
{
99+
"title": "example",
100+
"description": "var result = returnTwo(4);\n// result is 6",
101+
"lineNumber": 6
102+
}
99103
],
100104
"name": "returnTwo",
101105
"kind": "function",

test/lib/flatten.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,13 @@ test('flatten', function (t) {
4343
/** @returns {number} test */
4444
})[0].returns[0].description, 'test', 'returns');
4545

46-
t.equal(evaluate(function () {
46+
t.deepEqual(evaluate(function () {
4747
/** @example test */
48-
})[0].examples[0], 'test', 'example');
48+
})[0].examples[0], {
49+
lineNumber: 0,
50+
title: 'example',
51+
description: 'test'
52+
}, 'example');
4953

5054
t.equal(evaluate(function () {
5155
/** @throws {Object} exception */

0 commit comments

Comments
 (0)