compose-doc.js 1.5 KB

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