Skip to content

Commit 3fbc42b

Browse files
authored
fix: filtering empty lines in docblocks preventing markdown tables from working (#275)
1 parent b569eca commit 3fbc42b

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

__tests__/extractor.test.js

+24
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,30 @@ describe('Extractor', () => {
7575
expect(typeof endpoint.responses['200']).toBe('object');
7676
});
7777

78+
it('extracts markdown tables from endpoint comment strings', () => {
79+
const operation = [
80+
' @api [get] /pets',
81+
' summary: Get pets',
82+
' description: |',
83+
' Get pets',
84+
' ',
85+
' |a|b|',
86+
' |-|-|',
87+
' |1|2|',
88+
].join('\n');
89+
90+
const endpoint = Extractor.extractEndpoint(operation);
91+
92+
expect(endpoint.method).toBe('get');
93+
expect(endpoint.route).toBe('/pets');
94+
expect(endpoint.description).toContain(`Get pets
95+
96+
|a|b|
97+
|-|-|
98+
|1|2|
99+
`);
100+
});
101+
78102
it('extracts endpoints from comment strings + summary', () => {
79103
const operationWithSummary = [
80104
'',

src/extractor.js

+5-15
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
const extractComments = require('multilang-extract-comments');
22
const jsYaml = require('js-yaml');
33

4-
function pushLine(array, line) {
5-
if (line.trim()) {
6-
array.push(line);
7-
return true;
8-
}
9-
return false;
10-
}
11-
124
function loadYamlWithPrettyErrors(prettyObject, yamlLines) {
135
try {
146
return jsYaml.load(yamlLines.join('\n').replace(/\t/g, ' '));
@@ -85,12 +77,12 @@ class Extractor {
8577
let route = null;
8678
let scopeMatched = false;
8779

88-
lines.some(line => {
80+
lines.forEach(line => {
8981
if (route) {
9082
if (options && options.scope) {
9183
if (line.trim().indexOf('scope:') === 0 && line.indexOf(options.scope) >= 0) {
9284
scopeMatched = true;
93-
return false;
85+
return;
9486
}
9587
} else {
9688
scopeMatched = true;
@@ -100,16 +92,14 @@ class Extractor {
10092
// Only return false here if this line is an explicit `scope: {string}` property and not perhaps a `scope`
10193
// property within a request body, parameter, or response schema.
10294
if (line.trim().match(/scope: (.*)/)) {
103-
return false;
95+
return;
10496
}
10597
}
10698

107-
pushLine(yamlLines, line);
108-
// eslint-disable-next-line consistent-return
99+
yamlLines.push(line);
109100
return;
110101
}
111102
route = route || line.match(this.ROUTE_REGEX);
112-
return false;
113103
});
114104

115105
if (!scopeMatched) {
@@ -138,7 +128,7 @@ class Extractor {
138128
if (line.trim().indexOf('scope:') === 0) {
139129
return;
140130
}
141-
pushLine(yamlLines, line);
131+
yamlLines.push(line);
142132
return;
143133
}
144134
route = route || line.match(this.SCHEMA_REGEX);

0 commit comments

Comments
 (0)