-
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.
Merge pull request #4 from ChrisOh431/custom-html-markup
Adding support for custom HTML markup
- Loading branch information
Showing
6 changed files
with
122 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { remarkEleventyImage } from "./src/astro-remark-images"; | ||
export { remarkEleventyImage } from "./src/astro-remark-images"; | ||
export type { createHTMLProps, MarkupValues } from "./src/types"; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,71 @@ | ||
import { MarkupValues } from "./types"; | ||
import path from "path"; | ||
import Image from "@11ty/eleventy-img"; | ||
|
||
export function defaultMarkup({ src, sources, width, height, alt }: MarkupValues) | ||
{ | ||
return ` | ||
<picture> | ||
${sources} | ||
<img | ||
src="${src}" | ||
width="${width}" | ||
height="${height}" | ||
alt="${alt}" | ||
loading="lazy" | ||
decoding="async"> | ||
</picture>`; | ||
} | ||
|
||
interface CreateHTMLProps | ||
{ | ||
imageDir: string, | ||
metadata: Image.Metadata, | ||
alt: string, | ||
sizes: string, | ||
isRemote: boolean, | ||
mdFilePath: string, | ||
customMarkup: ((attributes: MarkupValues) => string) | null, | ||
} | ||
export function createHTML({ imageDir, metadata, alt, sizes, isRemote, mdFilePath, customMarkup }: CreateHTMLProps) | ||
{ | ||
let baseSource: Image.MetadataEntry[]; | ||
let highsrc: Image.MetadataEntry; | ||
|
||
// picking the src for the base <img> tag | ||
if (metadata.jpeg && metadata.jpeg.length > 0) | ||
{ | ||
baseSource = metadata.jpeg; | ||
highsrc = metadata.jpeg[metadata.jpeg.length - 1]; | ||
} | ||
else | ||
{ | ||
// when the image is remote, there's no jpeg, so just use the first format there is | ||
baseSource = Object.values(metadata)[0] as Image.MetadataEntry[]; | ||
highsrc = baseSource[baseSource.length - 1]; | ||
} | ||
|
||
function generateSrcsets(metadata: Image.Metadata) | ||
{ | ||
function correctSrcset(entry: Image.MetadataEntry) | ||
{ | ||
const filename = path.join(imageDir, path.basename(entry.url)) + ` ${entry.width}w`; | ||
return filename; | ||
} | ||
|
||
return Object.values(metadata).map(imageFormat => | ||
{ | ||
return ` <source type="${imageFormat[0].sourceType}" srcset="${imageFormat.map(entry => correctSrcset(entry)).join(", ")}" sizes="${sizes}">\n`; | ||
}).join("\n"); | ||
} | ||
|
||
if (customMarkup) | ||
{ | ||
return customMarkup({ src: path.join(imageDir, path.basename(highsrc.url)), width: highsrc.width, height: highsrc.height, alt: alt, format: baseSource[0].format, sources: generateSrcsets(metadata), isRemote: isRemote, mdFilePath: mdFilePath }); | ||
} | ||
else | ||
{ | ||
return defaultMarkup({ src: path.join(imageDir, path.basename(highsrc.url)), width: highsrc.width, height: highsrc.height, alt: alt, format: baseSource[0].format, sources: generateSrcsets(metadata), isRemote: isRemote, mdFilePath: mdFilePath }); | ||
} | ||
} | ||
|
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,24 @@ | ||
import type Image from "@11ty/eleventy-img"; | ||
|
||
type MarkupValues = { | ||
src: string, | ||
width: number, | ||
height: number, | ||
alt: string, | ||
format: string, | ||
sources: string, | ||
isRemote: boolean, | ||
mdFilePath: string, | ||
}; | ||
|
||
interface createHTMLProps | ||
{ | ||
imageDir: string, | ||
metadata: Image.Metadata, | ||
alt: string, | ||
sizes: string, | ||
isRemote: boolean, | ||
mdFilePath: string; | ||
} | ||
|
||
export type { MarkupValues, createHTMLProps }; |