compose-doc.js 1.5 KB

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