From b8f41fe69157cda73af3a9f028cbccb986751821 Mon Sep 17 00:00:00 2001 From: Raymonzut <40148684+Raymonzut@users.noreply.github.com> Date: Sat, 26 Sep 2020 19:10:58 +0200 Subject: [PATCH 1/2] Remove js for posts page --- client/public/lib/remote.mjs | 24 -------------- client/public/posts.html | 10 ------ client/public/posts.mjs | 61 ------------------------------------ 3 files changed, 95 deletions(-) delete mode 100644 client/public/lib/remote.mjs delete mode 100644 client/public/posts.mjs diff --git a/client/public/lib/remote.mjs b/client/public/lib/remote.mjs deleted file mode 100644 index a445c79..0000000 --- a/client/public/lib/remote.mjs +++ /dev/null @@ -1,24 +0,0 @@ -export async function getPosts(id) { - const BASE_URL = 'https://raymon.dev' - const BASE_ENDPOINT = '/api/posts' - const URL = BASE_URL + BASE_ENDPOINT + (id ? `/${id}` : '?sort=-1') - - let posts = [] - - return await fetch(URL) - .then(res => res.json()) - .then(res => { - if (id !== undefined) { - if (res === undefined) { - throw Error("Response body empty") - } - return [res] - } - else { - return res - } - }) - .catch(err => { - console.log(`Error: ${err}`) - }) -} diff --git a/client/public/posts.html b/client/public/posts.html index f44fd78..2944fd5 100644 --- a/client/public/posts.html +++ b/client/public/posts.html @@ -8,9 +8,6 @@ Raymon Zutekouw - - - -
diff --git a/client/public/posts.mjs b/client/public/posts.mjs deleted file mode 100644 index e6fb25d..0000000 --- a/client/public/posts.mjs +++ /dev/null @@ -1,61 +0,0 @@ -import { getPosts } from "./lib/remote.mjs" - -function toMonthYearString(str) { - const date = new Date(str) - return `${date.toLocaleString('default', { month: 'long' })} ${date.getFullYear()}` -} -async function showPost(id) { - const post_list = await getPosts(id) - const post = post_list[0] - const title = document.createElement("h1") - title.textContent = post.title - - const postsDOM = document.getElementById("posts") - postsDOM.appendChild(title) - - post.content.split('\n').forEach(paragraph => { - const p = document.createElement("p") - p.innerHTML = paragraph - postsDOM.appendChild(p) - }); -} - -async function updatePosts() { - const posts = await getPosts() - - const months = posts.map(post => toMonthYearString(post.date)) - const uniques = Array.from(new Set(posts.map(post => toMonthYearString(post.date)))) - const month_lists = uniques.map(month => - posts.filter( - post => toMonthYearString(post.date) === month - ) - ) - const postsDOM = document.getElementById("posts") - uniques.forEach((month, i) => { - const month_DOM = document.createElement("h1") - month_DOM.textContent = month - - month_lists[i].forEach((post, i) => { - const post_DOM = document.createElement("h2") - post_DOM.textContent = `${post.date.substring(0, 10)} - ` - - const post_link = document.createElement("a") - post_link.href = 'posts?post=' + post._id - post_link.textContent = post.title - post_DOM.appendChild(post_link) - - month_DOM.appendChild(post_DOM) - }) - - postsDOM.appendChild(month_DOM) - }) -} - -// Check if a specific post is requested -let url = new URL(document.location.href); -url.searchParams.sort(); -let post_id = url.searchParams.values().next().value; - -const reg = /([0-9]|[a-f]){24}/ -if (post_id && reg.test(post_id)) showPost(post_id) -else updatePosts() From 6c671132270e830a34b7c6b5222e9251ddb72558 Mon Sep 17 00:00:00 2001 From: Raymonzut <40148684+Raymonzut@users.noreply.github.com> Date: Thu, 1 Oct 2020 20:07:31 +0200 Subject: [PATCH 2/2] Rewrite post related pages to be generated --- client/nginx.conf | 4 ++ client/public/.gitignore | 2 + client/public/assets/styling/general.css | 6 ++- client/public/gen/.gitkeep | 0 client/public/gen_posts.ex | 49 +++++++++++++++++++ client/public/posts/.gitkeep | 0 .../{posts.html => templates/index.html} | 13 +++-- client/public/templates/post.html | 36 ++++++++++++++ 8 files changed, 102 insertions(+), 8 deletions(-) create mode 100644 client/public/.gitignore create mode 100644 client/public/gen/.gitkeep create mode 100644 client/public/gen_posts.ex create mode 100644 client/public/posts/.gitkeep rename client/public/{posts.html => templates/index.html} (75%) create mode 100644 client/public/templates/post.html diff --git a/client/nginx.conf b/client/nginx.conf index b626f1e..9bb0238 100644 --- a/client/nginx.conf +++ b/client/nginx.conf @@ -33,6 +33,10 @@ http { alias /app/lib; } + location /posts { + alias /app/gen/; + } + location /api { proxy_pass https://raymon.dev/api; proxy_buffering on; diff --git a/client/public/.gitignore b/client/public/.gitignore new file mode 100644 index 0000000..690991b --- /dev/null +++ b/client/public/.gitignore @@ -0,0 +1,2 @@ +posts/* +gen/* diff --git a/client/public/assets/styling/general.css b/client/public/assets/styling/general.css index c1376ef..6aef140 100644 --- a/client/public/assets/styling/general.css +++ b/client/public/assets/styling/general.css @@ -21,8 +21,12 @@ nav { font-style: italic; } +h1 { + font-size: 2.0em; +} + h2 { - font-size: 0.6em; + font-size: 1.25em; margin: auto 0; } diff --git a/client/public/gen/.gitkeep b/client/public/gen/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/client/public/gen_posts.ex b/client/public/gen_posts.ex new file mode 100644 index 0000000..03d3da6 --- /dev/null +++ b/client/public/gen_posts.ex @@ -0,0 +1,49 @@ +# Templates will be filled by posts +index_template = File.read!("./templates/index.html") +post_template = File.read!("./templates/post.html") + +post_contents = File.ls!("./posts") + |> Enum.reject(fn(x) -> String.starts_with?(x, ".") end) + |> Enum.map(fn f -> File.read!("./posts/" <> f) end) + |> Enum.map(fn c -> String.split(c, "\n") end) + |> Enum.map(fn c -> Enum.reject(c, fn(x) -> x == "" end) end) + |> Enum.map(fn c -> Enum.chunk_every(c, 2) end) + |> Enum.map(fn c -> + Enum.map(c, fn [k, v] -> %{String.to_atom(k) => v} end) + |> Enum.reduce(%{}, fn(x, acc) -> Map.merge(x, acc) end) + end) + +index_file = post_contents + |> Enum.sort_by(fn m -> Map.get(m, :date) end) + |> Enum.reverse() + # Group by month + |> Enum.group_by(fn m -> Map.get(m, :date) |> String.slice(0..6) end) + |> Enum.reverse() + |> Enum.map(fn {month, posts} -> "\n

" <> month <> "

\n" <> (posts |> + Enum.map(fn post -> "

" <> (Map.get(post, :date) |> String.slice(0..9)) <> + " - Map.get(post, :id) <> ".html\">" <> Map.get(post, :title) <> "

" + end) |> Enum.join("\n")) end) + |> (fn template -> Regex.replace(Regex.compile!("{{index}}"), index_template, fn _, __ -> template end) end).() + +File.open!("./gen/index.html", [:write]) + |> IO.binwrite(index_file) + |> File.close() + + +post_contents + |> Enum.each(fn post -> + File.open!("./gen/" <> Map.get(post, :id) <> ".html", [:write]) + |> IO.binwrite( + # Converting handlebars to values + Regex.compile!("{{(.*)}}") |> + Regex.replace(post_template, fn _, key -> if key != "content" do Map.get(post, String.to_atom(key)) else + Map.get(post, String.to_atom(key)) + # Converting \n to paragraphs + |> String.split("\\n") + |> Enum.reject(fn(x) -> x == "" end) + |> Enum.map(fn paragraph -> ("

" <> paragraph <> "

\n") end) + |> Enum.join("") + end + end)) + |> File.close() + end) diff --git a/client/public/posts/.gitkeep b/client/public/posts/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/client/public/posts.html b/client/public/templates/index.html similarity index 75% rename from client/public/posts.html rename to client/public/templates/index.html index 2944fd5..f8b29d8 100644 --- a/client/public/posts.html +++ b/client/public/templates/index.html @@ -14,19 +14,18 @@ media="all" > - -
+
- -

For those using a RSS reader, subscribe here: rss.xml

- -
-
+
+

Post listing

+
+ {{index}} + diff --git a/client/public/templates/post.html b/client/public/templates/post.html new file mode 100644 index 0000000..bb6dd51 --- /dev/null +++ b/client/public/templates/post.html @@ -0,0 +1,36 @@ + + + + + + + Raymon Zutekouw + + + + + +
+ + +
+
+

{{title}}

+

For those using a RSS reader, subscribe here: rss.xml

+
+ + {{content}} +
+
+ +