bluszcz / jekyll-strapi-4

Jekyll plugin to retrieve content from any Strapi v4 API.
https://bluszcz.github.io/jekyll-strapi/
MIT License
6 stars 1 forks source link

Permalink variables #25

Open wlabarron opened 2 years ago

wlabarron commented 2 years ago

Hello, I'm struggling to get the :variable permalink structure working like in the Strapi v3 version of the plugin.

permalink: /:id works as expected with the Strapi record ID in the permalink, but using the name of other fields in the content type (like permalink: /:link to put the "link" field in) doesn't work and gives this error:

/opt/homebrew/lib/ruby/gems/3.1.0/gems/addressable-2.8.1/lib/addressable/uri.rb:2353:in `to_s': Cannot assemble URI string with ambiguous path: ':link' (Addressable::URI::InvalidURIError)

Is there something I need to enable or change to make this work? Thank you!

bluszcz commented 2 years ago

Helo, this one works only with Strapi 4...

wlabarron commented 2 years ago

Hello, I'm using Strapi v4. I meant that in the previous version I was able to use :link etc in permalinks, but that doesn't work in this version.

bluszcz commented 2 years ago

@mkrajewski90 have been working on permalinks - any quick comment here perhaps? :)

mkrajewski90 commented 2 years ago

@wlabarron - could you give me a config code example, please?

wlabarron commented 2 years ago

Sure! I've got a field in Strapi named link, and my Jekyll config looks like this:

plugins:
  - jekyll-strapi-4

strapi:  
    # Your API endpoint (optional, default to http://localhost:1337)
    endpoint: http://localhost:1337
    # Collections, key is used to access in the strapi.collections template variable
    collections:
        pages:
            # Collection name . Used to construct the url requested. Example: type `foo` would generate the following url `http://localhost:1337/foo`.
            type: pages
            # Permalink used to generate the output files (eg. /posts/:id).
            permalink: /:link
            # Layout file for this collection
            layout: default.html
            # Generate output files or not (default: false)
            output: true
bluszcz commented 2 years ago

@wlabarron something like this? image

bluszcz commented 2 years ago

I tried to replicate config and I do confirm:

/home/bluszcz/gems/gems/addressable-2.8.1/lib/addressable/uri.rb:2353:in `to_s': Cannot assemble URI string with ambiguous path: ':link' (Addressable::URI::InvalidURIError)
    from /home/bluszcz/gems/gems/addressable-2.8.1/lib/addressable/uri.rb:854:in `initialize'
    from /home/bluszcz/gems/gems/addressable-2.8.1/lib/addressable/uri.rb:152:in `new'
    from /home/bluszcz/gems/gems/addressable-2.8.1/lib/addressable/uri.rb:152:in `parse'
    from /home/bluszcz/gems/gems/addressable-2.8.1/lib/addressable/uri.rb:623:in `encode'
    from /home/bluszcz/gems/gems/jekyll-4.2.2/lib/jekyll/url.rb:147:in `escape_path'
    from /home/bluszcz/gems/gems/jekyll-4.2.2/lib/jekyll/url.rb:76:in `block in generate_url_from_hash'
    from /home/bluszcz/gems/gems/jekyll-4.2.2/lib/jekyll/url.rb:69:in `each'
    from /home/bluszcz/gems/gems/jekyll-4.2.2/lib/jekyll/url.rb:69:in `inject'
    from /home/bluszcz/gems/gems/jekyll-4.2.2/lib/jekyll/url.rb:69:in `generate_url_from_hash'
    from /home/bluszcz/gems/gems/jekyll-4.2.2/lib/jekyll/url.rb:64:in `generate_url'
    from /home/bluszcz/gems/gems/jekyll-4.2.2/lib/jekyll/url.rb:46:in `generated_permalink'
    from /home/bluszcz/gems/gems/jekyll-4.2.2/lib/jekyll/url.rb:39:in `to_s'
    from /home/bluszcz/gems/gems/jekyll-4.2.2/lib/jekyll/page.rb:99:in `url'
    from /home/bluszcz/gems/gems/jekyll-4.2.2/lib/jekyll/page.rb:67:in `dir'
    from /home/bluszcz/gems/gems/jekyll-4.2.2/lib/jekyll/convertible.rb:117:in `block in to_liquid'
    from /home/bluszcz/gems/gems/jekyll-4.2.2/lib/jekyll/convertible.rb:116:in `each'
    from /home/bluszcz/gems/gems/jekyll-4.2.2/lib/jekyll/convertible.rb:116:in `each_with_object'
    from /home/bluszcz/gems/gems/jekyll-4.2.2/lib/jekyll/convertible.rb:116:in `to_liquid'
    from /home/bluszcz/gems/gems/jekyll-4.2.2/lib/jekyll/renderer.rb:215:in `assign_pages!'
    from /home/bluszcz/gems/gems/jekyll-4.2.2/lib/jekyll/renderer.rb:55:in `run'
    from /home/bluszcz/gems/gems/jekyll-4.2.2/lib/jekyll/site.rb:547:in `render_regenerated'
    from /home/bluszcz/gems/gems/jekyll-4.2.2/lib/jekyll/site.rb:539:in `block in render_pages'
    from /home/bluszcz/gems/gems/jekyll-4.2.2/lib/jekyll/site.rb:538:in `each'
    from /home/bluszcz/gems/gems/jekyll-4.2.2/lib/jekyll/site.rb:538:in `render_pages'
    from /home/bluszcz/gems/gems/jekyll-4.2.2/lib/jekyll/site.rb:211:in `render'
    from /home/bluszcz/gems/gems/jekyll-4.2.2/lib/jekyll/site.rb:80:in `process'
    from /home/bluszcz/gems/gems/jekyll-4.2.2/lib/jekyll/command.rb:28:in `process_site'
    from /home/bluszcz/gems/gems/jekyll-4.2.2/lib/jekyll/commands/build.rb:65:in `build'
    from /home/bluszcz/gems/gems/jekyll-4.2.2/lib/jekyll/commands/build.rb:36:in `process'
    from /home/bluszcz/gems/gems/jekyll-4.2.2/lib/jekyll/command.rb:91:in `block in process_with_graceful_fail'
    from /home/bluszcz/gems/gems/jekyll-4.2.2/lib/jekyll/command.rb:91:in `each'
    from /home/bluszcz/gems/gems/jekyll-4.2.2/lib/jekyll/command.rb:91:in `process_with_graceful_fail'
    from /home/bluszcz/gems/gems/jekyll-4.2.2/lib/jekyll/commands/build.rb:18:in `block (2 levels) in init_with_program'
mkrajewski90 commented 2 years ago

@wlabarron , @bluszcz - I've found the bug but I don't have a solution now.

The problem is that current implementation support only searching by id, uid or slug:

module Jekyll
  module Strapi
    class StrapiPage < Page
      def initialize(site, base, document, collection)
        # ...
        url = Jekyll::URL.new(
          :placeholders => {
            :id => document.id.to_s,
            :uid => document.uid,
            :slug => document.attributes.slug,
            # :link => document.attributes.link, # NOT SUPPORTED HERE!
            :type => document.attributes.type,
            :date => document.attributes.date,
            :title => document.title
          },
          permalink: collection.permalink.to_s
        )
        #...
      end
    end
  end
end

Another part when URL is get is in this repository, file site.rb, module Jekyll::Site.strapi_link_resolver.

To fix this there should be a way to change placeholders. Do you have idea how we can do this in a proper way?

wlabarron commented 2 years ago

It worked in the Strapi v3 version of this plugin, how did that code do it?