Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support other plugins using the @apiExample element #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Arxcis
Copy link

@Arxcis Arxcis commented Mar 21, 2019

Stricter pattern matching

Previously this plugin would catch all elements with the pattern:

@apiExample .*

This PR makes sure that apidoc-plugin-example only runs if the element matches any of these 3 patterns

@apiExample {json=.*
@apiExample {jsonschema=.*
@apiExample {xml=.*

Full regex here
/^\s*\{(json|jsonschema|xml)\s*=(.+?)\}\s*(?:\s+(.+?))?\s(.+?)$/g;

Bugfix

PR fixes a bug mentioned in #4, which happened because the plugin did not check if regex-match returned null before running .pop() and .push(). As an example, this would crash the apidoc command if apiExample {curl} was used together with plugin.

Further with the stricter pattern-matching mentioned above, we can remove an if-statement because we can trust parsed values more.

function parserExampleElements(elements, element, block, filename) {

  if ( element.name !== 'apiexample' ) {
    return elements;
  }

  const values = elementParser.parse(element.content, element.source);
  if (!values) {
    return elements
  }

  const data = fs.readFileSync(path.join(path.dirname(filename), values.path), 'utf8').toString();

  element = schemas[values.schema](data, values.element, values.title);
  elements.pop()
  elements.push(element);

  return elements;
}

Also we now make sure to run .pop() and .push() right next to each other, as the program will be in an inconsistent state if only one of them runs.

@Arxcis Arxcis changed the title Support other plugins which use the @apiExample element Support other plugins using the @apiExample element Mar 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant