1 | 'use strict';
|
---|
2 |
|
---|
3 | var Document = require('../doc/Document.js');
|
---|
4 | var composeNode = require('./compose-node.js');
|
---|
5 | var resolveEnd = require('./resolve-end.js');
|
---|
6 | var resolveProps = require('./resolve-props.js');
|
---|
7 |
|
---|
8 | function 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 | atRoot: true,
|
---|
13 | directives: doc.directives,
|
---|
14 | options: doc.options,
|
---|
15 | schema: doc.schema
|
---|
16 | };
|
---|
17 | const props = resolveProps.resolveProps(start, {
|
---|
18 | indicator: 'doc-start',
|
---|
19 | next: value ?? end?.[0],
|
---|
20 | offset,
|
---|
21 | onError,
|
---|
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 |
|
---|
43 | exports.composeDoc = composeDoc;
|
---|