How to use the parseDocument function from yaml

Find comprehensive JavaScript yaml.parseDocument code examples handpicked from public code repositorys.

yaml.parseDocument is a JavaScript function that converts a YAML document string into a YAML document object, which can be accessed and manipulated using JavaScript.

44
45
46
47
48
49
50
51
52
53
setUp() {

  if (fs.existsSync(this.getProjectPath('.wordup','config.yml'))) {

    try {
      this.dotWordupYml = YAML.parseDocument(fs.readFileSync(this.getProjectPath('.wordup','config.yml'), 'utf8'))
      this.dotWordupJson = this.dotWordupYml.toJSON()
    } catch (err) {
      this.error('Could not parse wordup config: '+err, {exit:1})
    }
fork icon15
star icon119
watch icon0

48
49
50
51
52
53
54
55
56
57
58
59


let developmentYaml
let productionYaml


if (fs.existsSync(stackDevelopmentPath)) {
  developmentYaml = yaml.parseDocument(
    fs.readFileSync(stackDevelopmentPath, 'utf8')
  )
} else {
  console.error('Development stack file not found!')
fork icon2
star icon0
watch icon1

+ 13 other calls in file

How does yaml.parseDocument work?

yaml.parseDocument works by taking a YAML document string as its input and returning a YAML document object representing the parsed document. The function first reads the YAML string and performs lexical and syntactic analysis to identify the different YAML nodes, such as scalars, sequences, and mappings, that make up the document. It then constructs a tree-like structure, where each node is represented by a node object in the tree and the relationships between the nodes reflect the parent-child relationships between the nodes in the document. Each node object in the tree has a type, which can be a scalar, sequence, mapping, or a document, and various properties that describe its value, anchor, tag, and style. Once the tree is constructed, the resulting YAML document object can be traversed and manipulated using JavaScript. Note that yaml.parseDocument is part of the js-yaml library, which provides a set of utility functions for working with YAML documents in JavaScript, and supports various YAML versions and features.

244
245
246
247
248
249
250
251
252
253
if(filter && !service_name.includes(filter))
  continue;

let service_current = await this._read_remote_state(service_name);

let doc = parseDocument(service_current, {merge : true});
deepMixIn(remote_stack, doc.toJS({maxAliasCount : -1 }));

let stack_slice = {
  version,
fork icon1
star icon1
watch icon2

+ 17 other calls in file

131
132
133
134
135
136
137
138
139
140

if (openapi.exists()) {
  // Can't use the yaml provided by mrm-core due to a bug that messes up
  // existing file formatting and comments.
  const output = file(openapiFile);
  const yamlDoc = parseDocument(output.get(), { keepSourceTokens: true });
  yamlDoc.set("info", info);
  output.save(yamlDoc.toString());
} else {
  // If the file didn't already exist, just set the info object and save the
fork icon0
star icon1
watch icon1

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const yaml = require("js-yaml");

const yamlString = `
name: John Doe
age: 30
address:
street: 123 Main St
city: Anytown
state: CA
zip: 12345
`;

const doc = yaml.parseDocument(yamlString);

console.log(doc.toJSON());

In this example, we first import the js-yaml library and define a YAML string representing a person's name, age, and address. We then use yaml.parseDocument to create a new YAML document object representing the parsed YAML structure. We can now access and manipulate the elements of the document using JavaScript. For example, to get the person's name from the document, we can use the following code: javascript Copy code

231
232
233
234
235
236
237
238
239
240
const dest = get(this.stores, `${store}.file`);

// if this is a yaml file then lets try to reconcile comments and data
if (['.yaml', '.yml'].includes(extname(dest))) {
  // load the yaml doc
  const yamlDoc = yaml.parseDocument(fs.readFileSync(dest, 'utf8'));
  // remove the prop
  yamlDoc.deleteIn(path.split('.'));

  // write the result
fork icon0
star icon1
watch icon0

+ 31 other calls in file

86
87
88
89
90
91
92
93
94
// Main program
fs.readFile(args[0], "utf8", function (err, data) {
    if (err) {
        throw err;
    }
    var composeFile = YAML.parseDocument(data);
    sortServices(composeFile);
    console.log(composeFile.toString().replace(/\\\n\s+/g, ''));
});
fork icon0
star icon1
watch icon0

287
288
289
290
291
292
293
294
295
296
297
 * @param {String} str source yaml string to parse
 *
 * @private
 */
YamlFile.prototype._parseComments = function(prefix, str) {
    var document = yaml.parseDocument(str);


    document.contents.items.forEach(node => {
        this._parseNodeComment(prefix, node);
    });
fork icon0
star icon0
watch icon3

57
58
59
60
61
62
63
64
65
66
const { parseAllDocuments, parseDocument } = require("yaml");

const run = async () => {
  try {
    const file   = fs.readFileSync("../../metadata/sns_metadata.yaml", "utf8");
    const doc = parseDocument(file, {lineCounter: true});
    const result = doc.toJSON();
    console.log(result)
  } catch (err) {
    console.log("something happened");
fork icon0
star icon0
watch icon0

3
4
5
6
7
8
9
10
11
12
13
14
15


const yaml = require('yaml');
const yaml_types = require('yaml/types');


function parse_document(file){
  return yaml.parseDocument(file);
}


function append_paths(root, target, name){
  var map = target.get('paths');
fork icon0
star icon0
watch icon0

69
70
71
72
73
74
75
76
77
78
79


if( fs.existsSync(fname) ){
  const stats_file = fs.statSync(fname);
  if( stats_file.isFile() ){
    try{
      const swagger = yaml.parseDocument(fs.readFileSync(fname, 'utf-8'));
      const base_path = swagger.get('basePath');
      if( base_path )
        swagger_basePath = base_path;

fork icon0
star icon0
watch icon0

+ 2 other calls in file