jekyll-server-side-redirects 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 74d0db5a60bef8efed746b530acafd98ec7516510cfb39c3519cf7d694e5d66d
4
+ data.tar.gz: 44fb17ef5b55b31d0e3f297ee0086225dc1f3db3f637ff60e55034d64b277d40
5
+ SHA512:
6
+ metadata.gz: 83e50d23e0d8eb818079d1b5e9f3a493c5f8155782300abf2df8eacc5f3beb4f9baa79e2b5beebfebfc4594a2c567763e33886e066f7355d3d4d3556bb13d04e
7
+ data.tar.gz: 19e6aa05b93ceb88fa2020e48752785f8b2c8be76c33f024b1ac2b706ad603fcee23459f738cc6c2a1c1fbc9989b48799a1c89320bf7ba20793cc32834415581
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Aline Marques
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,100 @@
1
+ # Jekyll Server-Side Redirects
2
+
3
+ A Jekyll plugin that generates server-specific redirection files (e.g., `.htaccess` or `firebase.json`) to improve SEO by allowing you to create server-side redirects with appropriate HTTP status codes.
4
+
5
+ ## Features
6
+
7
+ - **Server Support**: Generate redirect configuration for:
8
+ - Firebase Hosting (`firebase.json`)
9
+ - Apache (`.htaccess`)
10
+ - **SEO Optimization**: Properly inform search engines about page moves using HTTP status codes.
11
+ - **Jekyll Integration**: Define redirects directly in your Jekyll pages and posts using `redirect_from` in the frontmatter.
12
+
13
+ ## Installation
14
+
15
+ ### As a Ruby Gem
16
+ 1. Add the gem to your `Gemfile`:
17
+
18
+ ```ruby
19
+ gem 'jekyll-server-side-redirects'
20
+ ```
21
+
22
+ 2. Run `bundle install` to install the gem.
23
+
24
+ ### Manually
25
+
26
+ If you're not using Bundler, you can install the gem manually:
27
+
28
+ ```ruby
29
+ gem install jekyll-server-side-redirects
30
+ ```
31
+
32
+ Then, add it to your _config.yml file:
33
+
34
+ ```yaml
35
+ plugins:
36
+ - jekyll-server-side-redirects
37
+ ```
38
+
39
+
40
+ ## Configuration
41
+
42
+ Add the following configuration to your _config.yml file:
43
+
44
+
45
+ ```yaml
46
+ server_redirects:
47
+ server: 'apache' # Options: 'firebase', 'apache'
48
+ ```
49
+
50
+ ## Usage
51
+
52
+ ### Add Redirects in Frontmatter
53
+
54
+ Define redirects in the redirect_from frontmatter field for your pages or posts:
55
+ ```yaml
56
+ ---
57
+ title: "My New Page"
58
+ redirect_from:
59
+ - /old-page1/
60
+ - /another-old-page/
61
+ redirect_type: 302 # Optional: Defaults to 301 (permanent redirect)
62
+ ---
63
+
64
+ ```
65
+
66
+ ### Build Your Jekyll Site
67
+
68
+ When you build your site, the plugin will automatically generate the appropriate redirection file based on the configured server:
69
+
70
+ For Firebase, a firebase.json file will be created or updated.
71
+ For Apache, a .htaccess file will be created or updated.
72
+
73
+
74
+ ## Examples
75
+
76
+ ### Firebase
77
+
78
+ A firebase.json file will be generated as follows:
79
+
80
+ ```json
81
+ {
82
+ "hosting": {
83
+ "redirects": [
84
+ { "source": "/old-page1/", "destination": "/new-page1/", "type": 301 },
85
+ { "source": "/old-page2/", "destination": "/new-page2/", "type": 302 }
86
+ ]
87
+ }
88
+ }
89
+ ```
90
+
91
+ ### Apache
92
+
93
+ A .htaccess file will be generated as follows:
94
+
95
+ ```
96
+ # .htaccess file generated by Jekyll Server-Side Redirects
97
+
98
+ Redirect 301 /old-page1/ /new-page1/
99
+ Redirect 302 /old-page2/ /new-page2/
100
+ ```
@@ -0,0 +1,22 @@
1
+ require_relative './servers/firebase'
2
+ require_relative './servers/apache'
3
+
4
+ module Jekyll
5
+ module RedirectGenerator
6
+ def self.get_server(site)
7
+ config = site.config['server_redirects']
8
+ return unless config
9
+ server = config['server']
10
+
11
+ case server
12
+ when 'firebase'
13
+ Jekyll::ServerSideRedirects::Firebase.generate_redirects(site)
14
+ when 'apache'
15
+ Jekyll::ServerSideRedirects::Apache.generate_redirects(site)
16
+ else
17
+ raise 'Invalid server specified'
18
+ end
19
+ end
20
+ end
21
+ end
22
+
@@ -0,0 +1,73 @@
1
+ module Jekyll
2
+ module ServerSideRedirects
3
+ module Apache
4
+ def self.generate_redirects(site)
5
+ redirects_data = []
6
+
7
+ redirects_data += process_redirects(site.pages)
8
+ redirects_data += process_redirects(site.posts.docs)
9
+ generate_htaccess(redirects_data)
10
+
11
+ redirects_data
12
+ end
13
+
14
+ def self.generate_htaccess(redirects_data)
15
+ file_path = ".htaccess"
16
+ existing_redirects = []
17
+
18
+ if File.exist?(file_path)
19
+ existing_redirects = parse_existing_redirects(file_path)
20
+ end
21
+
22
+ all_redirects = (existing_redirects + redirects_data).uniq { |r| r['source'] }
23
+
24
+ File.open(file_path, "w") do |file|
25
+ file.puts "# .htaccess file generated by Jekyll Server-Side Redirects"
26
+ file.puts ""
27
+
28
+ all_redirects.each do |redirect|
29
+ file.puts "Redirect #{redirect['type']} #{redirect['source']} #{redirect['destination']}"
30
+ end
31
+ end
32
+ end
33
+
34
+ def self.parse_existing_redirects(file_path)
35
+ redirects = []
36
+
37
+ File.readlines(file_path).each do |line|
38
+ next unless line.strip.start_with?("Redirect")
39
+
40
+ if line =~ /^Redirect\s+(\d{3})\s+(\S+)\s+(\S+)/
41
+ type, source, destination = $1.to_i, $2, $3
42
+ redirects << {
43
+ 'source' => source,
44
+ 'destination' => destination,
45
+ 'type' => type
46
+ }
47
+ end
48
+ end
49
+
50
+ redirects
51
+ end
52
+
53
+ def self.process_redirects(data)
54
+ redirects = []
55
+ data.each do |item|
56
+ next unless item.data['redirect_from']
57
+
58
+ redirect_from = item.data['redirect_from']
59
+ redirect_from = [redirect_from] unless redirect_from.is_a?(Array)
60
+
61
+ redirect_from.each do |source|
62
+ redirects << {
63
+ 'source' => source,
64
+ 'destination' => item.permalink,
65
+ 'type' => item.data['redirect_type'] || 301
66
+ }
67
+ end
68
+ end
69
+ redirects
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,57 @@
1
+ module Jekyll
2
+ module ServerSideRedirects
3
+ module Firebase
4
+ def self.generate_redirects(site)
5
+ redirects_data = []
6
+
7
+ redirects_data += process_redirects(site.pages)
8
+ redirects_data += process_redirects(site.posts.docs)
9
+ generate_firebase_json(redirects_data)
10
+
11
+ redirects_data
12
+ end
13
+
14
+ def self.generate_firebase_json(redirects_data)
15
+ file_path = "firebase.json"
16
+ firebase_content = {}
17
+
18
+ if File.exist?(file_path)
19
+ file_content = File.read(file_path)
20
+ firebase_content = JSON.parse(file_content)
21
+ else
22
+ firebase_content["hosting"] = { "redirects" => [] }
23
+ end
24
+
25
+ valid_keys = ['source', 'destination', 'type']
26
+ existing_redirects = firebase_content["hosting"]["redirects"].map do |redirect|
27
+ redirect.select { |key, _| valid_keys.include?(key) }
28
+ end
29
+
30
+ firebase_content["hosting"]["redirects"] = (existing_redirects + redirects_data).uniq { |r| r["source"] }
31
+
32
+ File.open(file_path, "w") do |file|
33
+ file.write(JSON.pretty_generate(firebase_content))
34
+ end
35
+ end
36
+
37
+ def self.process_redirects(data)
38
+ redirects = []
39
+ data.each do |item|
40
+ next unless item.data['redirect_from']
41
+
42
+ redirect_from = item.data['redirect_from']
43
+ redirect_from = [redirect_from] unless redirect_from.is_a?(Array)
44
+
45
+ redirect_from.each do |source|
46
+ redirects << {
47
+ 'source' => source,
48
+ 'destination' => item.permalink,
49
+ 'type' => item.data['redirect_type'] || 301
50
+ }
51
+ end
52
+ end
53
+ redirects
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,4 @@
1
+ module Jekyll
2
+ module RedirectGenerator
3
+ end
4
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-server-side-redirects
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Aline Marques
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-12-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.13'
27
+ description: Generates server-specific files for handling redirects (e.g., .htaccess,
28
+ firebase.json).
29
+ email:
30
+ - alinem_oliveira@yahoo.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - LICENSE
36
+ - README.md
37
+ - lib/jekyll-server-side-redirects.rb
38
+ - lib/jekyll-server-side-redirects/generator.rb
39
+ - lib/jekyll-server-side-redirects/servers/apache.rb
40
+ - lib/jekyll-server-side-redirects/servers/firebase.rb
41
+ homepage: https://github.com/marquesaline/jekyll-server-side-redirects
42
+ licenses:
43
+ - MIT
44
+ metadata: {}
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubygems_version: 3.4.19
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Jekyll plugin to generate server-side redirects
64
+ test_files: []