jekyll-shiki 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: a2f07a20e89e2fbbbda3fbf6d70bc9cfa218af8267e77f5f1db84ce33af33dd5
4
+ data.tar.gz: 5677419d80f1d162ad84b36e56d570b9b000e566c7599bfa082a02ea19a58440
5
+ SHA512:
6
+ metadata.gz: f3935ddf6bbd692a45991eb8d4d67773df64c45e4adeb552ad251adbe922438edc800ed1d90efd5d795d07460cdbafec85450a806f2dd92dc8aaf81c14a39d02
7
+ data.tar.gz: c12a5c2f1b15520b6f935efc5e21f9cd327eb208b5e2ac53bb45e7d99d7fe9df5f9547230b93c48a56f8cc3eb5b98cd9378a1cb9328d7c4b83e507f9a4735510
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 phothinmg
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Jekyll::Shiki
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/jekyll/shiki`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ ```bash
14
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
+ ```
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ ```bash
20
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/jekyll-shiki. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/jekyll-shiki/blob/master/CODE_OF_CONDUCT.md).
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Jekyll::Shiki project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/jekyll-shiki/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ # cspell:disable
4
+ require "jekyll"
5
+ require "nokogiri"
6
+ require "json"
7
+ require "open3"
8
+ require "pathname"
9
+ require "digest"
10
+
11
+ require_relative "version"
12
+
13
+ module Jekyll
14
+ # module Jekyll::ShikiCodeBlock
15
+ module Shiki
16
+ def self.resolve_shiki_bundle_path(site)
17
+ shiki_config = site.config["shiki"]
18
+ raise "Shiki highlight failed: Shiki config not found in jekyll config" unless shiki_config
19
+
20
+ bundle_path = shiki_config["bundle_path"]
21
+ raise "Shiki highlight failed: Required shiki bundle path." unless bundle_path
22
+
23
+ File.join(site.source, bundle_path)
24
+ end
25
+
26
+ def self.shiki_highlight(code, lang, site)
27
+ script_path = resolve_shiki_bundle_path(site)
28
+
29
+ cache_key = Digest::SHA256.hexdigest("#{lang}::#{code}")
30
+ Jekyll::Cache.new("Shiki").getset(cache_key) do
31
+ input = JSON.generate({ code: code, lang: lang }.compact)
32
+ stdout, stderr, status = Open3.capture3("node", script_path, stdin_data: input)
33
+ raise "Shiki highlight failed: #{stderr}" unless status.success?
34
+
35
+ stdout
36
+ end
37
+ end
38
+
39
+ def self.create_wrapper(lan, code, site)
40
+ lang = lan.capitalize
41
+ highlighted_code = shiki_highlight(code, lan, site)
42
+ <<~HTML
43
+ <div class="shiki_code" data-shiki-highlighter>
44
+ <div class="code_head">
45
+ <span>#{lan}</span>
46
+ <button type="button" aria-label="Highlight-#{lang}" data-copy-btn></button>
47
+ </div>
48
+ #{highlighted_code}
49
+ </div>
50
+ HTML
51
+ end
52
+
53
+ def self.replace_elements(node, site)
54
+ code_el = node.at_css('> code[class^="language-"]')
55
+ return unless code_el
56
+
57
+ code = code_el.text
58
+ lang = code_el["class"]
59
+ &.split
60
+ &.find { |class_name| class_name.start_with?("language-") }
61
+ &.delete_prefix("language-")
62
+ fragment = Nokogiri::HTML::DocumentFragment.parse(create_wrapper(lang, code, site))
63
+ node.replace(fragment)
64
+ end
65
+
66
+ def self.full_document?(html_content)
67
+ html_content.match?(/\A\s*(<!doctype\s+html|<html\b)/i)
68
+ end
69
+
70
+ def self.transform_html(html_content, site) # rubocop:disable Metrics/MethodLength
71
+ Jekyll.logger.info ""
72
+ Jekyll.logger.info "[jekyll-shiki] : Highlight with shiki will take time for first build"
73
+ doc = if full_document?(html_content)
74
+ Nokogiri::HTML.parse(html_content)
75
+ else
76
+ Nokogiri::HTML::DocumentFragment.parse(html_content)
77
+ end
78
+ elements = doc.css("pre").select { |pre| pre.at_css('> code[class^="language-"]') }
79
+ return html_content if elements.empty?
80
+
81
+ elements.each { |node| replace_elements(node, site) }
82
+ doc.to_html
83
+ end
84
+ end
85
+ end
86
+
87
+ Jekyll::Hooks.register :pages, :post_render do |page|
88
+ next unless page.output_ext == ".html"
89
+
90
+ page.output = Jekyll::Shiki.transform_html(page.output, page.site)
91
+ end
92
+
93
+ Jekyll::Hooks.register :documents, :post_render do |document|
94
+ next unless document.output_ext == ".html"
95
+
96
+ document.output = Jekyll::Shiki.transform_html(document.output, document.site)
97
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module Shiki
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-shiki
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - phothinmg
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.4'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '4.4'
26
+ - !ruby/object:Gem::Dependency
27
+ name: nokogiri
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.19.4
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 1.19.4
40
+ email:
41
+ - phothinmg@disroot.org
42
+ executables: []
43
+ extensions: []
44
+ extra_rdoc_files: []
45
+ files:
46
+ - LICENSE.txt
47
+ - README.md
48
+ - lib/jekyll-shiki.rb
49
+ - lib/version.rb
50
+ homepage: https://rubygems.org/gems/jekyll-shiki
51
+ licenses:
52
+ - MIT
53
+ metadata:
54
+ source_code_uri: https://github.com/mmdocs/jekyll-shiki
55
+ changelog_uri: https://github.com/mmdocs/jekyll-shiki/blob/main/CHANGELOG.md
56
+ rubygems_mfa_required: 'true'
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 3.3.0
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubygems_version: 4.0.15
72
+ specification_version: 4
73
+ summary: Jekyll plugin for Shiki Js, for mmdocs theme
74
+ test_files: []