Skip to content

Commit

Permalink
fix: filtering empty lines in docblocks preventing markdown tables fr…
Browse files Browse the repository at this point in the history
…om working (#275)
  • Loading branch information
erunion authored Aug 23, 2022
1 parent b569eca commit 3fbc42b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
24 changes: 24 additions & 0 deletions __tests__/extractor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,30 @@ describe('Extractor', () => {
expect(typeof endpoint.responses['200']).toBe('object');
});

it('extracts markdown tables from endpoint comment strings', () => {
const operation = [
' @api [get] /pets',
' summary: Get pets',
' description: |',
' Get pets',
' ',
' |a|b|',
' |-|-|',
' |1|2|',
].join('\n');

const endpoint = Extractor.extractEndpoint(operation);

expect(endpoint.method).toBe('get');
expect(endpoint.route).toBe('/pets');
expect(endpoint.description).toContain(`Get pets
|a|b|
|-|-|
|1|2|
`);
});

it('extracts endpoints from comment strings + summary', () => {
const operationWithSummary = [
'',
Expand Down
20 changes: 5 additions & 15 deletions src/extractor.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
const extractComments = require('multilang-extract-comments');
const jsYaml = require('js-yaml');

function pushLine(array, line) {
if (line.trim()) {
array.push(line);
return true;
}
return false;
}

function loadYamlWithPrettyErrors(prettyObject, yamlLines) {
try {
return jsYaml.load(yamlLines.join('\n').replace(/\t/g, ' '));
Expand Down Expand Up @@ -85,12 +77,12 @@ class Extractor {
let route = null;
let scopeMatched = false;

lines.some(line => {
lines.forEach(line => {
if (route) {
if (options && options.scope) {
if (line.trim().indexOf('scope:') === 0 && line.indexOf(options.scope) >= 0) {
scopeMatched = true;
return false;
return;
}
} else {
scopeMatched = true;
Expand All @@ -100,16 +92,14 @@ class Extractor {
// Only return false here if this line is an explicit `scope: {string}` property and not perhaps a `scope`
// property within a request body, parameter, or response schema.
if (line.trim().match(/scope: (.*)/)) {
return false;
return;
}
}

pushLine(yamlLines, line);
// eslint-disable-next-line consistent-return
yamlLines.push(line);
return;
}
route = route || line.match(this.ROUTE_REGEX);
return false;
});

if (!scopeMatched) {
Expand Down Expand Up @@ -138,7 +128,7 @@ class Extractor {
if (line.trim().indexOf('scope:') === 0) {
return;
}
pushLine(yamlLines, line);
yamlLines.push(line);
return;
}
route = route || line.match(this.SCHEMA_REGEX);
Expand Down

0 comments on commit 3fbc42b

Please sign in to comment.