Skip to content

Commit c047ce6

Browse files
authored
Merge pull request #501 from phanect/feat-typescriptify
Convert JavaScript files to TypeScript
2 parents 18add82 + d0299e9 commit c047ce6

File tree

8 files changed

+1927
-955
lines changed

8 files changed

+1927
-955
lines changed

astro.config.mjs astro.config.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,16 @@ import mdx from '@astrojs/mdx';
99
import partytown from '@astrojs/partytown';
1010
import icon from 'astro-icon';
1111
import compress from 'astro-compress';
12+
import type { AstroIntegration } from 'astro';
1213

1314
import astrowind from './vendor/integration';
1415

15-
import {
16-
readingTimeRemarkPlugin,
17-
responsiveTablesRehypePlugin,
18-
lazyImagesRehypePlugin,
19-
} from './src/utils/frontmatter.mjs';
16+
import { readingTimeRemarkPlugin, responsiveTablesRehypePlugin, lazyImagesRehypePlugin } from './src/utils/frontmatter';
2017

2118
const __dirname = path.dirname(fileURLToPath(import.meta.url));
2219

2320
const hasExternalScripts = false;
24-
const whenExternalScripts = (items = []) =>
21+
const whenExternalScripts = (items: (() => AstroIntegration) | (() => AstroIntegration)[] = []) =>
2522
hasExternalScripts ? (Array.isArray(items) ? items.map((item) => item()) : [items()]) : [];
2623

2724
export default defineConfig({

package-lock.json

+1,892-928
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@
5959
"prettier": "^3.3.3",
6060
"prettier-plugin-astro": "^0.14.1",
6161
"reading-time": "^1.5.0",
62-
"rehype-plugin-image-native-lazy-loading": "^1.2.0",
6362
"sharp": "0.33.5",
6463
"tailwind-merge": "^2.5.2",
6564
"tailwindcss": "^3.4.10",
6665
"typescript": "^5.5.4",
67-
"typescript-eslint": "^8.2.0"
66+
"typescript-eslint": "^8.2.0",
67+
"unist-util-visit": "^5.0.0"
6868
}
6969
}
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
import getReadingTime from 'reading-time';
22
import { toString } from 'mdast-util-to-string';
3-
import lazyLoadPlugin from 'rehype-plugin-image-native-lazy-loading';
3+
import { visit } from 'unist-util-visit';
4+
import type { MarkdownAstroData, RehypePlugin, RemarkPlugin } from '@astrojs/markdown-remark';
45

5-
export function readingTimeRemarkPlugin() {
6+
export const readingTimeRemarkPlugin: RemarkPlugin = () => {
67
return function (tree, file) {
78
const textOnPage = toString(tree);
89
const readingTime = Math.ceil(getReadingTime(textOnPage).minutes);
910

10-
file.data.astro.frontmatter.readingTime = readingTime;
11+
(file.data.astro as MarkdownAstroData).frontmatter.readingTime = readingTime;
1112
};
12-
}
13+
};
1314

14-
export function responsiveTablesRehypePlugin() {
15+
export const responsiveTablesRehypePlugin: RehypePlugin = () => {
1516
return function (tree) {
1617
if (!tree.children) return;
1718

1819
for (let i = 0; i < tree.children.length; i++) {
1920
const child = tree.children[i];
2021

2122
if (child.type === 'element' && child.tagName === 'table') {
22-
const wrapper = {
23+
tree.children[i] = {
2324
type: 'element',
2425
tagName: 'div',
2526
properties: {
@@ -28,12 +29,20 @@ export function responsiveTablesRehypePlugin() {
2829
children: [child],
2930
};
3031

31-
tree.children[i] = wrapper;
32-
3332
i++;
3433
}
3534
}
3635
};
37-
}
36+
};
37+
38+
export const lazyImagesRehypePlugin: RehypePlugin = () => {
39+
return function (tree) {
40+
if (!tree.children) return;
3841

39-
export const lazyImagesRehypePlugin = lazyLoadPlugin;
42+
visit(tree, 'element', function (node) {
43+
if (node.tagName === 'img') {
44+
node.properties.loading = 'lazy';
45+
}
46+
});
47+
};
48+
};

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
"paths": {
88
"~/*": ["src/*"]
99
}
10-
}
10+
},
11+
"exclude": ["dist/"]
1112
}

vendor/integration/index.mjs vendor/integration/index.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import fs from 'node:fs';
22
import os from 'node:os';
3+
import type { AstroConfig, AstroIntegration } from 'astro';
34

4-
import configBuilder from './utils/configBuilder';
5+
import configBuilder, { type Config } from './utils/configBuilder';
56
import loadConfig from './utils/loadConfig';
67

7-
export default ({ config: _themeConfig = 'src/config.yaml' } = {}) => {
8-
let cfg;
8+
export default ({ config: _themeConfig = 'src/config.yaml' } = {}): AstroIntegration => {
9+
let cfg: AstroConfig;
910
return {
1011
name: 'astrowind-integration',
1112

@@ -24,7 +25,7 @@ export default ({ config: _themeConfig = 'src/config.yaml' } = {}) => {
2425
const virtualModuleId = 'astrowind:config';
2526
const resolvedVirtualModuleId = '\0' + virtualModuleId;
2627

27-
const rawJsonConfig = await loadConfig(_themeConfig);
28+
const rawJsonConfig = (await loadConfig(_themeConfig)) as Config;
2829
const { SITE, I18N, METADATA, APP_BLOG, UI, ANALYTICS } = configBuilder(rawJsonConfig);
2930

3031
updateConfig({
@@ -89,19 +90,19 @@ export default ({ config: _themeConfig = 'src/config.yaml' } = {}) => {
8990
const sitemapExists = fs.existsSync(sitemapFile);
9091

9192
if (hasIntegration && sitemapExists) {
92-
const robotsTxt = fs.readFileSync(robotsTxtFile, { encoding: 'utf8', flags: 'a+' });
93+
const robotsTxt = fs.readFileSync(robotsTxtFile, { encoding: 'utf8', flag: 'a+' });
9394
const sitemapUrl = new URL(sitemapName, String(new URL(cfg.base, cfg.site)));
9495
const pattern = /^Sitemap:(.*)$/m;
9596

9697
if (!pattern.test(robotsTxt)) {
9798
fs.appendFileSync(robotsTxtFileInOut, `${os.EOL}${os.EOL}Sitemap: ${sitemapUrl}`, {
9899
encoding: 'utf8',
99-
flags: 'w',
100+
flag: 'w',
100101
});
101102
} else {
102103
fs.writeFileSync(robotsTxtFileInOut, robotsTxt.replace(pattern, `Sitemap: ${sitemapUrl}`), {
103104
encoding: 'utf8',
104-
flags: 'w',
105+
flag: 'w',
105106
});
106107
}
107108
}

vendor/integration/utils/configBuilder.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import merge from 'lodash.merge';
22

33
import type { MetaData } from '~/types';
44

5-
type Config = {
5+
export type Config = {
66
site?: SiteConfig;
77
metadata?: MetaDataConfig;
88
i18n?: I18NConfig;

0 commit comments

Comments
 (0)