-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
other seo improvement and analytics: images
- Loading branch information
1 parent
371efa4
commit 5370d58
Showing
155 changed files
with
185 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
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; | ||
|
||
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("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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
target | ||
cargo.lock | ||
.webp | ||
*.(large|medium|small|tiny).webp | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
File renamed without changes.
Binary file added
BIN
+420 KB
...ngs_about_ecology_programming_languages_and_OOP_not_java/bands_family_tree.1515x1114.webp
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file added
BIN
+808 KB
...about_ecology_programming_languages_and_OOP_not_java/chamber.1600x693.large.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
Binary file added
BIN
+808 KB
...lings_about_ecology_programming_languages_and_OOP_not_java/chamber.1600x693.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
BIN
+514 KB
...ical_ramblings_about_ecology_programming_languages_and_OOP_not_java/chamber.1600x693.webp
Binary file not shown.
Binary file added
BIN
+72.2 KB
...s_about_ecology_programming_languages_and_OOP_not_java/chamber.300x130.tiny.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
Binary file added
BIN
+152 KB
..._about_ecology_programming_languages_and_OOP_not_java/chamber.480x208.small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
Binary file added
BIN
+214 KB
...about_ecology_programming_languages_and_OOP_not_java/chamber.600x260.medium.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
Binary file added
BIN
+22.7 KB
..._languages_and_OOP_not_java/compexity_and_years_of_programming.300x117.tiny.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
Binary file added
BIN
+923 KB
...nguages_and_OOP_not_java/compexity_and_years_of_programming.3262x1272.large.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
Binary file added
BIN
+923 KB
...ing_languages_and_OOP_not_java/compexity_and_years_of_programming.3262x1272.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
BIN
+472 KB
..._programming_languages_and_OOP_not_java/compexity_and_years_of_programming.3262x1272.webp
Binary file not shown.
Binary file added
BIN
+44.9 KB
...languages_and_OOP_not_java/compexity_and_years_of_programming.480x188.small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
Binary file added
BIN
+62.9 KB
...anguages_and_OOP_not_java/compexity_and_years_of_programming.600x234.medium.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
Binary file added
BIN
+23.7 KB
...ecology_programming_languages_and_OOP_not_java/couplingcohesion.300x75.tiny.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
Binary file added
BIN
+43.8 KB
...ology_programming_languages_and_OOP_not_java/couplingcohesion.480x120.small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
Binary file added
BIN
+57.8 KB
...logy_programming_languages_and_OOP_not_java/couplingcohesion.600x150.medium.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
Binary file added
BIN
+70.6 KB
...ology_programming_languages_and_OOP_not_java/couplingcohesion.893x222.large.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
Binary file added
BIN
+70.6 KB
...out_ecology_programming_languages_and_OOP_not_java/couplingcohesion.893x222.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
BIN
+87.7 KB
...blings_about_ecology_programming_languages_and_OOP_not_java/couplingcohesion.893x222.webp
Binary file not shown.
Binary file added
BIN
+422 KB
...t_ecology_programming_languages_and_OOP_not_java/ecosystem_levels.1600x1128.jpg
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
BIN
+422 KB
...ogy_programming_languages_and_OOP_not_java/ecosystem_levels.1600x1128.large.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
Binary file added
BIN
+1.66 MB
...ings_about_ecology_programming_languages_and_OOP_not_java/ecosystem_levels.1600x1128.webp
Binary file not shown.
Binary file added
BIN
+28.6 KB
...cology_programming_languages_and_OOP_not_java/ecosystem_levels.300x212.tiny.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
Binary file added
BIN
+61.2 KB
...ology_programming_languages_and_OOP_not_java/ecosystem_levels.480x339.small.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
Binary file added
BIN
+87.4 KB
...logy_programming_languages_and_OOP_not_java/ecosystem_levels.600x423.medium.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
Binary file added
BIN
+61.2 KB
...es_and_OOP_not_java/functional_design_patterns_scott_wlashchin.300x213.tiny.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
Binary file added
BIN
+130 KB
...s_and_OOP_not_java/functional_design_patterns_scott_wlashchin.480x340.small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
Binary file added
BIN
+188 KB
..._and_OOP_not_java/functional_design_patterns_scott_wlashchin.600x425.medium.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
Binary file added
BIN
+258 KB
...s_and_OOP_not_java/functional_design_patterns_scott_wlashchin.736x521.large.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
Binary file added
BIN
+258 KB
...nguages_and_OOP_not_java/functional_design_patterns_scott_wlashchin.736x521.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
BIN
+149 KB
...amming_languages_and_OOP_not_java/functional_design_patterns_scott_wlashchin.736x521.webp
Binary file not shown.
Binary file added
BIN
+19.6 KB
...gramming_languages_and_OOP_not_java/johon_carmack_about_tooling.300x68.tiny.png
Oops, something went wrong.
File renamed without changes.
Binary file added
BIN
+38.9 KB
...amming_languages_and_OOP_not_java/johon_carmack_about_tooling.480x108.small.png
Oops, something went wrong.
File renamed without changes.
Binary file added
BIN
+54.4 KB
...mming_languages_and_OOP_not_java/johon_carmack_about_tooling.600x135.medium.png
Oops, something went wrong.
File renamed without changes.
Binary file added
BIN
+46 KB
...amming_languages_and_OOP_not_java/johon_carmack_about_tooling.712x160.large.png
Oops, something went wrong.
File renamed without changes.
Binary file added
BIN
+46 KB
..._programming_languages_and_OOP_not_java/johon_carmack_about_tooling.712x160.png
Oops, something went wrong.
Binary file added
BIN
+29.5 KB
...t_ecology_programming_languages_and_OOP_not_java/johon_carmack_about_tooling.712x160.webp
Binary file not shown.
Binary file added
BIN
+36.3 KB
...amming_languages_and_OOP_not_java/josevalim_about_polymorphism.300x125.tiny.png
Oops, something went wrong.
File renamed without changes.
Binary file added
BIN
+71.5 KB
...mming_languages_and_OOP_not_java/josevalim_about_polymorphism.480x200.small.png
Oops, something went wrong.
File renamed without changes.
Binary file added
BIN
+102 KB
...ming_languages_and_OOP_not_java/josevalim_about_polymorphism.600x250.medium.png
Oops, something went wrong.
File renamed without changes.
Binary file added
BIN
+115 KB
...mming_languages_and_OOP_not_java/josevalim_about_polymorphism.686x285.large.png
Oops, something went wrong.
File renamed without changes.
Binary file added
BIN
+115 KB
...programming_languages_and_OOP_not_java/josevalim_about_polymorphism.686x285.png
Oops, something went wrong.
Binary file added
BIN
+51.3 KB
..._ecology_programming_languages_and_OOP_not_java/josevalim_about_polymorphism.686x285.webp
Binary file not shown.
Binary file added
BIN
+667 KB
...ecology_programming_languages_and_OOP_not_java/langfamilyFP.1500x1865.large.png
Oops, something went wrong.
File renamed without changes.
Binary file added
BIN
+667 KB
...about_ecology_programming_languages_and_OOP_not_java/langfamilyFP.1500x1865.png
Oops, something went wrong.
Binary file added
BIN
+438 KB
...amblings_about_ecology_programming_languages_and_OOP_not_java/langfamilyFP.1500x1865.webp
Binary file not shown.
Binary file added
BIN
+128 KB
...ut_ecology_programming_languages_and_OOP_not_java/langfamilyFP.300x373.tiny.png
Oops, something went wrong.
File renamed without changes.
Binary file added
BIN
+239 KB
...t_ecology_programming_languages_and_OOP_not_java/langfamilyFP.480x597.small.png
Oops, something went wrong.
File renamed without changes.
Binary file added
BIN
+317 KB
..._ecology_programming_languages_and_OOP_not_java/langfamilyFP.600x746.medium.png
Oops, something went wrong.
File renamed without changes.
Binary file added
BIN
+100 KB
...t_ecology_programming_languages_and_OOP_not_java/langfamilyOOP.300x291.tiny.png
Oops, something went wrong.
File renamed without changes.
Binary file added
BIN
+188 KB
..._ecology_programming_languages_and_OOP_not_java/langfamilyOOP.480x465.small.png
Oops, something went wrong.
File renamed without changes.
Binary file added
BIN
+251 KB
...ecology_programming_languages_and_OOP_not_java/langfamilyOOP.600x582.medium.png
Oops, something went wrong.
File renamed without changes.
Binary file added
BIN
+284 KB
..._ecology_programming_languages_and_OOP_not_java/langfamilyOOP.858x831.large.png
Oops, something went wrong.
File renamed without changes.
Binary file added
BIN
+284 KB
..._about_ecology_programming_languages_and_OOP_not_java/langfamilyOOP.858x831.png
Oops, something went wrong.
File renamed without changes.
Binary file added
BIN
+54.4 KB
...cology_programming_languages_and_OOP_not_java/languages_logo.1028x100.large.png
Oops, something went wrong.
File renamed without changes.
Binary file added
BIN
+54.4 KB
...bout_ecology_programming_languages_and_OOP_not_java/languages_logo.1028x100.png
Oops, something went wrong.
Binary file added
BIN
+72.1 KB
...mblings_about_ecology_programming_languages_and_OOP_not_java/languages_logo.1028x100.webp
Binary file not shown.
Binary file added
BIN
+15.8 KB
...t_ecology_programming_languages_and_OOP_not_java/languages_logo.300x30.tiny.png
Oops, something went wrong.
File renamed without changes.
Binary file added
BIN
+29.3 KB
..._ecology_programming_languages_and_OOP_not_java/languages_logo.480x47.small.png
Oops, something went wrong.
File renamed without changes.
Binary file added
BIN
+38.5 KB
...ecology_programming_languages_and_OOP_not_java/languages_logo.600x59.medium.png
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file added
BIN
+195 KB
...ngs_about_ecology_programming_languages_and_OOP_not_java/natural_lang_family.960x540.webp
Binary file not shown.
Binary file added
BIN
+109 KB
...ology_programming_languages_and_OOP_not_java/objects_good_part.300x180.tiny.png
Oops, something went wrong.
File renamed without changes.
Binary file added
BIN
+266 KB
...logy_programming_languages_and_OOP_not_java/objects_good_part.480x288.small.png
Oops, something went wrong.
File renamed without changes.
Binary file added
BIN
+271 KB
...ut_ecology_programming_languages_and_OOP_not_java/objects_good_part.500x299.png
Oops, something went wrong.
Binary file added
BIN
+170 KB
...lings_about_ecology_programming_languages_and_OOP_not_java/objects_good_part.500x299.webp
Binary file not shown.
Binary file added
BIN
+100 KB
...ut_ecology_programming_languages_and_OOP_not_java/oopinfluence.300x291.tiny.png
Oops, something went wrong.
File renamed without changes.
Binary file added
BIN
+188 KB
...t_ecology_programming_languages_and_OOP_not_java/oopinfluence.480x465.small.png
Oops, something went wrong.
File renamed without changes.
Binary file added
BIN
+251 KB
..._ecology_programming_languages_and_OOP_not_java/oopinfluence.600x582.medium.png
Oops, something went wrong.
File renamed without changes.
Binary file added
BIN
+284 KB
...t_ecology_programming_languages_and_OOP_not_java/oopinfluence.858x831.large.png
Oops, something went wrong.
Binary file added
BIN
+231 KB
...ings_about_ecology_programming_languages_and_OOP_not_java/oopinfluence.858x831.large.webp
Binary file not shown.
Binary file added
BIN
+284 KB
...s_about_ecology_programming_languages_and_OOP_not_java/oopinfluence.858x831.png
Oops, something went wrong.
Binary file added
BIN
+231 KB
..._ramblings_about_ecology_programming_languages_and_OOP_not_java/oopinfluence.858x831.webp
Binary file not shown.
Binary file added
BIN
+948 KB
...logy_programming_languages_and_OOP_not_java/toomanyspiderman.1140x570.large.png
Oops, something went wrong.
File renamed without changes.
Binary file added
BIN
+948 KB
...ut_ecology_programming_languages_and_OOP_not_java/toomanyspiderman.1140x570.png
Oops, something went wrong.
File renamed without changes.
Binary file added
BIN
+108 KB
...cology_programming_languages_and_OOP_not_java/toomanyspiderman.300x150.tiny.png
Oops, something went wrong.
File renamed without changes.
Binary file added
BIN
+240 KB
...ology_programming_languages_and_OOP_not_java/toomanyspiderman.480x240.small.png
Oops, something went wrong.
File renamed without changes.
Binary file added
BIN
+349 KB
...logy_programming_languages_and_OOP_not_java/toomanyspiderman.600x300.medium.png
Oops, something went wrong.
File renamed without changes.
Binary file removed
BIN
-289 KB
...mblings_about_ecology_programming_languages_and_OOP_not_java/toomanyspiderman.medium.webp
Binary file not shown.
Binary file removed
BIN
-196 KB
...amblings_about_ecology_programming_languages_and_OOP_not_java/toomanyspiderman.small.webp
Binary file not shown.
Binary file removed
BIN
-85.3 KB
...ramblings_about_ecology_programming_languages_and_OOP_not_java/toomanyspiderman.tiny.webp
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters