Skip to content

Commit

Permalink
Fix generating URLs for index documents
Browse files Browse the repository at this point in the history
Changelog: fixed
  • Loading branch information
yorickpeterse committed Jan 25, 2024
1 parent 882d014 commit 8a2bde7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/wobsite.inko
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import wobsite.fnmatch
import wobsite.time.(parse_date)
import wobsite.url.(file_url)

let INDEX_FILE = 'index.md'

# A type that maps asset paths/URIs to their checksums/hashes.
class Hashes {
# The keys of this map start with a `/`, such that relative URLs such as
Expand Down Expand Up @@ -605,7 +607,7 @@ class async Worker {
) {
let source = recover source
let rel_source = source.strip_prefix(@files.source).unwrap
let target = if rel_source.tail == 'index.md' {
let target = if rel_source.tail == INDEX_FILE {
@files.output.join(rel_source.with_extension('html'))
} else {
@files.output.join(rel_source.with_extension('').join('index.html'))
Expand Down
15 changes: 9 additions & 6 deletions src/wobsite/url.inko
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import std.fs.path.Path
import wobsite.INDEX_FILE

let TAB = 9
let LF = 10
Expand Down Expand Up @@ -67,11 +68,13 @@ fn pub normalize(string: String) -> String {

# Returns the relative URL for a file path in a directory.
fn pub file_url(directory: ref Path, path: ref Path) -> String {
let rel = path
.strip_prefix(directory)
.unwrap_or_else fn { path.clone }
.with_extension('')
.to_string
let rel = path.strip_prefix(directory).unwrap_or_else fn { path.clone }

if rel == 'index' { '/' } else { "/{rel}/" }
if rel.to_string == INDEX_FILE {
'/'
} else if rel.tail == INDEX_FILE {
"/{rel.directory.with_extension('')}/"
} else {
"/{rel.with_extension('')}/"
}
}
9 changes: 9 additions & 0 deletions test/wobsite/test_url.inko
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ fn pub tests(t: mut Tests) {
'/bar/foo/bar/'
)

t.equal(
url.file_url(Path.new('source'), Path.new('bar/foo/index.md')),
'/bar/foo/'
)

t.equal(url.file_url(Path.new('source'), Path.new('source/index.md')), '/')
t.equal(url.file_url(Path.new('source'), Path.new('index.md')), '/')
t.equal(
url.file_url(Path.new('source'), Path.new('source/foo/index.md')),
'/foo/'
)
}
}

0 comments on commit 8a2bde7

Please sign in to comment.