Compare commits
32 Commits
master
...
strapi-v4-
Author | SHA1 | Date | |
---|---|---|---|
b50df4e6de | |||
2f8ba8513e | |||
ca1dac246b | |||
ef5b6a7c16 | |||
d11b959a23 | |||
b4b10d0c7b | |||
d4916735ce | |||
369c51f543 | |||
a5f4af95d5 | |||
1d266102c7 | |||
7ceee20c23 | |||
2b36dbcec7 | |||
d006a57f09 | |||
6bd9a7c422 | |||
1b26c0d7e0 | |||
dfd03ccfa8 | |||
b2e79f0557 | |||
8a48d1d910 | |||
8c4a56c1de | |||
ea40459421 | |||
84094c1d83 | |||
e11f7d2e4c | |||
92d3a5d341 | |||
f83cd1082a | |||
479ac66a8f | |||
b7fad54e44 | |||
|
1aa4c8642e | ||
|
12b189ccab | ||
|
dc44d2ada4 | ||
|
91f4f855b5 | ||
|
65760193c2 | ||
|
ac911ed3f0 |
75
README.md
75
README.md
@ -1,4 +1,11 @@
|
||||
# jekyll-strapi
|
||||
# jekyll-strapi reboot
|
||||
|
||||
## Features
|
||||
|
||||
* Support for Strapi 4
|
||||
* Authentication
|
||||
* Permalinks
|
||||
* Caching and collecting assets from Strapi
|
||||
|
||||
## Install
|
||||
|
||||
@ -24,18 +31,30 @@ strapi:
|
||||
# Collections, key is used to access in the strapi.collections
|
||||
# template variable
|
||||
collections:
|
||||
# Example for a "articles" collection
|
||||
articles:
|
||||
# Example for a "Photo" collection
|
||||
photos:
|
||||
# Collection name (optional)
|
||||
type: article
|
||||
# type: photos
|
||||
# Permalink used to generate the output files (eg. /articles/:id).
|
||||
permalink: /articles/:id/
|
||||
permalink: /photos/:id/
|
||||
# Layout file for this collection
|
||||
layout: strapi_article.html
|
||||
layout: photo.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.
|
||||
@ -44,17 +63,39 @@ This plugin provides the `strapi` template variable. This template provides acce
|
||||
|
||||
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:
|
||||
To list all documents of the collection ```_layouts/home.html```:
|
||||
|
||||
```
|
||||
{% for post in strapi.collections.articles %}
|
||||
<article>
|
||||
<header>
|
||||
{{ post.title }}
|
||||
</header>
|
||||
<div class="body">
|
||||
{{ post.content }}
|
||||
</div>
|
||||
</article>
|
||||
{% endfor %}
|
||||
---
|
||||
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>
|
||||
```
|
||||
|
@ -16,7 +16,9 @@ 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
|
||||
|
@ -1,6 +1,8 @@
|
||||
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'
|
||||
|
@ -23,7 +23,9 @@ module Jekyll
|
||||
uri = URI("#{@site.endpoint}#{path}")
|
||||
Jekyll.logger.info "Jekyll Strapi:", "Fetching entries from #{uri}"
|
||||
# Get entries
|
||||
response = Net::HTTP.get_response(uri)
|
||||
# response = Net::HTTP.get_response(uri)
|
||||
# add auth token
|
||||
response = strapi_request(uri)
|
||||
# Check response code
|
||||
if response.code == "200"
|
||||
result = JSON.parse(response.body, object_class: OpenStruct)
|
||||
@ -34,14 +36,14 @@ module Jekyll
|
||||
end
|
||||
|
||||
# Add necessary properties
|
||||
result.each do |document|
|
||||
result.data.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.each {|x| yield(x)}
|
||||
result.data.each {|x| yield(x)}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -38,7 +38,7 @@ module Jekyll
|
||||
end
|
||||
|
||||
def url_placeholders
|
||||
requiredValues = @document.to_h.select {|k, v|
|
||||
requiredValues = @document.attributes.to_h.select {|k, v|
|
||||
v.class == String and @collection.config['permalink'].include? k.to_s
|
||||
}
|
||||
|
||||
|
@ -30,8 +30,13 @@ module Jekyll
|
||||
:placeholders => {
|
||||
:id => document.id.to_s,
|
||||
:uid => document.uid,
|
||||
:slug => document.slug,
|
||||
:type => document.type
|
||||
: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,
|
||||
}
|
||||
)
|
||||
|
||||
|
13
lib/jekyll/strapi/strapihttp.rb
Normal file
13
lib/jekyll/strapi/strapihttp.rb
Normal file
@ -0,0 +1,13 @@
|
||||
##
|
||||
# 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
|
@ -1,5 +1,5 @@
|
||||
module Jekyll
|
||||
module Strapi
|
||||
VERSION = "0.1.2"
|
||||
VERSION = "0.1.3"
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user