Checking for tag duplicates with jekyll excludes tags that are contained in other tags












1















I am trying to build a (simple, unweighted) tag cloud with liquid in my jekyll site. The tag cloud renders fine when I use this code:



{% assign sitetags = "" %}
{% for page in site.pages %}
{% for tag in page.tags %}
{% unless sitetags contains tag %}
{% assign sitetags = sitetags | append:tag | append:', ' %}
{% endunless %}
{% endfor %}
{% endfor %}

{% assign sitetags = sitetags | split:', ' | sort %}
{% for tag in sitetags %}
{% capture tag_name %}{{ tag }}{% endcapture %}
<div>
<a href="/tag/{{ tag_name }}">{{ tag_name }}</a>
</div>
{% endfor %}


However, there is one issue: if the name of a tag is contained within another tag, it won't show up in the tag cloud. For example, "art" is contained in "art-history" so it does not show up. In order to deal with this, I tried to fix it, however, it is not working. Here is my code:



{% assign sitetags = "" %}
{% for page in site.pages %}
{% for tag in page.tags %}
{% if sitetags contains tag %}
{% assign sitetags = sitetags | split:', ' %}
{% assign truetag = true %}
{% for taggo in sitetags %}
{% if taggo != tag %}
{% continue %}
{% else %}
{% assign truetag = false %}
{% break %}
{% endif %}
{% endfor %}
{% if truetag == true %}
{% assign sitetags = sitetags | append:tag | append:', ' %}
{% endif %}
{% else %}
{% assign sitetags = sitetags | append:tag | append:', ' %}
{% endif %}
{% endfor %}
{% endfor %}

{% assign sitetags = sitetags | split:', ' | sort %}
{% for tag in sitetags %}
{% capture tag_name %}{{ tag }}{% endcapture %}
<div>
<a href="/tag/{{ tag }}">{{ tag }}</a>
</div>
{% endfor %}


The reason I am attempting it this way as opposed to just using site.tags is because I have articles/essays in other directories that I am trying to include. When I use site.tags, it only includes the tags that are on the blog posts but not the essays. Essentially, my site has both regular blog posts under /_blog and also essays in a different location, but I want to aggregate the tags of all of these in the same tag cloud.



I would really appreciate advice on what I am doing wrong or how to fix it. Thanks!










share|improve this question





























    1















    I am trying to build a (simple, unweighted) tag cloud with liquid in my jekyll site. The tag cloud renders fine when I use this code:



    {% assign sitetags = "" %}
    {% for page in site.pages %}
    {% for tag in page.tags %}
    {% unless sitetags contains tag %}
    {% assign sitetags = sitetags | append:tag | append:', ' %}
    {% endunless %}
    {% endfor %}
    {% endfor %}

    {% assign sitetags = sitetags | split:', ' | sort %}
    {% for tag in sitetags %}
    {% capture tag_name %}{{ tag }}{% endcapture %}
    <div>
    <a href="/tag/{{ tag_name }}">{{ tag_name }}</a>
    </div>
    {% endfor %}


    However, there is one issue: if the name of a tag is contained within another tag, it won't show up in the tag cloud. For example, "art" is contained in "art-history" so it does not show up. In order to deal with this, I tried to fix it, however, it is not working. Here is my code:



    {% assign sitetags = "" %}
    {% for page in site.pages %}
    {% for tag in page.tags %}
    {% if sitetags contains tag %}
    {% assign sitetags = sitetags | split:', ' %}
    {% assign truetag = true %}
    {% for taggo in sitetags %}
    {% if taggo != tag %}
    {% continue %}
    {% else %}
    {% assign truetag = false %}
    {% break %}
    {% endif %}
    {% endfor %}
    {% if truetag == true %}
    {% assign sitetags = sitetags | append:tag | append:', ' %}
    {% endif %}
    {% else %}
    {% assign sitetags = sitetags | append:tag | append:', ' %}
    {% endif %}
    {% endfor %}
    {% endfor %}

    {% assign sitetags = sitetags | split:', ' | sort %}
    {% for tag in sitetags %}
    {% capture tag_name %}{{ tag }}{% endcapture %}
    <div>
    <a href="/tag/{{ tag }}">{{ tag }}</a>
    </div>
    {% endfor %}


    The reason I am attempting it this way as opposed to just using site.tags is because I have articles/essays in other directories that I am trying to include. When I use site.tags, it only includes the tags that are on the blog posts but not the essays. Essentially, my site has both regular blog posts under /_blog and also essays in a different location, but I want to aggregate the tags of all of these in the same tag cloud.



    I would really appreciate advice on what I am doing wrong or how to fix it. Thanks!










    share|improve this question



























      1












      1








      1








      I am trying to build a (simple, unweighted) tag cloud with liquid in my jekyll site. The tag cloud renders fine when I use this code:



      {% assign sitetags = "" %}
      {% for page in site.pages %}
      {% for tag in page.tags %}
      {% unless sitetags contains tag %}
      {% assign sitetags = sitetags | append:tag | append:', ' %}
      {% endunless %}
      {% endfor %}
      {% endfor %}

      {% assign sitetags = sitetags | split:', ' | sort %}
      {% for tag in sitetags %}
      {% capture tag_name %}{{ tag }}{% endcapture %}
      <div>
      <a href="/tag/{{ tag_name }}">{{ tag_name }}</a>
      </div>
      {% endfor %}


      However, there is one issue: if the name of a tag is contained within another tag, it won't show up in the tag cloud. For example, "art" is contained in "art-history" so it does not show up. In order to deal with this, I tried to fix it, however, it is not working. Here is my code:



      {% assign sitetags = "" %}
      {% for page in site.pages %}
      {% for tag in page.tags %}
      {% if sitetags contains tag %}
      {% assign sitetags = sitetags | split:', ' %}
      {% assign truetag = true %}
      {% for taggo in sitetags %}
      {% if taggo != tag %}
      {% continue %}
      {% else %}
      {% assign truetag = false %}
      {% break %}
      {% endif %}
      {% endfor %}
      {% if truetag == true %}
      {% assign sitetags = sitetags | append:tag | append:', ' %}
      {% endif %}
      {% else %}
      {% assign sitetags = sitetags | append:tag | append:', ' %}
      {% endif %}
      {% endfor %}
      {% endfor %}

      {% assign sitetags = sitetags | split:', ' | sort %}
      {% for tag in sitetags %}
      {% capture tag_name %}{{ tag }}{% endcapture %}
      <div>
      <a href="/tag/{{ tag }}">{{ tag }}</a>
      </div>
      {% endfor %}


      The reason I am attempting it this way as opposed to just using site.tags is because I have articles/essays in other directories that I am trying to include. When I use site.tags, it only includes the tags that are on the blog posts but not the essays. Essentially, my site has both regular blog posts under /_blog and also essays in a different location, but I want to aggregate the tags of all of these in the same tag cloud.



      I would really appreciate advice on what I am doing wrong or how to fix it. Thanks!










      share|improve this question
















      I am trying to build a (simple, unweighted) tag cloud with liquid in my jekyll site. The tag cloud renders fine when I use this code:



      {% assign sitetags = "" %}
      {% for page in site.pages %}
      {% for tag in page.tags %}
      {% unless sitetags contains tag %}
      {% assign sitetags = sitetags | append:tag | append:', ' %}
      {% endunless %}
      {% endfor %}
      {% endfor %}

      {% assign sitetags = sitetags | split:', ' | sort %}
      {% for tag in sitetags %}
      {% capture tag_name %}{{ tag }}{% endcapture %}
      <div>
      <a href="/tag/{{ tag_name }}">{{ tag_name }}</a>
      </div>
      {% endfor %}


      However, there is one issue: if the name of a tag is contained within another tag, it won't show up in the tag cloud. For example, "art" is contained in "art-history" so it does not show up. In order to deal with this, I tried to fix it, however, it is not working. Here is my code:



      {% assign sitetags = "" %}
      {% for page in site.pages %}
      {% for tag in page.tags %}
      {% if sitetags contains tag %}
      {% assign sitetags = sitetags | split:', ' %}
      {% assign truetag = true %}
      {% for taggo in sitetags %}
      {% if taggo != tag %}
      {% continue %}
      {% else %}
      {% assign truetag = false %}
      {% break %}
      {% endif %}
      {% endfor %}
      {% if truetag == true %}
      {% assign sitetags = sitetags | append:tag | append:', ' %}
      {% endif %}
      {% else %}
      {% assign sitetags = sitetags | append:tag | append:', ' %}
      {% endif %}
      {% endfor %}
      {% endfor %}

      {% assign sitetags = sitetags | split:', ' | sort %}
      {% for tag in sitetags %}
      {% capture tag_name %}{{ tag }}{% endcapture %}
      <div>
      <a href="/tag/{{ tag }}">{{ tag }}</a>
      </div>
      {% endfor %}


      The reason I am attempting it this way as opposed to just using site.tags is because I have articles/essays in other directories that I am trying to include. When I use site.tags, it only includes the tags that are on the blog posts but not the essays. Essentially, my site has both regular blog posts under /_blog and also essays in a different location, but I want to aggregate the tags of all of these in the same tag cloud.



      I would really appreciate advice on what I am doing wrong or how to fix it. Thanks!







      arrays tags jekyll liquid






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 27 '18 at 3:27







      leucotic

















      asked Nov 27 '18 at 3:19









      leucoticleucotic

      64




      64
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Instead of using a string to store your tags, you can use an array.



          {% assign sitetags = "" | split:"" %}
          {% for page in site.pages %}
          {% if page.tags.size %}
          {% assign sitetags = sitetags | concat: page.tags %}
          {{ sitetags | inspect }} <br>
          {% endif %}
          {% endfor %}

          {% assign sitetags = sitetags | sort | uniq %}
          {{ sitetags | inspect }} <br>

          {% for tag in sitetags %}
          <div>
          <a href="/tag/{{ tag }}">{{ tag }}</a>
          </div>
          {% endfor %}





          share|improve this answer
























          • This answer could use an explanation for why it's an improvement to use an array. (I think it has something to do with the way contains works for looking things up in the string used in the questioner's code, but I think the answer would be more educational if it spelled things out.)

            – Robert Del Favero
            Nov 30 '18 at 1:02











          • I tried this out but it didn't work, got weird garbled mess output. I can't find any liquid documentation about the inspect filter.

            – leucotic
            Dec 2 '18 at 6:26











          • inspect is a jekyll filter, you can use it to debug by seeing what's happening behind the scene.

            – David Jacquel
            Dec 2 '18 at 9:54











          • ohh I see, thank you!

            – leucotic
            Dec 2 '18 at 16:11



















          0














          I was able to figure this out myself actually. Part of the issue was in initializing the array, so I ended up working with a string instead because I just couldn't get the arrays to work for some reason. I skipped over initialization and went straight to adding items to it. After that all I had to do was just run the uniq filter and it filtered out all the duplicates, so I didn't have to do any string comparison or anything like that.



          Here is the solution:



          {% for post in site.pages %}
          {% for tag in post.tags %}
          {% assign tagcloud = tagcloud | append:tag | append:', ' %}
          {% endfor %}
          {% endfor %}

          {% assign tagcloud = tagcloud | split:", " | uniq | sort %}

          {% for tag in tagcloud %}
          {% capture tag_name %}{{ tag }}{% endcapture %}
          <div>
          <a href="/tag/{{ tag_name }}">{{ tag_name }}</a>
          </div>
          {% endfor %}





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53492238%2fchecking-for-tag-duplicates-with-jekyll-excludes-tags-that-are-contained-in-othe%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Instead of using a string to store your tags, you can use an array.



            {% assign sitetags = "" | split:"" %}
            {% for page in site.pages %}
            {% if page.tags.size %}
            {% assign sitetags = sitetags | concat: page.tags %}
            {{ sitetags | inspect }} <br>
            {% endif %}
            {% endfor %}

            {% assign sitetags = sitetags | sort | uniq %}
            {{ sitetags | inspect }} <br>

            {% for tag in sitetags %}
            <div>
            <a href="/tag/{{ tag }}">{{ tag }}</a>
            </div>
            {% endfor %}





            share|improve this answer
























            • This answer could use an explanation for why it's an improvement to use an array. (I think it has something to do with the way contains works for looking things up in the string used in the questioner's code, but I think the answer would be more educational if it spelled things out.)

              – Robert Del Favero
              Nov 30 '18 at 1:02











            • I tried this out but it didn't work, got weird garbled mess output. I can't find any liquid documentation about the inspect filter.

              – leucotic
              Dec 2 '18 at 6:26











            • inspect is a jekyll filter, you can use it to debug by seeing what's happening behind the scene.

              – David Jacquel
              Dec 2 '18 at 9:54











            • ohh I see, thank you!

              – leucotic
              Dec 2 '18 at 16:11
















            0














            Instead of using a string to store your tags, you can use an array.



            {% assign sitetags = "" | split:"" %}
            {% for page in site.pages %}
            {% if page.tags.size %}
            {% assign sitetags = sitetags | concat: page.tags %}
            {{ sitetags | inspect }} <br>
            {% endif %}
            {% endfor %}

            {% assign sitetags = sitetags | sort | uniq %}
            {{ sitetags | inspect }} <br>

            {% for tag in sitetags %}
            <div>
            <a href="/tag/{{ tag }}">{{ tag }}</a>
            </div>
            {% endfor %}





            share|improve this answer
























            • This answer could use an explanation for why it's an improvement to use an array. (I think it has something to do with the way contains works for looking things up in the string used in the questioner's code, but I think the answer would be more educational if it spelled things out.)

              – Robert Del Favero
              Nov 30 '18 at 1:02











            • I tried this out but it didn't work, got weird garbled mess output. I can't find any liquid documentation about the inspect filter.

              – leucotic
              Dec 2 '18 at 6:26











            • inspect is a jekyll filter, you can use it to debug by seeing what's happening behind the scene.

              – David Jacquel
              Dec 2 '18 at 9:54











            • ohh I see, thank you!

              – leucotic
              Dec 2 '18 at 16:11














            0












            0








            0







            Instead of using a string to store your tags, you can use an array.



            {% assign sitetags = "" | split:"" %}
            {% for page in site.pages %}
            {% if page.tags.size %}
            {% assign sitetags = sitetags | concat: page.tags %}
            {{ sitetags | inspect }} <br>
            {% endif %}
            {% endfor %}

            {% assign sitetags = sitetags | sort | uniq %}
            {{ sitetags | inspect }} <br>

            {% for tag in sitetags %}
            <div>
            <a href="/tag/{{ tag }}">{{ tag }}</a>
            </div>
            {% endfor %}





            share|improve this answer













            Instead of using a string to store your tags, you can use an array.



            {% assign sitetags = "" | split:"" %}
            {% for page in site.pages %}
            {% if page.tags.size %}
            {% assign sitetags = sitetags | concat: page.tags %}
            {{ sitetags | inspect }} <br>
            {% endif %}
            {% endfor %}

            {% assign sitetags = sitetags | sort | uniq %}
            {{ sitetags | inspect }} <br>

            {% for tag in sitetags %}
            <div>
            <a href="/tag/{{ tag }}">{{ tag }}</a>
            </div>
            {% endfor %}






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 27 '18 at 5:49









            David JacquelDavid Jacquel

            37.8k377107




            37.8k377107













            • This answer could use an explanation for why it's an improvement to use an array. (I think it has something to do with the way contains works for looking things up in the string used in the questioner's code, but I think the answer would be more educational if it spelled things out.)

              – Robert Del Favero
              Nov 30 '18 at 1:02











            • I tried this out but it didn't work, got weird garbled mess output. I can't find any liquid documentation about the inspect filter.

              – leucotic
              Dec 2 '18 at 6:26











            • inspect is a jekyll filter, you can use it to debug by seeing what's happening behind the scene.

              – David Jacquel
              Dec 2 '18 at 9:54











            • ohh I see, thank you!

              – leucotic
              Dec 2 '18 at 16:11



















            • This answer could use an explanation for why it's an improvement to use an array. (I think it has something to do with the way contains works for looking things up in the string used in the questioner's code, but I think the answer would be more educational if it spelled things out.)

              – Robert Del Favero
              Nov 30 '18 at 1:02











            • I tried this out but it didn't work, got weird garbled mess output. I can't find any liquid documentation about the inspect filter.

              – leucotic
              Dec 2 '18 at 6:26











            • inspect is a jekyll filter, you can use it to debug by seeing what's happening behind the scene.

              – David Jacquel
              Dec 2 '18 at 9:54











            • ohh I see, thank you!

              – leucotic
              Dec 2 '18 at 16:11

















            This answer could use an explanation for why it's an improvement to use an array. (I think it has something to do with the way contains works for looking things up in the string used in the questioner's code, but I think the answer would be more educational if it spelled things out.)

            – Robert Del Favero
            Nov 30 '18 at 1:02





            This answer could use an explanation for why it's an improvement to use an array. (I think it has something to do with the way contains works for looking things up in the string used in the questioner's code, but I think the answer would be more educational if it spelled things out.)

            – Robert Del Favero
            Nov 30 '18 at 1:02













            I tried this out but it didn't work, got weird garbled mess output. I can't find any liquid documentation about the inspect filter.

            – leucotic
            Dec 2 '18 at 6:26





            I tried this out but it didn't work, got weird garbled mess output. I can't find any liquid documentation about the inspect filter.

            – leucotic
            Dec 2 '18 at 6:26













            inspect is a jekyll filter, you can use it to debug by seeing what's happening behind the scene.

            – David Jacquel
            Dec 2 '18 at 9:54





            inspect is a jekyll filter, you can use it to debug by seeing what's happening behind the scene.

            – David Jacquel
            Dec 2 '18 at 9:54













            ohh I see, thank you!

            – leucotic
            Dec 2 '18 at 16:11





            ohh I see, thank you!

            – leucotic
            Dec 2 '18 at 16:11













            0














            I was able to figure this out myself actually. Part of the issue was in initializing the array, so I ended up working with a string instead because I just couldn't get the arrays to work for some reason. I skipped over initialization and went straight to adding items to it. After that all I had to do was just run the uniq filter and it filtered out all the duplicates, so I didn't have to do any string comparison or anything like that.



            Here is the solution:



            {% for post in site.pages %}
            {% for tag in post.tags %}
            {% assign tagcloud = tagcloud | append:tag | append:', ' %}
            {% endfor %}
            {% endfor %}

            {% assign tagcloud = tagcloud | split:", " | uniq | sort %}

            {% for tag in tagcloud %}
            {% capture tag_name %}{{ tag }}{% endcapture %}
            <div>
            <a href="/tag/{{ tag_name }}">{{ tag_name }}</a>
            </div>
            {% endfor %}





            share|improve this answer




























              0














              I was able to figure this out myself actually. Part of the issue was in initializing the array, so I ended up working with a string instead because I just couldn't get the arrays to work for some reason. I skipped over initialization and went straight to adding items to it. After that all I had to do was just run the uniq filter and it filtered out all the duplicates, so I didn't have to do any string comparison or anything like that.



              Here is the solution:



              {% for post in site.pages %}
              {% for tag in post.tags %}
              {% assign tagcloud = tagcloud | append:tag | append:', ' %}
              {% endfor %}
              {% endfor %}

              {% assign tagcloud = tagcloud | split:", " | uniq | sort %}

              {% for tag in tagcloud %}
              {% capture tag_name %}{{ tag }}{% endcapture %}
              <div>
              <a href="/tag/{{ tag_name }}">{{ tag_name }}</a>
              </div>
              {% endfor %}





              share|improve this answer


























                0












                0








                0







                I was able to figure this out myself actually. Part of the issue was in initializing the array, so I ended up working with a string instead because I just couldn't get the arrays to work for some reason. I skipped over initialization and went straight to adding items to it. After that all I had to do was just run the uniq filter and it filtered out all the duplicates, so I didn't have to do any string comparison or anything like that.



                Here is the solution:



                {% for post in site.pages %}
                {% for tag in post.tags %}
                {% assign tagcloud = tagcloud | append:tag | append:', ' %}
                {% endfor %}
                {% endfor %}

                {% assign tagcloud = tagcloud | split:", " | uniq | sort %}

                {% for tag in tagcloud %}
                {% capture tag_name %}{{ tag }}{% endcapture %}
                <div>
                <a href="/tag/{{ tag_name }}">{{ tag_name }}</a>
                </div>
                {% endfor %}





                share|improve this answer













                I was able to figure this out myself actually. Part of the issue was in initializing the array, so I ended up working with a string instead because I just couldn't get the arrays to work for some reason. I skipped over initialization and went straight to adding items to it. After that all I had to do was just run the uniq filter and it filtered out all the duplicates, so I didn't have to do any string comparison or anything like that.



                Here is the solution:



                {% for post in site.pages %}
                {% for tag in post.tags %}
                {% assign tagcloud = tagcloud | append:tag | append:', ' %}
                {% endfor %}
                {% endfor %}

                {% assign tagcloud = tagcloud | split:", " | uniq | sort %}

                {% for tag in tagcloud %}
                {% capture tag_name %}{{ tag }}{% endcapture %}
                <div>
                <a href="/tag/{{ tag_name }}">{{ tag_name }}</a>
                </div>
                {% endfor %}






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 2 '18 at 6:30









                leucoticleucotic

                64




                64






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53492238%2fchecking-for-tag-duplicates-with-jekyll-excludes-tags-that-are-contained-in-othe%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Futebolista

                    Lallio

                    Jornalista