Skip to content

Commit

Permalink
Reinstate the index option
Browse files Browse the repository at this point in the history
This wasn't needed for AWS as you can configure a custom 404 page, but
Cloudflare Pages expects a "404.html" file somewhere and you can't
change this behaviour.

Changelog: added
  • Loading branch information
yorickpeterse committed Apr 30, 2024
1 parent 1281980 commit fe20f76
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 13 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ To build the site, you can use the following methods on the `Site` type:
(relative to the output directory). This is useful when generating files using
arbitrary logic, such as an Atom feed.
- `page(pattern, index, func)`: finds Markdown files matching `pattern`, turning
them into HTML files by calling the `func` closure.
them into HTML files by calling the `func` closure. If `index` is set to
`true`, the output is `NAME/index.html` (e.g. `articles/foo/index.html`). If
`index` is `false`, the output is instead `NAME.html` (e.g.
`articles/foo.html`).

For example:

Expand Down Expand Up @@ -80,7 +83,7 @@ import wobsite.(Site, Page)
class async Main {
fn async main {
Site.build fn (site) {
site.page('/index.md') fn {
site.page('/index.md', index: false) fn {
recover fn (_, page: Page) { Result.Ok(page.to_html([])) }
}
}
Expand Down
23 changes: 16 additions & 7 deletions src/wobsite.inko
Original file line number Diff line number Diff line change
Expand Up @@ -498,19 +498,27 @@ class pub Site {
# typical setting, this closure converts the Markdown to HTML and wraps it in
# a layout of sorts.
#
# Input files are mapped to output files as follows:
# Input files are mapped to output files as follows when using `index: true`:
#
# - `./source/index.md` results in `./public/index.html`
# - `./source/foo.md` results in `./public/foo/index.html`
# - `./source/foo/bar/index.md` results in `./public/foo/bar/index.html`
# - `./source/foo/bar.md` results in `./public/foo/bar/index.html`
# - `./source/index.md` becomes `./public/index.html`
# - `./source/foo.md` becomes `./public/foo/index.html`
# - `./source/foo/bar/index.md` becomes `./public/foo/bar/index.html`
# - `./source/foo/bar.md` becomes `./public/foo/bar/index.html`
#
# When using `index: false`, the mapping is instead as follows:
#
# - `./source/index.md` becomes `./public/index.html`
# - `./source/foo.md` becomes `./public/foo.html`
# - `./source/foo/bar/index.md` becomes `./public/foo/bar.html`
# - `./source/foo/bar.md` becomes `./public/foo/bar.html`
fn pub mut page(
pattern: String,
index: Bool,
builder: fn -> uni fn (ref Files, Page) -> Result[html.Document, String],
) {
@files.matching(pattern).each fn (path) {
@pending += 1
spawn.page(recover path.clone, builder.call)
spawn.page(recover path.clone, index, builder.call)
}
}

Expand Down Expand Up @@ -593,11 +601,12 @@ class async Worker {

fn async page(
source: uni Path,
index: Bool,
layout: uni fn (ref Files, Page) -> Result[html.Document, String],
) {
let source = recover source
let rel_source = source.strip_prefix(@files.source).unwrap
let target = if rel_source.tail == INDEX_FILE {
let target = if rel_source.tail == INDEX_FILE or index.false? {
@files.output.join(rel_source.with_extension('html'))
} else {
@files.output.join(rel_source.with_extension('').join('index.html'))
Expand Down
56 changes: 52 additions & 4 deletions test/test_wobsite.inko
Original file line number Diff line number Diff line change
Expand Up @@ -414,14 +414,38 @@ test'

let site = Site.new(dir.path.clone, dir.path.clone).unwrap

site.page('*.md') fn {
site.page('*.md', index: true) fn {
recover fn (_, page: Page) { Result.Ok(page.to_html([])) }
}

t.true(site.wait.ok?)
t.equal(read(dir.path.join('foo/index.html')), Result.Ok("<p>\ntest</p>"))
}

t.test('Site.page with a regular Markdown file and index: false') fn (t) {
let dir = TempPath.directory(t.id)
let md = '\
---
{
"title": "test",
"date": "2024-01-01T13:00:00Z"
}
---

test'

write(dir.path.join('foo.md'), md)

let site = Site.new(dir.path.clone, dir.path.clone).unwrap

site.page('*.md', index: false) fn {
recover fn (_, page: Page) { Result.Ok(page.to_html([])) }
}

t.true(site.wait.ok?)
t.equal(read(dir.path.join('foo.html')), Result.Ok("<p>\ntest</p>"))
}

t.test('Site.page with the index Markdown file') fn (t) {
let dir = TempPath.directory(t.id)
let md = '\
Expand All @@ -438,7 +462,31 @@ test'

let site = Site.new(dir.path.clone, dir.path.clone).unwrap

site.page('*.md') fn {
site.page('*.md', index: true) fn {
recover fn (_, page: Page) { Result.Ok(page.to_html([])) }
}

t.true(site.wait.ok?)
t.equal(read(dir.path.join('index.html')), Result.Ok("<p>\ntest</p>"))
}

t.test('Site.page with the index Markdown file and index: false') fn (t) {
let dir = TempPath.directory(t.id)
let md = '\
---
{
"title": "test",
"date": "2024-01-01T13:00:00Z"
}
---

test'

write(dir.path.join('index.md'), md)

let site = Site.new(dir.path.clone, dir.path.clone).unwrap

site.page('*.md', index: false) fn {
recover fn (_, page: Page) { Result.Ok(page.to_html([])) }
}

Expand All @@ -461,7 +509,7 @@ test'

let site = Site.new(dir.path.clone, dir.path.clone).unwrap

site.page('*.md') fn {
site.page('*.md', index: true) fn {
recover fn (_, page: Page) { Result.Ok(page.to_html([])) }
}

Expand All @@ -484,7 +532,7 @@ test'

let site = Site.new(dir.path.clone, Path.new('/dev/null/invalid')).unwrap

site.page('*.md') fn {
site.page('*.md', index: true) fn {
recover fn (_, page: Page) { Result.Ok(page.to_html([])) }
}

Expand Down

0 comments on commit fe20f76

Please sign in to comment.