Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: prepend website url path to computed paths + add test workflow #9

Merged
merged 9 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .babilema.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
website_url = "https://babilema.github.io"
website_url = "https://bytebakersco.github.io/babilema"
blog_title = "Babilema: A Minimalist Static Blog Generator"
output_dir = "." # This could be ommited or just be an empty string
2 changes: 1 addition & 1 deletion .github/workflows/generate-blog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
workflow_dispatch:

jobs:
generate-blog:
generate_blog:
runs-on: ubuntu-latest

steps:
Expand Down
19 changes: 19 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Run tests

on: pull_request

jobs:
run_tests:
runs-on: ubuntu-latest

steps:
- name: 'Check out code'
uses: actions/checkout@v4

- name: 'Set up Go'
uses: actions/setup-go@v5
with:
go-version: '>=1.22'

- name: 'Run go test'
run: 'go test ./...'
2 changes: 1 addition & 1 deletion internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func TestLoadConfig(t *testing.T) {
root, _ := utils.RootDir()
expected := Config{
WebsiteURL: "https://babilema.github.io",
WebsiteURL: "https://bytebakersco.github.io/babilema",
BlogTitle: "Babilema: A Minimalist Static Blog Generator",
BlogPostIssuePrefix: "[BLOG]",
TemplatePostFilePath: filepath.Join(root, "templates", "post.html"),
Expand Down
30 changes: 25 additions & 5 deletions internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"html/template"
"io"
"log"
"net/url"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -99,7 +100,7 @@ func extractPlainText(content template.HTML) string {
return result
}

func extractCSSLinks(cssDir string) ([]string, error) {
func extractCSSLinks(cssDir string, cfg config.Config) ([]string, error) {
if cssDir == "" {
return nil, nil
}
Expand All @@ -118,6 +119,16 @@ func extractCSSLinks(cssDir string) ([]string, error) {
return err
}

websiteURL, err := url.Parse(cfg.WebsiteURL)
if err != nil {
return err
}

relativeFilePath = filepath.Join(
websiteURL.Path,
relativeFilePath,
)

cssLinks = append(cssLinks, relativeFilePath)
}

Expand All @@ -140,6 +151,15 @@ func generateBlogIndexPage(
return nil
}

websiteURL, err := url.Parse(cfg.WebsiteURL)
if err != nil {
return err
}

for i := range articles {
articles[i].URL = filepath.Join(websiteURL.Path, articles[i].URL)
}

data := struct {
Header template.HTML
Footer template.HTML
Expand Down Expand Up @@ -203,7 +223,7 @@ func GenerateBlogPosts(
}

data := templateData{}
data.CSSLinks, err = extractCSSLinks(cfg.CSSDir)
data.CSSLinks, err = extractCSSLinks(cfg.CSSDir, cfg)
if err != nil {
return err
}
Expand Down Expand Up @@ -235,8 +255,8 @@ func GenerateBlogPosts(

writer = outputFile

var url string
url, err = utils.RelativeFilePath(
var articleURL string
articleURL, err = utils.RelativeFilePath(
filepath.Join(cfg.OutputDir, filename),
)
if err != nil {
Expand All @@ -249,7 +269,7 @@ func GenerateBlogPosts(
Author: data.Metadata.Author,
Preview: template.HTML(extractPlainText(data.Content)),
DatePublished: data.Metadata.DatePublished,
URL: url,
URL: articleURL,
})
}

Expand Down
21 changes: 11 additions & 10 deletions internal/generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestGenerateBlogPosts(t *testing.T) {
CSSDir: filepath.Join(basePath, "test-data"),
TempDir: filepath.Join(basePath, "test-data", "tmp"),
BlogPostIssuePrefix: "[BLOG]",
WebsiteURL: "http://localhost:8080",
WebsiteURL: "http://localhost:8080/foo",
},
&buf,
)
Expand All @@ -76,9 +76,9 @@ func TestGenerateBlogPosts(t *testing.T) {
<title>Test Title - Website name</title>


<link rel="stylesheet" type="text/css" href="/internal/generator/test-data/css/bar.css">
<link rel="stylesheet" type="text/css" href="/foo/internal/generator/test-data/css/bar.css">

<link rel="stylesheet" type="text/css" href="/internal/generator/test-data/foo.css">
<link rel="stylesheet" type="text/css" href="/foo/internal/generator/test-data/foo.css">


</head>
Expand Down Expand Up @@ -119,14 +119,14 @@ func TestGenerateBlogIndexPage(t *testing.T) {
Preview: "Test preview",
Title: "Test Title 1",
DatePublished: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
URL: "example.com",
URL: "bar/baz.html",
},
{
Author: "Test Author",
Preview: "Test preview without an image",
Title: "Test Title 2",
DatePublished: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
URL: "example.com",
URL: "bar/qux.html",
},
}

Expand All @@ -149,7 +149,8 @@ func TestGenerateBlogIndexPage(t *testing.T) {
"test-data",
"footer.html",
),
OutputDir: filepath.Join(".", "test-data"),
OutputDir: filepath.Join(".", "test-data"),
WebsiteURL: "https://localhost:8080/foo",
},
&buf,
)
Expand All @@ -166,23 +167,23 @@ func TestGenerateBlogIndexPage(t *testing.T) {
</header>

<article>
<a href="example.com">
<a href="/foo/bar/baz.html">
<h1>Test Title 1</h1>
</a>
<p>Test preview</p>
<p>Author: Test Author</p>
<p>Published: 1970-01-01 00:00:00 &#43;0000 UTC</p>
<a href="example.com"><img src="test-data/image.jpg" alt="Test Title 1" /></a>
<a href="/foo/bar/baz.html"><img src="test-data/image.jpg" alt="Test Title 1" /></a>
</article>

<article>
<a href="example.com">
<a href="/foo/bar/qux.html">
<h1>Test Title 2</h1>
</a>
<p>Test preview without an image</p>
<p>Author: Test Author</p>
<p>Published: 1970-01-01 00:00:00 &#43;0000 UTC</p>
<a href="example.com"><img src="" alt="Test Title 2" /></a>
<a href="/foo/bar/qux.html"><img src="" alt="Test Title 2" /></a>
</article>

<footer><div>Test Footer</div>
Expand Down
2 changes: 1 addition & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<!-- TODO: add image -->
<!-- <meta property="twitter:image" content=""> -->

<link rel="stylesheet" type="text/css" href="/templates/css/style.css">
<link rel="stylesheet" type="text/css" href="/babilema/templates/css/style.css">
</head>

<body>
Expand Down