Search code examples
yamljekyllliquidjekyll-theme

How can I output social url's in JSON+LD from a yaml data file?


My code will capture items from the data file. I need those to be comma separated. I've had no luck!

Data File 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'

I have tried this. It doesn't comma separate them, though:

 "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 %}
   ],

Desired Output Format:

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

Actual Invalid Output:

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

Solution

  • You have to check if an item is the last in the 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 %}
     ],