Skip to content

Commit

Permalink
Add support for imgstyle
Browse files Browse the repository at this point in the history
  • Loading branch information
lilith committed Apr 3, 2023
1 parent bc81208 commit a68d0e5
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ pnpm-lock.yaml


#cached openai responses
.aicache/
.aicache/
.vercel/output/
2 changes: 2 additions & 0 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import astroI18next from "astro-i18next";
import vercel from '@astrojs/vercel/static';


console.log("Ouptut dir: " + SITE.outDir);
// https://astro.build/config
export default defineConfig({
trailingSlash: "always", //Sitemap uses trailing /, we gotta be consistent. This affects dev and production differently
outDir: SITE.outDir,
integrations: [
// Enable Preact to support Preact JSX components.
preact(),
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
"dev": "astro dev",
"start": "astro dev",
"build": "astro build",
"build-imgstyle": "tsx tasks/switch.ts imgstyle && astro build && tsx tasks/switch.ts srcset",
"preview": "astro preview",
"astro": "astro",
"switch": "tsx tasks/switch.ts",
"translate": "tsx tasks/translate.ts",
"localize": "tsx tasks/localize.ts",
"jsonld": "tsx tasks/jsonld.ts",
"feedback": "tsx tasks/ai_feedback.ts",
"dates": "tsx tasks/update-frontmatter-dates.ts"
"dates": "tsx tasks/update-frontmatter-dates.ts",
"site-info": "tsx tasks/site_info.ts"
},
"dependencies": {
"@algolia/client-search": "^4.13.1",
Expand Down
12 changes: 12 additions & 0 deletions public/locales/en/imgstyle.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"siteTitleSuffix": " - img.style",
"siteTitleName": "image styling & optimization",
"skipToContentText": "Skip to Content",
"editPage": "Edit this page",
"joinCommunity": "Join our community",
"footerLicense": "This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License",
"translatedContentDisclaimer": "Please let us know if something isn't right. Our translator isn't quite sentient yet, but does incorporate feedback, so please leave some tips!",
"reportIssue": "Get in touch on Github",
"sidebarTitle": "Topics",
"communitySidebarTitle": "Community"
}
2 changes: 2 additions & 0 deletions src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import SITE_INFO from './site-info';

export const SITE = SITE_INFO;

export const SITES_KEYS = ['srcset', 'imgstyle'];

export const CONTENT_COLLECTIONS = ['docs', 'imgstyle'];

export const GITHUB_EDIT_URL = `https://github.com/imazen/sites/tree/main`;
Expand Down
5 changes: 3 additions & 2 deletions src/site-info.imgstyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ export default {
title: '<img style> layout and reference', // Used for logo text and og:sitename
titleSuffix: ' - img.style', // Appended to page content title
description: 'Examples and tools for image layout and styling',
defaultLanguage: 'en-us',
defaultLanguage: 'en',
address: 'https://www.img.style',
defaultLocale: 'en',
algoliaIndex: 'imgstyle',
algoliaDomain: 'img.style',
contentCollection: 'imgstyle',
translationNamespace: 'imgstyle'
translationNamespace: 'imgstyle',
outDir: './dist-imgstyle',
} as const;
6 changes: 4 additions & 2 deletions src/site-info.srcset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ export default {
description: 'Templates, guides, examples, and tools for correct and efficient responsive images',
defaultLanguage: 'en',
address: 'https://www.srcset.tips/',
algoliaIndex: 'srcset', //
algoliaIndex: 'srcset',
defaultLocale: 'en',
algoliaDomain: 'srcset.tips',
contentCollection: 'docs',
translationNamespace: 'srcset'
translationNamespace: 'srcset',
outDir: './dist',
} as const;
6 changes: 6 additions & 0 deletions tasks/site_info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { SITE } from "../src/consts";

// print info

console.log("SITE", SITE);

10 changes: 10 additions & 0 deletions tasks/switch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { switchSite } from "./switch_sites";
import { CONTENT_COLLECTIONS } from "../src/consts";

const site = process.argv[2];
if (!site) {
console.error("Please specify site key such as 'srcset' or 'imgstyle'. Ex. npm run switch -- srcset OR npm run switch -- imgstyle");
}else{
switchSite(site);
}

66 changes: 66 additions & 0 deletions tasks/switch_sites.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import fs from 'fs';
import path from 'path';
import { SITES_KEYS } from "../src/consts";

async function searchFilenamesRecursive(directory: string, include: string): Promise<string[]> {
const entries = await fs.promises.readdir(directory, { withFileTypes: true });
const files: string[] = [];

for (const entry of entries) {
const fullPath = path.join(directory, entry.name);
if (entry.isDirectory()) {
const subDirectoryFiles = await searchFilenamesRecursive(fullPath, include);
files.push(...subDirectoryFiles);
} else if (entry.isFile() && entry.name.includes(include)) {
files.push(fullPath);
}
}
return files;
}

async function switchSymlinks(directory: string, newSitekey: string) {
const searchPart = `.${newSitekey}.`;
const files = await searchFilenamesRecursive(directory, searchPart);
for (const newFile of files) {
const defaultFile = newFile.replace(searchPart, '.');
// Check if defaultFile is a symlink
const stats = await fs.promises.lstat(defaultFile);
if (stats.isSymbolicLink()) {
// print what it points to
const link = await fs.promises.readlink(defaultFile);
// if link is already newFile, skip
if (link === newFile) {
console.log('Link already exists from ' + defaultFile + " to " + link);
continue;
}
console.log('Unlinking ' + defaultFile + ' from using ' + link);
// delete symlink
await fs.promises.unlink(defaultFile);
// symlink defaultFile to newFile
console.log('Linking ' + defaultFile + ' to use ' + newFile);
await fs.promises.symlink(newFile, defaultFile);
} else {
console.log('!!!! Error, not a symlink: ' + defaultFile);

console.log('Not a symlink');
}
}
}
export async function switchSite(siteKey: string) {
// check siteKey is valid
if (!SITES_KEYS.includes(siteKey)) {
console.log('Error: invalid site key (' + siteKey + ')');
return;
}

console.log('Switching active site to ' + siteKey);

// check the current folder contains 'astro.config.mjs', if not fail
const astroConfigPath = path.join(process.cwd(), 'astro.config.mjs');
if (!fs.existsSync(astroConfigPath)) {
console.log('Error: astro.config.mjs not found in current directory. Run this from root');
return;
}

await switchSymlinks(process.cwd(), siteKey);
}

0 comments on commit a68d0e5

Please sign in to comment.