wrap.js 535 B

12345678910111213141516171819202122232425262728293031
  1. 'use strict'
  2. module.exports = wrap
  3. var u = require('unist-builder')
  4. // Wrap `nodes` with newlines between each entry. Optionally adds newlines at
  5. // the start and end.
  6. function wrap(nodes, loose) {
  7. var result = []
  8. var index = -1
  9. var length = nodes.length
  10. if (loose) {
  11. result.push(u('text', '\n'))
  12. }
  13. while (++index < length) {
  14. if (index) {
  15. result.push(u('text', '\n'))
  16. }
  17. result.push(nodes[index])
  18. }
  19. if (loose && nodes.length !== 0) {
  20. result.push(u('text', '\n'))
  21. }
  22. return result
  23. }