Skip to content

Commit

Permalink
Migrate to astro v2
Browse files Browse the repository at this point in the history
  • Loading branch information
prototypa committed Jan 21, 2023
1 parent b575a89 commit 09e827c
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ pnpm-debug.log*
package-lock.json
pnpm-lock.yaml

src/content/types.generated.d.ts
.astro
5 changes: 0 additions & 5 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ export default defineConfig({

markdown: {
remarkPlugins: [remarkReadingTime],
extendDefaultPlugins: true,
},

vite: {
Expand All @@ -68,8 +67,4 @@ export default defineConfig({
},
},
},

experimental: {
contentCollections: true,
},
});
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@onwidget/astrowind",
"description": "A template to make your website using Astro + Tailwind CSS.",
"version": "0.9.7",
"version": "0.9.8",
"private": true,
"scripts": {
"dev": "astro dev",
Expand All @@ -14,23 +14,23 @@
"subfont": "subfont -ir --no-fallbacks --silent --root dist"
},
"devDependencies": {
"@astrojs/image": "^0.12.1",
"@astrojs/mdx": "^0.14.0",
"@astrojs/partytown": "^1.0.2",
"@astrojs/image": "^1.0.0-beta.2",
"@astrojs/mdx": "^1.0.0-beta.2",
"@astrojs/partytown": "^1.0.3-beta.0",
"@astrojs/rss": "^2.0.0",
"@astrojs/sitemap": "^1.0.0",
"@astrojs/tailwind": "^2.1.3",
"@astrojs/tailwind": "^3.0.0-beta.1",
"@astrolib/analytics": "^0.3.0",
"@astrolib/seo": "^0.3.0",
"@fontsource/inter": "^4.5.15",
"@tailwindcss/typography": "^0.5.9",
"@typescript-eslint/eslint-plugin": "^5.48.2",
"@typescript-eslint/parser": "^5.48.2",
"astro": "^1.9.2",
"astro-compress": "1.1.27",
"astro": "^2.0.0-beta.3",
"astro-compress": "1.1.28",
"astro-icon": "^0.8.0",
"eslint": "^8.32.0",
"eslint-plugin-astro": "^0.22.0",
"eslint-plugin-astro": "^0.23.0",
"eslint-plugin-jsx-a11y": "^6.7.1",
"limax": "^2.1.0",
"mdast-util-to-string": "^3.1.0",
Expand Down
2 changes: 1 addition & 1 deletion public/_headers
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/assets/*
/_astro/*
Cache-Control: public, max-age=31536000, immutable
4 changes: 2 additions & 2 deletions src/content/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { z, defineCollection } from 'astro:content';

const blog = defineCollection({
schema: {
schema: z.object({
title: z.string(),
description: z.string().optional(),
image: z.string().optional(),
Expand All @@ -16,7 +16,7 @@ const blog = defineCollection({
category: z.string().optional(),
tags: z.array(z.string()).optional(),
author: z.string().optional(),
},
}),
slug: ({ defaultSlug, data }) => {
return data.permalink || defaultSlug;
},
Expand Down
1 change: 1 addition & 0 deletions src/env.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/// <reference path="../.astro/types.d.ts" />
/// <reference types="@astrojs/image/client" />
20 changes: 11 additions & 9 deletions src/utils/blog.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { getCollection, getEntry } from 'astro:content';
import { getCollection } from 'astro:content';
import type { CollectionEntry } from 'astro:content';
import type { Post } from '~/types';
import { cleanSlug } from './permalinks';

const getNormalizedPost = async (post: CollectionEntry<'blog'>): Promise<Post> => {
const { id, slug = '', data } = post;
const { Content, injectedFrontmatter } = await post.render();
const { Content, remarkPluginFrontmatter } = await post.render();

const { tags = [], category = 'default', author = 'Anonymous', publishDate = new Date(), ...rest } = data;

Expand All @@ -23,7 +23,7 @@ const getNormalizedPost = async (post: CollectionEntry<'blog'>): Promise<Post> =
Content: Content,
// or 'body' in case you consume from API

readingTime: injectedFrontmatter.readingTime,
readingTime: remarkPluginFrontmatter?.readingTime,
};
};

Expand Down Expand Up @@ -67,12 +67,14 @@ export const findPostsBySlugs = async (slugs: Array<string>): Promise<Array<Post
export const findPostsByIds = async (ids: Array<string>): Promise<Array<Post>> => {
if (!Array.isArray(ids)) return [];

return await Promise.all(
ids.map(async (id: never) => {
const post = await getEntry('blog', id);
return await getNormalizedPost(post);
})
);
const posts = await fetchPosts();

return ids.reduce(function (r: Array<Post>, id: string) {
posts.some(function (post: Post) {
return id === post.id && r.push(post);
});
return r;
}, []);
};

/** */
Expand Down
5 changes: 3 additions & 2 deletions src/utils/frontmatter.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import getReadingTime from 'reading-time';
import { toString } from 'mdast-util-to-string';

export function remarkReadingTime() {
return function (tree, { data }) {
return function (tree, file) {
const text = toString(tree);
const readingTime = Math.ceil(getReadingTime(text).minutes);

data.astro.frontmatter.readingTime = readingTime;
const { frontmatter } = file.data.astro;
frontmatter.readingTime = readingTime;
};
}
2 changes: 1 addition & 1 deletion vercel.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"trailingSlash": false,
"headers": [
{
"source": "/assets/(.*)",
"source": "/_astro/(.*)",
"headers": [
{
"key": "Cache-Control",
Expand Down

0 comments on commit 09e827c

Please sign in to comment.