avillafiorita / jekyll-datapage_gen

Generate one page per yaml record in Jekyll sites.
369 stars 80 forks source link

Wrong subdirectory when generating links in subdir #51

Closed thmsklngr closed 4 years ago

thmsklngr commented 5 years ago

Hi,

the title might be a little confusing or misleading, however, I found a potential bug in this excellent plugin.

For instance, I created a subdirectory called inktober, created an index.html file there with the following content:

---
layout: default
---

<section class="section">
    <div class="container">
        {% for image in site.data.inktober %}
        <article class="media">
            <div class="media-content">
                <div class="content">
                    <p class="is-size-3 title"><strong><a href="{{ image.title | datapage_url: 'inktober' }}">{{ image.title }}</a></strong></p>
                    <p class="subtitle is-size-6">Veröffentlicht am: {{ image.date }}</p>
                </div>
            </div>
        </article>
        {% endfor %}
    </div>
</section>

Now, when building the website it will create links to /inktober/inktober/whatevertitle/. Calling the same code from a file called /inktober.html (in the website root), the links to the corresponding links are correct set as /inktober/whatevertitle/. Means that the link creating expects that the caller for reading the data resides in the webroot directory of the site, or it will add the subdirectory of the caller as well.

Since I'm not a Ruby programmer, I can't provide a fix for this right now. I can just report it.

Regards, Thomas

avillafiorita commented 5 years ago

Hi, thanks for reporting! It is not clear to me what could originate the problem, but I'll have a look and let you know. In the meantime, could you also add the relevant part of the _config.yml file, so that I can try and reproduce the issue?

thmsklngr commented 5 years ago

Hi,

here we go:

baseurl: "/"
# Build settings
markdown: kramdown
plugins:
  - jekyll-feed
  - jekyll-email-protect
  - jekyll-seo-tag
# Exclude from processing.
# The following items will not be processed, by default. Create a custom list
# to override the default setting.
# exclude:
#   - Gemfile
#   - Gemfile.lock
#   - node_modules
#   - vendor/bundle/
#   - vendor/cache/
#   - vendor/gems/
#   - vendor/ruby/

defaults:
    - scope:
        path: "images"
      values:
        image: true
    - scope:
        path: "assets/img"
      values:
        image: true

page_gen:
    - index_files: false
      data: inktober
      template: image
      name: title
      filter: published

I left out the settings for author and other stuff, but all other settings from the configfile you see here.

Regards, Thomas

avillafiorita commented 5 years ago

Hi, you are right; there are two simple fixes.

Fix 1. Prepend a "/" to the path in index.html:

<a href="{{ image.title | datapage_url: '/inktober' }}" ...

this makes the URL absolute wrt the root of your website. If you want, you can also prepend {{site.url}} to also add the domain name:

<a href="{{site.url}}{{ image.title | datapage_url: '/inktober' }}" ...

Fix 2. Don't use datapage_url for the URLs in index.html:

<a href="{{ image.title}}"> ...

this makes the URL relative to that of index.html.

let me know if it solves your issue!

ps. as a side remark: notice that if you want to generate links for the "published" records only (like the filter specification in _config.yml seems to suggest), you might want to add an "if" condition in the loop in index.html (double check the liquid, I did not try it myself):

 {% for image in site.data.inktober %}
 {% if image.published %}
        <article class="media">
            <div class="media-content">
                <div class="content">
                    <p class="is-size-3 title"><strong><a href="{{ image.title | datapage_url: 'inktober' }}">{{ image.title }}</a></strong></p>
                    <p class="subtitle is-size-6">Veröffentlicht am: {{ image.date }}</p>
                </div>
            </div>
        </article>
 {% endif %}
        {% endfor %}