preface

When we use VuePress to write documents, we will come into contact with YAML. I will introduce you to know YAML and YAML Front Matter in VuePress. If it is helpful to you, please remember to like and follow it, let us continue to improve together. Don’t miss every piece.

What is a YAML

YAML(YAML Ain’t Markup Language) file, which is not a Markup language. Previously, our configuration files were XML, properties, but YAML is more data-centric and suitable for configuration files

YAML has a similar syntax to other high-level languages and can easily express data forms such as lists, hash tables, and scalars. Its use of whitespace indentation and a number of cosmetic features makes it particularly suitable for expressing or editing data structures, various configuration files, debugging content, and file Outlines (for example, many E-mail header formats are very close to YAML).

The extension of the YAML configuration file is.yml, for example, application.yml

The basic grammar

  • Case sensitivity
  • Use indentation to indicate hierarchy
  • Indent cannot be created using TAB, only space bar
  • The number of indent Spaces doesn’t matter, as long as elements of the same rank are left aligned
  • ‘#’ indicates a comment

The data type

YAML supports the following data types:

  • constant
  • Objects and MAP
  • An array of
  • Composite structure
  • Special symbols

constant

Scalar quantities are the most basic, non-separable values, including:

  • string
  • Boolean value
  • The integer
  • Floating point Numbers
  • Null
  • time
  • The date of

Example:

boolean: 
    - TRUE    # true, true
    - FALSE   #false, false will do
float:
    - 3.14
    - 6.8523015 e+5  Scientific notation can be used
int:
    - 123
    - 0b1010_0111_0100_1010_1110    # binary representation
null:
    nodeName: 'node'
    parent: ~  # use ~ to indicate null
string:
    - Fast intellectual island
    - 'Hello world'  You can use double or single quotation marks around special characters
    - newline
      newline2    The # string can be broken into multiple lines, and each line is converted to a space
date:
    - 2021-06-26    The date must be in ISO 8601 format, yyyY-MM-DD
datetime: 
    -  2018-02-17T15:02:31+08:00    The time is in ISO 8601 format, with T concatenation between time and date, and + for time zone at the end
Copy the code

YAML object

Object key-value pairs use the colon structure for key: value, followed by a space.

You can also use key:{key1: value1, key2: value2… }.

You can also use indentation to indicate hierarchy;

key: 
    child-key1: value1
    child-key2: value2
Copy the code

YAML also supports the flow syntax for representing objects.

key:{child-key1: value1,child-key2: value2}
Copy the code

For more complex object formats, you can use a question mark with a space for a complex key, and a colon with a space for a value:

?  
    - complexkey1
    - complexkey2
:
    - complexvalue1
    - complexvalue2
Copy the code

An array of properties [complexkey1,complexkey2] The corresponding value is an array [complexvalue1 complexvalue2] using streaming syntax is the [complexkey1 complexkey2] : [complexvalue1 complexvalue2]

  • MAP uses key-value pairs
# Common writing
name: kuaizhidao
age: 12
# inline
`{name: kuaizhidao`,age: 12}
Copy the code

YAML array

Lines beginning with – form an array:

key:
  - A
  - B
  - C
Copy the code

YAML supports multidimensional arrays, which can be expressed inline:

Key: [A, B,C] if the child members of the data structure are an array, you can indent A space below the item.

-
 - A
 - B
 - C
Copy the code

YAML supports multidimensional arrays, which can be expressed inline:

[[A, B,C]]
Copy the code

A relatively complicated example:

companies:
    -
        id: 1
        name: company1
        price: 200W
    -
        id: 2
        name: company2
        price: 500W
Copy the code

The companies attribute is an array, and each array element is composed of three attributes: ID, name and price.

Arrays can also be represented as flows:

companies: [
  {id: 1,name: companyA,price: 1200W},
  {id: 2,name: companyB,price: 1500W}
]
Copy the code

Composite structure

Arrays and objects can form composite structures

languages:
  - Ruby
  - Perl
  - Python 
websites:
  YAML: yaml.org 
  Ruby: ruby-lang.org 
  Python: python.org 
  Perl: use.perl.org
Copy the code

Convert to JSON to:

{ 
  languages: [ 'Ruby', 'Perl', 'Python'],
  websites: {
    YAML: 'yaml.org',
    Ruby: 'ruby-lang.org',
    Python: 'python.org',
    Perl: 'use.perl.org' 
  } 
}
Copy the code

Special symbols

  1. Yaml can be used in the same file to indicate the beginning of a document:
  2. . Used in combination with the end of a file in a configuration file
  3. !!!!! Used in YAML!! Do a type cast
  4. Fold line breaks in the string, | reserve line, the two symbols are string in a yaml often use of symbols

  5. In YAML you can use ampersand for anchor definition and * for anchor reference
  6. Merge content. Used mainly with anchor points, you can merge the contents of an anchor point directly into an object

reference

& anchor points and * aliases, which can be used to reference:

defaults: &defaults
  adapter:  postgres
  host:     localhost

development:
  database: myapp_development
  < < : *defaults

test:
  database: myapp_test
  < < : *defaults
Is equivalent to:

defaults:
  adapter:  postgres
  host:     localhost

development:
  database: myapp_development
  adapter:  postgres
  host:     localhost

test:
  database: myapp_test
  adapter:  postgres
  host:     localhost
Copy the code

& is used to create anchor points, << means to merge into current data, and * refers to anchor points.

Here’s another example:

- &showell Steve 
- Clark 
- Brian 
- Oren 
- *showell 
Copy the code

The code for converting to JavaScript is as follows:

[ 'Steve', 'Clark', 'Brian', 'Oren', 'Steve' ]
Copy the code