newline.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Proceess '\n'
  2. 'use strict';
  3. var isSpace = require('../common/utils').isSpace;
  4. module.exports = function newline(state, silent) {
  5. var pmax, max, pos = state.pos;
  6. if (state.src.charCodeAt(pos) !== 0x0A/* \n */) { return false; }
  7. pmax = state.pending.length - 1;
  8. max = state.posMax;
  9. // ' \n' -> hardbreak
  10. // Lookup in pending chars is bad practice! Don't copy to other rules!
  11. // Pending string is stored in concat mode, indexed lookups will cause
  12. // convertion to flat mode.
  13. if (!silent) {
  14. if (pmax >= 0 && state.pending.charCodeAt(pmax) === 0x20) {
  15. if (pmax >= 1 && state.pending.charCodeAt(pmax - 1) === 0x20) {
  16. state.pending = state.pending.replace(/ +$/, '');
  17. state.push('hardbreak', 'br', 0);
  18. } else {
  19. state.pending = state.pending.slice(0, -1);
  20. state.push('softbreak', 'br', 0);
  21. }
  22. } else {
  23. state.push('softbreak', 'br', 0);
  24. }
  25. }
  26. pos++;
  27. // skip heading spaces for next line
  28. while (pos < max && isSpace(state.src.charCodeAt(pos))) { pos++; }
  29. state.pos = pos;
  30. return true;
  31. };