jekyll-refergen 0.1.3 → 0.2.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: 975de0a6fd9db5c2daef29a57671df169bab0502a3073d70deb01279e89721d0
4
- data.tar.gz: 745ae6b1033ea2eecd73c2cefd388038fb9270b3c2db6878f62c775f6d10de36
3
+ metadata.gz: e590b644a9abe0f2de11955ae50d0da67ac54dbcd1cdd3478ef7196dc6bbbead
4
+ data.tar.gz: 8927925a82183080a7e5c90d82aa0b3c21519fdc40acfbd78ce1f28592aa1faf
5
5
  SHA512:
6
- metadata.gz: b02d8376e10b863a151d5e5d1421dca541eb4cb30887f6b832b5eb3b7b003c56d7c27a681242fd2a6e11390ce304c8c1c1074af68ff75d537fe77b5bef458355
7
- data.tar.gz: 9d6cb4baf1cb0310596edbb9e55f9bb64202a96185a466069638eeac3e0a4a9be03a6989e8a5e6af3a1c43e61bd3730ef6bdd1d2152a9331a4c6f5b0c2c2835b
6
+ metadata.gz: 70c27dc044e30365a61638e579089ed9be3ca66c9f2f9c84f0c7e348661a90f0c295c196fb1de22c1952a8b00c8618fd42ed53acdf34dbbead525c6e7e7c02ed
7
+ data.tar.gz: a2e822df48d961b41cc44643a5fb7c1b7d628a749215d6239ed72e54482de034e126d2ccf33366ad057fd20ad0b5c1926d08b80e316ffe044108ad2f73caf9e0
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # jeklly-refergen
1
+ # jekyll-refergen
2
2
  Don’t forget to include a reference at the end of your posts.
3
3
 
4
4
  ---
@@ -8,35 +8,76 @@ Don’t forget to include a reference at the end of your posts.
8
8
  1. **Gem Installation**
9
9
  - Add to your Gemfile:
10
10
  ```ruby
11
- gem 'jeklly-refergen', git: 'https://github.com/yourname/jeklly-refergen.git'
11
+ gem 'jekyll-refergen', git: 'https://github.com/yourname/jekyll-refergen.git'
12
12
  ```
13
13
  - Or after publishing to rubygems.org:
14
14
  ```ruby
15
- gem 'jeklly-refergen'
15
+ gem 'jekyll-refergen'
16
16
  ```
17
17
  - Run `bundle install`
18
18
 
19
19
  2. **_config.yml Configuration**
20
20
  ```yaml
21
- reference_plugin:
21
+ refergen-plugin:
22
22
  collections: ["posts", "wiki"] # Specify which collections to process
23
23
  ```
24
- - The plugin will process all documents in the specified collections.
24
+ - The plugin processes all documents in the listed collections.
25
25
 
26
- 3. **Usage Example**
27
- - In your post/page content:
26
+ 3. **Usage Examples**
27
+ - Deduplicate by URL (same URL → same reference number):
28
28
  ```markdown
29
29
  [Example1](https://www.example.com), [Example2](https://www.example2.com), [Again Example1](https://www.example.com)
30
30
  ```
31
- - Will be rendered as:
31
+ Renders as:
32
32
  ```markdown
33
- Example1<sup><a href="#ref-1" id="ref-link-1">[1]</a></sup>, Example2<sup><a href="#ref-2" id="ref-link-2">[2]</a></sup>, Again Example1<sup><a href="#ref-1" id="ref-link-1">[1]</a></sup>
33
+ Example1<sup><a href="#ref-1" id="ref-link-1">[1]</a></sup>, Example2<sup><a href="#ref-2" id="ref-link-2">[2]</a></sup>, Again Example1<sup><a href="#ref-1" id="ref-link-3">[1]</a></sup>
34
34
 
35
35
  ---
36
36
  ## Reference
37
- 1. <a id="ref-1"></a>[Example1](https://www.example.com)<br/>
37
+ 1. <a id="ref-1"></a>[Example1](https://www.example.com) ^ <a href="#ref-link-1">a</a> <a href="#ref-link-3">b</a><br/>
38
38
  2. <a id="ref-2"></a>[Example2](https://www.example2.com)<br/>
39
39
  ```
40
+ When the same URL is referenced multiple times, the Reference section shows `^a b c` format where each letter links to the corresponding reference location in the text.
41
+
42
+ - Custom reference label: use the Markdown link title (`"..."` or unquoted) as the reference display name.
43
+ ```markdown
44
+ [Visible text](https://www.example.com "Custom label")
45
+ [Another link](https://www.example.com Custom label)
46
+ ```
47
+ Renders as:
48
+ ```markdown
49
+ Visible text<sup><a href="#ref-1" id="ref-link-1">[1]</a></sup>
50
+ Another link<sup><a href="#ref-1" id="ref-link-2">[1]</a></sup>
51
+
52
+ ---
53
+ ## Reference
54
+ 1. <a id="ref-1"></a>[Custom label](https://www.example.com) ^ <a href="#ref-link-1">a</a> <a href="#ref-link-2">b</a><br/>
55
+ ```
56
+
57
+ 4. **Local Test (example)**
58
+ - Install dependencies:
59
+ ```bash
60
+ bundle install
61
+ ```
62
+ - Prepare a test Jekyll site (create one if needed):
63
+ ```bash
64
+ bundle exec jekyll new demo-site --skip-bundle
65
+ cd demo-site
66
+ bundle add ../jekyll-refergen # reference local plugin
67
+ ```
68
+ - Add plugin to `_config.yml`:
69
+ ```yaml
70
+ plugins:
71
+ - jekyll-refergen
72
+ refergen-plugin:
73
+ collections: ["posts"]
74
+ ```
75
+ - Add the usage examples to a post, then build/serve:
76
+ ```bash
77
+ bundle exec jekyll build
78
+ bundle exec jekyll serve
79
+ ```
80
+ - In the rendered HTML, verify same URLs share the same number and the title is used as the reference label when provided.
40
81
 
41
82
  ---
42
83
 
@@ -73,7 +114,7 @@ jobs:
73
114
  ```
74
115
 
75
116
  - This workflow builds your Jekyll site and deploys the `_site` folder to GitHub Pages on every push to the master branch.
76
- - If your Gemfile includes `jeklly-refergen`, the plugin will work in the GitHub Actions environment.
117
+ - If your Gemfile includes `jekyll-refergen`, the plugin will work in the GitHub Actions environment.
77
118
 
78
119
  ---
79
120
 
@@ -1,5 +1,4 @@
1
1
  # frozen_string_literal: true
2
-
3
2
  module Jekyll
4
3
  module Refergen
5
4
  class ReferenceGenerator < Jekyll::Generator
@@ -21,31 +20,96 @@ module Jekyll
21
20
  private
22
21
 
23
22
  def process_references(content)
24
- # Extract [text](url) patterns
25
- references = content.scan(/\[([^\]]+)\]\(([^\)]+)\)/)
23
+ link_data = extract_link_data(content)
24
+ return content if link_data.empty?
25
+
26
+ replaced_content = replace_links(content, link_data)
27
+ reference_section = generate_reference_section(link_data)
28
+ replaced_content + reference_section
29
+ end
30
+
31
+ def extract_link_data(content)
26
32
  url_to_number = {}
27
- number = 1
28
- references.each do |name, url|
29
- url_to_number[url] ||= number
30
- number += 1 if url_to_number[url] == number
33
+ url_to_label = {}
34
+ url_to_link_positions = {}
35
+ link_index = 0
36
+
37
+ content.scan(/\[([^\]]+)\]\(([^\)]+)\)/) do |text, url_part|
38
+ link_index += 1
39
+ url, ref_name = parse_link(url_part)
40
+
41
+ # Assign reference number
42
+ num = url_to_number[url] ||= url_to_number.length + 1
43
+
44
+ # Store label (use first occurrence)
45
+ label = ref_name&.strip
46
+ label = text if label.nil? || label.empty?
47
+ url_to_label[url] ||= label
48
+
49
+ # Track link positions for this URL
50
+ url_to_link_positions[url] ||= []
51
+ url_to_link_positions[url] << link_index
52
+ end
53
+
54
+ {
55
+ url_to_number: url_to_number,
56
+ url_to_label: url_to_label,
57
+ url_to_link_positions: url_to_link_positions
58
+ }
59
+ end
60
+
61
+ def parse_link(url_part)
62
+ # Try to match quoted title: url "title"
63
+ if url_part.match(/^([^\s\)]+)\s+"([^"]+)"$/)
64
+ [Regexp.last_match(1), Regexp.last_match(2)]
65
+ # Try to match unquoted title: url title
66
+ elsif url_part.match(/^([^\s\)]+)\s+(.+)$/)
67
+ [Regexp.last_match(1), Regexp.last_match(2)]
68
+ # No title: just url
69
+ else
70
+ [url_part, nil]
31
71
  end
32
- # Replace [name](url) with name<sup><a href="#ref-1" id="ref-link-1">[1]</a></sup>
33
- replaced_content = content.gsub(/\[([^\]]+)\]\(([^\)]+)\)/) do
34
- name = Regexp.last_match(1)
35
- url = Regexp.last_match(2)
72
+ end
73
+
74
+ def replace_links(content, link_data)
75
+ url_to_number = link_data[:url_to_number]
76
+ url_to_label = link_data[:url_to_label]
77
+ link_counter = 0
78
+
79
+ content.gsub(/\[([^\]]+)\]\(([^\)]+)\)/) do
80
+ link_counter += 1
81
+ text = Regexp.last_match(1)
82
+ url_part = Regexp.last_match(2)
83
+ url, = parse_link(url_part)
84
+
36
85
  num = url_to_number[url]
37
- "#{name}<sup><a href=\"#ref-#{num}\" id=\"ref-link-#{num}\">[#{num}]</a></sup>"
86
+ "#{text}<sup><a href=\"#ref-#{num}\" id=\"ref-link-#{link_counter}\">[#{num}]</a></sup>"
38
87
  end
39
- # Generate Reference section
40
- if url_to_number.any?
41
- reference_section = "\n\n---\n## Reference\n"
42
- url_to_number.sort_by { |_, num| num }.each do |url, num|
43
- name = references.find { |n, u| u == url }&.first || url
44
- reference_section += "#{num}. <a id=\"ref-#{num}\"></a>[#{name}](#{url})<br/>\n"
45
- end
46
- replaced_content += reference_section
88
+ end
89
+
90
+ def generate_reference_section(link_data)
91
+ url_to_number = link_data[:url_to_number]
92
+ url_to_label = link_data[:url_to_label]
93
+ url_to_link_positions = link_data[:url_to_link_positions]
94
+
95
+ reference_section = "\n\n---\n## Reference\n"
96
+
97
+ url_to_number.sort_by { |_, num| num }.each do |url, num|
98
+ label = url_to_label[url]
99
+ positions = url_to_link_positions[url]
100
+
101
+ # Generate position links (^a b c format)
102
+ position_links = positions.map.with_index do |pos, idx|
103
+ letter = ('a'.ord + idx).chr
104
+ "<a href=\"#ref-link-#{pos}\">#{letter}</a>"
105
+ end.join(' ')
106
+
107
+ position_prefix = positions.length > 1 ? " ^ #{position_links}" : ""
108
+
109
+ reference_section += "#{num}. <a id=\"ref-#{num}\"></a>[#{label}](#{url})#{position_prefix}<br/>\n"
47
110
  end
48
- replaced_content
111
+
112
+ reference_section
49
113
  end
50
114
  end
51
115
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Jekyll
4
4
  module Refergen
5
- VERSION = "0.1.3"
5
+ VERSION = "0.2.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-refergen
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Minseong Lee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-07-10 00:00:00.000000000 Z
11
+ date: 2025-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll