Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 39 additions & 2 deletions packages/apib-parser/lib/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,46 @@ function parse({
requireBlueprintName,
};

return drafter.parse(source, options);
const formatLink = {
links: {
element: 'array',
content: [
{
element: 'link',
meta: {
title: {
element: 'string',
content: 'Apiary Blueprint',
},
},
attributes: {
relation: {
element: 'string',
content: 'via',
},
href: {
element: 'string',
content: 'https://apiary.io/blueprint',
},
},
},
],
},
};

return drafter.parse(source, options).then((result) => {
const refractElement = {};
refractElement.element = result.element;
refractElement.meta = formatLink;
refractElement.content = result.content;
return refractElement;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

One caveat here is that if the underlying parser added any other meta or attributes they would go missing. It might be a bit simpler to construct a minim elements from the given result. In options there is namespace which can be used as a minim namespace. You can get hold of a ParseResult element instance in the same way that the other parsers work. Returning a ParseResult here does not change the behaviour, the API Elements Core logic detects if the returned value is a Minim element, in the cases it is not, it will convert it to be one (thus doing what you'd be doing here).

const { namespace } = options;
const parseResult = namespace.fromRefract(source);
.. new namespace.elements.Link(...);
return parseResult;

What do you think?

@marcofriso marcofriso Aug 13, 2020

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@kylef in options there is not namespace, I don't know how to access it here. Seems to me that API Elements are managed internally by the drafter function, so by Protagonist.

@kylef kylef Aug 13, 2020

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The options passed into the function:


function parse({
  source, generateSourceMap, generateMessageBody, generateMessageBodySchema,
  requireBlueprintName,
}) {

can be adjusted to add namespace. It doesn't need to be added to the options passed to drafter.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

changed

});
}

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://apiary.io/blueprint'
);
});
});

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