Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

feat(apib): add format link #534

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions packages/apib-parser/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# API Elements: API Blueprint Parser Changelog

## Master

### Enhancements

- added a Link element to the specific format/version in the parse result.

## 0.20.1 (2020-08-05)

Adds compatibility for @apielements/core 0.2.0.
Expand Down
26 changes: 23 additions & 3 deletions packages/apib-parser/lib/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function validate({ source, requireBlueprintName }) {
*/
function parse({
source, generateSourceMap, generateMessageBody, generateMessageBodySchema,
requireBlueprintName,
requireBlueprintName, namespace,
}) {
const options = {
exportSourcemap: !!generateSourceMap,
Expand All @@ -42,9 +42,29 @@ function parse({
requireBlueprintName,
};

return drafter.parse(source, options);
return drafter.parse(source, options).then((result) => {
const parseResult = namespace.fromRefract(result);
const isAnnotation = element => element.element === 'annotation';
const { Link } = namespace.elements;

if (!isAnnotation(parseResult.content[0])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please explain the intent of this check? I am not sure it is needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of error it does not add the format link, do you want to remove it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in all cases we should include the link element, some editors such as Apiary show the source format on annotations which the link element's title will be useful for:

Screenshot 2020-08-17 at 17 03 48

The use-case may be finding the underlying format which caused the error. The "Unknown" here would become "OpenAPI" etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed condition e310ea3

const link = new Link();

link.title = 'Apiary Blueprint';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
link.title = 'Apiary Blueprint';
link.title = 'API Blueprint';

This is the API Blueprint adapter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

link.relation = 'via';
link.href = 'https://apiblueprint.org/';

parseResult.links.push(link);
}

return parseResult;
});
}

module.exports = {
name, mediaTypes, detect, validate, parse,
name,
mediaTypes,
detect,
validate,
parse,
};
14 changes: 13 additions & 1 deletion packages/apib-parser/test/adapter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,23 @@ describe('API Blueprint parser adapter', () => {
});

it('has API category inside parse result', () => {
const filtered = result.children.filter(item => item.element === 'category' && item.classes.includes('api'));
const filtered = result.children.filter(
item => item.element === 'category' && item.classes.includes('api')
);

expect(filtered).to.have.length(1);
expect(filtered.first).to.be.an('object');
});

it('has the format link', () => {
const link = result.links.get(0);

expect(link.relation.toValue()).to.equal('via');
expect(link.title.toValue()).to.equal('Apiary Blueprint');
expect(link.href.toValue()).to.equal(
'https://apiblueprint.org/'
);
});
});

it('can parse an API Blueprint with require blueprint name', (done) => {
Expand Down