index.js
function parserFor(rules) {
  // Sorts rules in order of increasing order, then
  // ascending rule name in case of ties.
  let ruleList = Object.keys(rules);

  /* istanbul ignore next */
  if (process.env.NODE_ENV !== 'production') {
    ruleList.forEach(function(type) {
      let order = rules[type].order;
      if (
        process.env.NODE_ENV !== 'production' &&
        (typeof order !== 'number' || !isFinite(order))
      ) {
        console.warn(
          'markdown-to-jsx: Invalid order for rule `' + type + '`: ' + order
        );
      }
    });
  }

  ruleList.sort(function(typeA, typeB) {
    let orderA = rules[typeA].order;
    let orderB = rules[typeB].order;

    // First sort based on increasing order
    if (orderA !== orderB) {
      return orderA - orderB;

      // Then based on increasing unicode lexicographic ordering
    } else if (typeA < typeB) {
      return -1;
    }

    return 1;
  });

  function nestedParse(source, state) {
    let result = [];

    // We store the previous capture so that match functions can
    // use some limited amount of lookbehind. Lists use this to
    // ensure they don't match arbitrary '- ' or '* ' in inline
    // text (see the list rule for more information).
    let prevCapture = '';
    while (source) {
      let i = 0;
      while (i < ruleList.length) {
        const ruleType = ruleList[i];
        const rule = rules[ruleType];
        const capture = rule.match(source, state, prevCapture);

        if (capture) {
          const currCaptureString = capture[0];
          source = source.substring(currCaptureString.length);
          const parsed = rule.parse(capture, nestedParse, state);

          // We also let rules override the default type of
          // their parsed node if they would like to, so that
          // there can be a single output function for all links,
          // even if there are several rules to parse them.
          if (parsed.type == null) {
            parsed.type = ruleType;
          }

          result.push(parsed);

          prevCapture = currCaptureString;
          break;
        }

        i++;
      }
    }

    return result;
  }

  return function outerParse(source, state) {
    return nestedParse(normalizeWhitespace(source), state);
  };
}
function parserFor(rules) {
  // Sorts rules in order of increasing order, then
  // ascending rule name in case of ties.
  let ruleList = Object.keys(rules);

  /* istanbul ignore next */
  if (process.env.NODE_ENV !== 'production') {
    ruleList.forEach(function(type) {
      let order = rules[type].order;
      if (
        process.env.NODE_ENV !== 'production' &&
        (typeof order !== 'number' || !isFinite(order))
      ) {
        console.warn(
          'markdown-to-jsx: Invalid order for rule `' + type + '`: ' + order
        );
      }
    });
  }

  ruleList.sort(function(typeA, typeB) {
    let orderA = rules[typeA].order;
    let orderB = rules[typeB].order;

    // First sort based on increasing order
    if (orderA !== orderB) {
      return orderA - orderB;

      // Then based on increasing unicode lexicographic ordering
    } else if (typeA < typeB) {
      return -1;
    }

    return 1;
  });

  function nestedParse(source, state) {
    let result = [];

    // We store the previous capture so that match functions can
    // use some limited amount of lookbehind. Lists use this to
    // ensure they don't match arbitrary '- ' or '* ' in inline
    // text (see the list rule for more information).
    let prevCapture = '';
    while (source) {
      let i = 0;
      while (i < ruleList.length) {
        const ruleType = ruleList[i];
        const rule = rules[ruleType];
        const capture = rule.match(source, state, prevCapture);

        if (capture) {
          const currCaptureString = capture[0];
          source = source.substring(currCaptureString.length);
          const parsed = rule.parse(capture, nestedParse, state);

          // We also let rules override the default type of
          // their parsed node if they would like to, so that
          // there can be a single output function for all links,
          // even if there are several rules to parse them.
          if (parsed.type == null) {
            parsed.type = ruleType;
          }

          result.push(parsed);

          prevCapture = currCaptureString;
          break;
        }

        i++;
      }
    }

    return result;
  }

  return function outerParse(source, state) {
    return nestedParse(normalizeWhitespace(source), state);
  };
}
function parserFor ( rules ) { // Sorts rules in order of increasing order, then // ascending rule name in case of ties. let ruleList = Object . keys ( rules ) ; /* istanbul ignore next */ if ( process . env . NODE_ENV !== 'production' ) { ruleList . forEach ( function ( type ) { let order = rules [ type ] . order ; if ( process . env . NODE_ENV !== 'production' && ( typeof order !== 'number' || ! isFinite ( order ) ) ) { console . warn ( 'markdown-to-jsx: Invalid order for rule `' + type + '`: ' + order ) ; } } ) ; } ruleList . sort ( function ( typeA , typeB ) { let orderA = rules [ typeA ] . order ; let orderB = rules [ typeB ] . order ; // First sort based on increasing order if ( orderA !== orderB ) { return orderA - orderB ; // Then based on increasing unicode lexicographic ordering } else if ( typeA < typeB ) { return - 1 ; } return 1 ; } ) ; function nestedParse ( source , state ) { let result = [ ] ; // We store the previous capture so that match functions can // use some limited amount of lookbehind. Lists use this to // ensure they don't match arbitrary '- ' or '* ' in inline // text (see the list rule for more information). let prevCapture = '' ; while ( source ) { let i = 0 ; while ( i < ruleList . length ) { const ruleType = ruleList [ i ] ; const rule = rules [ ruleType ] ; const capture = rule . match ( source , state , prevCapture ) ; if ( capture ) { const currCaptureString = capture [ 0 ] ; source = source . substring ( currCaptureString . length ) ; const parsed = rule . parse ( capture , nestedParse , state ) ; // We also let rules override the default type of // their parsed node if they would like to, so that // there can be a single output function for all links, // even if there are several rules to parse them. if ( parsed . type == null ) { parsed . type = ruleType ; } result . push ( parsed ) ; prevCapture = currCaptureString ; break ; } i ++ ; } } return result ; } return function outerParse ( source , state ) { return nestedParse ( normalizeWhitespace ( source ) , state ) ; } ; }
Creates a parser for a given set of rules, with the precedence
specified as a list of rules.

@rules: an object containing
rule type -> {match, order, parse} objects
(lower order is higher precedence)
(Note: `order` is added to defaultRules after creation so that
the `order` of defaultRules in the source matches the `order`
of defaultRules in terms of `order` fields.)

@returns The resulting parse function, with the following parameters:
@source: the input source string to be parsed
@state: an optional object to be threaded through parse
calls. Allows clients to add stateful operations to
parsing, such as keeping track of how many levels deep
some nesting is. For an example use-case, see passage-ref
parsing in src/widgets/passage/passage-markdown.jsx
Creates a parser for a given set of rules with the precedence specified as a list of rules .