inline-code.js 687 B

1234567891011121314151617181920212223242526272829303132333435
  1. 'use strict';
  2. var streak = require('longest-streak');
  3. var repeat = require('repeat-string');
  4. module.exports = inlineCode;
  5. /* Stringify inline code.
  6. *
  7. * Knows about internal ticks (`\``), and ensures one more
  8. * tick is used to enclose the inline code:
  9. *
  10. * ```foo ``bar`` baz```
  11. *
  12. * Even knows about inital and final ticks:
  13. *
  14. * `` `foo ``
  15. * `` foo` ``
  16. */
  17. function inlineCode(node) {
  18. var value = node.value;
  19. var ticks = repeat('`', streak(value, '`') + 1);
  20. var start = ticks;
  21. var end = ticks;
  22. if (value.charAt(0) === '`') {
  23. start += ' ';
  24. }
  25. if (value.charAt(value.length - 1) === '`') {
  26. end = ' ' + end;
  27. }
  28. return start + value + end;
  29. }