bridgetown_directus 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2311ee5e15d9cab7cd7cd04746afd03d17a85982e2cc43cdadcc25ffba3646cb
4
- data.tar.gz: 2cce7fa9fe95891b1559968d5e3d1c139752182966160a1fa6ac44933a6b36d3
3
+ metadata.gz: b86e165473c96ffa32fc96cf04ec0b5564169bcdd53866489c6f806f16235dfe
4
+ data.tar.gz: c7ea854a2e0263589a31d467b164f061fe15399126a90de273b4c1b8a6366226
5
5
  SHA512:
6
- metadata.gz: 8aa565c2c68cb96330213ace4efcd99a31948aa7349594004de99018dc9195518ce6620e97f4e6f578d8def0354f4b2519e92f5128b1a57cb6179948f1f3900f
7
- data.tar.gz: 91b5f0df720865f92c388a27b2613dfeeed7c4b95190d2dd56699eaec6719f0c495be02f5ccd7504c748615ba7ca41851dd8d437aff089d84f4268712c48d8fd
6
+ metadata.gz: 80a9bfad8a419d19159a2f0a6f7538afc142837a3baff3737bd9c1c5f127f039d7990432ace15dffea1cf3b474129a23335919e1c9e6fa975e4df653ecf83e67
7
+ data.tar.gz: '098a9aeb10f146c6a30ee1a60d428390d52998e3bf438735b95f5de0b7fbd0afdfbb61f3982f816c1c12b43224b71afcda6f1783aaf8cb4a52a9b9b1ad545aab'
data/README.md CHANGED
@@ -1,11 +1,13 @@
1
1
  # Bridgetown Directus Plugin
2
2
 
3
- This Bridgetown plugin integrates with the [Directus](https://directus.io/) which is among other things a [headless CMS](https://en.wikipedia.org/wiki/Headless_content_management_system). The plugin allows Bridgetown to pull content from a Directus API during the build process and generate static content in your site. It currently supports fetching published posts, with future plans for more flexibility and features.
3
+ [![Gem Version](https://badge.fury.io/rb/bridgetown_directus.svg)](https://badge.fury.io/rb/bridgetown_directus)
4
+
5
+ This Bridgetown plugin integrates with [Directus](https://directus.io/), which is among other things a [headless CMS](https://en.wikipedia.org/wiki/Headless_content_management_system). The plugin allows Bridgetown to pull content from a Directus API during the build process and generate static content in your site. It supports both single-language and multilingual content through Directus translations.
4
6
 
5
7
  ## Features
6
8
 
7
9
  - Fetch **published posts** from Directus during the build process
8
- - Simple setup and configuration
10
+ - Support for **multilingual content** through Directus translations
9
11
 
10
12
  ## Installation
11
13
 
@@ -14,81 +16,116 @@ Before installing the plugin make sure you have an [Auth Token](https://docs.dir
14
16
  ### Recommended Installation (Bridgetown Automation)
15
17
 
16
18
  1. Run the plugin's automation setup:
19
+
17
20
  ```bash
18
21
  bin/bridgetown apply https://github.com/munkun-estudio/bridgetown_directus
19
22
  ```
20
- 2. The setup will guide you to provide the Directus API URL and Auth Token, and configure the plugin automatically.
23
+
24
+ 2. The setup will guide you through:
25
+ - Providing the Directus API URL and Auth Token
26
+ - Specifying your content collection name
27
+ - Enabling/disabling translations support
28
+ - Configuring translatable fields (if translations enabled)
21
29
 
22
30
  ### Manual Installation
23
31
 
24
- 1. Add the gem to your Gemfile:
25
- ```ruby
26
- bundle add "bridgetown_directus"
27
- ```
28
- 2. Run bundle install to install the gem.
29
- 3. Add the plugin configuration to your config/initializers.rb file:
30
- ```ruby
31
- init :bridgetown_directus do
32
- api_url "https://your-directus-instance.com"
33
- token ENV['DIRECTUS_AUTH_TOKEN'] || "your_token"
34
-
35
- # Required field mappings
36
- mappings do
37
- title "title"
38
- content "content"
39
- slug "slug"
40
- date "date"
41
- category "category"
42
- excerpt "excerpt"
43
- image "image"
44
- end
45
- end
46
- ```
32
+ 1. Add the gem to your Gemfile:
33
+
34
+ ```ruby
35
+ bundle add "bridgetown_directus"
36
+ ```
37
+
38
+ 2. Run bundle install to install the gem.
39
+ 3. Add the plugin configuration to your config/initializers.rb file:
40
+
41
+ ```ruby
42
+ init :"bridgetown_directus" do
43
+ api_url "https://your-directus-instance.com"
44
+ token ENV['DIRECTUS_AUTH_TOKEN'] || "your_token"
45
+ collection config.directus["collection"]
46
+ mappings config.directus["mappings"]
47
+ end
48
+ ```
49
+
50
+ 4. Configure your bridgetown.config.yml:
51
+
52
+ ```yaml
53
+ directus:
54
+ collection: "posts"
55
+ mappings:
56
+ title: "title" # Required field
57
+ content: "body" # Required field
58
+ slug: "slug" # Optional, will be auto-generated if not provided
59
+ date: "date" # Optional, defaults to current date/time if not provided
60
+ category: "category" # Optional
61
+ excerpt: "excerpt" # Optional, defaults to content excerpt if not provided
62
+ image: "image" # Optional, URL for the image associated with the post
63
+ translations:
64
+ enabled: false # Set to true for multilingual support
65
+ fields: # Only required if translations are enabled
66
+ - title
67
+ - excerpt
68
+ - body
69
+ ```
47
70
 
48
71
  ## Configuration
49
72
 
50
- To configure the plugin:
51
-
52
- 1. You can either use environment variables for the API URL and token:
53
- ```bash
54
- export DIRECTUS_API_URL="https://your-directus-instance.com"
55
- export DIRECTUS_AUTH_TOKEN="your-token"
56
- ```
57
-
58
- 2. Or hard-code the values directly in your initializer:
59
- ```ruby
60
- init :bridgetown_directus do
61
- api_url "https://your-directus-instance.com"
62
- token "your_token"
63
-
64
- # Required field mappings
65
- mappings do
66
- title "title"
67
- content "content"
68
- slug "slug"
69
- date "date"
70
- category "category"
71
- excerpt "excerpt"
72
- image "image"
73
- end
74
- end
75
- ```
73
+ ### Basic Configuration
74
+
75
+ You can configure the plugin either through environment variables or direct configuration:
76
+
77
+ 1. Using environment variables:
78
+
79
+ ```bash
80
+ export DIRECTUS_API_URL="https://your-directus-instance.com"
81
+ export DIRECTUS_AUTH_TOKEN="your-token"
82
+ ```
83
+
84
+ 2. Or through bridgetown.config.yml as shown in the installation section.
85
+
86
+ ### Translations Configuration
87
+
88
+ To enable multilingual support:
89
+
90
+ 1. In your bridgetown.config.yml, set translations.enabled to true:
91
+
92
+ ```yaml
93
+ directus:
94
+ # ... other config ...
95
+ translations:
96
+ enabled: true
97
+ fields:
98
+ - title
99
+ - excerpt
100
+ - body
101
+ ```
102
+
103
+ 2. Ensure your Directus collection has translations enabled and configured for the specified fields.
104
+
105
+ 3. The plugin will automatically:
106
+
107
+ - Generate posts for each available language
108
+ - Create appropriate URLs based on locale
109
+ - Handle fallback content if translations are missing
110
+
76
111
  ## Usage
77
112
 
78
113
  Once the plugin is installed and configured, it will fetch posts from your Directus instance during each build. These posts will be generated as in-memory resources, meaning they are not written to disk but are treated as normal posts by Bridgetown.
79
114
 
80
115
  ### Directus Setup
81
116
 
82
- To use the plugin, ensure that you’ve set up a collection in your Directus instance with the following fields (you can name the collection anything you like):
117
+ #### Basic Collection Setup
118
+
119
+ Create a collection in your Directus instance with these fields:
83
120
 
84
- - **title**: The title of the post (Text field)
85
- - **content**: The content of the post (Rich Text or Text field)
86
- - **slug**: Optional. A unique slug for the post (Text field)
87
- - **date**: Optional.The publish date (Datetime field)
88
- - **status**: Optional. The status of the post (Option field with values like published”, draft”, etc.)
89
- - **category**: Optional. The category for the post (Text field)
90
- - **excerpt**: Optional. A short excerpt (Text field)
91
- - **image**: Optional. An image associated with the post (File/Media field)
121
+ - **title**: The title of the post (Text field)
122
+ - **body**: The content of the post (Rich Text or Text field)
123
+ - **slug**: Optional. A unique slug for the post (Text field)
124
+ - **date**: Optional. The publish date (Datetime field)
125
+ - **status**: Optional. The status of the post (Option field with values like "published", "draft", etc.)
126
+ - **category**: Optional. The category for the post (Text field)
127
+ - **excerpt**: Optional. A short excerpt (Text field)
128
+ - **image**: Optional. An image associated with the post (File/Media field)
92
129
 
93
130
  Make sure the **status** field uses `"published"` for posts that you want to be visible on your site.
94
131
 
@@ -106,7 +143,6 @@ If your posts contain images, and you want to display them in your Bridgetown si
106
143
  - When users upload images to posts, ensure that the images are associated with the **directus_files** collection.
107
144
  - By default, Directus will store image URLs, which the plugin can reference directly. Ensure that the **image** field or URL is added to the **body** field (or wherever applicable).
108
145
 
109
-
110
146
  ### Fetching Posts
111
147
 
112
148
  Posts are fetched from Directus during each build and treated as Bridgetown resources. These resources are available in your site just like regular posts, and you can access them through your templates or layouts.
@@ -117,19 +153,19 @@ By default, only posts with a status of "published" are fetched from Directus.
117
153
 
118
154
  Here are features that are planned for future versions of the plugin:
119
155
 
120
- - [ ] Support for Additional Content Types: Extend the plugin to handle other Directus collections and custom content types.
121
- - [ ] Custom Field Mapping via DSL: Implement a DSL for more advanced field mapping.
122
- - [ ] Asset Handling: Add functionality to download and manage images and other assets.
123
- - [ ] Caching & Incremental Builds: Implement caching to improve build performance when fetching content.
156
+ - [ ] Support for Additional Content Types: Extend the plugin to handle other Directus collections and custom content types.
157
+ - [ ] Custom Field Mapping via DSL: Implement a DSL for more advanced field mapping.
158
+ - [ ] Asset Handling: Add functionality to download and manage images and other assets.
159
+ - [ ] Caching & Incremental Builds: Implement caching to improve build performance when fetching content.
124
160
  - [ ] Draft Previews: Add support for previewing unpublished (draft) posts.
125
161
 
126
162
  ## Testing
127
163
 
128
- Testing isnt fully set up yet, but contributions and improvements are welcome.
164
+ Testing isn't fully set up yet, but contributions and improvements are welcome.
129
165
 
130
166
  ## Contributing
131
167
 
132
- We welcome contributions to this project! To contribute:
168
+ We welcome contributions! To contribute:
133
169
 
134
170
  1. Fork the repository
135
171
  2. Create a new branch (git checkout -b feature-branch)
@@ -3,34 +3,60 @@ say_status :directus, "Installing the bridgetown_directus plugin..."
3
3
  # Prompt the user for Directus API URL and Auth Token
4
4
  api_url = ask("What's your Directus instance URL? (Example: https://your-instance.example.com)")
5
5
  auth_token = ask("What's your Directus API auth token? (Leave blank to use ENV['DIRECTUS_AUTH_TOKEN'])")
6
- collection = ask("What's the name of the collection? (Example: posts")
6
+ collection = ask("What's the name of the collection (Directus Model)? (Example: posts)")
7
+
8
+ # Ask if translations should be enabled with a default of 'n'
9
+ translations_enabled_input = ask("Do you want to enable translations? (y/n) default:", :yellow, default: "n")
10
+ translations_enabled = translations_enabled_input.strip.downcase.start_with?("y")
11
+
12
+ # Prepare the translations YAML block based on the user’s response
13
+ translations_yaml = if translations_enabled
14
+ translatable_fields_input = ask("List the translatable fields separated by commas (e.g., title, excerpt, content)")
15
+ translatable_fields = translatable_fields_input.split(',').map(&:strip)
16
+
17
+ " translations:\n enabled: true\n fields:\n#{translatable_fields.map { |field| " - #{field}" }.join("\n")}"
18
+ else
19
+ " translations:\n enabled: false"
20
+ end
7
21
 
8
22
  # Add the bridgetown_directus gem
9
23
  add_gem "bridgetown_directus"
10
24
 
11
- # Add Directus configuration to config/initializers.rb using add_initializer method
12
- add_initializer :"bridgetown_directus" do
25
+ # Add Directus configuration to config/initializers.rb
26
+ add_initializer :bridgetown_directus do
13
27
  <<~RUBY
14
- do
15
- api_url "#{api_url}"
16
- token "#{auth_token.present? ? auth_token : "<%= ENV['DIRECTUS_AUTH_TOKEN'] %>"}"
17
- collection "#{collection}"
18
-
19
- # Field Mappings (Ensure your Directus collection has these fields)
20
- mappings do
21
- title "title" # Required field
22
- content "content" # Required field
23
- slug "slug" # Optional, will be auto-generated if not provided
24
- date "date" # Optional, defaults to the current date/time if not provided
25
- category "category" # Optional
26
- excerpt "excerpt" # Optional, defaults to content excerpt if not provided
27
- image "image" # Optional, URL for the image associated with the post
28
- end
29
- end
28
+ do
29
+ api_url "#{api_url}"
30
+ token "#{auth_token.present? ? auth_token : "<%= ENV['DIRECTUS_AUTH_TOKEN'] %>"}"
31
+ collection config.directus["collection"]
32
+ mappings config.directus["mappings"]
33
+ end
30
34
  RUBY
31
35
  end
32
- # Success message
33
- say_status :directus, "Directus integration is complete! Please make sure your Directus collection contains the required fields as specified in the initializer."
34
- say_status :directus, "Check config/initializers.rb for your Directus setup and adjust mappings if necessary."
36
+
37
+ # Append the configuration to bridgetown.config.yml
38
+ append_to_file "bridgetown.config.yml" do
39
+ <<~YAML
40
+
41
+ directus:
42
+ collection: "#{collection}"
43
+ mappings:
44
+ title: "title" # Required field
45
+ content: "body" # Required field
46
+ slug: "slug" # Optional, will be auto-generated if not provided
47
+ date: "date" # Optional, defaults to the current date/time if not provided
48
+ category: "category" # Optional
49
+ excerpt: "excerpt" # Optional, defaults to content excerpt if not provided
50
+ image: "image" # Optional, URL for the image associated with the post
51
+ #{translations_yaml}
52
+ YAML
53
+ end
54
+
55
+ say_status :success, "Bridgetown Directus plugin has been installed!", :green
56
+ say_status :info, "Add your posts to Directus and they will be automatically imported when you build your site.", :yellow
57
+ if translations_enabled
58
+ say_status :info, "Translations are enabled. Make sure your Directus collection has translations configured.", :yellow
59
+ end
60
+ say_status :directus, "Check config/initializers.rb for your Directus setup and config.bridgetown.yml to adjust fields mappings if necessary."
35
61
  say_status :directus, "For usage help visit:"
36
- say_status :directus, "https://github.com/Munkun-Estudio/bridgetown_directus/blob/main/README.md"
62
+ say_status :directus, "https://github.com/Munkun-Estudio/bridgetown_directus/blob/main/README.md"
@@ -15,6 +15,13 @@ Gem::Specification.new do |spec|
15
15
  spec.test_files = spec.files.grep(%r!^test/!)
16
16
  spec.require_paths = ["lib"]
17
17
 
18
+ spec.metadata = {
19
+ "source_code_uri" => spec.homepage,
20
+ "bug_tracker_uri" => "#{spec.homepage}/issues",
21
+ "changelog_uri" => "#{spec.homepage}/releases",
22
+ "homepage_uri" => spec.homepage
23
+ }
24
+
18
25
  spec.required_ruby_version = ">= 2.7.0"
19
26
 
20
27
  spec.add_dependency "bridgetown", ">= 1.2.0", "< 2.0"
@@ -16,6 +16,7 @@ module BridgetownDirectus
16
16
 
17
17
  response = connection.get("/items/#{@site.config.bridgetown_directus.collection}") do |req|
18
18
  req.params['filter'] = { status: { _eq: "published" } }.to_json
19
+ req.params['fields'] = '*,translations.*'
19
20
  end
20
21
 
21
22
  if response.success?
@@ -49,7 +50,7 @@ module BridgetownDirectus
49
50
  elsif posts_data.is_a?(Array)
50
51
  posts_data
51
52
  else
52
- raise "Unexpected structure of posts_data: #{posts_data.inspect}"
53
+ raise "Invalid posts data structure: #{posts_data.inspect}"
53
54
  end
54
55
  end
55
56
  end
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module BridgetownDirectus
4
2
  class Builder < Bridgetown::Builder
5
3
  def build
@@ -30,33 +28,76 @@ module BridgetownDirectus
30
28
  raise "Unexpected structure of posts_data: #{posts_data.inspect}"
31
29
  end
32
30
 
33
- posts_array.each_with_index do |post, index|
31
+ created_posts = 0
32
+ posts_array.each do |post|
33
+ if translations_enabled?
34
+ created_posts += create_translated_posts(post)
35
+ else
36
+ created_posts += create_single_post(post)
37
+ end
38
+ end
39
+
40
+ Utils.log_directus "Finished generating #{created_posts} posts."
41
+ end
42
+
43
+ def translations_enabled?
44
+ site.config.dig("directus", "translations", "enabled") == true
45
+ end
46
+
47
+ def create_single_post(post)
48
+ slug = post["slug"] || Bridgetown::Utils.slugify(post["title"])
49
+ api_url = site.config.dig("directus", "api_url")
50
+
51
+ begin
52
+ add_resource :posts, "#{slug}.md" do
53
+ layout "post"
54
+ title post["title"]
55
+ content post["body"]
56
+ date post["date"] || Time.now.iso8601
57
+ category post["category"]
58
+ excerpt post["excerpt"]
59
+ image post["image"] ? "#{api_url}/assets/#{post['image']}" : nil
60
+ end
61
+ 1
62
+ rescue => e
63
+ Utils.log_directus "Error creating post #{slug}: #{e.message}"
64
+ 0
65
+ end
66
+ end
67
+
68
+ def create_translated_posts(post)
69
+ posts_created = 0
70
+ translations = post["translations"] || []
34
71
 
35
- # Fallback to slugify if no slug is provided
36
- slug = post["slug"] || Bridgetown::Utils.slugify(post["title"])
37
- date = post["date"] || Time.now.iso8601
72
+ translations.each do |translation|
73
+ lang_code = translation["languages_code"].split("-").first.downcase
74
+ bridgetown_locale = lang_code.to_sym
75
+
76
+ next unless site.config["available_locales"].include?(bridgetown_locale)
38
77
 
39
- # Construct the image URL if the image ID is present
40
- image = post["image"]
41
- image = image ? "#{site.config.bridgetown_directus.api_url}/assets/#{image}" : nil
78
+ slug = translation["slug"] || Bridgetown::Utils.slugify(translation["title"])
79
+ api_url = site.config.dig("directus", "api_url")
42
80
 
43
81
  begin
44
82
  add_resource :posts, "#{slug}.md" do
45
83
  layout "post"
46
- title post["title"]
47
- content post["body"] # Make sure content is directly from post["body"]
48
- date date
84
+ title translation["title"]
85
+ content translation["body"]
86
+ date post["date"] || Time.now.iso8601
49
87
  category post["category"]
50
- excerpt post["excerpt"]
51
- image image
88
+ excerpt translation["excerpt"]
89
+ image post["image"] ? "#{api_url}/assets/#{post['image']}" : nil
90
+ locale bridgetown_locale
91
+ translations translations
52
92
  end
93
+
94
+ posts_created += 1
53
95
  rescue => e
54
- Utils.log_directus "Error processing post at index #{index}: #{e.message}"
55
- raise e
96
+ Utils.log_directus "Error creating post #{slug} for locale #{bridgetown_locale}: #{e.message}"
56
97
  end
57
98
  end
58
99
 
59
- Utils.log_directus "Finished generating #{posts_array.size} posts."
100
+ posts_created
60
101
  end
61
102
  end
62
103
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BridgetownDirectus
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  end
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "bridgetown"
4
- require_relative "bridgetown_directus/version"
5
4
  require_relative "bridgetown_directus/utils"
6
5
  require_relative "bridgetown_directus/api_client"
7
6
  require_relative "bridgetown_directus/builder"
@@ -12,12 +11,23 @@ module BridgetownDirectus
12
11
  config.bridgetown_directus ||= {}
13
12
  config.bridgetown_directus.api_url ||= api_url || ENV.fetch("DIRECTUS_API_URL")
14
13
  config.bridgetown_directus.token ||= token || ENV.fetch("DIRECTUS_API_TOKEN")
15
- config.bridgetown_directus.collection ||= collection
16
- config.bridgetown_directus.mappings ||= mappings
14
+
15
+ # Access collection and mappings from the bridgetown.config.yml
16
+ config.bridgetown_directus.collection ||= config.directus.collection
17
+ config.bridgetown_directus.mappings ||= config.directus.mappings
18
+ config.bridgetown_directus.translations ||= config.directus["translations"]
17
19
 
18
20
  # Register the builder
19
21
  config.builder BridgetownDirectus::Builder
20
22
 
23
+ # Log translations status
24
+ if config.bridgetown_directus.translations["enabled"]
25
+ translatable_fields = config.bridgetown_directus.translations["fields"] || []
26
+ Bridgetown.logger.info "Directus translations enabled for fields: #{translatable_fields.join(', ')}"
27
+ else
28
+ Bridgetown.logger.info "Directus translations are disabled"
29
+ end
30
+
21
31
  # Validate Directus config before proceeding
22
32
  unless config.bridgetown_directus.api_url && config.bridgetown_directus.token
23
33
  Bridgetown.logger.error "Invalid Directus configuration detected. Please check your API URL and token."
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bridgetown_directus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Munkun
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-10-15 00:00:00.000000000 Z
11
+ date: 2024-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bridgetown
@@ -180,7 +180,11 @@ files:
180
180
  homepage: https://github.com/munkun-estudio/bridgetown_directus
181
181
  licenses:
182
182
  - MIT
183
- metadata: {}
183
+ metadata:
184
+ source_code_uri: https://github.com/munkun-estudio/bridgetown_directus
185
+ bug_tracker_uri: https://github.com/munkun-estudio/bridgetown_directus/issues
186
+ changelog_uri: https://github.com/munkun-estudio/bridgetown_directus/releases
187
+ homepage_uri: https://github.com/munkun-estudio/bridgetown_directus
184
188
  post_install_message:
185
189
  rdoc_options: []
186
190
  require_paths: