Skip to content

Commit

Permalink
feat!: Auto-gen thoughts page proper from articles
Browse files Browse the repository at this point in the history
  • Loading branch information
ripixel committed Jun 6, 2020
1 parent e6f3868 commit ccfb5ea
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
21 changes: 4 additions & 17 deletions pages/thoughts.html
Expand Up @@ -10,24 +10,11 @@
<p>A peak inside my brain you ask? Reader, beware...</p>
<section class="area">
<div class="header-details">
<h3>The Importance of Monitoring and Alerting</h3>
<p class="tag">9th September, 2019</p>
<h3>{title0}</h3>
<p class="tag">{date0}</p>
</div>
<p>
We all know that monitoring and alerting are important, right? But
just how important do we really think they are? How much do we show
that in what we do?
</p>
<p>
This is a story about an experience I had at a previous company, where
I had the pleasure of being at least partially responsible for the
system going down, and what we learned.
</p>
<a
class="button"
href="/thoughts/2019-09-09_The-Importance-of-Monitoring-and-Alerting"
>Read the rest</a
>
{body0}
<a class="button" href="/thoughts/{link0}">Read the rest</a>
</section>
</main>

Expand Down
31 changes: 27 additions & 4 deletions scripts/generateThoughts.ts
Expand Up @@ -30,17 +30,22 @@ const articles = findInDir("./thoughts/articles", ".md");

const mdConverter = new showdown.Converter();

const articlesGenerated = [];
const articlesGenerated: Array<{
title: string;
link: string;
body: string;
date: string;
}> = [];

articles.forEach((article) => {
const articleWithoutFolder = article
.replace("thoughts/articles/", "")
.replace(".md", ".html");
let [datestring, title] = articleWithoutFolder
let [datestring, titleWithDash] = articleWithoutFolder
.replace(".html", "")
.split("_");
const date = dateFormat(new Date(datestring), "do LLL, u");
title = title.replace(/-/g, " ");
const title = titleWithDash.replace(/-/g, " ");
const body = mdConverter.makeHtml(fs.readFileSync(article, "utf8"));

const articleContents = thoughtsPageTemplateContents
Expand All @@ -55,13 +60,31 @@ articles.forEach((article) => {
"utf8"
);

const splitBody = body.split("</p>");

articlesGenerated.push({
link: `${datestring}_${titleWithDash}`,
title,
date,
body,
body: `${splitBody[0]}</p>${splitBody[1]}</p>`,
});
});

console.log(`Generated ${articlesGenerated.length} articles`);

console.log("Updating thoughts page proper");
let thoughtsPageContents = fs.readFileSync("./public/thoughts.html", "utf8");

for (let i = 0; i < Math.min(2, articlesGenerated.length); i++) {
thoughtsPageContents = thoughtsPageContents
.replace(`{title${i}}`, articlesGenerated[i].title)
.replace(`{date${i}}`, articlesGenerated[i].date)
.replace(`{body${i}}`, articlesGenerated[i].body)
.replace(`{link${i}}`, articlesGenerated[i].link);
}

console.log("Updated thoughts page");

fs.writeFileSync("./public/thoughts.html", thoughtsPageContents, "utf8");

console.log("/// Finished generation of thoughts");

0 comments on commit ccfb5ea

Please sign in to comment.