Module: Jekyll::VitePressTheme::RougeStyles

Defined in:
lib/jekyll/vitepress_theme/hooks.rb

Constant Summary collapse

DEFAULT_LIGHT =
'github'.freeze
DEFAULT_DARK =
'github.dark'.freeze

Class Method Summary collapse

Class Method Details

.apply(site) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/jekyll/vitepress_theme/hooks.rb', line 101

def apply(site)
  theme_config = site.config['jekyll_vitepress']
  return unless theme_config.is_a?(Hash)

  light_name, dark_name = resolved_theme_names(theme_config['syntax'])
  light_name = valid_theme_name(light_name, DEFAULT_LIGHT)
  dark_name = valid_theme_name(dark_name, DEFAULT_DARK)

  theme_config['syntax'] = {
    'light_theme' => light_name,
    'dark_theme' => dark_name
  }
  theme_config['_generated_rouge_css'] = generated_css(light_name, dark_name)
rescue StandardError => e
  Jekyll.logger.warn('jekyll-vitepress-theme', "Rouge theme generation failed: #{e.message}")
end

.generated_css(light_name, dark_name) ⇒ Object



143
144
145
146
147
148
149
150
151
152
# File 'lib/jekyll/vitepress_theme/hooks.rb', line 143

def generated_css(light_name, dark_name)
  light_theme = Rouge::Theme.find(light_name)
  dark_theme = Rouge::Theme.find(dark_name)
  return '' unless light_theme && dark_theme

  [
    light_theme.render(scope: '.vp-doc .highlighter-rouge .highlight'),
    dark_theme.render(scope: '.dark .vp-doc .highlighter-rouge .highlight')
  ].join("\n")
end

.normalized_name(name) ⇒ Object



127
128
129
130
131
132
133
134
# File 'lib/jekyll/vitepress_theme/hooks.rb', line 127

def normalized_name(name)
  return nil unless name

  value = name.to_s.strip
  return nil if value.empty?

  value
end

.resolved_theme_names(syntax_config) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/jekyll/vitepress_theme/hooks.rb', line 118

def resolved_theme_names(syntax_config)
  return [DEFAULT_LIGHT, DEFAULT_DARK] unless syntax_config.is_a?(Hash)

  light = normalized_name(syntax_config['light_theme'] || syntax_config[:light_theme])
  dark = normalized_name(syntax_config['dark_theme'] || syntax_config[:dark_theme])

  [light || DEFAULT_LIGHT, dark || DEFAULT_DARK]
end

.valid_theme_name(name, fallback) ⇒ Object



136
137
138
139
140
141
# File 'lib/jekyll/vitepress_theme/hooks.rb', line 136

def valid_theme_name(name, fallback)
  return name if Rouge::Theme.find(name)

  Jekyll.logger.warn('jekyll-vitepress-theme', "Unknown Rouge theme '#{name}', falling back to '#{fallback}'.")
  fallback
end