Module: Jekyll::VitePressTheme::SearchIndex

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

Defined Under Namespace

Classes: GeneratedPage

Constant Summary collapse

TEMPLATE =
<<~LIQUID.freeze
  [
  {% assign first = true %}
  {% assign home_page = site.pages | where: 'url', '/' | first %}
  {% if home_page %}
    {% assign home_excerpt = home_page.content | markdownify | strip_html | strip_newlines | replace: '  ', ' ' | strip | truncate: 2400, '' %}
    {
      "title": {{ home_page.title | default: site.title | strip_html | strip | jsonify }},
      "url": {{ home_page.url | relative_url | jsonify }},
      "content": {{ home_excerpt | jsonify }}
    }
    {% assign first = false %}
  {% endif %}
  {% assign sidebar_groups = site.data.sidebar %}
  {% for group in sidebar_groups %}
    {% assign docs = site[group.collection] | sort: 'nav_order' %}
    {% for doc in docs %}
      {% if doc.title and doc.url %}
        {% unless first %},{% endunless %}
        {% assign excerpt = doc.content | markdownify | strip_html | strip_newlines | replace: '  ', ' ' | strip | truncate: 2400, '' %}
        {
          "title": {{ doc.title | strip_html | strip | jsonify }},
          "url": {{ doc.url | relative_url | jsonify }},
          "content": {{ excerpt | jsonify }}
        }
        {% assign first = false %}
      {% endif %}
    {% endfor %}
  {% endfor %}
  ]
LIQUID

Class Method Summary collapse

Class Method Details

.apply(site) ⇒ Object



51
52
53
54
55
# File 'lib/jekyll/vitepress_theme/hooks.rb', line 51

def apply(site)
  return if custom_page?(site)

  site.pages << GeneratedPage.new(site)
end

.custom_page?(site) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
# File 'lib/jekyll/vitepress_theme/hooks.rb', line 57

def custom_page?(site)
  site.pages.any? { |page| search_index_path?(page.path) || search_index_url?(page.url) } ||
    site.static_files.any? { |file| search_index_path?(file.path) || search_index_url?(file.relative_path) }
end

.search_index_path?(value) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
# File 'lib/jekyll/vitepress_theme/hooks.rb', line 62

def search_index_path?(value)
  return false unless value

  Pathname.new(value.to_s).basename.to_s == 'search.json'
rescue ArgumentError
  false
end

.search_index_url?(value) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/jekyll/vitepress_theme/hooks.rb', line 70

def search_index_url?(value)
  value.to_s.strip == '/search.json'
end