Jekyll YAML cyclic reference

蓝咒 提交于 2019-12-22 09:47:09

问题


I have a jekyll project with two pages, each backed by YAML maps that both reference each other. For example:

a: &a
  name: "Ay"
  parents: []
  children: [*b]

b: &b 
  name: "Bee"
  parents: [*a]
  children: []

Vanilla YAML seems not to support using an alias/anchor before its been defined, which invalidates this strategy. Is there any way, perhaps using liquid-fu that would let me generate pages that enumerate an entry's parents and children?


回答1:


You just need to give the value on first occurrence:

a: &a
  name: "Ay"
  parents: []
  children:
    - &b
      name: "Bee"
      parents: [*a]
      children: []
b: *b

The alias/anchor construct has been designed specifically for this use-case. Since the parsed YAML data does not distinguish the place where the object is anchored and the place where it is referenced, this is equivalent to what you want to have.




回答2:


As the question above is worded, @flyx’s is the most appropriate answer, however given external constraints (see my other question) I ended writing my own plugin to let data files textually include one another through liquid.

The goals of this plugin are to let the data be:

  1. DRY - (don’t repeat yourself) each model should be defined only once.
  2. Grouped - all similar data should be defined in the same format next to each other.
  3. Separated - different data should be defined in different places.

@flyx’s solutions here fail goals #2 and #3, requiring all different types of data to be defined in the same place and intermixing the definitions of foods and ingredients.

My proposed solution allows textual inclusion of one data file into another. This allows different models to be defined in different files, yet referenced from other files as if they were defined in the same place, in an arbitrary order. Applied to this problem, my solution would like this:

A.yml

{% include_relative_once _data/B.yml %}

a: &a
  name: "Ay"
  parents: []
  children: [*b]

B.yml

{% include_relative_once _data/A.yml %}

b: &b 
  name: "Bee"
  parents: [*a]
  children: []

For the plugin itself, see this gist



来源:https://stackoverflow.com/questions/42102792/jekyll-yaml-cyclic-reference

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!