From 27d1607bba3476416e49ba43463b2b385c2a7268 Mon Sep 17 00:00:00 2001 From: Lewis Dale Date: Sat, 30 Dec 2023 21:55:36 +0000 Subject: [PATCH] Better styling for the aside --- downloadImages.js | 34 ++++++++++++++++++++++++++++++++++ downloadPosts.js | 36 ++++++++++++++++++++++++++++++++++++ src/blog/index.html | 2 +- src/css/globals.css | 32 ++++++++++++++++++++++++++++---- 4 files changed, 99 insertions(+), 5 deletions(-) create mode 100644 downloadImages.js create mode 100644 downloadPosts.js diff --git a/downloadImages.js b/downloadImages.js new file mode 100644 index 0000000..1cb9a14 --- /dev/null +++ b/downloadImages.js @@ -0,0 +1,34 @@ +const { glob } = require('glob'); +const fs = require('fs'); +const path = require('path'); + +(async () => { + const imagesDirectory = path.join(__dirname, 'src', 'images'); + + if (!fs.existsSync(imagesDirectory)) { + fs.mkdirSync(imagesDirectory, { recursive: true }); + } + + const markdownFiles = await glob('src/blog/posts/**/*.md'); + const imageRegex = /!\[(.*?)\]\((.*?)\)/g; + const imageRegex2 = //g; + + for (const markdownFile of markdownFiles) { + const markdown = fs.readFileSync(markdownFile, 'utf-8'); + const images = [...markdown.matchAll(imageRegex), ...markdown.matchAll(imageRegex2)]; + for (const image of images) { + const imageUrl = image[2] || image[1]; + const imageFile = imageUrl.split('/').pop(); + const imagePath = path.join(imagesDirectory, imageFile); + if (!fs.existsSync(imagePath)) { + console.log(`Downloading ${imageUrl} to ${imagePath}`); + const imageResponse = await fetch(imageUrl); + const imageBuffer = Buffer.from(await imageResponse.arrayBuffer()); + fs.writeFileSync(imagePath, imageBuffer); + } + + const newMarkdown = markdown.replace(imageUrl, `./src/images/${imageFile}`); + fs.writeFileSync(markdownFile, newMarkdown); + } + } +})(); \ No newline at end of file diff --git a/downloadPosts.js b/downloadPosts.js new file mode 100644 index 0000000..a96befe --- /dev/null +++ b/downloadPosts.js @@ -0,0 +1,36 @@ +const fs = require('fs'); +const path = require('path'); +const posts = require('./src/_data/old_posts'); + +const htmlDecode = (input) => { + const doc = new DOMParser().parseFromString(input, "text/html"); + return doc.documentElement.textContent; + } + + +(async () => { + const postList = await posts(); + + const postsDirectory = path.join(__dirname, 'src', 'blog', 'posts'); + if (!fs.existsSync(postsDirectory)) { + fs.mkdirSync(postsDirectory, { recursive: true }); + } + + for (const post of postList) { + const date = new Date(post.date); + const dir = path.join(__dirname, 'src', 'blog', 'posts', date.getFullYear().toString(), (date.getMonth() + 1).toString()); + if (!fs.existsSync(dir)) { + fs.mkdirSync(dir, { recursive: true }); + } + const postPath = path.join(dir, `${post.slug}.md`); + const postContent = `--- +title: "${post.title.rendered}" +date: ${post.date} +slug: ${post.slug} +${post.tags.length ? `tags: [${post.tags.map(tag => tag.slug).join(', ')}]` : ''} +--- +${post.markdown.replaceAll('\\\'','\'')} +`; + fs.writeFileSync(postPath, postContent); + } +})(); \ No newline at end of file diff --git a/src/blog/index.html b/src/blog/index.html index 8e2a691..9885874 100644 --- a/src/blog/index.html +++ b/src/blog/index.html @@ -9,7 +9,7 @@ pagination:

Blog posts

-