newline.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Proceess '\n'
  2. 'use strict';
  3. var isSpace = require('../common/utils').isSpace;
  4. module.exports = function newline(state, silent) {
  5. var pmax, max, ws, 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. // Find whitespaces tail of pending chars.
  17. ws = pmax - 1;
  18. while (ws >= 1 && state.pending.charCodeAt(ws - 1) === 0x20) ws--;
  19. state.pending = state.pending.slice(0, ws);
  20. state.push('hardbreak', 'br', 0);
  21. } else {
  22. state.pending = state.pending.slice(0, -1);
  23. state.push('softbreak', 'br', 0);
  24. }
  25. } else {
  26. state.push('softbreak', 'br', 0);
  27. }
  28. }
  29. pos++;
  30. // skip heading spaces for next line
  31. while (pos < max && isSpace(state.src.charCodeAt(pos))) { pos++; }
  32. state.pos = pos;
  33. return true;
  34. };