Skip to content

Commit

Permalink
Social Media Images for Notes (Years)
Browse files Browse the repository at this point in the history
  • Loading branch information
kristofzerbe committed Jul 15, 2024
1 parent 4e2e44e commit f65a55f
Show file tree
Hide file tree
Showing 37 changed files with 90 additions and 44 deletions.
23 changes: 23 additions & 0 deletions lib/_run_social-media-image-generator-by-photo.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

/**
* This is only for executing the generator manually.
* Refer to /scripts/on-ready-generate-social-media-images.js to see how it runs automatically in Hexo.
*
* Execution:
* node "./lib/_run_social-media-image-generator-by-photo.cjs" "../static/photos/normal" "../templates/social-media-image.handlebars" "../static/images/social-media" "$19-05-Israel-0228.jpg" "#test-123" "A Test" "... to see if it works" "TEST"
*/

const SocialMediaImageGenerator = require("./social-media-image-generator.cjs").SocialMediaImageGenerator;

const photoFolder = process.argv[2].toString();
const templateFile = process.argv[3].toString();
const targetFolder = process.argv[4].toString();

const photoFile = process.argv[5].toString();
const fileSlug = process.argv[6].toString();
const title = process.argv[7].toString();
const subtitle = process.argv[8].toString();
const category = process.argv[9].toString();

const generator = new SocialMediaImageGenerator(photoFolder, templateFile, targetFolder);
generator.generateByPhoto(photoFile, fileSlug, title, subtitle, category);
19 changes: 19 additions & 0 deletions lib/_run_social-media-image-generator-for-posts.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

/**
* This is only for executing the generator manually.
* Refer to /scripts/on-ready-generate-social-media-images.js to see how it runs automatically in Hexo.
*
* Execution:
* node "./lib/_run_social-media-image-generator-for-posts.cjs" "../static/photos/normal" "../templates/social-media-image.handlebars" "../static/images/social-media" "../source/_posts"
*/

const SocialMediaImageGenerator = require("./social-media-image-generator.cjs").SocialMediaImageGenerator;

const photoFolder = process.argv[2].toString();
const templateFile = process.argv[3].toString();
const targetFolder = process.argv[4].toString();

const postFolder = process.argv[5].toString();

const generator = new SocialMediaImageGenerator(photoFolder, templateFile, targetFolder);
generator.generateForPosts(postFolder);
19 changes: 0 additions & 19 deletions lib/_run_social-media-image-generator.cjs

This file was deleted.

42 changes: 24 additions & 18 deletions lib/social-media-image-generator.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,19 @@ const sharp = require('sharp');
const _currentPath = __dirname;
const _tempFolder = "./~temp";

var _postFolder;
var _photoFolder;
var _templateFile;
var _template;
var _targetFolder;

class SocialMediaImageGenerator {

constructor(postFolder, photoFolder, templateFile, targetFolder) {
constructor(photoFolder, templateFile, targetFolder) {

_postFolder = path.join(_currentPath, postFolder);
_photoFolder = path.join(_currentPath, photoFolder);
_templateFile = path.join(_currentPath, templateFile);
_targetFolder = path.join(_currentPath, targetFolder);

if (!fs.existsSync(_postFolder)) {
throw "Post folder not found";
}
if (!fs.existsSync(_photoFolder)) {
throw "Photo folder not found";
}
Expand All @@ -51,19 +46,31 @@ class SocialMediaImageGenerator {
_template = handlebars.compile(source);
}

generateByPhoto(photoFile, fileSlug, title, subtitle, category) {
let self = this;

self.process(
fileSlug,
{
title: title,
subtitle: subtitle,
categories: [category],
photo: url.pathToFileURL(path.join(_photoFolder, photoFile))
})
.then(() => {
fs.rmSync(_tempFolder, { recursive: true });
});
}

/**
* Runs the generation of a social media image per post
* Runs the generation of missing social media image of posts
*/
generate() {
generateForPosts(postFolder) {
let self = this;

const postFiles = this.getPostFiles(_postFolder);

//TODO: Filter out existing SM files here to show message or break
//if (postFiles.length === 0) return;
//console.log("Running Social-Media-Image-Generator on", magenta(postFiles.length), "posts without social media file");
const postPath = path.join(_currentPath, postFolder);

//console.log("Running Social-Media-Image-Generator...");
const postFiles = this.getPostFiles(postPath);

var postsProcessed = 0;
postFiles.forEach((file) => {
Expand All @@ -81,7 +88,7 @@ class SocialMediaImageGenerator {
content.attributes.photograph?.file &&
!fs.existsSync(path.join(_targetFolder, fileName + ".png"))) {

self.processPost(
self.process(
fileName,
{
title: content.attributes.title,
Expand Down Expand Up @@ -119,18 +126,17 @@ class SocialMediaImageGenerator {
//console.log("DIR: " + file);
} else if (file.indexOf(".md")>=0) {
allFiles.push(path.join(dirPath, "/", file))
//console.log("FILE:" + path.join(_postFolder, file));
}
});
return allFiles;
}

/**
* Processes a post and creates a temporary HTML file, which will be converted to an image.
* Process Social Media Image by creating a temporary HTML file, which will be converted to an image.
* @param {String} fileName Name of the image file without extension.
* @param {Object} vars Template variables.
*/
async processPost(fileName, vars) {
async process(fileName, vars) {
//console.log(fileName + " >> " + vars.title + " - " + vars.subtitle + " : " + vars.photo);

console.log("Generating Social Media Image " + magenta(fileName));
Expand Down
5 changes: 2 additions & 3 deletions scripts/events/on-ready-generate-social-media-images.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ const SocialMediaImageGenerator = require("../../lib/social-media-image-generato

hexo.on("ready", function() {

const postFolder = "../source/_posts";
const photoFolder = "../static/photos/normal";
const templateFile = "../templates/social-media-image.handlebars";
const targetFolder = "../static/images/social-media";

const generator = new SocialMediaImageGenerator(postFolder, photoFolder, templateFile, targetFolder);
generator.generate();
const generator = new SocialMediaImageGenerator(photoFolder, templateFile, targetFolder);
generator.generateForPosts("../source/_posts");

});
1 change: 1 addition & 0 deletions source/_notes/2021/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ title: Notes
photograph:
file: $DSC_5031.jpg
name: Riga Locks
socialmedia: /static/images/social-media/$Notes-2021.png
---
1 change: 1 addition & 0 deletions source/_notes/2022/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ title: Notes
photograph:
file: $21-08-05-Kroatien-3011.jpg
name: Lilla & Zoli Lock
socialmedia: /static/images/social-media/$Notes-2022.png
---
1 change: 1 addition & 0 deletions source/_notes/2023/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ title: Notes
photograph:
file: $D50_3000.jpg
name: Rusty Locks
socialmedia: /static/images/social-media/$Notes-2023.png
---
1 change: 1 addition & 0 deletions source/_notes/2024/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ title: Notes
photograph:
file: $D50_1433.jpg
name: Rhine Locks V
socialmedia: /static/images/social-media/$Notes-2024.png
---
1 change: 1 addition & 0 deletions source/_notes/2025/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ title: Notes
photograph:
file: $21-08-05-Kroatien-3015.jpg
name: Combination Lock
socialmedia: /static/images/social-media/$Notes-2025.png
---
1 change: 1 addition & 0 deletions source/_notes/2026/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ title: Notes
photograph:
file: $D50_1436.jpg
name: Rhine Locks VI
socialmedia: /static/images/social-media/$Notes-2026.png
---
1 change: 1 addition & 0 deletions source/_notes/2027/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ title: Notes
photograph:
file: $D50_1420.jpg
name: Rhine Locks II
socialmedia: /static/images/social-media/$Notes-2027.png
---
1 change: 1 addition & 0 deletions source/_notes/2028/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ title: Notes
photograph:
file: $D50_1409.jpg
name: Rhine Locks I
socialmedia: /static/images/social-media/$Notes-2028.png
---
1 change: 1 addition & 0 deletions source/_notes/2029/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ title: Notes
photograph:
file: $23-05-Holland-0404.jpg
name: Rusty Heart
socialmedia: /static/images/social-media/$Notes-2029.png
---
Binary file added static/images/social-media/$Notes-2021.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/images/social-media/$Notes-2021.thumb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/images/social-media/$Notes-2022.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/images/social-media/$Notes-2022.thumb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/images/social-media/$Notes-2023.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/images/social-media/$Notes-2023.thumb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/images/social-media/$Notes-2024.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/images/social-media/$Notes-2025.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/images/social-media/$Notes-2025.thumb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/images/social-media/$Notes-2026.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/images/social-media/$Notes-2026.thumb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/images/social-media/$Notes-2027.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/images/social-media/$Notes-2027.thumb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/images/social-media/$Notes-2028.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/images/social-media/$Notes-2028.thumb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/images/social-media/$Notes-2029.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/images/social-media/$Notes-2029.thumb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified static/images/social-media/404-Page-in-Hexo-for-GitHub-Pages.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion templates/feed-posts-atom-excerpt.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
}
}
}
const regEx = /<(script|style)[^>]*>(?<content>[^<]*)<\/(script|style)>/gim;
const content = (item) => {
let fc = item.excerpt.replace(regEx, "");
Expand Down
1 change: 0 additions & 1 deletion templates/feed-posts-atom-full.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
}
}
}
const regEx = /<(script|style)[^>]*>(?<content>[^<]*)<\/(script|style)>/gim;
const content = (item) => {
let fc = item.content.replace(regEx, "");
Expand Down
15 changes: 13 additions & 2 deletions templates/feed-posts-json-excerpt.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
let upd = moment(item.updated).toISOString();
return (pub === upd) ? null : upd;
}
const imageSource = (item) => {
if (item.photograph) {
if (item.photograph.socialmedia) {
return config.url + item.photograph.socialmedia
.replace("/static", "")
.replace(".png", ".thumb.png");
} else {
return config.url + "/photos/mobile/" + item.photograph.file;
}
}
}
const slug = (item) => {
let slugArray = item.slug.split("/");
let slug = slugArray[slugArray.length - 1];
Expand All @@ -13,7 +24,7 @@
const content = (item) => {
let fc = item.excerpt.replace(regEx, "");
fc = fc + '<p><a href="' + item.permalink + '">Continue reading <strong>`' + item.title + '`</strong> on kiko.io ...</a></p>';
if (item.photograph) { fc = '<p><img src="' + config.url + '/images/social-media/' + slug(item) + '.png" /></p>' + fc; }
if (item.photograph) { fc = '<p><img src="' + imageSource(item) + '" /></p>' + fc; }
return fc;
}
_%>
Expand All @@ -34,7 +45,7 @@ _%>
id: item.permalink,
url: item.permalink,
title: item.title,
image: config.url + '/images/social-media/' + slug(item) + '.thumb.png',
image: imageSource(item),
date_published: moment(item.date).toISOString(),
date_modified: updated(item),
content_html: content(item) || '',
Expand Down

0 comments on commit f65a55f

Please sign in to comment.