#Understanding the Components
- PageFind: A powerful search library designed for static websites, offering a fast and efficient search experience without server-side components.
- Jekyll Plugin: A custom Ruby plugin (
_plugins/pagefind.rb
) that automatically triggers PageFind indexing after your Jekyll site is built. - GitHub Actions: A CI/CD platform that automates the build and deployment process, ensuring your site is updated whenever changes are pushed to your repository.
#Step-by-Step Implementation
-
Create the Search Page:
Add a new Markdown file namedsearch.md
to your Jekyll site’spages
directory. This file will contain the search interface:--- layout: default title: Site Search permalink: /search/ --- <link href="/pagefind/pagefind-ui.css" rel="stylesheet"> <script src="/pagefind/pagefind-ui.js"></script> <h1 style="text-align: center">Site Search</h1> <div id="search"></div> <p style="text-align: center; font-size: 0.7em">Powered by <a target="_blank" href="https://pagefind.app/">PageFind</a></p> <script> window.addEventListener('DOMContentLoaded', (event) => { new PagefindUI({ element: "#search", showSubResults: true }); }); </script>
-
Configure Jekyll:
Update your_config.yml
file to include the custom PageFind plugin:plugins: - jekyll-sitemap - jekyll-gist - jekyll-feed - jekyll-toc - kramdown - jekyll-sitemap - ./_plugins/pagefind.rb
-
Create the PageFind Plugin:
Create a new directory named_plugins
at the root of your Jekyll project and add a file namedpagefind.rb
with the following content:Jekyll::Hooks.register :site, :post_write do |site| system("pagefind --source '_site'") end
-
Configure PageFind:
Create a file namedpagefind.yml
in your root directory to configure PageFind’s behavior:source: _site bundle_dir: _pagefind split_pages_on: "h2"
-
Set up GitHub Actions:
Create a new file named.github/workflows/deploy.yml
in your repository with the following content. This setup will install PageFind during the deployment process, eliminating the need for a local installation:name: GitHub Pages on: push: branches: - main # Change this to your default branch if it's not 'main' jobs: build: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v2 - name: Setup Ruby uses: ruby/setup-ruby@v1 with: ruby-version: '3.0' - name: Install Jekyll and Bundler run: | gem install jekyll bundler bundle install - name: Install Node.js uses: actions/setup-node@v2 with: node-version: '14' - name: Install PageFind run: npm install -g @pagefind/core - name: Build site with Jekyll run: bundle exec jekyll build - name: Run PageFind indexing run: pagefind --source _site - name: Deploy to GitHub Pages uses: peaceiris/actions-gh-pages@v3 with: github_token: $ publish_dir: ./_site
#Explanation and Key Considerations
_plugins/pagefind.rb
: This plugin ensures that PageFind indexes your site content after each build.pagefind.yml
: This file configures PageFind, specifying the source directory, output directory, and content splitting behavior..github/workflows/deploy.yml
: This workflow automates the entire process, building your Jekyll site, running PageFind indexing, and deploying the final output to GitHub Pages.pagefind-entry.json
: This file, generated by PageFind during indexing, is crucial for the search functionality. Ensure it’s correctly generated and located in the_pagefind
directory.- CSS Customization: Include and customize the
pagefind-modular-ui.css
file to style the search interface according to your website’s design.