Module: Jekyll::VitePressTheme::CopyPage

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

Class Method Summary collapse

Class Method Details

.enabled?(item) ⇒ Boolean

Returns:

  • (Boolean)


408
409
410
# File 'lib/jekyll/vitepress_theme/hooks.rb', line 408

def enabled?(item)
  site_enabled?(item.site) && page_enabled?(item)
end

.leading_h1?(markdown) ⇒ Boolean

Returns:

  • (Boolean)


452
453
454
455
456
457
458
459
460
461
# File 'lib/jekyll/vitepress_theme/hooks.rb', line 452

def leading_h1?(markdown)
  return false if markdown.nil?

  stripped = markdown.lstrip
  return false if stripped.empty?

  stripped.match?(/\A#\s+\S/) ||
    stripped.match?(/\A<h1(?:\s|>)/i) ||
    stripped.match?(/\A[^\n]+\n=+\s*(?:\n|$)/)
end

.liquid_render_info(item, payload) ⇒ Object



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

def liquid_render_info(item, payload)
  liquid_options = item.site.config['liquid'] || {}

  {
    registers: { site: item.site, page: payload['page'] },
    strict_filters: liquid_options['strict_filters'],
    strict_variables: liquid_options['strict_variables']
  }
end

.page_enabled?(item) ⇒ Boolean

Returns:

  • (Boolean)


419
420
421
422
423
# File 'lib/jekyll/vitepress_theme/hooks.rb', line 419

def page_enabled?(item)
  page_theme = item.data['jekyll_vitepress']

  page_theme != false && !(page_theme.is_a?(Hash) && page_theme['copy_page'] == false)
end

.resolved_markdown(item, payload) ⇒ Object



425
426
427
428
429
430
431
432
433
434
# File 'lib/jekyll/vitepress_theme/hooks.rb', line 425

def resolved_markdown(item, payload)
  raw = item.content.to_s
  return raw unless item.respond_to?(:render_with_liquid?) && item.render_with_liquid?

  item.renderer.render_liquid(raw, payload, liquid_render_info(item, payload), item.path)
rescue StandardError => e
  relative_path = item.respond_to?(:relative_path) ? item.relative_path : item.path
  Jekyll.logger.warn('jekyll-vitepress-theme', "Copy page markdown capture failed for #{relative_path}: #{e.message}")
  raw
end

.site_enabled?(site) ⇒ Boolean

Returns:

  • (Boolean)


412
413
414
415
416
417
# File 'lib/jekyll/vitepress_theme/hooks.rb', line 412

def site_enabled?(site)
  theme_config = site.config['jekyll_vitepress']
  copy_page = theme_config['copy_page'] if theme_config.is_a?(Hash)

  !(copy_page.is_a?(Hash) && copy_page['enabled'] == false)
end

.with_title(markdown, title) ⇒ Object



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

def with_title(markdown, title)
  return markdown if markdown.to_s.empty? || title.to_s.empty? || leading_h1?(markdown)

  "# #{title}\n\n#{markdown}"
end