Skip to content

Commit eb14cb9

Browse files
authored
feat(renderer): implement docstrap renderer (#65)
![capture d ecran de 2018-06-17 20-37-43](https://user-images.githubusercontent.com/2103975/41510975-55ed6d7a-726e-11e8-8a3c-ba9a25510adf.png)
1 parent 5e1747b commit eb14cb9

File tree

3 files changed

+130
-10
lines changed

3 files changed

+130
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Added `@vue-computed` tag (#59)
1010
- Added rendering system to support more JSDoc templates (#59)
1111
- Rewrote default renderer (#64)
12+
- Added [docstrap](https://github.com/docstrap/docstrap) renderer (#65)
1213

1314
### Removals
1415

Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,102 @@
11
describe('Renderers: docstrap', () => {
2-
beforeEach(() => {
2+
before(() => {
33
cy.visit('/../../../example/docs-docstrap/BetterCounter.html');
44
});
55

66
it('should renders props correctly', () => {
7-
cy.get('[data-vue="section-props"]').contains('Props');
7+
cy.get('[data-jsdoc-vuejs="section-props"]').contains('Props').should('have.class', 'subsection-title');
8+
cy.get('[data-jsdoc-vuejs="table-props"]').as('table-props').should('have.class', 'table table-responsive table-hover table-striped');
9+
10+
cy
11+
.get('@table-props')
12+
.find('> thead > tr > th')
13+
.should(($headers) => {
14+
expect($headers).to.have.length(5);
15+
expect($headers.eq(0).text()).to.contains('Name');
16+
expect($headers.eq(1).text()).to.contains('Type');
17+
expect($headers.eq(2).text()).to.contains('Default value');
18+
expect($headers.eq(3).text()).to.contains('Required ?');
19+
expect($headers.eq(4).text()).to.contains('Description');
20+
});
21+
22+
cy
23+
.get('@table-props')
24+
.find('> tbody > tr')
25+
.then(($rows) => {
26+
const $firstRowChildren = $rows.eq(0).children();
27+
const $secondRowChildren = $rows.eq(1).children();
28+
29+
expect($rows).to.have.length(2);
30+
31+
expect($firstRowChildren.eq(0).html()).to.eq('<b>initialCounter</b>');
32+
expect($firstRowChildren.eq(1).html()).to.eq('Number');
33+
expect($firstRowChildren.eq(2).html()).to.eq('-');
34+
expect($firstRowChildren.eq(3).html()).to.eq('<b>Yes</b>');
35+
expect($firstRowChildren.eq(4).html()).to.eq('-');
36+
37+
expect($secondRowChildren.eq(0).html()).to.eq('<b>step</b>');
38+
expect($secondRowChildren.eq(1).html()).to.eq('Number');
39+
expect($secondRowChildren.eq(2).html()).to.eq('<code>1</code>');
40+
expect($secondRowChildren.eq(3).html()).to.eq('No');
41+
expect($secondRowChildren.eq(4).html()).to.eq('Step');
42+
});
843
});
944

1045
it('should renders data correctly', () => {
11-
cy.get('[data-vue="section-data"]').contains('Data');
46+
cy.get('[data-jsdoc-vuejs="section-data"]').contains('Data').should('have.class', 'subsection-title');
47+
cy.get('[data-jsdoc-vuejs="table-data"]').as('table-data').should('have.class', 'table table-responsive table-hover table-striped');
48+
49+
cy
50+
.get('@table-data')
51+
.find('> thead > tr > th')
52+
.should(($headers) => {
53+
expect($headers).to.have.length(4);
54+
expect($headers.eq(0).text()).to.contains('Name');
55+
expect($headers.eq(1).text()).to.contains('Type');
56+
expect($headers.eq(2).text()).to.contains('Default value');
57+
expect($headers.eq(3).text()).to.contains('Description');
58+
});
59+
60+
cy
61+
.get('@table-data')
62+
.find('> tbody > tr')
63+
.then(($rows) => {
64+
const $rowChildren = $rows.eq(0).children();
65+
66+
expect($rows).to.have.length(1);
67+
68+
expect($rowChildren.eq(0).html()).to.eq('<b>counter</b>');
69+
expect($rowChildren.eq(1).html()).to.eq('Number');
70+
expect($rowChildren.eq(2).html()).to.eq('-');
71+
expect($rowChildren.eq(3).html()).to.eq('Current counter\'s value');
72+
});
1273
});
1374

1475
it('should renders computed correctly', () => {
15-
cy.get('[data-vue="section-computed"]').contains('Computed');
76+
cy.get('[data-jsdoc-vuejs="section-computed"]').contains('Computed').should('have.class', 'subsection-title');
77+
cy.get('[data-jsdoc-vuejs="table-computed"]').as('table-data').should('have.class', 'table table-responsive table-hover table-striped');
78+
79+
cy
80+
.get('@table-data')
81+
.find('> thead > tr > th')
82+
.should(($headers) => {
83+
expect($headers).to.have.length(3);
84+
expect($headers.eq(0).text()).to.contains('Name');
85+
expect($headers.eq(1).text()).to.contains('Type');
86+
expect($headers.eq(2).text()).to.contains('Description');
87+
});
88+
89+
cy
90+
.get('@table-data')
91+
.find('> tbody > tr')
92+
.then(($rows) => {
93+
const $rowChildren = $rows.eq(0).children();
94+
95+
expect($rows).to.have.length(1);
96+
97+
expect($rowChildren.eq(0).html()).to.eq('<b>message</b>');
98+
expect($rowChildren.eq(1).html()).to.eq('String');
99+
expect($rowChildren.eq(2).html()).to.eq('A message');
100+
});
16101
});
17102
});

lib/renderers/docstrap.js

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,57 @@
1+
const makeTableHead = headers => `<thead><th>${headers.join('</th><th>')}</th></thead>`;
2+
const makeTableBody = (items, cb) => `<tbody>${items.map(item => `<tr>${cb(item).trim()}</tr>`).join('')}</tbody>`;
3+
14
module.exports = function renderDocstrap(description, props = [], data = [], computed = []) {
25
let html = description;
36

7+
html += '</p></div></div>';
8+
49
// eslint-disable-next-line no-console
510
console.log('Using docstrap renderer');
611

712
if (props.length > 0) {
8-
html += '<h3 data-vue="section-props">Props</h3>';
9-
html += JSON.stringify(props);
13+
html += '<h3 class="subsection-title" data-jsdoc-vuejs="section-props">Props</h3>';
14+
html += '<hr>';
15+
html += '<table class="table table-responsive table-hover table-striped" data-jsdoc-vuejs="table-props">';
16+
html += makeTableHead(['Name', 'Type', 'Default value', 'Required ?', 'Description']);
17+
html += makeTableBody(props, item => `
18+
<td><b>${item.name}</b></td>
19+
<td>${(item.type.names || []).join(', ')}</td>
20+
<td>${typeof item.defaultvalue === 'undefined' ? '-' : `<code>${item.defaultvalue}</code>`}</td>
21+
<td>${item.optional ? 'No' : '<b>Yes</b>'}</td>
22+
<td>${typeof item.description === 'undefined' ? '-' : item.description}</td>
23+
`);
24+
html += '</table>';
1025
}
1126

1227
if (data.length > 0) {
13-
html += '<h3 data-vue="section-data">Data</h3>';
14-
html += JSON.stringify(data);
28+
html += '<h3 class="subsection-title" data-jsdoc-vuejs="section-data">Data</h3>';
29+
html += '<hr>';
30+
html += '<table class="table table-responsive table-hover table-striped" data-jsdoc-vuejs="table-data">';
31+
html += makeTableHead(['Name', 'Type', 'Default value', 'Description']);
32+
html += makeTableBody(data, item => `
33+
<td><b>${item.name}</b></td>
34+
<td>${(item.type.names || []).join(', ')}</td>
35+
<td>${typeof item.defaultvalue === 'undefined' ? '-' : `<code>${item.defaultvalue}</code>`}</td>
36+
<td>${typeof item.description === 'undefined' ? '-' : item.description}</td>
37+
`);
38+
html += '</table>';
1539
}
1640

1741
if (computed.length > 0) {
18-
html += '<h3 data-vue="section-computed">Computed</h3>';
19-
html += JSON.stringify(computed);
42+
html += '<h3 class="subsection-title" data-jsdoc-vuejs="section-computed">Computed</h3>';
43+
html += '<hr>';
44+
html += '<table class="table table-responsive table-hover table-striped" data-jsdoc-vuejs="table-computed">';
45+
html += makeTableHead(['Name', 'Type', 'Description']);
46+
html += makeTableBody(computed, item => `
47+
<td><b>${item.name}</b></td>
48+
<td>${(item.type.names || []).join(', ')}</td>
49+
<td>${typeof item.description === 'undefined' ? '-' : item.description}</td>
50+
`);
51+
html += '</table>';
2052
}
2153

54+
html += '<div class="container-overview"><div><p>';
55+
2256
return html;
2357
};

0 commit comments

Comments
 (0)