Module: Jekyll::VitePressTheme::VersionLabel

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

Constant Summary collapse

AUTO_VALUE =
'auto'.freeze
VERSION_PATTERN =
/VERSION\s*=\s*['"]([^'"]+)['"]/

Class Method Summary collapse

Class Method Details

.apply(site) ⇒ Object

Resolves current: auto to the version of the project being documented. Configure where to read it from:

jekyll_vitepress:
version:
  value: "2.0.0"              # literal, wins over file
  file: lib/my_gem/version.rb # or a file holding VERSION = '...'

With neither, it falls back to this theme's own version, which is only ever right for the theme's own documentation.



416
417
418
419
420
421
422
423
424
425
426
# File 'lib/jekyll/vitepress_theme/hooks.rb', line 416

def apply(site)
  versions = site.data['versions']
  return unless versions.is_a?(Hash)

  current_value = versions['current'] || versions[:current]
  return unless auto_value?(current_value)

  versions['current'] = "v#{site_version(site) || Jekyll::VitePressTheme::VERSION}"
rescue StandardError => e
  Jekyll.logger.warn('jekyll-vitepress-theme', "Version label resolution failed: #{e.message}")
end

.auto_value?(value) ⇒ Boolean

Returns:

  • (Boolean)


452
453
454
# File 'lib/jekyll/vitepress_theme/hooks.rb', line 452

def auto_value?(value)
  value.to_s.strip.casecmp(AUTO_VALUE).zero?
end

.normalize(value) ⇒ Object



448
449
450
# File 'lib/jekyll/vitepress_theme/hooks.rb', line 448

def normalize(value)
  value.to_s.strip.sub(/\Av/, '')
end

.site_version(site) ⇒ Object



428
429
430
431
432
433
434
435
436
# File 'lib/jekyll/vitepress_theme/hooks.rb', line 428

def site_version(site)
  config = site.config.dig('jekyll_vitepress', 'version')
  return nil unless config.is_a?(Hash)

  literal = config['value'].to_s.strip
  return normalize(literal) unless literal.empty?

  version_from_file(site, config['file'])
end

.version_from_file(site, path) ⇒ Object



438
439
440
441
442
443
444
445
446
# File 'lib/jekyll/vitepress_theme/hooks.rb', line 438

def version_from_file(site, path)
  return nil if path.to_s.strip.empty?

  full_path = File.expand_path(path.to_s, site.source)
  return nil unless File.file?(full_path)

  match = File.read(full_path)[VERSION_PATTERN]
  match && normalize(Regexp.last_match(1))
end