-
-
Save jschoolcraft/d2fbc93444af7647824f650909d6c502 to your computer and use it in GitHub Desktop.
Sitepress Pagefind Integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div data-controller="search"> | |
<div data-search-target="button"> | |
<div role="button" data-action="click->search#open keydown.meta+k@document->search#open keydown.ctrl+k@document->search#open" class="outline secondary search"> | |
<%= heroicon "magnifying-glass" %> | |
<span>Search</span> | |
<kbd>Cmd/Ctrl+K</kbd> | |
</div> | |
<dialog data-search-target="dialog"> | |
<article> | |
<header> | |
<button aria-label="Close" rel="prev" data-action="search#close"></button> | |
<p> | |
<strong>Global Site Search</strong> | |
</p> | |
</header> | |
<div data-search-target="search"></div> | |
</article> | |
</dialog> | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Controller } from "@hotwired/stimulus"; | |
import { PagefindUI } from "@pagefind/default-ui"; | |
// Connects to data-controller="search" | |
export default class extends Controller { | |
static targets = ["button", "dialog", "search"]; | |
connect() { | |
this.search = new PagefindUI({ | |
element: this.searchTarget, | |
highlightParam: "highlight", | |
autofocus: true, | |
showImages: false, | |
}); | |
} | |
disconnect() { | |
this.search.destroy(); | |
} | |
open() { | |
this.dialogTarget.showModal(); | |
} | |
close() { | |
this.dialogTarget.close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :sitepress do | |
namespace :pagefind do | |
desc "Compile the sitepress site and build a pagefind index" | |
task build: :environment do | |
site = Sitepress::Site.new(root_path: "app/content") | |
build_path = Rails.root.join("tmp/sitepress") | |
pagefind_output_path = Rails.root.join("public/pagefind") | |
puts "Purging output directory" | |
FileUtils.rm_r build_path | |
compiler = Sitepress::Compiler::Files.new(root_path: build_path, site:) | |
compiler.compile | |
puts <<~END | |
Building Pagefind Index | |
END | |
system "npx pagefind --site #{build_path} --output-path #{pagefind_output_path}" | |
end | |
end | |
end | |
unless ENV["SKIP_SITEPRESS_BUILD"] | |
if Rake::Task.task_defined?("assets:precompile") | |
Rake::Task["assets:precompile"].enhance(["sitepress:pagefind:build"]) | |
end | |
if Rake::Task.task_defined?("test:prepare") | |
Rake::Task["test:prepare"].enhance(["sitepress:pagefind:build"]) | |
elsif Rake::Task.task_defined?("spec:prepare") | |
Rake::Task["spec:prepare"].enhance(["sitepress:pagefind:build"]) | |
elsif Rake::Task.task_defined?("db:test:prepare") | |
Rake::Task["db:test:prepare"].enhance(["sitepress:pagefind:build"]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment