Skip to content

Commit 4dcfe0c

Browse files
committed
Add support for relative base uri.
1 parent 68985ad commit 4dcfe0c

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/utopia/content/document.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,27 @@ def initialize(request, attributes = {})
3838
super()
3939
end
4040

41+
# @returns [Path] The original request path, if known.
42+
def request_path
43+
Path[request.env["REQUEST_PATH"]]
44+
end
45+
46+
protected def current_base_uri_path
47+
self.current.node.uri_path.dirname
48+
end
49+
50+
# Compute the relative path from the curent base uri (e.g. the node being rendered) to the request uri. This path can be used to ensure resources are loaded relative to a given path.
51+
#
52+
# | Base URI | Request Path | Relative URI |
53+
# |---------------|------------------------|--------------|
54+
# | "/page" | "/index" | "" |
55+
# | "/blog/entry" | "/blog/2025/05/my-cat" | "../.." |
56+
#
57+
# @returns [String] the base uri for the current page.
58+
def base_uri(relative_to = self.current_base_uri_path)
59+
Path[relative_to].shortest_path(request_path)
60+
end
61+
4162
def [] key
4263
@attributes[key]
4364
end

setup/site/pages/_page.xnode

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<title>Utopia</title>
1010
<?r end ?>
1111

12-
<base href="#{first.node.uri_path}"/>
12+
<base href="#{document.base_uri}"/>
1313
<meta name="viewport" content="width=device-width, initial-scale=1"/>
1414

1515
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous" />

0 commit comments

Comments
 (0)