11import { DateTime } from "luxon" ;
22import striptags from "striptags" ;
3+ import Image from "@11ty/eleventy-img" ;
4+ import path from 'node:path' ;
5+ import { Console } from "node:console" ;
36
47export default function ( eleventyConfig ) {
58 eleventyConfig . addFilter ( "readableDate" , ( dateObj , format , zone ) => {
@@ -70,6 +73,8 @@ export default function(eleventyConfig) {
7073 extractExcerpt ( post )
7174 ) ;
7275
76+ eleventyConfig . addFilter ( "contentImgUrlShortcode" , contentImgUrlShortcode ) ;
77+
7378} ;
7479
7580// Taken from here => https://keepinguptodate.com/pages/2019/06/creating-blog-with-eleventy/
@@ -85,3 +90,21 @@ function extractExcerpt(article) {
8590
8691 return striptags ( content . slice ( 0 , content . indexOf ( "\n" ) ) ) ;
8792}
93+
94+ async function contentImgUrlShortcode ( src ) {
95+ const projectPath = this . eleventy . env . root ;
96+ const contentPath = path . dirname ( this . page . inputPath ) . replace ( "./" , this . eleventy . env . root + "/" ) ;
97+ const imageSrc = ( contentPath + "/" + src ) . replace ( "//" , "/" ) ;
98+
99+ let metadata = await Image ( imageSrc , {
100+ widths : [ 600 ] ,
101+ formats : [ "png" ] ,
102+ urlPath : this . page . url ,
103+ outputDir : `./_site/${ this . page . url } ` ,
104+ } ) ;
105+
106+ const data = metadata . png [ 0 ] ;
107+ // data.url might be /blog/hello-world/xfO_genLg4-600.jpeg
108+ // note the filename is a content hash-width combination
109+ return data . url ;
110+ }
0 commit comments