如何从 yaml 数据文件中输出 JSON+LD 中的社交网址?



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

数据文件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" ],

您必须检查项目是否是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 %}
],

最新更新