Files
new-website-jekyll-theme/_plugins/rouge.rb
2025-05-07 17:41:38 -03:00

22 lines
622 B
Ruby

# frozen_string_literal: true
Jekyll::Hooks.register :documents, :post_convert do |doc|
code_blocks = doc.to_html.css('pre>code')
next if code_blocks.empty?
require 'rouge'
formatter = Rouge::Formatters::HTMLPygments.new(Rouge::Formatters::HTML.new)
code_blocks.each do |code_block|
lang = code_block['data-lang']&.strip
lang = 'guess' if !lang || lang.empty?
lexer = Rouge::Lexer.find_fancy(lang, code_block.content) || Rouge::Lexers::PlainText
format = formatter.format(lexer.lex(code_block.content))
next unless format.start_with? '<'
code_block.parent.replace(format)
end
end