UNPKG

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