compose-doc.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. var Document = require('../doc/Document.js');
  3. var composeNode = require('./compose-node.js');
  4. var resolveEnd = require('./resolve-end.js');
  5. var resolveProps = require('./resolve-props.js');
  6. function composeDoc(options, directives, { offset, start, value, end }, onError) {
  7. const opts = Object.assign({ _directives: directives }, options);
  8. const doc = new Document.Document(undefined, opts);
  9. const ctx = {
  10. atKey: false,
  11. atRoot: true,
  12. directives: doc.directives,
  13. options: doc.options,
  14. schema: doc.schema
  15. };
  16. const props = resolveProps.resolveProps(start, {
  17. indicator: 'doc-start',
  18. next: value ?? end?.[0],
  19. offset,
  20. onError,
  21. parentIndent: 0,
  22. startOnNewline: true
  23. });
  24. if (props.found) {
  25. doc.directives.docStart = true;
  26. if (value &&
  27. (value.type === 'block-map' || value.type === 'block-seq') &&
  28. !props.hasNewline)
  29. onError(props.end, 'MISSING_CHAR', 'Block collection cannot start on same line with directives-end marker');
  30. }
  31. // @ts-expect-error If Contents is set, let's trust the user
  32. doc.contents = value
  33. ? composeNode.composeNode(ctx, value, props, onError)
  34. : composeNode.composeEmptyNode(ctx, props.end, start, null, props, onError);
  35. const contentEnd = doc.contents.range[2];
  36. const re = resolveEnd.resolveEnd(end, contentEnd, false, onError);
  37. if (re.comment)
  38. doc.comment = re.comment;
  39. doc.range = [offset, contentEnd, re.offset];
  40. return doc;
  41. }
  42. exports.composeDoc = composeDoc;