parse.js 900 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict'
  2. var xtend = require('xtend')
  3. var removePosition = require('unist-util-remove-position')
  4. module.exports = parse
  5. var lineFeed = '\n'
  6. var lineBreaksExpression = /\r\n|\r/g
  7. // Parse the bound file.
  8. function parse() {
  9. var self = this
  10. var value = String(self.file)
  11. var start = {line: 1, column: 1, offset: 0}
  12. var content = xtend(start)
  13. var node
  14. // Clean non-unix newlines: `\r\n` and `\r` are all changed to `\n`.
  15. // This should not affect positional information.
  16. value = value.replace(lineBreaksExpression, lineFeed)
  17. // BOM.
  18. if (value.charCodeAt(0) === 0xfeff) {
  19. value = value.slice(1)
  20. content.column++
  21. content.offset++
  22. }
  23. node = {
  24. type: 'root',
  25. children: self.tokenizeBlock(value, content),
  26. position: {start: start, end: self.eof || xtend(start)}
  27. }
  28. if (!self.options.position) {
  29. removePosition(node, true)
  30. }
  31. return node
  32. }