Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Github flavored markdown #3

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,40 @@ ejs.filters.undef = function (obj) {
};

ejs.filters.mlink = function (obj) {
return (obj || '').toLowerCase().replace(/\s/g, '-');
return (obj || '').toLowerCase().replace(/\s/g, '-').replace(/\;|\/|\?|:|@|&|=|\+|\$|\,/g, '');
};

var tmplFile = argv.template ? argv.template : __dirname + '/templates/default.md',
ejs.filters.badge = function (t) {
var type = t ? t.toUpperCase() : 'GET';
var img = 'https://img.shields.io/badge/method-' + type + '-';

switch (type) {
case 'GET':
img += 'brightgreen';
break;
case 'POST':
img += 'orange';
break;
case 'PUT':
img += 'blue';
break;
case 'PATCH':
img += '00aeef';
break;
case 'DELETE':
img += 'red';
break;
default:
img += 'blue';
break;
}

img += '.svg';

return img;
};

var tmplFile = argv.template ? argv.template : __dirname + '/templates/github.md',
apiData = JSON.parse(fs.readFileSync(argv.path + '/api_data.json')),
projData = JSON.parse(fs.readFileSync(argv.path + '/api_project.json')),
template = ejs.compile(fs.readFileSync(tmplFile).toString());
Expand All @@ -56,4 +86,4 @@ var data = {

data.prepend = argv.prepend ? fs.readFileSync(argv.prepend).toString() : null;
fs.writeFileSync(argv.output, template(data));
console.log('Wrote apidoc-markdown template output to: ' + argv.output);
console.log('Wrote apidoc-markdown template output to: ' + argv.output);
4 changes: 3 additions & 1 deletion templates/default.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

<%-: data[group][sub][0].description | undef %>

<%-: data[group][sub][0].type | upcase %> <%= data[group][sub][0].url %>
```
<%-: data[group][sub][0].type | upcase %> <%= data[group][sub][0].url %>
```

<% if (data[group][sub][0].header && data[group][sub][0].header.fields.Header.length) { -%>
### Headers
Expand Down
116 changes: 116 additions & 0 deletions templates/github.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<a name="top"></a>
# <%= project.name %> v<%= project.version %>

<%= project.description %>

<% Object.keys(data).sort().forEach(function (group) { -%>
- [<%= group %>](#<%=: group | mlink %>)
<% Object.keys(data[group]).sort(function(a, b) { return data[group][a][0].title.localeCompare(data[group][b][0].title) }).forEach(function (sub) { -%>
- [<%= data[group][sub][0].title %>](#<%=: data[group][sub][0].title | mlink %>)
<% }); -%>

<% }); %>

<% if (prepend) { -%>
<%- prepend %>
<% } -%>
<% Object.keys(data).sort().forEach(function (group) { -%>
# <%= group %>

<% Object.keys(data[group]).sort(function(a, b) { return data[group][a][0].title.localeCompare(data[group][b][0].title) }).forEach(function (sub) { -%>
## <%= data[group][sub][0].title %>

![<%=: data[group][sub][0].type | upcase %>](<%=: data[group][sub][0].type | badge %>)

> <%-: data[group][sub][0].description | undef %>

```
<%-: data[group][sub][0].type | upcase %> <%= data[group][sub][0].url %>
```

<% if (data[group][sub][0].header && data[group][sub][0].header.fields.Header.length) { -%>
### Headers

| Name | Type | Description |
|---------|-----------|--------------------------------------|
<% data[group][sub][0].header.fields.Header.forEach(function (header) { -%>
| <%- header.field %> | <%- header.type %> | <%- header.optional ? '**optional**' : '' %><%- header.description %>|
<% }); //forech parameter -%>
<% } //if parameters -%>
<% if (data[group][sub][0].parameter) { -%>

<% Object.keys(data[group][sub][0].parameter.fields).forEach(function(g) { -%>

### <%= g %> Parameters

| Name | Type | Description |
|:---------|:-----------|:--------------------------------------|
<% data[group][sub][0].parameter.fields[g].forEach(function (param) { -%>
| <%- param.field %> | <%- param.type %> | <%- param.optional ? '**optional**' : '' %><%- param.description -%>
<% if (param.defaultValue) { -%>
_Default value: <%= param.defaultValue %>_<br><% } -%>
<% if (param.size) { -%>
_Size range: <%- param.size %>_<br><% } -%>
<% if (param.allowedValues) { -%>
_Allowed values: <%- param.allowedValues %>_<% } %>|
<% }); //forech (group) parameter -%>
<% }); //forech param parameter -%>
<% } //if parameters -%>
<% if (data[group][sub][0].examples && data[group][sub][0].examples.length) { -%>
### Examples

<% data[group][sub][0].examples.forEach(function (example) { -%>
<%= example.title %>

```
<%- example.content %>
```
<% }); //foreach example -%>
<% } //if example -%>

<% if (data[group][sub][0].success && data[group][sub][0].success.examples && data[group][sub][0].success.examples.length) { -%>
### Success Response

<% data[group][sub][0].success.examples.forEach(function (example) { -%>
<%= example.title %>

```
<%- example.content %>
```
<% }); //foreach success example -%>
<% } //if examples -%>

<% if (data[group][sub][0].success && data[group][sub][0].success.fields) { -%>
<% Object.keys(data[group][sub][0].success.fields).forEach(function(g) { -%>
### <%= g %>

| Name | Type | Description |
|:---------|:-----------|:--------------------------------------|
<% data[group][sub][0].success.fields[g].forEach(function (param) { -%>
| <%- param.field %> | <%- param.type %> | <%- param.optional ? '**optional**' : '' %><%- param.description -%>
<% if (param.defaultValue) { -%>
_Default value: <%- param.defaultValue %>_<br><% } -%>
<% if (param.size) { -%>
_Size range: <%- param.size -%>_<br><% } -%>
<% if (param.allowedValues) { -%>
_Allowed values: <%- param.allowedValues %>_<% } %>|
<% }); //forech (group) parameter -%>
<% }); //forech field -%>
<% } //if success.fields -%>

<% if (data[group][sub][0].error && data[group][sub][0].error.examples && data[group][sub][0].error.examples.length) { -%>
### Error Response

<% data[group][sub][0].error.examples.forEach(function (example) { -%>
<%= example.title %>

```
<%- example.content %>
```
<% }); //foreach error example -%>
<% } //if examples -%>
[:arrow_up:](#top)
---
<% }); //foreach sub -%>
<% }); //foreach group -%>