-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: basic blog functionality by using nimblepublisher
- index page - blog page - markdown based
- Loading branch information
Showing
12 changed files
with
116 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
defmodule HidekXyz.Article do | ||
@enforce_keys [:slug, :title, :body, :publish_date] | ||
defstruct [:slug, :title, :body, :publish_date] | ||
|
||
def build(filename, attrs, body) do | ||
slug = Path.basename(filename, ".md") | ||
|
||
publish_date = Date.from_iso8601!(attrs.publish_date) | ||
|
||
struct!(__MODULE__, [slug: slug, title: attrs.title, body: body, publish_date: publish_date]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
defmodule HidekXyz.Contents do | ||
alias HidekXyz.Article | ||
|
||
use NimblePublisher, | ||
build: Article, | ||
from: Application.app_dir(:hidek_xyz, "priv/content/**/*.md"), | ||
as: :articles, | ||
highlighters: [:markup_elixir, :markup_erlang] | ||
|
||
@articles Enum.sort_by(@articles, &(&1.publish_date), {:desc, Date}) | ||
|
||
defmodule NotFoundError do | ||
defexception [:message, plug_status: 404] | ||
end | ||
|
||
def all_posts, do: @articles | ||
|
||
def get_post_by_slug!(slug) when is_binary(slug) do | ||
Enum.find(all_posts(), &(&1.slug == slug)) || raise NotFoundError, "post with slug=#{slug} not found" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
defmodule HidekXyzWeb.Content.IndexLive do | ||
use HidekXyzWeb, :live_view | ||
|
||
alias HidekXyz.Contents | ||
|
||
@impl true | ||
def mount(_params, _session, socket) do | ||
articles = Contents.all_posts() | ||
|
||
{:ok, assign(socket, articles: articles)} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<main class="container mx-auto py-8"> | ||
<p class="font-bold text-3xl mb-4">Content</p> | ||
<ul> | ||
<%= for article <- @articles do %> | ||
<li class="border mb-4"> | ||
<.link class="block p-6" navigate={~p"/content/#{article.slug}"}> | ||
<p class="text-xl font-bold"><%= article.title %></p> | ||
<h3><%= article.title %></h3> | ||
<p><%= article.publish_date %></p> | ||
</.link> | ||
</li> | ||
<% end %> | ||
</ul> | ||
</main> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
defmodule HidekXyzWeb.Content.ShowLive do | ||
use HidekXyzWeb, :live_view | ||
|
||
alias HidekXyz.Contents | ||
|
||
@impl true | ||
def mount(%{"slug" => slug}, _session, socket) do | ||
article = Contents.get_post_by_slug!(slug) | ||
|
||
{:ok, assign(socket, article: article)} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<main> | ||
<h1 class="font-bold text-3xl mb-2"><%= @article.title %></h1> | ||
<p class="mb-4"><%= @article.publish_date %></p> | ||
|
||
<div><%= raw(@article.body) %></div> | ||
</main> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
%{ | ||
title: "test article", | ||
publish_date: "2023-11-15" | ||
} | ||
--- | ||
|
||
this post is a test... | ||
|
||
<br> | ||
|
||
<script> | ||
function test() { | ||
alert("hello world"); | ||
} | ||
</script> | ||
|
||
<button class="text-white font-bold bg-purple-700 rounded px-5 py-3" onclick="test()">Alert!</button> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
%{ | ||
title: "second article", | ||
publish_date: "2023-11-12" | ||
} | ||
--- | ||
|
||
this post is a test too... |