Skip to content

Commit

Permalink
other seo improvement and analytics: images
Browse files Browse the repository at this point in the history
  • Loading branch information
AngeloChecked committed Oct 5, 2024
1 parent 371efa4 commit 0ecbf23
Show file tree
Hide file tree
Showing 225 changed files with 202 additions and 83 deletions.
7 changes: 3 additions & 4 deletions blog/components/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const Base = (props: BaseProps) =>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta property="og:type" content="website">
${
!props.page?.relativeWebsitePath ? "" : `
Expand All @@ -41,7 +42,6 @@ export const Base = (props: BaseProps) =>
`
}
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link rel="icon" type="image/x-icon" href="favicon.ico">
Expand Down Expand Up @@ -80,9 +80,8 @@ function googleTagManagerScript() {
<script async src="https://www.googletagmanager.com/gtag/js?id=G-LE27PDG685"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-LE27PDG685');
dataLayer.push(['js', new Date()]);
dataLayer.push(['config', 'G-LE27PDG685']);
</script>
`;
}
13 changes: 7 additions & 6 deletions blog/components/Footer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ export function Footer(props: { currentPageMenu?: string; menus: MenuInfo[] }) {
const footherClass = cssClass({
className: "foother",
properties: {
justifyContent: "space-around",
justifyContent: "space-between",
margin: "50px 0 50px 0",
padding: "0 30px 0 30px",
flexWrap: "wrap",
gap: "20px"
gap: "20px",
},
from: rowClass,
});
Expand All @@ -28,7 +29,7 @@ export function Footer(props: { currentPageMenu?: string; menus: MenuInfo[] }) {
return html`
<footer style="border-top: solid white 1px;">
<div style="${footherClass.inlineStyle}">
<div style="max-width: 30ch;">
<div style="max-width: 30ch; text-align: center;">
© 2024 Angelo Ceccato. Self-built blog using
<a href="https://deno.com/">Deno</a>, heavily inspired by
<a href="https://lume.land/">Lume</a>,
Expand All @@ -44,8 +45,8 @@ export function Footer(props: { currentPageMenu?: string; menus: MenuInfo[] }) {
can <a href="https://github.com/AngeloChecked/AngeloChecked-blog">find the source code here</a>.
</div>
<div>
Menu:
<div style="text-align: left">
<p>Menu:</p>
<ul>
${props.menus
.map((menu) => {
Expand All @@ -63,7 +64,7 @@ export function Footer(props: { currentPageMenu?: string; menus: MenuInfo[] }) {
</ul>
</div>
<div>
Social:
<p>Social:</p>
${social
.map((s) => {
return html`
Expand Down
2 changes: 1 addition & 1 deletion blog/components/Graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SimpleBlogNetworkGraph } from "../scripts/SimpleBlogNetworkGraph.js";
import type { Node, Edge } from "../scripts/graph.js";
import { GraphNode, graphNodes } from "../graph/knowledgeGraph.ts";
import { GraphNodeAuthorTable } from "./GraphNodeAuthorTable.ts";
import { myPersonalTechLimboElixirVsRustPost } from "../post/my-personal-tech-limbo-elixir-vs-rust.data.ts";
import { myPersonalTechLimboElixirVsRustPost } from "../post/my_personal_tech_limbo_elixir_vs_rust.data.ts";
import { philosophicalRamblingsAboutEcologyProgrammingLanguagesAndOOPNotJava } from "../post/philosophical_ramblings_about_ecology_programming_languages_and_OOP_not_java.data.ts";
import { GraphNodeFocusTable } from "./GraphNodeFocusTable.ts";

Expand Down
5 changes: 3 additions & 2 deletions blog/createPageHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { Menu } from "./components/Menu.ts";
import { RoutedPage } from "./routes.ts";
import { styleCssFile } from "./style/mainCss.ts";
import { websocketScript } from "./server.ts";
import { allMenus } from "./main.ts";
import { allMenus, domain } from "./main.ts";
import { fromStringToDomToString } from "./utils/utils.ts";

export function createPageHtml(
page?: RoutedPage,
Expand All @@ -28,6 +29,6 @@ export function createPageHtml(
page: page!,
site: { domain: domain },
});
const html = fromStringToDomToString(body);
const html = fromStringToDomToString(page!, body);
return html;
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ export const myPersonalTechLimboElixirVsRustPost: GraphNode = {
"Hype Cycle",
],
date: "2022-06-13T16:00:00.000Z",
thumbnail: { src: "/img/elixir_vs_rust_resized.webp" },
thumbnail: { src: "/img/my_personal_tech_limbo_elixir_vs_rust/elixir_vs_rust.webp" },
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

_who will be the winner?_

![rust vs elixir](/img/elixir_vs_rust.webp)
![rust vs elixir](/img/my_personal_tech_limbo_elixir_vs_rust/elixir_vs_rust.webp)

### Why

Expand Down Expand Up @@ -80,7 +80,7 @@ development.
To achieve this goal the compiler is considered an extraordinary teacher but
strict like Mormons.

![rust developer compiler cycle](/img/rust_developer_compiler_cycle.webp)
![rust developer compiler cycle](/img/my_personal_tech_limbo_elixir_vs_rust/rust_developer_compiler_cycle.webp)

The influence of ML-derived programming languages makes Rust extremely suitable
for correctness. Rust programs do not compile if all compiler formal rules are
Expand Down Expand Up @@ -114,7 +114,7 @@ developer to be aware of how memory is handled in the program.

[Many choices and a lot of different ways to do the same thing.](https://stackoverflow.com/questions/64769333/rust-used-to-have-a-ton-of-pointer-types-what-were-they-and-what-happened-to-e)

![rust choices](/img/rust_choices.webp)
![rust choices](/img/my_personal_tech_limbo_elixir_vs_rust/rust_choices.webp)

I am not a system programmer... my languages are Kotlin and TypeScript. Mainly I
develop web applications and microservices. In my day-by-day, it is very
Expand All @@ -125,7 +125,7 @@ thinking? Right?**

_But also I'm looking for something practical_

![rust risk learning curve](/img/learning_curve.webp)
![rust risk learning curve](/img/my_personal_tech_limbo_elixir_vs_rust/learning_curve.webp)

If I start learning Rust, I fear the
[learning curve](https://endler.dev/2017/go-vs-rust/) will take me into another
Expand Down Expand Up @@ -246,7 +246,7 @@ but powerful structures for developing distributed systems.

**is It what I want?**

![The 9th Circle of Erl](https://learnyousomeerlang.com/static/img/9-circles-of-erl.png)
![The 9th Circle of Erl](https://learnyousomeerlang.com/static/img/my_personal_tech_limbo_elixir_vs_rust/9-circles-of-erl.png)

[Reading about the Erlang community hype phase](https://ferd.ca/ten-years-of-erlang.html?utm_source=pocket_mylist)
is common for people to come and go. The goal for me should be to learn
Expand Down Expand Up @@ -315,7 +315,7 @@ the learning curve of the basics is not so high.

### Conclusions

![tech hype cycle limbo](/img/tech_hype_cycle_limbo.webp)
![tech hype cycle limbo](/img/my_personal_tech_limbo_elixir_vs_rust/tech_hype_cycle_limbo.webp)

My purpose here is not to choose the right tool for the right problem. But pick
a topic worth exploring and studying.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const philosophicalRamblingsAboutEcologyProgrammingLanguagesAndOOPNotJava
],
date: "2024-04-13T13:00:00.000Z",
thumbnail: {
src: "/img/philosophical_ramblings_about_ecology_programming_languages_and_OOP_not_java/toomanyspiderman.small.webp",
src: "/img/philosophical_ramblings_about_ecology_programming_languages_and_OOP_not_java/toomanyspiderman.480x240.small.webp",
},
},
};
126 changes: 126 additions & 0 deletions blog/preprocess/apply_picture_resolutions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { HTMLDocument } from "../deps/dom.ts";
import { RoutedPage } from "../routes.ts";

type Options = {
mediaConfig: {
tiny?: string;
small?: string;
medium?: string;
large?: string;
};
};

const defaults: Options = {
mediaConfig: {
tiny: undefined,
small: "(min-width: 300px)",
medium: "(min-width: 480px)",
large: "(min-width: 600px)",
},
};

export default function pictureRelosution(
page: RoutedPage,
document: HTMLDocument,
imageInFolders: string[],
maxWidth: number,
) {
const options = defaults;

if (!page?.data) return;

if (!page?.data?.thumbnail) {
page.data.thumbnail = { src: "" };
}

const images = document?.querySelectorAll("img");
if (!images) return;

for (const img of images) {
const src = img.getAttribute("src");
const alt = img.getAttribute("alt");

src && img.setAttribute("src", src);
alt && img.setAttribute("alt", alt);

if (!src) return;
const sizes = ["large", "medium", "small", "tiny"];
const imagesFound: [string, string][] = [];
const pathFileNameWithoutExtension = src.replace(/(\..*)$/, ``);

for (const size of sizes) {
const regex = new RegExp(
`${
pathFileNameWithoutExtension.split("/").slice(-1)
}\.[0-9]{1,4}x[0-9]{1,4}\.${size}.webp`,
);
const sizeImageFound = imageInFolders.find((img) => regex.test(img));
sizeImageFound && imagesFound.push([size, "/img" + sizeImageFound]);
}

const mediaConfig: Record<string, string | undefined> = options.mediaConfig;

const picture = document.createElement("picture");
alt && picture.setAttribute("alt", alt);

if (imagesFound.length > 0 && document) {
for (const [size, path] of imagesFound) {
const source = document.createElement("source");
source.setAttribute("srcset", path);

const regex = new RegExp("\.([0-9]{1,4}x[0-9]{1,4})\.");
const resolutionFromName = regex.exec(path)?.[1];
const [w, h] = resolutionFromName?.split("x") ?? [];
if (w && h && !img.getAttribute("width")) {
let ww = parseInt(w);
let hh = parseInt(h);
if (ww >= maxWidth) {
hh = Math.trunc((hh * maxWidth) / ww);
ww = maxWidth;
}
source.setAttribute("width", `${ww}px`);
source.setAttribute("height", `${hh}px`);
source.setAttribute(
"style",
`width:100%; height: auto; max-width:${ww}px; max-height: ${hh}px;`,
);
}

const media = mediaConfig[size];
media && source.setAttribute("media", media);

picture.appendChild(source);
}
}

img.replaceWith(picture);
const regex = new RegExp(
`${
pathFileNameWithoutExtension.split("/").slice(-1)
}\.[0-9]{1,4}x[0-9]{1,4}\.webp$`,
);
const sizeImageFound = imageInFolders.find((img) => regex.test(img));
if (sizeImageFound) {
const regex = new RegExp("\.([0-9]{1,4}x[0-9]{1,4})\.");
const resolutionFromName = regex.exec(sizeImageFound)?.[1];
const [w, h] = resolutionFromName?.split("x") ?? [];
if (w && h && !img.getAttribute("width")) {
let ww = parseInt(w);
let hh = parseInt(h);
if (ww >= maxWidth) {
hh = Math.trunc((hh * maxWidth) / ww);
ww = maxWidth;
}
img.setAttribute("width", `${ww}px`);
img.setAttribute("height", `${hh}px`);
img.setAttribute(
"style",
`width:100%; height: auto; max-width:${ww}px; max-height: ${hh}px;`,
);
}
img.setAttribute("src", "/img" + sizeImageFound);
}
picture.appendChild(img);
}
return document;
}
29 changes: 1 addition & 28 deletions blog/preprocess/md_picture_resolution_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,34 +52,7 @@ export default function pictureRelosution(
imageInFolders.some((img) => sizedFilePath.endsWith(img)) &&
imagesFound.push([size, sizedFilePath]);
}

const mediaConfig: Record<string, string | undefined> = options.mediaConfig;

if (imagesFound.length > 0 && document) {
const picture = document.createElement("picture");
alt && picture.setAttribute("alt", alt);

for (const [size, path] of imagesFound) {
const source = document.createElement("source");
source.setAttribute("data-srcset", path);

const media = mediaConfig[size];
media && source.setAttribute("media", media);

picture.appendChild(source);
}
img.replaceWith(picture);
imageInFolders.some((img) => src.endsWith(img)) &&
picture.appendChild(img);
}
}
const script = document.createElement("script");
script.innerText = `
window.onload = () => {
document.querySelectorAll('source').forEach(el => el.setAttribute('srcset', el.getAttribute('data-srcset')));
document.querySelectorAll('img').forEach(el => el.setAttribute('src', el.getAttribute('data-src')));
}`;
const body = document.querySelector("body");
body && body.appendChild(script);

return document;
}
3 changes: 2 additions & 1 deletion blog/resize-img/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target
cargo.lock
.webp
*.(large|medium|small|tiny).webp

4 changes: 4 additions & 0 deletions blog/resize-img/delete-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cd ../static/img/philosophical_ramblings_about_ecology_programming_languages_and_OOP_not_java/
rm -f test2.webp
ls | grep -E "(large|medium|small|tiny)" | xargs rm
ls | grep -E "[0-9]{1,4}x[0-9]{1,4}.webp" | xargs rm
4 changes: 3 additions & 1 deletion blog/resize-img/src/img_to_webp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use image::{DynamicImage, EncodableLayout, ImageReader};
use image::{DynamicImage, EncodableLayout, GenericImageView, ImageReader};
use std::fs::File;
use std::io::Write;
use std::path::Path;
Expand All @@ -7,6 +7,8 @@ use webp::{Encoder, WebPMemory};
pub fn image_to_webp(file_path_png: &Path, file_path_webp: &Path) -> Option<()> {
let image = ImageReader::open(file_path_png).unwrap();
let image: DynamicImage = image.with_guessed_format().unwrap().decode().unwrap();
let (w, h) = image.dimensions();
dbg!(w, h);
let encoder: Encoder = Encoder::from_image(&image).unwrap();
let encoded_webp: WebPMemory = encoder.encode(65f32);
let mut webp_image = File::create(&file_path_webp).unwrap();
Expand Down
Loading

0 comments on commit 0ecbf23

Please sign in to comment.