compose-doc.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. atRoot: true,
  11. directives: doc.directives,
  12. options: doc.options,
  13. schema: doc.schema
  14. };
  15. const props = resolveProps.resolveProps(start, {
  16. indicator: 'doc-start',
  17. next: value ?? end?.[0],
  18. offset,
  19. onError,
  20. startOnNewline: true
  21. });
  22. if (props.found) {
  23. doc.directives.docStart = true;
  24. if (value &&
  25. (value.type === 'block-map' || value.type === 'block-seq') &&
  26. !props.hasNewline)
  27. onError(props.end, 'MISSING_CHAR', 'Block collection cannot start on same line with directives-end marker');
  28. }
  29. // @ts-expect-error If Contents is set, let's trust the user
  30. doc.contents = value
  31. ? composeNode.composeNode(ctx, value, props, onError)
  32. : composeNode.composeEmptyNode(ctx, props.end, start, null, props, onError);
  33. const contentEnd = doc.contents.range[2];
  34. const re = resolveEnd.resolveEnd(end, contentEnd, false, onError);
  35. if (re.comment)
  36. doc.comment = re.comment;
  37. doc.range = [offset, contentEnd, re.offset];
  38. return doc;
  39. }
  40. exports.composeDoc = composeDoc;