Skip to content

Commit

Permalink
Merge pull request #501 from phanect/feat-typescriptify
Browse files Browse the repository at this point in the history
Convert JavaScript files to TypeScript
  • Loading branch information
prototypa committed Aug 29, 2024
2 parents 18add82 + d0299e9 commit c047ce6
Show file tree
Hide file tree
Showing 8 changed files with 1,927 additions and 955 deletions.
9 changes: 3 additions & 6 deletions astro.config.mjs → astro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,16 @@ import mdx from '@astrojs/mdx';
import partytown from '@astrojs/partytown';
import icon from 'astro-icon';
import compress from 'astro-compress';
import type { AstroIntegration } from 'astro';

import astrowind from './vendor/integration';

import {
readingTimeRemarkPlugin,
responsiveTablesRehypePlugin,
lazyImagesRehypePlugin,
} from './src/utils/frontmatter.mjs';
import { readingTimeRemarkPlugin, responsiveTablesRehypePlugin, lazyImagesRehypePlugin } from './src/utils/frontmatter';

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

const hasExternalScripts = false;
const whenExternalScripts = (items = []) =>
const whenExternalScripts = (items: (() => AstroIntegration) | (() => AstroIntegration)[] = []) =>
hasExternalScripts ? (Array.isArray(items) ? items.map((item) => item()) : [items()]) : [];

export default defineConfig({
Expand Down
2,820 changes: 1,892 additions & 928 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@
"prettier": "^3.3.3",
"prettier-plugin-astro": "^0.14.1",
"reading-time": "^1.5.0",
"rehype-plugin-image-native-lazy-loading": "^1.2.0",
"sharp": "0.33.5",
"tailwind-merge": "^2.5.2",
"tailwindcss": "^3.4.10",
"typescript": "^5.5.4",
"typescript-eslint": "^8.2.0"
"typescript-eslint": "^8.2.0",
"unist-util-visit": "^5.0.0"
}
}
File renamed without changes.
29 changes: 19 additions & 10 deletions src/utils/frontmatter.mjs → src/utils/frontmatter.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import getReadingTime from 'reading-time';
import { toString } from 'mdast-util-to-string';
import lazyLoadPlugin from 'rehype-plugin-image-native-lazy-loading';
import { visit } from 'unist-util-visit';
import type { MarkdownAstroData, RehypePlugin, RemarkPlugin } from '@astrojs/markdown-remark';

export function readingTimeRemarkPlugin() {
export const readingTimeRemarkPlugin: RemarkPlugin = () => {
return function (tree, file) {
const textOnPage = toString(tree);
const readingTime = Math.ceil(getReadingTime(textOnPage).minutes);

file.data.astro.frontmatter.readingTime = readingTime;
(file.data.astro as MarkdownAstroData).frontmatter.readingTime = readingTime;
};
}
};

export function responsiveTablesRehypePlugin() {
export const responsiveTablesRehypePlugin: RehypePlugin = () => {
return function (tree) {
if (!tree.children) return;

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

if (child.type === 'element' && child.tagName === 'table') {
const wrapper = {
tree.children[i] = {
type: 'element',
tagName: 'div',
properties: {
Expand All @@ -28,12 +29,20 @@ export function responsiveTablesRehypePlugin() {
children: [child],
};

tree.children[i] = wrapper;

i++;
}
}
};
}
};

export const lazyImagesRehypePlugin: RehypePlugin = () => {
return function (tree) {
if (!tree.children) return;

export const lazyImagesRehypePlugin = lazyLoadPlugin;
visit(tree, 'element', function (node) {
if (node.tagName === 'img') {
node.properties.loading = 'lazy';
}
});
};
};
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"paths": {
"~/*": ["src/*"]
}
}
},
"exclude": ["dist/"]
}
15 changes: 8 additions & 7 deletions vendor/integration/index.mjs → vendor/integration/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import fs from 'node:fs';
import os from 'node:os';
import type { AstroConfig, AstroIntegration } from 'astro';

import configBuilder from './utils/configBuilder';
import configBuilder, { type Config } from './utils/configBuilder';
import loadConfig from './utils/loadConfig';

export default ({ config: _themeConfig = 'src/config.yaml' } = {}) => {
let cfg;
export default ({ config: _themeConfig = 'src/config.yaml' } = {}): AstroIntegration => {
let cfg: AstroConfig;
return {
name: 'astrowind-integration',

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

const rawJsonConfig = await loadConfig(_themeConfig);
const rawJsonConfig = (await loadConfig(_themeConfig)) as Config;
const { SITE, I18N, METADATA, APP_BLOG, UI, ANALYTICS } = configBuilder(rawJsonConfig);

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

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

if (!pattern.test(robotsTxt)) {
fs.appendFileSync(robotsTxtFileInOut, `${os.EOL}${os.EOL}Sitemap: ${sitemapUrl}`, {
encoding: 'utf8',
flags: 'w',
flag: 'w',
});
} else {
fs.writeFileSync(robotsTxtFileInOut, robotsTxt.replace(pattern, `Sitemap: ${sitemapUrl}`), {
encoding: 'utf8',
flags: 'w',
flag: 'w',
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion vendor/integration/utils/configBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import merge from 'lodash.merge';

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

type Config = {
export type Config = {
site?: SiteConfig;
metadata?: MetaDataConfig;
i18n?: I18NConfig;
Expand Down

0 comments on commit c047ce6

Please sign in to comment.