Skip to content

Commit

Permalink
Make relative_to_absolute more configurable
Browse files Browse the repository at this point in the history
Changelog: changed
  • Loading branch information
yorickpeterse committed Jan 27, 2024
1 parent e118d51 commit 30fe36f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/wobsite.inko
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ class UpdateAssetLinks {

fn hashed_url(url: String) -> String {
let key = if url.starts_with?('..') {
relative_to_absolute(@url, url)
relative_to_absolute(@url, url, as_file: true)
} else {
url
}
Expand Down
22 changes: 15 additions & 7 deletions src/wobsite/url.inko
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,22 @@ fn pub file_url(directory: ref Path, path: ref Path) -> String {
}
}

fn pub relative_to_absolute(current: String, url: String) -> String {
# Converts a relative URL to an absolute URL, based on the current/source URL of
# the document that links to the relative URL.
#
# The `current` argument specifies the URL of the document we are linking
# _from_, while `url` specifies the relative URL linked _to_.
#
# If `as_file` is set to `true`, the last component of the target URL is treated
# as if it were a file instead of a directory. This affects how many times we
# have to move "up" in the returned relative URL.
fn pub relative_to_absolute(
current: String,
url: String,
as_file: Bool,
) -> String {
let steps = url.split('/').take_while fn (v) { v == '..' }.count

# We add 1 here such that if the current URL is /foo/bar and we link to
# foo.css, the returned URL is /foo/foo.css, not /foo/bar/foo.css. In other
# words, the final component is treated as if it were a file instead of a
# directory.
let mut pending = steps + 1
let mut pending = steps + as_file.to_int
let chunks = current.split('/').to_array

while pending > 0 {
Expand Down
23 changes: 18 additions & 5 deletions test/wobsite/test_url.inko
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,29 @@ fn pub tests(t: mut Tests) {
}

t.test('url.relative_to_absolute') fn (t) {
t.equal(url.relative_to_absolute('/', '../style.css'), '/style.css')
t.equal(url.relative_to_absolute('/foo/', '../style.css'), '/style.css')
t.equal(url.relative_to_absolute('/foo/', 'style.css'), '/style.css')
t.equal(
url.relative_to_absolute('/foo/bar', '../style.css'),
url.relative_to_absolute('/', '../style.css', as_file: true),
'/style.css'
)
t.equal(
url.relative_to_absolute('/foo/bar/baz', '../style.css'),
url.relative_to_absolute('/foo/', '../style.css', as_file: true),
'/style.css'
)
t.equal(
url.relative_to_absolute('/foo/', 'style.css', as_file: true),
'/style.css'
)
t.equal(
url.relative_to_absolute('/foo/bar', '../style.css', as_file: true),
'/style.css'
)
t.equal(
url.relative_to_absolute('/foo/bar/baz', '../style.css', as_file: true),
'/foo/style.css'
)
t.equal(
url.relative_to_absolute('/articles/article1', 'article2', as_file: true),
'/articles/article2'
)
}
}

0 comments on commit 30fe36f

Please sign in to comment.