jekyll-link-decorator 1.0.3

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.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +15 -0
  4. data/lib/jekyll-link-decorator.rb +260 -0
  5. metadata +219 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a0f9b808eb1a08ff1ec662bbe580907599e65a41f0841dcd1fc00afd753b4964
4
+ data.tar.gz: f013b30a4b34f9da6e30ff10f5d305513f2b2b7a8ef675ff3630a665ce069e58
5
+ SHA512:
6
+ metadata.gz: 1c3f7778766e27f923060547092f58a3632e46caacb21aeaa0a6c027465ef3c6fd3cf17064379cbe00c10c608a4f2c91c195096816be71bcdd40551a4e21475d
7
+ data.tar.gz: 97643a1ee4b81a731e9b7f23341f30f38db54de807c94fe54322413e20877e4c675d36863dcd9f69471942706b504c401280977786148c5dd04d77313e40c131
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Alain Reguera Delgado
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,15 @@
1
+ # jekyll-link-decorator
2
+
3
+ A Jekyll plugin that automatically enhances Markdown links with Bootstrap CSS
4
+ classes, security attributes, Font Awesome icons, and context-aware styling.
5
+ Simplifies link management across Jekyll sites by handling styling, external
6
+ link protection, and icon injection without manual intervention.
7
+
8
+ ## Documentation
9
+
10
+ For complete documentation, configuration options, and examples, visit the
11
+ [documentation site](https://centos.gitlab.io/artwork/centos-web/jekyll-link-decorator/).
12
+
13
+ ## License
14
+
15
+ MIT License - see LICENSE file for details.
@@ -0,0 +1,260 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This Jekyll plugin, `link_decorator`, automatically enhances `<a>` tags
4
+ # within Markdown content by adding specific Bootstrap-related CSS classes and
5
+ # external-link icons. This functionality is conditional, applying different
6
+ # sets of classes based on whether a link is located inside a Bootstrap alert
7
+ # box or is a regular link. It explicitly avoids modifying elements that are
8
+ # designated as buttons by having the `.btn` class.
9
+ #
10
+ # === External Link Icon Support ===
11
+ #
12
+ # For links pointing to external domains (different from site.url), the plugin
13
+ # automatically adds a Font Awesome external-link icon (fa-solid fa-external-link)
14
+ # at the end of the link text, similar to the link component behavior.
15
+ #
16
+ # === External Link Detection & Target Management ===
17
+ #
18
+ # The plugin automatically detects external links and manages their behavior:
19
+ # - Relative URLs (starting with /, ../) and same-domain absolute URLs open in the same window
20
+ # - Absolute URLs pointing to different domains (http:// or https://) automatically get target="_blank"
21
+ # - Links with target="_blank" automatically receive rel="noopener noreferrer" protection
22
+ # against security vulnerabilities (window.opener access, referrer leakage)
23
+ #
24
+ # === Configuration ===
25
+ #
26
+ # You can customize the classes applied by this plugin by adding
27
+ # `with_link_decorator` and `with_link_decorator_data` sections to your `_config.yml` file.
28
+ #
29
+ # * `with_link_decorator`: Boolean (default: true) that controls whether the plugin runs.
30
+ # Set to false to disable automatic link styling and icon insertion.
31
+ #
32
+ # * `with_link_decorator_data`: A dictionary containing optional configuration keys:
33
+ # - `default_link_classes`: Defines the classes to be applied to all links that are not
34
+ # inside an .alert. Example: `default_link_classes: "text-decoration-none"`
35
+ # - `alert_link_classes`: Defines the classes for links specifically located within an
36
+ # .alert box. Example: `alert_link_classes: "text-decoration-underline"`
37
+ # - `external_link_icon`: Boolean (default: true) that controls whether external-link
38
+ # icons are added to cross-domain external links. Set to false to disable.
39
+ #
40
+ # If no configuration is provided, the plugin will use default fallback classes
41
+ # and enable external link icons.
42
+ #
43
+ # Here is an example `_config.yml` setup:
44
+ #
45
+ # ```yaml
46
+ # with_link_decorator: true
47
+ # with_link_decorator_data:
48
+ # default_link_classes: "link link-offset-3 link-offset-3-hover link-underline-primary link-underline-opacity-0 link-underline-opacity-100-hover"
49
+ # alert_link_classes: "alert-link"
50
+ # external_link_icon: true
51
+ # ```
52
+
53
+ require 'nokogiri'
54
+
55
+ module Jekyll
56
+ module Converters
57
+ # This custom converter applies specific CSS classes to <a> tags
58
+ # based on their context (e.g., inside an alert box) and excludes
59
+ # buttons, all done via Nokogiri's powerful CSS selectors.
60
+ # It also adds external-link icons for cross-domain external links.
61
+ class LinkDecorator < Jekyll::Converters::Markdown
62
+ def self.name
63
+ 'LinkDecorator'
64
+ end
65
+
66
+ # Extract domain from a URL by removing protocol and path components
67
+ # Example: "https://example.com/path?query=1#anchor" -> "example.com"
68
+ def extract_domain(url)
69
+ return '' if url.nil? || url.empty?
70
+
71
+ # Remove protocol (http:// or https://)
72
+ domain = url.sub(%r{^https?://}, '')
73
+
74
+ # Extract just the domain part (before /, ?, or #)
75
+ domain = domain.split(%r{[/?#]}).first
76
+
77
+ # Convert to lowercase for case-insensitive comparison
78
+ domain.downcase
79
+ end
80
+
81
+ # Check if a link is an external link pointing to a different domain
82
+ # Returns true if:
83
+ # - Link starts with http:// or https://
84
+ # - Link domain is different from site.url domain
85
+ # - Link doesn't already have an external-link icon child
86
+ def is_external_different_domain_link?(link, site_domain)
87
+ href = link['href'].to_s
88
+
89
+ # Check if it's an absolute URL with http/https
90
+ return false unless %r{^https?://}.match?(href)
91
+
92
+ # Extract domain from the link
93
+ link_domain = extract_domain(href)
94
+
95
+ # Check if domains are different
96
+ return false if link_domain == site_domain || link_domain.empty?
97
+
98
+ # Check if an icon has already been added (avoid duplicates)
99
+ # Check for both new icons (fa-external-link) and old icons
100
+ # (fa-external-link-alt, fa-up-right-from-square, etc.)
101
+ return false if link.css('i[class*="fa-external-link"]').any?
102
+ return false if link.css('i[class*="up-right-from-square"]').any?
103
+ return false if link.css('svg[data-icon="external-link"]').any?
104
+ return false if link.css('svg[data-icon="up-right-from-square"]').any?
105
+ return false if link.css('svg[data-icon="arrow-up-right-from-square"]').any?
106
+
107
+ true
108
+ end
109
+
110
+ # Determine if a link points to an external domain
111
+ # Returns true if:
112
+ # - Link is absolute (starts with http:// or https://)
113
+ # - Link domain is different from site.url domain
114
+ # Returns false if:
115
+ # - Link is relative (starts with /, ../)
116
+ # - Link domain matches site.url domain
117
+ def cross_domain_external_link?(link, site_domain)
118
+ href = link['href'].to_s
119
+
120
+ # Relative URLs (starting with / or ../) are internal links
121
+ return false if %r{^(/|\.\.)}.match?(href)
122
+
123
+ # Check if it's an absolute URL with http/https
124
+ return false unless %r{^https?://}.match?(href)
125
+
126
+ # Extract domain from the link
127
+ link_domain = extract_domain(href)
128
+
129
+ # Check if domains are different
130
+ return false if link_domain == site_domain || link_domain.empty?
131
+
132
+ true
133
+ end
134
+
135
+ # Add target="_blank" to external cross-domain links
136
+ # Only adds if target is not already set
137
+ def add_external_target_blank(link, site_domain)
138
+ # Skip if link already has a target attribute
139
+ return if link['target'].to_s.strip != ''
140
+
141
+ # Add target="_blank" if this is a cross-domain external link
142
+ return unless cross_domain_external_link?(link, site_domain)
143
+
144
+ link['target'] = '_blank'
145
+ end
146
+
147
+ # Add security attributes to links with target="_blank"
148
+ # Adds rel="noopener noreferrer" to prevent security vulnerabilities
149
+ # Preserves any existing rel attribute values
150
+ def add_external_target_protection(link)
151
+ target = link['target'].to_s
152
+
153
+ # Only add protection for target="_blank"
154
+ return unless target == '_blank'
155
+
156
+ # Get existing rel attribute (if any)
157
+ existing_rel = link['rel'].to_s.strip
158
+ rel_values = existing_rel.empty? ? [] : existing_rel.split(/\s+/)
159
+
160
+ # Add required security values if not already present
161
+ rel_values << 'noopener' unless rel_values.include?('noopener')
162
+ rel_values << 'noreferrer' unless rel_values.include?('noreferrer')
163
+
164
+ # Set the updated rel attribute
165
+ link['rel'] = rel_values.join(' ')
166
+ end
167
+
168
+ def matches(ext)
169
+ ext =~ /^\.md$/i
170
+ end
171
+
172
+ def output_ext(_ext)
173
+ '.html'
174
+ end
175
+
176
+ def convert(content)
177
+ html = super
178
+ enabled = @config.key?('with_link_decorator') ? @config['with_link_decorator'] : true
179
+ config = @config['with_link_decorator_data'] || {}
180
+
181
+ return html unless plugin_enabled?(enabled)
182
+
183
+ doc = Nokogiri::HTML::DocumentFragment.parse(html)
184
+
185
+ apply_link_styles(doc, config)
186
+ add_external_link_features(doc, config)
187
+
188
+ doc.to_html
189
+ rescue StandardError => e
190
+ Jekyll.logger.error 'LinkDecorator Error:',
191
+ "An error occurred during link decoration: #{e.message}"
192
+ Jekyll.logger.error 'Backtrace:', e.backtrace.join("\n")
193
+
194
+ html
195
+ end
196
+
197
+ # Check if the plugin is enabled in configuration
198
+ # Defaults to true if not explicitly configured
199
+ def plugin_enabled?(enabled)
200
+ unless enabled
201
+ Jekyll.logger.info 'LinkDecorator:',
202
+ 'Disabled via configuration. Skipping custom link modification.'
203
+ end
204
+ enabled
205
+ end
206
+
207
+ # Apply CSS classes to links based on their context (alert or default)
208
+ def apply_link_styles(doc, config)
209
+ default_classes = config.fetch('default_link_classes',
210
+ 'link link-offset-3 link-offset-3-hover link-underline-primary link-underline-opacity-0 link-underline-opacity-100-hover')
211
+ alert_classes = config.fetch('alert_link_classes', 'alert-link')
212
+
213
+ # Apply alert classes to links inside p.alert
214
+ doc.css('p.alert a:not(.btn)').each do |link|
215
+ add_classes(link, alert_classes)
216
+ end
217
+
218
+ # Apply default classes to all other non-alert links
219
+ doc.css('a:not(.btn)').each do |link|
220
+ add_classes(link, default_classes) unless link.ancestors('p.alert').any?
221
+ end
222
+ end
223
+
224
+ # Add CSS classes to a link element
225
+ def add_classes(link, classes_string)
226
+ classes_string.split.each { |cls| link.add_class(cls) }
227
+ end
228
+
229
+ # Add external link features: target="_blank", icons, and security attributes
230
+ def add_external_link_features(doc, config)
231
+ site_domain = extract_site_domain
232
+ return if site_domain.empty?
233
+
234
+ doc.css('a:not(.btn)').each do |link|
235
+ add_external_target_blank(link, site_domain)
236
+ add_external_icon(link, site_domain, config, doc)
237
+ add_external_target_protection(link)
238
+ end
239
+ end
240
+
241
+ # Extract site domain from Jekyll configuration
242
+ def extract_site_domain
243
+ site_url = @config['url'] || @config['baseurl'] || ''
244
+ extract_domain(site_url)
245
+ end
246
+
247
+ # Add external-link icon to cross-domain external links
248
+ def add_external_icon(link, site_domain, config, doc)
249
+ external_link_icon = config.fetch('external_link_icon', true)
250
+
251
+ return unless external_link_icon
252
+ return unless is_external_different_domain_link?(link, site_domain)
253
+
254
+ icon = Nokogiri::XML::Node.new('i', doc)
255
+ icon['class'] = 'fa-solid fa-external-link ms-1'
256
+ link.add_child(icon)
257
+ end
258
+ end
259
+ end
260
+ end
metadata ADDED
@@ -0,0 +1,219 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-link-decorator
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.3
5
+ platform: ruby
6
+ authors:
7
+ - ReleaseBot
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: jekyll
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '4.0'
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '5.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '4.0'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '5.0'
32
+ - !ruby/object:Gem::Dependency
33
+ name: nokogiri
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '1.13'
39
+ - - "<"
40
+ - !ruby/object:Gem::Version
41
+ version: '2.0'
42
+ type: :runtime
43
+ prerelease: false
44
+ version_requirements: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '1.13'
49
+ - - "<"
50
+ - !ruby/object:Gem::Version
51
+ version: '2.0'
52
+ - !ruby/object:Gem::Dependency
53
+ name: bundler
54
+ requirement: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '2.0'
59
+ type: :development
60
+ prerelease: false
61
+ version_requirements: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '2.0'
66
+ - !ruby/object:Gem::Dependency
67
+ name: rake
68
+ requirement: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - "~>"
71
+ - !ruby/object:Gem::Version
72
+ version: '13.0'
73
+ type: :development
74
+ prerelease: false
75
+ version_requirements: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: '13.0'
80
+ - !ruby/object:Gem::Dependency
81
+ name: rspec
82
+ requirement: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - "~>"
85
+ - !ruby/object:Gem::Version
86
+ version: '3.13'
87
+ type: :development
88
+ prerelease: false
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - "~>"
92
+ - !ruby/object:Gem::Version
93
+ version: '3.13'
94
+ - !ruby/object:Gem::Dependency
95
+ name: simplecov
96
+ requirement: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - "~>"
99
+ - !ruby/object:Gem::Version
100
+ version: '0.22'
101
+ type: :development
102
+ prerelease: false
103
+ version_requirements: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - "~>"
106
+ - !ruby/object:Gem::Version
107
+ version: '0.22'
108
+ - !ruby/object:Gem::Dependency
109
+ name: simplecov-console
110
+ requirement: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - "~>"
113
+ - !ruby/object:Gem::Version
114
+ version: '0.9'
115
+ type: :development
116
+ prerelease: false
117
+ version_requirements: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - "~>"
120
+ - !ruby/object:Gem::Version
121
+ version: '0.9'
122
+ - !ruby/object:Gem::Dependency
123
+ name: rubocop
124
+ requirement: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - "~>"
127
+ - !ruby/object:Gem::Version
128
+ version: '1.85'
129
+ type: :development
130
+ prerelease: false
131
+ version_requirements: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - "~>"
134
+ - !ruby/object:Gem::Version
135
+ version: '1.85'
136
+ - !ruby/object:Gem::Dependency
137
+ name: rubocop-performance
138
+ requirement: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - "~>"
141
+ - !ruby/object:Gem::Version
142
+ version: '1.26'
143
+ type: :development
144
+ prerelease: false
145
+ version_requirements: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - "~>"
148
+ - !ruby/object:Gem::Version
149
+ version: '1.26'
150
+ - !ruby/object:Gem::Dependency
151
+ name: rubocop-rspec
152
+ requirement: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - "~>"
155
+ - !ruby/object:Gem::Version
156
+ version: '3.9'
157
+ type: :development
158
+ prerelease: false
159
+ version_requirements: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - "~>"
162
+ - !ruby/object:Gem::Version
163
+ version: '3.9'
164
+ - !ruby/object:Gem::Dependency
165
+ name: yard
166
+ requirement: !ruby/object:Gem::Requirement
167
+ requirements:
168
+ - - "~>"
169
+ - !ruby/object:Gem::Version
170
+ version: '0.9'
171
+ type: :development
172
+ prerelease: false
173
+ version_requirements: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - "~>"
176
+ - !ruby/object:Gem::Version
177
+ version: '0.9'
178
+ description: A Jekyll plugin that automatically enhances Markdown links with Bootstrap
179
+ CSS classes, security attributes, Font Awesome icons, and context-aware styling.
180
+ Simplifies link management across Jekyll sites by handling styling, external link
181
+ protection, and icon injection without manual intervention.
182
+ email:
183
+ - group_58921183_bot_c5520aeba366578e2e444dd181ff3e23@noreply.gitlab.com
184
+ executables: []
185
+ extensions: []
186
+ extra_rdoc_files: []
187
+ files:
188
+ - LICENSE
189
+ - README.md
190
+ - lib/jekyll-link-decorator.rb
191
+ homepage: https://gitlab.com/CentOS/artwork/centos-web/jekyll-link-decorator
192
+ licenses:
193
+ - MIT
194
+ metadata:
195
+ homepage_uri: https://gitlab.com/CentOS/artwork/centos-web/jekyll-link-decorator
196
+ source_code_uri: https://gitlab.com/CentOS/artwork/centos-web/jekyll-link-decorator
197
+ changelog_uri: https://gitlab.com/CentOS/artwork/centos-web/jekyll-link-decorator/-/releases
198
+ documentation_uri: https://centos.gitlab.io/artwork/centos-web/jekyll-link-decorator
199
+ bug_tracker_uri: https://gitlab.com/CentOS/artwork/centos-web/jekyll-link-decorator/-/issues
200
+ rubygems_mfa_required: 'true'
201
+ rdoc_options: []
202
+ require_paths:
203
+ - lib
204
+ required_ruby_version: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ version: 3.1.0
209
+ required_rubygems_version: !ruby/object:Gem::Requirement
210
+ requirements:
211
+ - - ">="
212
+ - !ruby/object:Gem::Version
213
+ version: '0'
214
+ requirements: []
215
+ rubygems_version: 3.6.9
216
+ specification_version: 4
217
+ summary: Jekyll plugin that decorates links with Bootstrap classes and external-link
218
+ icons.
219
+ test_files: []