Skip to content

Commit d48b9c5

Browse files
Add util func getAssetImagePath
1 parent 6884c27 commit d48b9c5

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

web/src/pages/Element_tree.mdx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import StarlightPage from '@astrojs/starlight/components/StarlightPage.astro';
2-
import { getSeeAlsoLinksFromList } from '@src/utils/general';
2+
import { getSeeAlsoLinksFromList, getAssetImagePath } from '@src/utils/general';
33
import SeeAlsoSection from '@src/components/SeeAlsoSection.astro';
44
import { Image } from 'astro:assets';
5-
import treeImage from '@src/assets/images/element_tree.webp';
65

76
<StarlightPage frontmatter={{
87
template: 'doc',
@@ -15,7 +14,7 @@ If you are familiar with the concept of *trees* in computer-science, this should
1514

1615
All elements that are created within scripts or from .map files are child elements of the resource they belong to. Thus, most elements (except for [players](/player)) exist only within resources and are also destroyed as soon as their resource is stopped.
1716

18-
<Image src={treeImage} alt="Element tree"/>
17+
<Image src={getAssetImagePath("element_tree.webp")} alt="Element tree"/>
1918

2019
### Tree elements
2120
* **root**: This is at the very base of the tree - all elements are children (or descendants) of this element.

web/src/pages/OOP_Introduction.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import StarlightPage from '@astrojs/starlight/components/StarlightPage.astro';
22
import { Image } from 'astro:assets';
3-
import imageDiagram from '@src/assets/images/oop_intro_diagram.png';
3+
import { getAssetImagePath } from '@src/utils/general';
44
import { getSeeAlsoLinksFromList } from '@src/utils/general';
55
import SeeAlsoSection from '@src/components/SeeAlsoSection.astro';
66

@@ -48,7 +48,7 @@ Throughout the tutorial when I say *object*, I don't mean `createObject` (unless
4848

4949
Here is a fancy Venn diagram I created to show the simple organisation of classes and elements:
5050

51-
<Image src={imageDiagram} width="600" alt="Diagram"/>
51+
<Image src={getAssetImagePath("oop_intro_diagram.png")} width="600" alt="Diagram"/>
5252

5353
The functions on the left are sorted to show what kind of category the returned value rests in. We've got Classes, Elements and "Problem children".
5454

web/src/pages/index.astro

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
---
22
import StarlightPage from '@astrojs/starlight/components/StarlightPage.astro';
33
import { Image } from 'astro:assets';
4-
5-
// IMAGES
6-
import image_mtaLogo from '@src/assets/images/mta_logo.png';
4+
import { getAssetImagePath } from '@src/utils/general';
75
86
import { MTA_CURRENT_VERSION } from '@src/content.constants';
97
import { Icon } from '@astrojs/starlight/components';
@@ -22,7 +20,7 @@ import { Icon } from '@astrojs/starlight/components';
2220
}}>
2321
<div class="mtahero">
2422
<div class="mtahero_left">
25-
<Image src={image_mtaLogo} alt="Multi Theft Auto Logo"/>
23+
<Image src={getAssetImagePath("mta_logo.png")} alt="Multi Theft Auto Logo"/>
2624
</div>
2725

2826
<div class="mtahero_right">

web/src/utils/general.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ import { getFunctionsByTypeByCategory } from '@src/utils/functions';
44
import { getEventsByTypeByCategory } from '@src/utils/events';
55
import { getElementsByCategory, getElementCategory } from '@src/utils/elements';
66

7+
import type { ImageMetadata } from 'astro';
8+
9+
const imagesFromAssets = import.meta.glob<{ default: ImageMetadata }>('/src/assets/images/*.{jpeg,jpg,png,gif,webp}');
10+
export function getAssetImagePath(imageFilename: string): any {
11+
const imagePath = `/src/assets/images/${imageFilename}`;
12+
if (imagesFromAssets[imagePath]) {
13+
return imagesFromAssets[imagePath]();
14+
} else {
15+
throw new Error(`Image not found: ${imageFilename}`);
16+
}
17+
}
718

819
export function renderInlineMarkdown(markdown: string): string | Promise<string> {
920
const html = marked.parseInline(markdown);

0 commit comments

Comments
 (0)