-
Notifications
You must be signed in to change notification settings - Fork 21
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 #183 from CloudCannon/feat/astro-virtual-modules
Feat/astro virtual modules
- Loading branch information
Showing
7 changed files
with
226 additions
and
45 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
5 changes: 5 additions & 0 deletions
5
javascript-modules/engines/astro-engine/lib/modules/assets.js
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,5 @@ | ||
import ImageInternal from './image.astro'; | ||
import PictureInternal from './picture.astro'; | ||
|
||
export const Image = ImageInternal; | ||
export const Picture = PictureInternal; |
45 changes: 45 additions & 0 deletions
45
javascript-modules/engines/astro-engine/lib/modules/content.js
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,45 @@ | ||
export const getCollection = (collectionKey, filter) => { | ||
if (!window.__bookshop_collections) { | ||
console.warn("[Bookshop] Failed to load site collections for live editing"); | ||
return []; | ||
} | ||
|
||
if (!window.__bookshop_collections[collectionKey]) { | ||
console.warn("[Bookshop] Failed to load collection: ", collectionKey); | ||
return []; | ||
} | ||
|
||
if (filter) { | ||
return window.__bookshop_collections[collectionKey].filter(filter); | ||
} | ||
return window.__bookshop_collections[collectionKey]; | ||
}; | ||
|
||
export const getEntry = (...args) => { | ||
if (args.length === 1) { | ||
const { collection: collectionKey, slug: entrySlug, id: entryId } = args[0]; | ||
const collection = getCollection(collectionKey); | ||
if (entryId) { | ||
return collection.find(({ id }) => id === entryId); | ||
} else if (entrySlug) { | ||
return collection.find(({ slug }) => slug === entrySlug); | ||
} | ||
return console.warn( | ||
"[Bookshop] Failed to load entries, invalid arguments: ", | ||
args | ||
); | ||
} | ||
|
||
const [collectionKey, entryKey] = args; | ||
const collection = getCollection(collectionKey); | ||
|
||
return collection.find(({ id, slug }) => entryKey === (slug ?? id)); | ||
}; | ||
|
||
export const getEntries = (entries) => { | ||
return entries.map(getEntry); | ||
}; | ||
|
||
export const getEntryBySlug = (collection, slug) => { | ||
return getEntry({ collection, slug }); | ||
}; |
23 changes: 23 additions & 0 deletions
23
javascript-modules/engines/astro-engine/lib/modules/image.astro
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,23 @@ | ||
--- | ||
const props = Astro.props; | ||
if (props.alt === undefined || props.alt === null) { | ||
throw new Error("Image missing alt"); | ||
} | ||
// As a convenience, allow width and height to be string with a number in them, to match HTML's native `img`. | ||
if (typeof props.width === "string") { | ||
props.width = parseInt(props.width); | ||
} | ||
if (typeof props.height === "string") { | ||
props.height = parseInt(props.height); | ||
} | ||
--- | ||
|
||
<img | ||
src={props.src} | ||
alt={props.alt} | ||
width={props.width} | ||
height={props.height} | ||
/> |
7 changes: 7 additions & 0 deletions
7
javascript-modules/engines/astro-engine/lib/modules/picture.astro
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,7 @@ | ||
--- | ||
import Image from "./image.astro"; | ||
--- | ||
|
||
<picture> | ||
<Image {...Astro.props} /> | ||
</picture> |