Skip to content

Commit 07984cd

Browse files
committed
fix: resolve conversations
1 parent 7559c15 commit 07984cd

File tree

6 files changed

+48
-20
lines changed

6 files changed

+48
-20
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ This is the source code repository for the documentation of [image-js](https://g
44

55
The documentation is available on <https://docs.image-js.org/>.
66

7+
## Generating demo images
8+
9+
To regenerate the demo images (only needed when adding/updating images):
10+
11+
```bash
12+
npm run generate-images
13+
```
14+
15+
This will:
16+
17+
- Fetch images from the URLs defined in `defaultImageUrls.ts`
18+
- Save them to the `static/` folder
19+
- Generate `imageData.json` with metadata
20+
721
## Create demos
822

923
A demo is simply a function which takes an image or mask as input and returns an image or mask as output. When imported in `md` files, it will be transformed into a demo component which allows choosing from various image or video sources to showcase the image transformation.

docusaurus.config.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,6 @@ async function createConfig() {
5353
baseUrl: '/',
5454

5555
plugins: [
56-
function generateImagesPlugin() {
57-
return {
58-
name: 'generate-images-plugin',
59-
async loadContent() {
60-
const { imageLoader } = await import('./imageLoader.mjs');
61-
await imageLoader();
62-
},
63-
};
64-
},
6556
demoLoaderPlugin,
6657
[
6758
'@dipakparmar/docusaurus-plugin-umami',

imageLoader.mjs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
import fs from 'fs';
2+
import path from 'path';
3+
import { fileURLToPath } from 'url';
24

35
import { fetchURL, write } from 'image-js';
46

57
import { defaultImages, defaultMasks } from './imageDataset.mjs';
68

9+
const __filename = fileURLToPath(import.meta.url);
10+
const __dirname = path.dirname(__filename);
11+
712
export async function imageLoader() {
813
const demoImagesDir = 'demoImages';
914
const staticDir = 'static';
1015

1116
const imageData = { masks: [], images: [] };
1217
try {
1318
// Create static directory if it doesn't exist
14-
if (!fs.existsSync(`./${staticDir}/${demoImagesDir}`)) {
15-
fs.mkdirSync(`./${staticDir}/${demoImagesDir}`, { recursive: true });
19+
const staticPath = path.join(__dirname, staticDir, demoImagesDir);
20+
21+
if (!fs.existsSync(staticPath)) {
22+
fs.mkdirSync(staticPath, { recursive: true });
1623
}
1724

1825
const images = await Promise.all(
@@ -23,9 +30,14 @@ export async function imageLoader() {
2330
const image = images[i];
2431
const imageDataUrl = defaultImages[i];
2532
const imageTitle = getFilename(imageDataUrl.value);
26-
write(`./${staticDir}/${demoImagesDir}/images/${imageTitle}`, image, {
27-
recursive: true,
28-
});
33+
const imagePath = path.join(
34+
__dirname,
35+
staticDir,
36+
demoImagesDir,
37+
'images',
38+
imageTitle,
39+
);
40+
write(imagePath, image, { recursive: true });
2941
// Keeping object structure for compatibility
3042
imageData.images.push({
3143
type: 'url',
@@ -43,9 +55,14 @@ export async function imageLoader() {
4355
const mask = masks[i];
4456
const maskDataUrl = defaultMasks[i];
4557
const maskTitle = getFilename(maskDataUrl.value);
46-
write(`./${staticDir}/${demoImagesDir}/masks/${maskTitle}`, mask, {
47-
recursive: true,
48-
});
58+
const maskPath = path.join(
59+
__dirname,
60+
staticDir,
61+
demoImagesDir,
62+
'masks',
63+
maskTitle,
64+
);
65+
write(maskPath, mask, { recursive: true });
4966
// Keeping object structure for compatibility
5067
imageData.masks.push({
5168
type: 'url',
@@ -55,7 +72,10 @@ export async function imageLoader() {
5572
});
5673
}
5774
// Write data about newly created files.
58-
const outputPath = `./${staticDir}/${demoImagesDir}/imageData.json`;
75+
const outputPath = path.join(
76+
__dirname,
77+
'src/demo/contexts/demo/imageData.json',
78+
);
5979

6080
fs.writeFileSync(outputPath, JSON.stringify(imageData, null, 2));
6181
} catch (error) {
@@ -68,3 +88,4 @@ export async function imageLoader() {
6888
function getFilename(filepath) {
6989
return filepath.replace(/^.*[\\/]/, '');
7090
}
91+
await imageLoader();

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"build": "docusaurus build",
77
"clear": "docusaurus clear",
88
"deploy": "docusaurus deploy",
9+
"generate-images": "node imageLoader.mjs",
910
"cspell": "cspell lint \"**/*.md\"",
1011
"eslint": "eslint --cache ./src ./docs",
1112
"eslint-fix": "npm run eslint -- --fix",

src/demo/contexts/demo/defaultImages.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import dataset from '../../../../static/demoImages/imageData.json';
21
import type { UrlOption } from '../importImage/importImageContext';
32

3+
import dataset from './imageData.json';
4+
45
const defaultImages = dataset.images as UrlOption[];
56
const defaultMasks = dataset.masks as UrlOption[];
67

static/demoImages/imageData.json renamed to src/demo/contexts/demo/imageData.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,4 @@
117117
"value": "/demoImages/images/airport.png"
118118
}
119119
]
120-
}
120+
}

0 commit comments

Comments
 (0)