Compare commits

..

2 Commits

Author SHA1 Message Date
ed4f12a442 Update 'LICENSE' 2022-06-09 18:37:10 -04:00
bb7dd71f32 Update 'lib/jekyll/strapi/version.rb' 2022-06-09 18:35:29 -04:00
8 changed files with 26 additions and 91 deletions

View File

@ -1,11 +1,4 @@
# jekyll-strapi reboot
## Features
* Support for Strapi 4
* Authentication
* Permalinks
* Caching and collecting assets from Strapi
# jekyll-strapi
## Install
@ -31,71 +24,37 @@ strapi:
# Collections, key is used to access in the strapi.collections
# template variable
collections:
# Example for a "Photo" collection
photos:
# Example for a "articles" collection
articles:
# Collection name (optional)
# type: photos
type: article
# Permalink used to generate the output files (eg. /articles/:id).
permalink: /photos/:id/
permalink: /articles/:id/
# Layout file for this collection
layout: photo.html
layout: strapi_article.html
# Generate output files or not (default: false)
output: true
```
This works for the following collection *Photo* in Strapi:
| Name | Type |
| ------- | ----- |
| Title | Text |
| Image | Media |
| Comment | Text |
### Authentication
To access non Public collections (and by default all Strapi collections are non Public) you must to generate a token inside your strapi instance and set it as enviromental variable `STRAPI_TOKEN`.
## Usage
This plugin provides the `strapi` template variable. This template provides access to the collections defined in the configuration.
This plugin provides the `strapi` template variable. This template provides access to the collections defined in the configuration.
### Using Collections
Collections are accessed by their name in `strapi.collections`. The `articles` collections is available at `strapi.collections.articles`.
To list all documents of the collection ```_layouts/home.html```:
To list all documents of the collection:
```
---
layout: default
---
<div class="home">
<h1 class="page-heading">Photos</h1>
{%- if strapi.collections.photos.size > 0 -%}
<ul>
{%- for photo in strapi.collections.photos -%}
<li>
<a href="{{ photo.url }}">Title: {{ photo.title }}</a>
</li>
{%- endfor -%}
</ul>
{%- endif -%}
</div>
```
### Attributes
All object's data you can access through ``` {{ page.document.strapi_attributes }}```. This plugin also introduces new filter asset_url which perform downloading the asset into the assets folder and provides correct url. Thanks for this you have copies of your assets locally without extra dependency on Strapi ```_layouts/photo.html```:
```
---
layout: default
---
<div class="home">
<h1 class="page-heading">{{ page.document.title }}</h1>
<h2>{{ page.document.strapi_attributes.Title }}</h2>
<p>{{ page.document.strapi_attributes.Comment }}</p>
<img src="{{ page.document.strapi_attributes.Image.data.attributes.formats.thumbnail| asset_url }}"/>
</div>
{% for post in strapi.collections.articles %}
<article>
<header>
{{ post.title }}
</header>
<div class="body">
{{ post.content }}
</div>
</article>
{% endfor %}
```

View File

@ -16,9 +16,7 @@ Gem::Specification.new do |spec|
A Jekyll plugin for retrieving content from a Strapi API
DESC
# spec.add_runtime_dependency("down", "~> 5.0")
spec.add_runtime_dependency("jekyll", ">= 4")
spec.add_runtime_dependency("http", "~> 3.2")
spec.add_runtime_dependency("json", "~> 2.1")
end

View File

@ -1,8 +1,6 @@
require 'jekyll/strapi/strapihttp'
require 'jekyll/strapi/collection'
require 'jekyll/strapi/drops'
require 'jekyll/strapi/generator'
require 'jekyll/strapi/hooks'
require 'jekyll/strapi/site'
require 'jekyll/strapi/version'
# require 'jekyll/strapi/strapiimagefilter'

View File

@ -23,9 +23,7 @@ module Jekyll
uri = URI("#{@site.endpoint}#{path}")
Jekyll.logger.info "Jekyll Strapi:", "Fetching entries from #{uri}"
# Get entries
# response = Net::HTTP.get_response(uri)
# add auth token
response = strapi_request(uri)
response = Net::HTTP.get_response(uri)
# Check response code
if response.code == "200"
result = JSON.parse(response.body, object_class: OpenStruct)
@ -36,14 +34,14 @@ module Jekyll
end
# Add necessary properties
result.data.each do |document|
result.each do |document|
document.type = collection_name
document.collection = collection_name
document.id ||= document._id
document.url = @site.strapi_link_resolver(collection_name, document)
end
result.data.each {|x| yield(x)}
result.each {|x| yield(x)}
end
end
end

View File

@ -38,7 +38,7 @@ module Jekyll
end
def url_placeholders
requiredValues = @document.attributes.to_h.select {|k, v|
requiredValues = @document.to_h.select {|k, v|
v.class == String and @collection.config['permalink'].include? k.to_s
}

View File

@ -30,13 +30,8 @@ module Jekyll
:placeholders => {
:id => document.id.to_s,
:uid => document.uid,
:slug => document.attributes.slug,
:type => document.type,
:date => document.attributes.date,
:title => document.attributes.title,
:category => document.attributes.category
# look inside jekyll _data folder
#:title => document.data,
:slug => document.slug,
:type => document.type
}
)
@ -47,4 +42,4 @@ module Jekyll
strapi_collections[collection_name]
end
end
end
end

View File

@ -1,13 +0,0 @@
##
# This is a helper method to authenticate during getting data from Strapi instance.
def strapi_request(url)
strapi_token = ENV['STRAPI_TOKEN']
uri = URI(url)
req = Net::HTTP::Get.new(uri)
req['Authorization'] = "Bearer #{strapi_token}"
headers = {
'Authorization'=>"Bearer #{strapi_token}"
}
Net::HTTP.get_response(uri, headers)
end

View File

@ -1,5 +1,5 @@
module Jekyll
module Strapi
VERSION = "0.1.3"
VERSION = "0.2.0"
end
end