如何从Yaml数据文件以JSON + LD输出社交网址?

时间:2019-09-12 22:50:48

标签: yaml jekyll liquid jekyll-theme

我的代码将从数据文件中捕获项目。我需要将它们用逗号分隔。我没有运气!

数据文件socialmedia.yml

facebook:
  id: 'dpcgco'
  href: 'https://www.facebook.com/'
  title: 'Facebook'
  fa-icon: 'fa-facebook-square'

twitter:
  id: 'DenverProphitJr'
  href: 'https://www.twitter.com/'
  title: 'Twitter'
  fa-icon: 'fa-twitter-square'

我已经尝试过了。不过,它不会以逗号分隔:

 "sameAs":[ 
{% if site.data.socialmedia %}
    {% assign sm = site.data.socialmedia %}
    {% for entry in sm %}
        {% assign key = entry | first | split%}
        {% if sm[key].id %}
{% capture social %}{{ sm[key].href }}{{ sm[key].id }}{% endcapture %}
    {{ social | replace: " ", "," | jsonify }}
        {% endif %}
    {% endfor %}
{% endif %}
   ],

所需的输出格式:

 "sameAs": [
    "http://www.facebook.com/your-profile",
    "http://instagram.com/yourProfile",
    "http://www.linkedin.com/in/yourprofile",
    "http://plus.google.com/your_profile"
  ]

实际无效的输出:

"sameAs":[ "https://www.facebook.com/dpcgco" "https://www.twitter.com/DenverProphitJr" ],

1 个答案:

答案 0 :(得分:1)

您必须检查某个项目是否是forloop.last中的最后一个项目。

  {% if site.data.socialmedia %}
  {% assign sm = site.data.socialmedia %}
"sameAs":[
    {% for entry in sm %}
      {% assign key = entry | first %}
        {% if sm[key].id %}"{{ sm[key].href }}
        {{ sm[key].id }}",
        {% if forloop.last %}
        "{{ sm[key].href }}{{ sm[key].id }}"
       {% endif %}
    {% endif %}
  {% endfor %}
  {% endif %}
 ],