jekyll_dynamic_assets 1.1.0 → 1.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ce45ad23c704195b61891db09c7f3d5e49c8152d5203d3ef1fb777eb82fa5c64
4
- data.tar.gz: acdc459add9164deac556f9a018ae473f9393a1c93a1c42176f9a8c783210079
3
+ metadata.gz: 8f4102ebcc09a9c91ad535a06179560476217a410d75511df6fd78762b2227c5
4
+ data.tar.gz: 1255d4fc1b4de77e09c30d9d388fa7f0dd6f75c0ba65a1ef3c35ecc3314cbf75
5
5
  SHA512:
6
- metadata.gz: 61e538c18026d13670756d41339abf91e41665e726ecf35f4a5aa9eff9f611febe9d3db84cb3686942bc414e23608d4baadc66fd84ef9445ab9439d2802301ba
7
- data.tar.gz: e83ebbe0089fc6b435d40e40d7512e5ee3836f511d4765333b1282ff94651ccb11bd776c72e674707d700f80ebdd758504f213f02d52caea7a268ae417a725fb
6
+ metadata.gz: 5543ce35aeb57e5aa44624e89dbb2393863fd1b80e3d1d1ac1394dd6463cc428f6756b4f416d447c629797f92d00b4d37128feb2eec0ca2ced1aa3ebab69f8b3
7
+ data.tar.gz: 3586a4f15eaabc0acdfbffe6e8080069f809f6f64ad5c71e2f56a801b3a352d85b9db6d62f44fee3015c1351d6aad6b85d975a7909430e31a7303b855379774d
data/.rubocop.yml CHANGED
@@ -1,5 +1,6 @@
1
1
  AllCops:
2
2
  TargetRubyVersion: 3.2
3
+ SuggestExtensions: false
3
4
 
4
5
  Style/StringLiterals:
5
6
  EnforcedStyle: double_quotes
@@ -17,4 +18,7 @@ Lint/ScriptPermission:
17
18
  Enabled: false
18
19
 
19
20
  Layout/EndOfLine:
21
+ Enabled: false
22
+
23
+ Metrics/ClassLength:
20
24
  Enabled: false
data/CHANGELOG.md CHANGED
@@ -23,4 +23,25 @@
23
23
  - woff2
24
24
  - ttf
25
25
  - otf
26
- - Added Source selection
26
+ - Added Source selection
27
+
28
+ ## [1.2.0] - 2025-06-06
29
+
30
+ - Added option to select formats
31
+
32
+ ## [1.3.0] - 2025-06-07
33
+
34
+ - Added Sub directory options
35
+ - Added Default Sub directories for
36
+ - css >> styles
37
+ - js >> scripts
38
+ - mjs >> scripts
39
+ - ts >> scripts
40
+ - json >> data
41
+ - ico >> icons
42
+ - woff >> fonts
43
+ - woff2 >> fonts
44
+ - ttf >> fonts
45
+ - otf >> fonts
46
+ - Updated Selection logic, added inline definition
47
+ - Added Absolute URL
data/README.md CHANGED
@@ -1,13 +1,16 @@
1
1
  # JekyllDynamicAssets
2
2
 
3
- JekyllDynamicAssets is a Jekyll plugin that allows you to dynamically manage and inject CSS, JS, and other head assets into your site using presets, per-page configuration, and flexible formatting.
3
+ JekyllDynamicAssets is a powerful Jekyll plugin for dynamic, flexible, and DRY asset management. It lets you define, group, and inject CSS, JS, and other head assets using presets, per-page config, and custom formatting.
4
4
 
5
5
  ## Features
6
6
  - Define global (master) assets and per-page assets
7
7
  - Use asset presets for reusable asset groups
8
- - Pre-defined default formats for common assets, can be overwritten
8
+ - Pre-defined and overrideable formats and sources for common assets
9
+ - Auto, Select, and Inline formats and sources
9
10
  - Liquid tag `{% assets %}` for easy asset injection in templates and includes
10
- - Error reporting for missing presets
11
+ - Error reporting for missing presets and formats
12
+ - Absolute/relative URL support
13
+ - Supports all head assets: CSS, JS, module JS, fonts, icons, JSON, etc.
11
14
 
12
15
  ## Installation
13
16
 
@@ -33,55 +36,96 @@ plugins:
33
36
 
34
37
  Finally, in your terminal run:
35
38
 
36
- ```bash
39
+ ```powershell
37
40
  bundle install
38
41
  ```
39
42
 
40
43
  ## Usage
41
44
 
42
- 1. **Configure your assets in `config.yml`:**
45
+ ### 1. Configure your assets in `config.yml`
43
46
 
44
47
  ```yaml
45
48
  assets:
46
- master: # Master assets
49
+ master:
47
50
  - main.css
48
51
  - main.js
49
- source: "/assets/" # Optional: base path for all assets
50
- formats:
51
- js: "<script defer src='%s'></script>" # Overwrite defaults
52
- xyz: "<custom> %s </custom>" # Define Custom formats
53
- presets: # Create presets to include multiple assets
52
+
53
+ source:
54
+ base: /assets
55
+ github: https://github.com/assets/
56
+ css: /css
57
+ js: /js
58
+ absolute: true # Use absolute URLs (uses `url` and `baseurl` from config)
59
+
60
+ presets:
54
61
  blog: [blog.css, blog.js]
55
62
  project: [project.css, project.js, code-highlight.css, slideshow.js, myApp.js]
63
+
64
+ formats:
65
+ js: <script defer src='%s'></script>
66
+ xyz: <custom> %s </custom>
67
+ screen-css: <link rel="stylesheet" href="%s" media="screen">
68
+ ```
69
+
70
+ If all your assets are in the same folder, you can simply do:
71
+
72
+ ```yaml
73
+ assets:
74
+ source: /asset_folder
56
75
  ```
57
76
 
58
- 2. **Per-page or per-collection configuration:**
77
+ **Path rules:** Always use a leading slash, never a trailing slash.
78
+
79
+ ### 2. Per-page or per-collection configuration
59
80
 
60
81
  In your page or post front matter:
61
82
 
62
83
  ```yaml
63
84
  assets:
64
- files:
65
- - manual.css # include singular files
85
+ files: # See Asset Definition Syntax below
86
+ - manual.css
87
+ - onscreen.css::screen-css
88
+ - no_script.css:::<noscript><link rel="stylesheet" href="%s"></noscript>
89
+ - github<<master.css
90
+ - /css/new<<<new-main.css
66
91
  presets:
67
- - blog # Use a preset
92
+ - blog
93
+ - project
68
94
  ```
69
95
 
70
- 3. **Inject assets in your templates:**
96
+ ### 3. Inject assets in your templates
71
97
 
72
98
  Use the Liquid tag where you want the assets to appear (typically in your `<head>`):
73
99
 
74
100
  ```liquid
75
101
  <head>
76
102
  <!-- other tags like meta etc. -->
77
-
78
103
  {% assets %}
79
104
  </head>
80
105
  ```
81
106
 
82
107
  This will output the appropriate HTML tags for all configured assets. The tag should generally be used inside your `<head>` tag but can be used anywhere else.
83
108
 
84
- For assets which you haven't defined any format and there isn't any default for it either, the code will simply write the name of that asset.
109
+ ---
110
+
111
+ ### Asset Definition Syntax
112
+
113
+ You can use the following syntax anywhere (config or front matter):
114
+
115
+ ```
116
+ Source<<Asset::Format
117
+ ```
118
+
119
+ - `<<` uses `Source` as a variable from config; `<<<` uses `Source` as a literal.
120
+ - `::` uses `Format` as a variable from config; `:::` uses `Format` as a literal.
121
+ - If either is not defined, source/format is taken from config using the file extension.
122
+ - If the source is not external, it will be determined using `base + Source`.
123
+
124
+ **NOTE:**
125
+ - If JDA can't find a source, it will use the `base` without sub-directories.
126
+ - If JDA can't find the format, it will raise an error. A format is required for each asset.
127
+
128
+ ---
85
129
 
86
130
  ## Contributing
87
131
 
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JekyllDynamicAssets
4
+ DEFAULT_SOURCES = {
5
+ "base" => "/assets",
6
+ "css" => "/styles",
7
+ "js" => "/scripts",
8
+ "mjs" => "/scripts",
9
+ "ts" => "/scripts",
10
+ "json" => "/data",
11
+ "ico" => "/icons",
12
+ "woff" => "/fonts",
13
+ "woff2" => "/fonts",
14
+ "ttf" => "/fonts",
15
+ "otf" => "/fonts"
16
+ }.freeze
17
+ end
@@ -7,49 +7,111 @@ module JekyllDynamicAssets
7
7
  @config = site.config["assets"] || {}
8
8
  @page = page
9
9
  @page_config = page["assets"] || {}
10
+ @path = @page["path"] || @page["relative_path"] || "unknown"
11
+
12
+ sub_configs(site:)
10
13
  end
11
14
 
15
+ # Get all link tags to assets for this page
12
16
  def assets
13
17
  all_assets = combined_assets
14
18
  asset_insertions = []
15
- all_assets.each do |asset|
16
- extension = asset.split(".").last
17
- asset_link = format_string(extension) % asset
18
- asset_insertions << asset_link
19
+ all_assets.each do |dir, asset, format_string|
20
+ directory = dir.start_with?("http") ? dir : prepare_dir(dir)
21
+ asset_link = directory + asset
22
+ asset_insertions << format_string % asset_link
19
23
  end
20
24
  asset_insertions
21
25
  end
22
26
 
23
27
  private
24
28
 
25
- def format_string(extension)
26
- formats ||= formats_merge(DEFAULT_FORMATS, @config["formats"])
27
- formats[extension] || "%s"
29
+ def sub_configs(site:)
30
+ @link_formats ||= setting_merge(DEFAULT_FORMATS, @config["formats"]) # Format Config
31
+
32
+ # Standardize Source Config
33
+ srcs ||= setting_merge(DEFAULT_SOURCES, @config["source"])
34
+ @asset_sources = if srcs.is_a?(String)
35
+ {
36
+ "base" => srcs
37
+ }.freeze
38
+ else
39
+ srcs
40
+ end
41
+
42
+ # Get URL for absolute_url filter
43
+ return unless @config["absolute"] == true
44
+
45
+ @url = (site.config["url"] || "") + (site.config["baseurl"] || "")
46
+ end
47
+
48
+ def setting_merge(default, custom)
49
+ if custom.nil?
50
+ default
51
+ elsif custom.is_a?(String)
52
+ custom
53
+ else
54
+ custom.merge default do |_key, custom_setting, default_setting|
55
+ custom_setting unless default_setting.respond_to? :merge
56
+ end
57
+ end
58
+ end
59
+
60
+ def prepare_dir(dir)
61
+ # Format URL assuming standard inputs, all leading slashes and no trailing
62
+ directory = @config["absolute"] ? @url : ""
63
+ directory += @asset_sources["base"] unless @asset_sources["base"] == ""
64
+ directory += dir unless dir.empty? || dir == ""
65
+ directory += "/"
66
+ directory
28
67
  end
29
68
 
30
- def formats_merge(default, custom)
31
- custom.merge default do |_key, custom_setting, default_setting|
32
- custom_setting unless default_setting.respond_to? :merge
69
+ def get_sub_dir(string)
70
+ return string.split("<<<").first if string.include?("<<<")
71
+
72
+ hash = string.include?("<<") ? string.split("<<").first : string.split(".").last
73
+
74
+ @asset_sources.fetch(hash) do
75
+ warn "DynamicAssets: No source defined for #{hash.inspect} in #{@path} - using base source"
76
+ ""
77
+ end
78
+ end
79
+
80
+ def get_format(string)
81
+ return string.split(":::").last if string.include?(":::")
82
+
83
+ hash = string.include?("::") ? string.split("::").last : string.split(".").last
84
+
85
+ @link_formats.fetch(hash) do
86
+ raise KeyError, "DynamicAssets: No #{hash.inspect} format defined"
33
87
  end
34
88
  end
35
89
 
36
90
  def combined_assets
37
91
  # Container
92
+ raw_assets = []
38
93
  assets = []
39
94
 
40
95
  # Add assets
41
- assets.concat(Array(@config["master"]))
42
- assets.concat(Array(preset_files))
43
- assets.concat(Array(@page_config["files"]))
96
+ raw_assets.concat(Array(@config["master"]))
97
+ raw_assets.concat(Array(preset_files))
98
+ raw_assets.concat(Array(@page_config["files"]))
44
99
 
45
- append_base(assets.uniq)
100
+ raw_assets.uniq.each do |asset|
101
+ assets << asset_array(asset)
102
+ end
103
+ assets
46
104
  end
47
105
 
48
- def append_base(assets)
49
- base = @config["source"].to_s
50
- base += "/" unless base.end_with?("/") || base.empty?
106
+ def asset_array(asset)
107
+ buff = Array.new(3)
108
+ buff[2] = get_format(asset) # Store Format
109
+ asset = asset.split(/:{2,}/).first # Remove Format selector
110
+ buff[0] = get_sub_dir(asset) # Store Source
111
+ asset = asset.split(/<{2,}/).last # Remove Source selector
112
+ buff[1] = asset # Store Asset path
51
113
 
52
- assets.map { |asset| asset.start_with?("http") ? asset : base + asset }
114
+ buff
53
115
  end
54
116
 
55
117
  def preset_files
@@ -58,6 +120,7 @@ module JekyllDynamicAssets
58
120
  bad_presets = []
59
121
  selected_presets = Array(@page_config["presets"])
60
122
  preset_map = @config["presets"] || {}
123
+
61
124
  selected_presets.each do |preset|
62
125
  if preset_map.key?(preset)
63
126
  preset_assets.concat(Array(preset_map[preset]))
@@ -65,17 +128,18 @@ module JekyllDynamicAssets
65
128
  bad_presets << preset
66
129
  end
67
130
  end
68
- remaining?(bad_presets) # Raise error for undefined errors
131
+
132
+ remaining_presets?(bad_presets) # Raise error for undefined errors
69
133
  preset_assets
70
134
  end
71
135
 
72
- def remaining?(remaining_presets)
73
- # Display the undefined presets
136
+ def remaining_presets?(remaining_presets)
137
+ # return if no undefined presets
74
138
  return if remaining_presets.empty?
75
139
 
76
- location = @page["path"] || @page["relative_path"] || "unknown"
140
+ # Raise error for all undefined presets
77
141
  missing_list = remaining_presets.to_a.join(", ")
78
- raise "DynamicAssets: No preset(s) defined: #{missing_list} at: #{location}"
142
+ raise KeyError, "DynamicAssets: No preset(s) defined: #{missing_list}"
79
143
  end
80
144
  end
81
145
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JekyllDynamicAssets
4
- VERSION = "1.1.0"
4
+ VERSION = "1.3.0"
5
5
  end
@@ -4,6 +4,7 @@ require_relative "jekyll_dynamic_assets/version"
4
4
  require_relative "jekyll_dynamic_assets/processor"
5
5
  require_relative "jekyll_dynamic_assets/assets_tag"
6
6
  require_relative "jekyll_dynamic_assets/defaults/formats"
7
+ require_relative "jekyll_dynamic_assets/defaults/sources"
7
8
 
8
9
  module JekyllDynamicAssets
9
10
  class Error < StandardError; end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll_dynamic_assets
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - M. Umar Shahbaz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-06-06 00:00:00.000000000 Z
11
+ date: 2025-06-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -67,6 +67,7 @@ files:
67
67
  - lib/jekyll_dynamic_assets.rb
68
68
  - lib/jekyll_dynamic_assets/assets_tag.rb
69
69
  - lib/jekyll_dynamic_assets/defaults/formats.rb
70
+ - lib/jekyll_dynamic_assets/defaults/sources.rb
70
71
  - lib/jekyll_dynamic_assets/processor.rb
71
72
  - lib/jekyll_dynamic_assets/version.rb
72
73
  - sig/jekyll_dynamic_assets.rbs