Skip to content

Commit a38858f

Browse files
authored
Merge pull request #7 from nemanjam/feature/refactor-theme-script
Refactor theme and mode
2 parents 9733bed + 66befef commit a38858f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1416
-1229
lines changed

.astro/types.d.ts

Lines changed: 3 additions & 208 deletions
Original file line numberDiff line numberDiff line change
@@ -1,208 +1,3 @@
1-
declare module 'astro:content' {
2-
interface Render {
3-
'.mdx': Promise<{
4-
Content: import('astro').MarkdownInstance<{}>['Content'];
5-
headings: import('astro').MarkdownHeading[];
6-
remarkPluginFrontmatter: Record<string, any>;
7-
}>;
8-
}
9-
}
10-
11-
declare module 'astro:content' {
12-
interface Render {
13-
'.md': Promise<{
14-
Content: import('astro').MarkdownInstance<{}>['Content'];
15-
headings: import('astro').MarkdownHeading[];
16-
remarkPluginFrontmatter: Record<string, any>;
17-
}>;
18-
}
19-
}
20-
21-
declare module 'astro:content' {
22-
type Flatten<T> = T extends { [K: string]: infer U } ? U : never;
23-
24-
export type CollectionKey = keyof AnyEntryMap;
25-
export type CollectionEntry<C extends CollectionKey> = Flatten<AnyEntryMap[C]>;
26-
27-
export type ContentCollectionKey = keyof ContentEntryMap;
28-
export type DataCollectionKey = keyof DataEntryMap;
29-
30-
type AllValuesOf<T> = T extends any ? T[keyof T] : never;
31-
type ValidContentEntrySlug<C extends keyof ContentEntryMap> = AllValuesOf<
32-
ContentEntryMap[C]
33-
>['slug'];
34-
35-
export function getEntryBySlug<
36-
C extends keyof ContentEntryMap,
37-
E extends ValidContentEntrySlug<C> | (string & {}),
38-
>(
39-
collection: C,
40-
// Note that this has to accept a regular string too, for SSR
41-
entrySlug: E
42-
): E extends ValidContentEntrySlug<C>
43-
? Promise<CollectionEntry<C>>
44-
: Promise<CollectionEntry<C> | undefined>;
45-
46-
export function getDataEntryById<C extends keyof DataEntryMap, E extends keyof DataEntryMap[C]>(
47-
collection: C,
48-
entryId: E
49-
): Promise<CollectionEntry<C>>;
50-
51-
export function getCollection<C extends keyof AnyEntryMap, E extends CollectionEntry<C>>(
52-
collection: C,
53-
filter?: (entry: CollectionEntry<C>) => entry is E
54-
): Promise<E[]>;
55-
export function getCollection<C extends keyof AnyEntryMap>(
56-
collection: C,
57-
filter?: (entry: CollectionEntry<C>) => unknown
58-
): Promise<CollectionEntry<C>[]>;
59-
60-
export function getEntry<
61-
C extends keyof ContentEntryMap,
62-
E extends ValidContentEntrySlug<C> | (string & {}),
63-
>(entry: {
64-
collection: C;
65-
slug: E;
66-
}): E extends ValidContentEntrySlug<C>
67-
? Promise<CollectionEntry<C>>
68-
: Promise<CollectionEntry<C> | undefined>;
69-
export function getEntry<
70-
C extends keyof DataEntryMap,
71-
E extends keyof DataEntryMap[C] | (string & {}),
72-
>(entry: {
73-
collection: C;
74-
id: E;
75-
}): E extends keyof DataEntryMap[C]
76-
? Promise<DataEntryMap[C][E]>
77-
: Promise<CollectionEntry<C> | undefined>;
78-
export function getEntry<
79-
C extends keyof ContentEntryMap,
80-
E extends ValidContentEntrySlug<C> | (string & {}),
81-
>(
82-
collection: C,
83-
slug: E
84-
): E extends ValidContentEntrySlug<C>
85-
? Promise<CollectionEntry<C>>
86-
: Promise<CollectionEntry<C> | undefined>;
87-
export function getEntry<
88-
C extends keyof DataEntryMap,
89-
E extends keyof DataEntryMap[C] | (string & {}),
90-
>(
91-
collection: C,
92-
id: E
93-
): E extends keyof DataEntryMap[C]
94-
? Promise<DataEntryMap[C][E]>
95-
: Promise<CollectionEntry<C> | undefined>;
96-
97-
/** Resolve an array of entry references from the same collection */
98-
export function getEntries<C extends keyof ContentEntryMap>(
99-
entries: {
100-
collection: C;
101-
slug: ValidContentEntrySlug<C>;
102-
}[]
103-
): Promise<CollectionEntry<C>[]>;
104-
export function getEntries<C extends keyof DataEntryMap>(
105-
entries: {
106-
collection: C;
107-
id: keyof DataEntryMap[C];
108-
}[]
109-
): Promise<CollectionEntry<C>[]>;
110-
111-
export function reference<C extends keyof AnyEntryMap>(
112-
collection: C
113-
): import('astro/zod').ZodEffects<
114-
import('astro/zod').ZodString,
115-
C extends keyof ContentEntryMap
116-
? {
117-
collection: C;
118-
slug: ValidContentEntrySlug<C>;
119-
}
120-
: {
121-
collection: C;
122-
id: keyof DataEntryMap[C];
123-
}
124-
>;
125-
// Allow generic `string` to avoid excessive type errors in the config
126-
// if `dev` is not running to update as you edit.
127-
// Invalid collection names will be caught at build time.
128-
export function reference<C extends string>(
129-
collection: C
130-
): import('astro/zod').ZodEffects<import('astro/zod').ZodString, never>;
131-
132-
type ReturnTypeOrOriginal<T> = T extends (...args: any[]) => infer R ? R : T;
133-
type InferEntrySchema<C extends keyof AnyEntryMap> = import('astro/zod').infer<
134-
ReturnTypeOrOriginal<Required<ContentConfig['collections'][C]>['schema']>
135-
>;
136-
137-
type ContentEntryMap = {
138-
"post": {
139-
"2022-02-16-example-article-1.mdx": {
140-
id: "2022-02-16-example-article-1.mdx";
141-
slug: "2022-02-16-example-article-1";
142-
body: string;
143-
collection: "post";
144-
data: InferEntrySchema<"post">
145-
} & { render(): Render[".mdx"] };
146-
"2022-03-17-example-article-2.mdx": {
147-
id: "2022-03-17-example-article-2.mdx";
148-
slug: "2022-03-17-example-article-2";
149-
body: string;
150-
collection: "post";
151-
data: InferEntrySchema<"post">
152-
} & { render(): Render[".mdx"] };
153-
"2023-01-19-example-article-3.mdx": {
154-
id: "2023-01-19-example-article-3.mdx";
155-
slug: "2023-01-19-example-article-3";
156-
body: string;
157-
collection: "post";
158-
data: InferEntrySchema<"post">
159-
} & { render(): Render[".mdx"] };
160-
"2024-01-20-example-article-4.mdx": {
161-
id: "2024-01-20-example-article-4.mdx";
162-
slug: "2024-01-20-example-article-4";
163-
body: string;
164-
collection: "post";
165-
data: InferEntrySchema<"post">
166-
} & { render(): Render[".mdx"] };
167-
"2024-01-21-example-article-5.mdx": {
168-
id: "2024-01-21-example-article-5.mdx";
169-
slug: "2024-01-21-example-article-5";
170-
body: string;
171-
collection: "post";
172-
data: InferEntrySchema<"post">
173-
} & { render(): Render[".mdx"] };
174-
};
175-
"project": {
176-
"2024-02-13-example-project-1.mdx": {
177-
id: "2024-02-13-example-project-1.mdx";
178-
slug: "2024-02-13-example-project-1";
179-
body: string;
180-
collection: "project";
181-
data: InferEntrySchema<"project">
182-
} & { render(): Render[".mdx"] };
183-
"2024-03-15-example-project-2.mdx": {
184-
id: "2024-03-15-example-project-2.mdx";
185-
slug: "2024-03-15-example-project-2";
186-
body: string;
187-
collection: "project";
188-
data: InferEntrySchema<"project">
189-
} & { render(): Render[".mdx"] };
190-
"2024-05-16-example-project-3.mdx": {
191-
id: "2024-05-16-example-project-3.mdx";
192-
slug: "2024-05-16-example-project-3";
193-
body: string;
194-
collection: "project";
195-
data: InferEntrySchema<"project">
196-
} & { render(): Render[".mdx"] };
197-
};
198-
199-
};
200-
201-
type DataEntryMap = {
202-
203-
};
204-
205-
type AnyEntryMap = ContentEntryMap & DataEntryMap;
206-
207-
export type ContentConfig = typeof import("../src/content/config.js");
208-
}
1+
/// <reference types="astro/client" />
2+
/// <reference path="astro/content.d.ts" />
3+
/// <reference path="astro/env.d.ts" />

.github/workflows/bash__deploy-nginx.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88
- 'v[0-9]+.[0-9]+.[0-9]+'
99
pull_request:
1010
branches:
11-
- 'main'
11+
- 'disabled-main'
1212
workflow_dispatch:
1313

1414
env:

astro.config.ts

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,34 @@ import { defineConfig } from 'astro/config';
66

77
// must use relative imports, and their entire import subtrees
88
import { remarkReadingTime } from './plugins/remark-reading-time.mjs';
9+
//
910
// all relative imports in subtree
10-
import { CONFIG } from './src/config';
11+
// any of these files must not import CONFIG with env vars
12+
import { envSchema, PROCESS_ENV } from './src/config/process-env';
1113
import { expressiveCodeIntegration } from './src/libs/integrations/expressive-code';
1214
import { sitemapIntegration } from './src/libs/integrations/sitemap';
1315

14-
const { SITE_URL } = CONFIG;
16+
const { SITE_URL } = PROCESS_ENV;
1517
const remarkPlugins = [remarkReadingTime];
1618

1719
export default defineConfig({
1820
site: SITE_URL,
21+
experimental: { env: envSchema },
1922
trailingSlash: 'ignore',
2023
// default
2124
compressHTML: true,
22-
server: {
23-
port: 3000,
24-
},
25-
devToolbar: {
26-
enabled: false,
27-
},
25+
server: { port: 3000 },
26+
devToolbar: { enabled: false },
2827
integrations: [
2928
expressiveCodeIntegration(),
3029
sitemapIntegration(),
3130
react(),
32-
// applyBaseStyles: false prevents double loading of tailwind
33-
tailwind({
34-
applyBaseStyles: false,
35-
}),
3631
mdx(),
37-
icon({
38-
iconDir: 'src/assets/icons',
39-
}),
32+
// applyBaseStyles: false prevents double loading of tailwind
33+
tailwind({ applyBaseStyles: false }),
34+
icon({ iconDir: 'src/assets/icons' }),
4035
],
41-
markdown: {
42-
remarkPlugins,
43-
},
36+
markdown: { remarkPlugins },
4437
vite: {
4538
build: {
4639
sourcemap: false,
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { SITE_URL } from 'astro:env/client';
2+
import { NODE_ENV, PREVIEW_MODE } from 'astro:env/server';
3+
4+
import { configSchema } from './schemas/config';
5+
import { prettyPrintObject } from './utils/log';
6+
import { validateData } from './utils/validation';
7+
8+
import type { ConfigType } from './types/config';
9+
10+
/*-------------------- configData -------------------*/
11+
12+
// const { SITE_URL, NODE_ENV, PREVIEW_MODE } = import.meta.env;
13+
// const { SITE_URL, NODE_ENV, PREVIEW_MODE } = {
14+
// SITE_URL: 'http://localhost:3000',
15+
// NODE_ENV: 'development',
16+
// PREVIEW_MODE: false,
17+
// } as const;
18+
19+
console.log('SITE_URL', SITE_URL, 'NODE_ENV', NODE_ENV, 'PREVIEW_MODE', PREVIEW_MODE);
20+
21+
/** SSG - all env vars are build time only. */
22+
const configData: ConfigType = {
23+
NODE_ENV,
24+
PREVIEW_MODE,
25+
/** all urls without '/' */
26+
SITE_URL,
27+
SITE_TITLE: 'Nemanja Mitic',
28+
SITE_DESCRIPTION: 'I am Nemanja, full stack developer',
29+
PAGE_SIZE_POST_CARD: 3,
30+
PAGE_SIZE_POST_CARD_SMALL: 6,
31+
MORE_POSTS_COUNT: 3,
32+
DEFAULT_MODE: 'light',
33+
DEFAULT_THEME: 'default-light',
34+
AUTHOR_NAME: 'Nemanja Mitic',
35+
AUTHOR_EMAIL: '[email protected]',
36+
AUTHOR_GITHUB: 'https://github.com/nemanjam',
37+
AUTHOR_LINKEDIN: 'https://www.linkedin.com/in/nemanja-mitic',
38+
AUTHOR_TWITTER: 'https://x.com/nemanja_codes',
39+
AUTHOR_YOUTUBE: 'https://www.youtube.com/@nemanja_codes',
40+
REPO_URL: 'https://github.com/nemanjam/nemanjam.github.io',
41+
};
42+
43+
// todo: Config should go into import.meta.env in astro.config.ts
44+
export const CONFIG = validateData(configData, configSchema);
45+
46+
// prettyPrintObject(CONFIG, 'parsed CONFIG');

docs/working-notes/todo3.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,5 +505,13 @@ a href open in new tab
505505
fix links page links color for history back view transition
506506

507507
refactor theme script, osDefaultMode, appDefaultMode, storedMode
508+
change meta theme bg color with js, astro-paper
509+
510+
replace import config with import.meta.env.VAR
511+
ask about app start handler for log env vars
512+
513+
fix image sizes for gallery and other
514+
update bg color meta tag
515+
fix links page on user dark mode preference text color
508516
------------
509517
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"@astrojs/tailwind": "^5.1.0",
3131
"@fontsource-variable/inter": "^5.0.20",
3232
"@tailwindcss/typography": "^0.5.13",
33-
"astro": "^4.13.1",
33+
"astro": "^4.14.4",
3434
"astro-embed": "^0.7.2",
3535
"astro-expressive-code": "^0.35.6",
3636
"astro-icon": "^1.1.0",

src/components/BaseHead.astro

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import { ViewTransitions } from 'astro:transitions';
77
import ThemeScript from '@/components/ThemeScript.astro';
88
import { DEFAULT_METADATA } from '@/constants/metadata';
99
import { ROUTES } from '@/constants/routes';
10-
import { CONFIG } from '@/config';
10+
import { CONFIG_CLIENT } from '@/config/client';
1111
import { handleTitle } from '@/utils/metadata';
1212
import { filterUndefined } from '@/utils/objects';
1313
1414
import type { Metadata } from '@/types/common';
1515
16-
const { AUTHOR_NAME } = CONFIG;
16+
const { AUTHOR_NAME } = CONFIG_CLIENT;
1717
1818
export interface BaseHeadProps {
1919
metadata: Metadata;
@@ -110,6 +110,7 @@ const ogImageUrl = new URL(image, url);
110110
<script src="set-url-here" crossorigin="anonymous"></script>
111111
-->
112112

113+
{/* MUST be inside <head /> to avoid white flash, IMPORTANT */}
113114
<ThemeScript />
114115

115116
<ViewTransitions fallback="none" />

src/components/Footer.astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { Icon } from 'astro-icon/components';
33
44
import Link from '@/components/Link.astro';
55
import { ROUTES } from '@/constants/routes';
6-
import { CONFIG } from '@/config';
6+
import { CONFIG_CLIENT } from '@/config/client';
77
import { getLatestCommitInfo } from '@/libs/git';
88
import { shortDate } from '@/utils/datetime';
99
import { limitString, trimHttpProtocol } from '@/utils/strings';
1010
11-
const { SITE_URL, AUTHOR_LINKEDIN, AUTHOR_GITHUB, REPO_URL } = CONFIG;
11+
const { SITE_URL, AUTHOR_LINKEDIN, AUTHOR_GITHUB, REPO_URL } = CONFIG_CLIENT;
1212
const domain = trimHttpProtocol(SITE_URL);
1313
const messageLength = 20 as const;
1414

src/components/Giscus.astro

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ const { class: className } = Astro.props;
3434
import 'giscus';
3535

3636
import { SELECTORS } from '@/constants/dom';
37-
import { THEME_CONFIG } from '@/constants/themes';
38-
import { getCurrentMode, sendModeToGiscus } from '@/utils/dom';
37+
import { THEME_CONFIG } from '@/constants/theme';
38+
import { sendModeToGiscus } from '@/utils/dom';
39+
import { getCurrentMode } from '@/utils/theme';
3940

4041
import type { ChangeThemeCustomEvent } from '@/types/constants';
4142

0 commit comments

Comments
 (0)