Skip to content

Commit

Permalink
Basic formatting for inline links
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcw committed Apr 15, 2015
1 parent caee6c9 commit ea2b02d
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"handlebars": "^3.0.0",
"hat": "0.0.3",
"highlight.js": "^8.4.0",
"jsdoc-inline-lex": "^1.0.1",
"module-deps": "^3.7.3",
"queue-async": "^1.0.7",
"remarkable": "^1.6.0",
Expand Down
6 changes: 3 additions & 3 deletions share/markdown.hbs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# `{{name}}`

{{#description}}
{{.}}
{{inlines .}}
{{/description}}

{{#if params}}
| name | description |
| ---- | ----------- |
{{#params}}
| `{{name}}` | {{description}} |
| `{{name}}` | {{inlines description}} |
{{/params}}
{{/if}}

Expand All @@ -23,6 +23,6 @@
{{/if}}

{{#returns}}
Returns {{#type.name}}`{{.}}`{{/type.name}} {{description}}
Returns {{#type.name}}`{{.}}`{{/type.name}} {{inlines description}}
{{/returns}}

33 changes: 32 additions & 1 deletion streams/output/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,32 @@ var through = require('through'),
fs = require('fs'),
path = require('path'),
Handlebars = require('handlebars'),
extend = require('extend');
extend = require('extend'),
inlineLex = require('jsdoc-inline-lex');

/**
* Format link & tutorial tags with simple code inline tags.
*
* @param {string} text input - typically a description
* @returns {string} markdown-friendly output
* @private
* @example
* formatInlineTags('{@link Foo}'); // "`Foo`"
*/
function formatInlineTags(text) {
var output = '';
var tokens = inlineLex(text);

for (var i = 0; i < tokens.length; i++) {
if (tokens[i].type === 'text') {
output += tokens[i].capture[0];
} else {
output += '`' + tokens[i].capture[1] + '`';
}
}

return output;
}

/**
* Create a transform stream that formats documentation as Markdown.
Expand All @@ -18,13 +43,19 @@ var through = require('through'),
* @return {stream.Transform}
*/
module.exports = function (opts) {

var options = extend({}, {
template: path.resolve(path.join(__dirname, '../../share/markdown.hbs'))
}, opts);

var template = Handlebars
.compile(
fs.readFileSync(options.template, 'utf8'));

Handlebars.registerHelper('inlines', function (string) {
return new Handlebars.SafeString(formatInlineTags(string));
});

return through(function (comment) {
this.push(template(comment));
});
Expand Down
18 changes: 18 additions & 0 deletions test/fixture/inline-link.input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Adds one to a number
* @param {number} a the input
* @returns {number} the output
*/
function addOne(a) {
return a + 1;
}

/**
* This function returns the number one. Internally, this uses
* {@link addOne} to do the math.
* @param {number} a the input
* @returns {number} numberone
*/
module.exports = function (a) {
return addOne(a);
};
17 changes: 17 additions & 0 deletions test/fixture/inline-link.output.custom.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## `addOne`

Adds one to a number

* `a`: the input

Returns `number` the output

## `exports`

This function returns the number one. Internally, this uses
{@link addOne} to do the math.

* `a`: the input

Returns `number` numberone

145 changes: 145 additions & 0 deletions test/fixture/inline-link.output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
[
{
"description": "Adds one to a number",
"tags": [
{
"title": "param",
"description": "the input",
"type": {
"type": "NameExpression",
"name": "number"
},
"name": "a"
},
{
"title": "returns",
"description": "the output",
"type": {
"type": "NameExpression",
"name": "number"
}
},
{
"title": "name",
"name": "addOne"
},
{
"title": "kind",
"kind": "function"
}
],
"context": {
"loc": {
"start": {
"line": 6,
"column": 0
},
"end": {
"line": 8,
"column": 1
}
},
"file": "fixture/inline-link.input.js",
"code": "function addOne(a) {\n return a + 1;\n}\n\n/**\n * This function returns the number one. Internally, this uses\n * {@link addOne} to do the math.\n * @param {number} a the input\n * @returns {number} numberone\n */\nmodule.exports = function (a) {\n return addOne(a);\n};"
},
"params": [
{
"title": "param",
"description": "the input",
"type": {
"type": "NameExpression",
"name": "number"
},
"name": "a"
}
],
"returns": [
{
"title": "returns",
"description": "the output",
"type": {
"type": "NameExpression",
"name": "number"
}
}
],
"name": "addOne",
"kind": "function"
},
{
"description": "This function returns the number one. Internally, this uses\n{@link addOne} to do the math.",
"tags": [
{
"title": "param",
"description": "the input",
"type": {
"type": "NameExpression",
"name": "number"
},
"name": "a"
},
{
"title": "returns",
"description": "numberone",
"type": {
"type": "NameExpression",
"name": "number"
}
},
{
"title": "name",
"name": "exports"
},
{
"title": "kind",
"kind": "function"
},
{
"title": "memberof",
"description": "module"
},
{
"title": "static"
}
],
"context": {
"loc": {
"start": {
"line": 16,
"column": 0
},
"end": {
"line": 18,
"column": 2
}
},
"file": "fixture/inline-link.input.js",
"code": "function addOne(a) {\n return a + 1;\n}\n\n/**\n * This function returns the number one. Internally, this uses\n * {@link addOne} to do the math.\n * @param {number} a the input\n * @returns {number} numberone\n */\nmodule.exports = function (a) {\n return addOne(a);\n};"
},
"params": [
{
"title": "param",
"description": "the input",
"type": {
"type": "NameExpression",
"name": "number"
},
"name": "a"
}
],
"returns": [
{
"title": "returns",
"description": "numberone",
"type": {
"type": "NameExpression",
"name": "number"
}
}
],
"name": "exports",
"kind": "function",
"memberof": "module",
"scope": "static"
}
]
23 changes: 23 additions & 0 deletions test/fixture/inline-link.output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# `addOne`

Adds one to a number

| name | description |
| ---- | ----------- |
| `a` | the input |


Returns `number` the output

# `exports`

This function returns the number one. Internally, this uses
`addOne` to do the math.

| name | description |
| ---- | ----------- |
| `a` | the input |


Returns `number` numberone

0 comments on commit ea2b02d

Please sign in to comment.