Build site using the pretty uris dir structure (#42)

* update build file to produce pretty uri dir structure

* remove unnecessary file server customization

* update target html logs in docs

* fix tests
This commit is contained in:
Facundo Olano 2024-07-04 15:18:23 -03:00 committed by GitHub
parent a94ea7069d
commit ea8041ed3d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 33 additions and 42 deletions

View file

@ -43,12 +43,12 @@ To preview your site locally, use `jorge serve`:
$ cd myblog $ cd myblog
$ jorge serve $ jorge serve
wrote target/feed.xml wrote target/feed.xml
wrote target/blog/goodbye-markdown.html wrote target/blog/goodbye-markdown/index.html
wrote target/blog/my-first-post.html wrote target/blog/my-first-post/index.html
wrote target/blog/hello-org.html wrote target/blog/hello-org/index.html
wrote target/blog/index.html wrote target/blog/index.html
wrote target/index.html wrote target/index.html
wrote target/blog/tags.html wrote target/blog/tags/index.html
serving at http://localhost:4001 serving at http://localhost:4001
``` ```
@ -96,10 +96,10 @@ Finally, you can render a minified version of your site with `jorge build`:
$ jorge build $ jorge build
wrote target/index.html wrote target/index.html
wrote target/assets/css/main.css wrote target/assets/css/main.css
wrote target/blog/hello.html wrote target/blog/hello/index.html
wrote target/blog/my-first-post.html wrote target/blog/my-first-post/index.html
wrote target/feed.xml wrote target/feed.xml
wrote target/tags.html wrote target/tags/index.html
``` ```
And that's about it. For more details see the: And that's about it. For more details see the:

View file

@ -42,7 +42,7 @@ func (cmd *Serve) Run(ctx *kong.Context) error {
defer watcher.Close() defer watcher.Close()
// serve the target dir with a file server // serve the target dir with a file server
fs := http.FileServer(HTMLFileSystem{http.Dir(config.TargetDir)}) fs := http.FileServer(http.Dir(config.TargetDir))
http.Handle("/", fs) http.Handle("/", fs)
if config.LiveReload { if config.LiveReload {
@ -156,24 +156,6 @@ func watchProjectFiles(watcher *fsnotify.Watcher, config *config.Config) error {
}) })
} }
// Tweaks the http file system to construct a server that hides the .html suffix from requests.
// Based on https://stackoverflow.com/a/57281956/993769
type HTMLFileSystem struct {
dirFS http.Dir
}
func (htmlFS HTMLFileSystem) Open(name string) (http.File, error) {
// Try name as supplied
f, err := htmlFS.dirFS.Open(name)
if os.IsNotExist(err) {
// Not found, try with .html
if f, err := htmlFS.dirFS.Open(name + ".html"); err == nil {
return f, nil
}
}
return f, err
}
// The event broker mediates between the file watcher // The event broker mediates between the file watcher
// that publishes site rebuild events // that publishes site rebuild events
// and the clients listening for them to refresh the browser // and the clients listening for them to refresh the browser

View file

@ -15,11 +15,11 @@ $ cd myblog
$ jorge serve $ jorge serve
building site building site
wrote target/feed.xml wrote target/feed.xml
wrote target/blog/goodbye-markdown.html wrote target/blog/goodbye-markdown/index.html
wrote target/blog/hello-org.html wrote target/blog/hello-org/index/.html
wrote target/index.html wrote target/index.html
wrote target/blog/index.html wrote target/blog/index.html
wrote target/blog/tags.html wrote target/blog/tags/index.html
done done
serving at http://localhost:4001 serving at http://localhost:4001
#+end_src #+end_src

View file

@ -13,12 +13,11 @@ So far you've seen how to [[file:jorge-init][start a project]], [[file:jorge-ser
#+begin_src console #+begin_src console
$ jorge build $ jorge build
skipping draft target/blog/my-own-blog-post.org skipping draft target/blog/my-own-blog-post.org
wrote target/2024-02-23-another-kind-of-post.html wrote target/2024-02-23-another-kind-of-post/index.html
wrote target/blog/my-own-blog-post.html wrote target/blog/goodbye-markdown/index.html
wrote target/blog/goodbye-markdown.html
wrote target/assets/css/main.css wrote target/assets/css/main.css
wrote target/blog/hello-org.html wrote target/blog/hello-org/index.html
wrote target/blog/tags.html wrote target/blog/tags/index.html
wrote target/index.html wrote target/index.html
wrote target/feed.xml wrote target/feed.xml
wrote target/blog/index.html wrote target/blog/index.html
@ -44,7 +43,7 @@ But for the sake of completeness, this is how this site is deployed: I have a VP
location / { location / {
# First attempt to serve request as file, # First attempt to serve request as file,
# then as directory. Otherwise respond 404. # then as directory. Otherwise respond 404.
try_files $uri $uri.html $uri/ =404; try_files $uri $uri/ =404;
} }
} }

View file

@ -327,9 +327,19 @@ func (site *site) buildFile(path string) error {
targetPath = strings.TrimSuffix(targetPath, filepath.Ext(targetPath)) + templ.TargetExt() targetPath = strings.TrimSuffix(targetPath, filepath.Ext(targetPath)) + templ.TargetExt()
contentReader = bytes.NewReader(content) contentReader = bytes.NewReader(content)
} }
targetExt := filepath.Ext(targetPath)
// arrange paths to ensure pretty uris, eg move blog/tags.html to blog/tags/index.html
if targetExt == ".html" && filepath.Base(targetPath) != "index.html" {
targetDir := strings.TrimSuffix(targetPath, ".html")
targetPath = filepath.Join(targetDir, "index.html")
err = os.MkdirAll(targetDir, DIR_RWE_MODE)
if err != nil {
return err
}
}
// post process file acording to extension and config // post process file acording to extension and config
targetExt := filepath.Ext(targetPath)
contentReader, err = markup.Smartify(targetExt, contentReader) contentReader, err = markup.Smartify(targetExt, contentReader)
if err != nil { if err != nil {
return err return err

View file

@ -560,9 +560,9 @@ layout: base
assertEqual(t, err, nil) assertEqual(t, err, nil)
// test target files generated // test target files generated
_, err = os.Stat(filepath.Join(config.TargetDir, "p1.html")) _, err = os.Stat(filepath.Join(config.TargetDir, "p1", "index.html"))
assertEqual(t, err, nil) assertEqual(t, err, nil)
_, err = os.Stat(filepath.Join(config.TargetDir, "p2.html")) _, err = os.Stat(filepath.Join(config.TargetDir, "p2", "index.html"))
assertEqual(t, err, nil) assertEqual(t, err, nil)
// test index includes p1 and p2 // test index includes p1 and p2
@ -629,9 +629,9 @@ layout: base
assertEqual(t, err, nil) assertEqual(t, err, nil)
// test target files generated // test target files generated
_, err = os.Stat(filepath.Join(config.TargetDir, "p1.html")) _, err = os.Stat(filepath.Join(config.TargetDir, "p1", "index.html"))
assertEqual(t, err, nil) assertEqual(t, err, nil)
_, err = os.Stat(filepath.Join(config.TargetDir, "p2.html")) _, err = os.Stat(filepath.Join(config.TargetDir, "p2", "index.html"))
assertEqual(t, err, nil) assertEqual(t, err, nil)
// test index includes p1 and p2 // test index includes p1 and p2
@ -654,9 +654,9 @@ layout: base
assertEqual(t, err, nil) assertEqual(t, err, nil)
// test only non drafts generated // test only non drafts generated
_, err = os.Stat(filepath.Join(config.TargetDir, "p1.html")) _, err = os.Stat(filepath.Join(config.TargetDir, "p1", "index.html"))
assertEqual(t, err, nil) assertEqual(t, err, nil)
_, err = os.Stat(filepath.Join(config.TargetDir, "p2.html")) _, err = os.Stat(filepath.Join(config.TargetDir, "p2", "index.html"))
assert(t, os.IsNotExist(err)) assert(t, os.IsNotExist(err))
// test index includes p1 but NOT p2 // test index includes p1 but NOT p2