Skip to content

Commit

Permalink
Fix: Do not rewrite paths to keep relative paths
Browse files Browse the repository at this point in the history
Assets had trouble finding the relative path because we were rewriting and changing the final path to the document.

Using the proc matcher, we can keep the path we were given and match on the split subpath to find the matching document.
  • Loading branch information
pusewicz committed Feb 16, 2022
1 parent a3fed42 commit 285eaf3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
43 changes: 18 additions & 25 deletions lib/statique/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,11 @@
require "roda"
require "slim"
require "digest/sha1"
require "rack/rewrite"

class Statique
class App < Roda
extend Forwardable

PAGE_REGEX = /(.*)\/page\/(\d+)/
use Rack::Rewrite do
rewrite PAGE_REGEX, "$1?page=$2"
end

def_delegators :Statique, :url, :root_url

opts[:root] = Statique.configuration.paths.pwd
Expand All @@ -39,30 +33,36 @@ class App < Roda
end
end

Statique.discover.documents.each do |document|
Statique.ui.debug "Defining route", {path: document.path, file: document.file}
static_get document.path do |r|
@document = Statique.discover.documents.find { _1.file == document.file }
route do |r|
if Statique.mode.server?
r.public if Statique.configuration.paths.public.exist?
r.assets if Statique.configuration.paths.assets.exist?
end

path, page = r.env["REQUEST_PATH"].split("/page/")

document = Statique.discover.documents.find { _1.path == path }

r.on(proc { document }) do
locals = {
documents: Statique.discover.documents,
collections: Statique.discover.collections,
document: @document
document: document
}

if @document.meta.paginates
paginator = Paginator.new(Statique.discover.collections[@document.meta.paginates].to_a, document.path, r.params.fetch("page", 1))
locals[@document.meta.paginates.to_sym] = paginator.documents
if document.meta.paginates
paginator = Paginator.new(Statique.discover.collections[document.meta.paginates].to_a, document.path, page)
locals[document.meta.paginates.to_sym] = paginator.documents
locals[:paginator] = paginator
end

options = {
engine: @document.engine_name,
inline: @document.content,
engine: document.engine_name,
inline: document.content,
locals:,
layout_opts: {locals:},
cache_key: Digest::SHA1.hexdigest(@document.file.to_s + @document.content),
layout: "../layouts/#{@document.layout_name}"
cache_key: Digest::SHA1.hexdigest(document.file.to_s + document.content),
layout: "../layouts/#{document.layout_name}"
}

if document.layout_name
Expand All @@ -72,12 +72,5 @@ class App < Roda
end
end
end

if Statique.mode.server?
route do |r|
r.public if Statique.configuration.paths.public.exist?
r.assets if Statique.configuration.paths.assets.exist?
end
end
end
end
1 change: 0 additions & 1 deletion statique.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ Gem::Specification.new do |spec|
spec.add_dependency "front_matter_parser", "~> 1.0.1"
spec.add_dependency "hashie", "~> 5.0.0"
spec.add_dependency "memo_wise", "~> 1.6.0"
spec.add_dependency "rack-rewrite", "~> 1.5.1"
spec.add_dependency "roda", "~> 3.52"
spec.add_dependency "slim", "~> 4.1.0"
spec.add_dependency "thor", "~> 1.2.1"
Expand Down

0 comments on commit 285eaf3

Please sign in to comment.