/
static.exs
146 lines (122 loc) · 4.25 KB
/
static.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
Mix.install([:phoenix_live_view, :earmark, :phoenix_html])
defmodule Components do
use Phoenix.Component
import Phoenix.HTML
defp current_url(slug), do: "https://kevinlang.me#{slug}"
defp title(nil), do: "Kevin Lang"
defp title(title), do: "#{title} | Kevin Lang"
def layout(assigns) do
~H"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><%= title(@title) %></title>
<meta name="author" content="Kevin Lang">
<meta name="description" content={@description} />
<meta property="og:site_name" content="Kevin Lang">
<meta property="og:locale" content="en_US">
<meta property="og:title" content={title(@title)}>
<meta property="og:url" content={current_url(@slug)}>
<meta property="og:image" content="https://kevinlang.me/apple-touch-icon.png">
<meta property="og:description" content={@description} />
<meta property="og:type" content="article" />
<meta property="article:published_time" content={@date} />
<meta name="twitter:title" content={title(@title)} />
<meta name="twitter:description" content={@description} />
<meta name="twitter:url" content={current_url(@slug)} />
<meta name="twitter:image" content="https://kevinlang.me/apple-touch-icon.png" />
<meta name="twitter:image:src" content="https://kevinlang.me/apple-touch-icon.png" />
<link rel="canonical" href={current_url(@slug)} />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="stylesheet" href="/my.css">
<link rel="stylesheet" href="/gfm.css">
<link rel="stylesheet" href="/highlight-atom-one-light.css">
<script src="/highlight.min.js"></script>
<script src="/highlight-elixir.js"></script>
<script>hljs.highlightAll();</script>
</head>
</html>
<body>
<header>
<div class="container">
<a class="site-title" href="/">Kevin Lang</a>
</div>
</header>
<main>
<%= @inner_content %>
</main>
</body>
"""
end
def index(assigns) do
~H"""
<h1 class="title">Blog posts</h1>
<div class="markdown-body">
<%= for post <- @posts do %>
<p>
<b><a href={post.slug}><%= post.title %></a></b> <small>- <%= Calendar.strftime(post.date, "%B %d, %Y") %></small>
</p>
<%end%>
</div>
"""
end
def post(assigns) do
~H"""
<article>
<div class="title">
<h1><%= @title %></h1>
<p class="date"><small><%= Calendar.strftime(@date, "%B %d, %Y") %></small></p>
</div>
<div class="markdown-body">
<%= raw @body %>
</div>
</article>
"""
end
end
defmodule Helpers do
def render(component, assigns) do
assigns
|> Map.put(:inner_content, component.(assigns))
|> Components.layout()
|> Phoenix.HTML.Safe.to_iodata()
end
end
### Parse markdown
posts =
Path.wildcard("posts/*.md")
|> Enum.sort(:desc)
|> Enum.map(fn path ->
[year, month, day, slug] =
path
|> Path.basename(".md")
|> String.split("-", parts: 4)
date = Date.from_iso8601!("#{year}-#{month}-#{day}")
contents = File.read!(path)
[attrs_code, markdown] = String.split(contents, "---", parts: 2)
body = Earmark.as_html!(markdown)
{attrs, _bindings} = Code.eval_string(attrs_code)
Map.merge(attrs, %{date: date, slug: "/#{slug}/", body: body})
end)
|> Enum.filter(&(!&1[:draft]))
### Generate HTML
File.cp_r("public", "_site")
html =
Helpers.render(&Components.index/1, %{
posts: posts,
title: nil,
description: "Kevin Lang's technical blog",
slug: "/",
date: Enum.map(posts, & &1.date) |> Enum.sort(:desc) |> List.first()
})
File.write!("_site/index.html", html)
Enum.each(posts, fn post ->
html = Helpers.render(&Components.post/1, post)
File.mkdir_p!("_site/#{post.slug}")
File.write!("_site/#{post.slug}/index.html", html)
end)