Module: Jekyll::VitePressTheme::CopyPage

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

Class Method Summary collapse

Class Method Details

.enabled?(item) ⇒ Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/jekyll/vitepress_theme/hooks.rb', line 178

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

.leading_h1?(markdown) ⇒ Boolean

Returns:

  • (Boolean)


222
223
224
225
226
227
228
229
230
231
# File 'lib/jekyll/vitepress_theme/hooks.rb', line 222

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



206
207
208
209
210
211
212
213
214
# File 'lib/jekyll/vitepress_theme/hooks.rb', line 206

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)


189
190
191
192
193
# File 'lib/jekyll/vitepress_theme/hooks.rb', line 189

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



195
196
197
198
199
200
201
202
203
204
# File 'lib/jekyll/vitepress_theme/hooks.rb', line 195

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)


182
183
184
185
186
187
# File 'lib/jekyll/vitepress_theme/hooks.rb', line 182

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



216
217
218
219
220
# File 'lib/jekyll/vitepress_theme/hooks.rb', line 216

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

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