Skip to content

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 added 3 commits March 21, 2019 19:19
- This makes it possible to play more nicely with other plugins like {curl=example}
@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