Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to allow for more flexibility and customization options in the future #45

Merged
merged 6 commits into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/studioCMS/src/components/Footer.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
// @ts-expect-error - This is a missing type definition for the `astro:db` import since its a virtual module during Astro Runtime
// @ts-expect-error - Some types can only be imported from the Astro runtime
import { SiteConfig, db } from 'astro:db';

const ConfigArray = await db.select().from(SiteConfig);
Expand Down
6 changes: 3 additions & 3 deletions packages/studioCMS/src/components/Header.astro
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
// @ts-expect-error - This is a missing type definition for the `astro:db` import since its a virtual module during Astro Runtime
// @ts-expect-error - Some types can only be imported from the Astro runtime
import { SiteConfig, db } from 'astro:db';
import Config from 'virtual:astro-studio-cms:config';
import Config from 'virtual:studiocms/config';
import profileImg from '../assets/profile.webp';
import type { Locals } from '../pages/dashboard/locals';
import type { Locals } from '../schemas/locals';
import HeaderLink from './HeaderLink.astro';

const {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
import { Image } from 'astro:assets';
import Config from 'virtual:astro-studio-cms:config';
import Config from 'virtual:studiocms/config';
import { Cloudinary } from '@cloudinary/url-gen';
import { fill } from '@cloudinary/url-gen/actions/resize';
import type { Locals } from '../pages/dashboard/locals';
import type { Locals } from '../../schemas/locals';

interface Props {
src: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
---
import Config from 'virtual:studiocms/config';

const { dateLocale: locales } = Config;

interface Props {
date: Date;
}
Expand All @@ -8,7 +12,7 @@ const { date } = Astro.props;

<time datetime={date.toISOString()}>
{
date.toLocaleDateString('en-us', {
date.toLocaleDateString(locales, {
year: 'numeric',
month: 'short',
day: 'numeric',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
import Config from 'virtual:studiocms/config';
import { renderMarkDoc } from '../../utils/markdoc';
import { renderMarked } from '../../utils/marked';

type Props = {
content: string;
};

const { content } = Astro.props;

function contentRenderer() {
if (Config.contentRenderer === 'markdoc') {
return renderMarkDoc(content);
}
return renderMarked(content);
}
---

<Fragment set:html={contentRenderer()} />
31 changes: 28 additions & 3 deletions packages/studioCMS/src/layouts/BlogIndex.astro
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
// @ts-expect-error - This is a missing type definition for the `astro:db` import since its a virtual module during Astro Runtime
import { SiteConfig, Blog, asc, db } from 'astro:db';
//@ts-expect-error - Some types can only be imported from the Astro runtime
import { SiteConfig, asc, db } from 'astro:db';
import Config from 'virtual:studiocms/config';
import BaseHead from '../components/BaseHead.astro';
import Footer from '../components/Footer.astro';
import Header from '../components/Header.astro';
Expand All @@ -12,9 +13,33 @@ type Props = {
title: string;
description: string;
heroImage?: string;
posts: BlogPost[]
};

const { title, description, heroImage } = Astro.props;
type BlogPost = {
id: number;
title: string;
description: string;
slug: string;
publishedAt: Date;
updatedAt: Date | null;
heroImage: string;
content: string;
}

const { title, description, heroImage, posts } = Astro.props;

if (Config.includedIntegrations.useInoxSitemap) {
import('sitemap-ext:config').then((sitemap) => {
sitemap.default(async ({ addToSitemap }) => {
addToSitemap(
posts.map((post: BlogPost) => ({
slug: post.slug,
}))
);
});
})
}

const pageDescription = `${description} - ${contextConfig.description}`;
const pageTitle = `${title} | ${contextConfig.title}`;
Expand Down
21 changes: 12 additions & 9 deletions packages/studioCMS/src/layouts/BlogPost.astro
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
---
// @ts-expect-error - This is a missing type definition for the `astro:db` import since its a virtual module during Astro Runtime
// @ts-expect-error - Some types can only be imported from the Astro runtime
import { SiteConfig, db } from 'astro:db';
import BaseHead from '../components/BaseHead.astro';
import CImage from '../components/CImage.astro';
import { CImage, FormattedDate } from 'studiocms:components';
import Footer from '../components/Footer.astro';
import FormattedDate from '../components/FormattedDate.astro';
import Header from '../components/Header.astro';

const ConfigArray = await db.select().from(SiteConfig);
const contextConfig = ConfigArray.pop();

type Props = {
title: string;
description: string;
heroImage: string;
publishedAt: Date;
updatedAt?: Date | null;
post: {
title: string;
description: string;
heroImage: string;
publishedAt: Date;
updatedAt?: Date | null;
}
};

const { title, description, publishedAt, updatedAt, heroImage } = Astro.props;
const { post } = Astro.props;
const { title, description, publishedAt, updatedAt, heroImage } = post;

---

<html lang="en">
Expand Down
2 changes: 1 addition & 1 deletion packages/studioCMS/src/layouts/Dashboard.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
// @ts-expect-error - This is a missing type definition for the `astro:db` import since its a virtual module during Astro Runtime
// @ts-expect-error - Some types can only be imported from the Astro runtime
import { SiteConfig, db } from 'astro:db';
import BaseHead from '../components/BaseHead.astro';
import Footer from '../components/Footer.astro';
Expand Down
2 changes: 1 addition & 1 deletion packages/studioCMS/src/layouts/Default.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
// @ts-expect-error - This is a missing type definition for the `astro:db` import since its a virtual module during Astro Runtime
// @ts-expect-error - Some types can only be imported from the Astro runtime
import { SiteConfig, db } from 'astro:db';
import BaseHead from '../components/BaseHead.astro';
import Footer from '../components/Footer.astro';
Expand Down
2 changes: 1 addition & 1 deletion packages/studioCMS/src/lib/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @ts-expect-error - This is a missing type definition for the `astro:db` import since its a virtual module during Astro Runtime
// @ts-expect-error - Some types can only be imported from the Astro runtime
import { db } from 'astro:db';
import { asDrizzleTable } from '@astrojs/db/utils';
import { DrizzleSQLiteAdapter } from '@lucia-auth/adapter-drizzle';
Expand Down
2 changes: 1 addition & 1 deletion packages/studioCMS/src/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @ts-expect-error - This is a missing type definition for the `astro:db` import since its a virtual module during Astro Runtime
// @ts-expect-error - Some types can only be imported from the Astro runtime
import { User, db, eq } from 'astro:db';
import { defineMiddleware } from 'astro/middleware';
import { verifyRequestOrigin } from 'lucia';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
---
// @ts-expect-error - This is a missing type definition for the `astro:db` import since its a virtual module during Astro Runtime
// @ts-expect-error - Some types can only be imported from the Astro runtime
import { Permissions, db, eq } from 'astro:db';
import Dashboard from '../../layouts/Dashboard.astro';
import { rankCheck } from '../../utils/rankcheck';
import type { Locals } from './locals';
import sitemap from 'sitemap-ext:config';
sitemap(false); // opt-out
import Dashboard from '../layouts/Dashboard.astro';
import type { Locals } from '../schemas/locals';
import Config from 'virtual:studiocms/config';
import authHelper from '../utils/authhelper';

if (Config.includedIntegrations.useInoxSitemap) {
import('sitemap-ext:config').then((sitemap) => {
sitemap.default(false)
})
}

function redirectToPath(path: string) {
return Astro.redirect(import.meta.env.BASE_URL + path);
}
const currentUserRank = await rankCheck(Astro.locals as Locals);

if ( currentUserRank === "unknown" ) {
const locals = Astro.locals as Locals;
const { permissionLevel } = await authHelper(locals);

if ( permissionLevel === "unknown" ) {
console.log('User is not logged in. Redirecting to login page.');
return redirectToPath('dashboard/login');
}
if (currentUserRank !== 'admin') {
if (permissionLevel !== 'admin') {
console.log('User is not an admin');
return redirectToPath('dashboard/profile');
}
Expand Down Expand Up @@ -57,7 +64,7 @@ const adminList = await db.select().from(Permissions);
<h4>Current Admins:</h4>
<ul>
{
adminList.map((admin: Permissions.$inferSelect) => {
adminList.map((admin: typeof Permissions.$inferSelect) => {
return (
<li>{admin.username} - {admin.rank}</li>
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
---
// @ts-expect-error - This is a missing type definition for the `astro:db` import since its a virtual module during Astro Runtime
// @ts-expect-error - Some types can only be imported from the Astro runtime
import { Blog, db, eq } from 'astro:db';
import Dashboard from '../../../layouts/Dashboard.astro';
import { rankCheck } from '../../../utils/rankcheck';
import type { Locals } from '../locals';
import sitemap from 'sitemap-ext:config';
sitemap(false); // opt-out
import Dashboard from '../../layouts/Dashboard.astro';
import type { Locals } from '../../schemas/locals';
import Config from 'virtual:studiocms/config';
import authHelper from '../../utils/authhelper';

if (Config.includedIntegrations.useInoxSitemap) {
import('sitemap-ext:config').then((sitemap) => {
sitemap.default(false)
})
}

function redirectToPath(path: string) {
return Astro.redirect(import.meta.env.BASE_URL + path);
Expand All @@ -20,19 +25,18 @@ if (!slug || !post) {
}

const locals = Astro.locals as Locals;
const currentUserRank = await rankCheck(locals);
const { permissionLevel, name } = await authHelper(locals);

// If the user is not logged in, redirect to the login page
if ( currentUserRank === "unknown" ) {
if ( permissionLevel === "unknown" ) {
console.log('User is not logged in. Redirecting to login page.');
return redirectToPath('dashboard/login');
}
if ( currentUserRank !== 'admin' && currentUserRank !== 'editor' ) {
if ( permissionLevel !== 'admin' && permissionLevel !== 'editor' ) {
console.log('User is not an admin or editor. Redirecting to profile page.');
return redirectToPath('dashboard/profile');
}

const getUserName = locals.dbUser.name ? locals.dbUser.name : locals.user.username;
if (Astro.request.method === 'POST') {
const formData = await new Request(Astro.request.url, {
method: Astro.request.method,
Expand Down Expand Up @@ -64,7 +68,7 @@ if (Astro.request.method === 'POST') {
</center>
<div>
<p class="font-small">
Currently logged in as: <bold>{getUserName}</bold>
Currently logged in as: <bold>{name}</bold>
</p>

<form method="post" enctype="multipart/form-data" >
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
---
// @ts-expect-error - This is a missing type definition for the `astro:db` import since its a virtual module during Astro Runtime
// @ts-expect-error - Some types can only be imported from the Astro runtime
import { Blog, db, eq } from 'astro:db';
import Dashboard from '../../../layouts/Dashboard.astro';
import { rankCheck } from '../../../utils/rankcheck';
import type { Locals } from '../locals';
import sitemap from 'sitemap-ext:config';
sitemap(false); // opt-out
import Dashboard from '../../layouts/Dashboard.astro';
import type { Locals } from '../../schemas/locals';
import Config from 'virtual:studiocms/config';
import authHelper from '../../utils/authhelper';

if (Config.includedIntegrations.useInoxSitemap) {
import('sitemap-ext:config').then((sitemap) => {
sitemap.default(false)
})
}

function redirectToPath(path: string) {
return Astro.redirect(import.meta.env.BASE_URL + path);
Expand All @@ -20,21 +25,18 @@ if (!slug || !post) {
}

const locals = Astro.locals as Locals;

const currentUserRank = await rankCheck(locals);
const { permissionLevel, name } = await authHelper(locals);

// If the user is not logged in, redirect to the login page
if ( currentUserRank === "unknown" ) {
if ( permissionLevel === "unknown" ) {
console.log('User is not logged in. Redirecting to login page.');
return redirectToPath('dashboard/login');
}
if ( currentUserRank !== 'admin' && currentUserRank !== 'editor' ) {
if ( permissionLevel !== 'admin' && permissionLevel !== 'editor' ) {
console.log('User is not an admin or editor. Redirecting to profile page.');
return redirectToPath('dashboard/profile');
}

const getUserName = locals.dbUser.name ? locals.dbUser.name : locals.user.username;

if (Astro.request.method === 'POST') {
const formData = await new Request(Astro.request.url, {
method: Astro.request.method,
Expand Down Expand Up @@ -71,7 +73,7 @@ if (Astro.request.method === 'POST') {
<Dashboard title='Edit Post'>
<div>
<p class="font-small">
Currently logged in as: <bold>{getUserName}</bold>
Currently logged in as: <bold>{name}</bold>
</p>

<form method="post" enctype="multipart/form-data" >
Expand Down Expand Up @@ -131,19 +133,24 @@ if (Astro.request.method === 'POST') {
</Dashboard>

<script>
import { markdown } from "../../../utils/marked";
import Config from 'virtual:astro-studio-cms:config';
import { renderMarkDoc } from '../../../utils/markdoc';
import Config from 'virtual:studiocms/config';
import { renderMarked } from "../../utils/marked";
import { renderMarkDoc } from '../../utils/markdoc';

//@ts-ignore
document.getElementById('content').addEventListener('input', async function () {
const markdownContent = document.getElementById('content');
const htmlPreview = document.getElementById('html-preview');

if (Config.contentRenderer === 'marked' ) {
const htmlContent = await markdown(markdownContent.value);
//@ts-ignore
const htmlContent = await renderMarked(markdownContent.value);
//@ts-ignore
htmlPreview.innerHTML = htmlContent;
} else if (Config.contentRenderer === 'markdoc') {
//@ts-ignore
const htmlContent = await renderMarkDoc(markdownContent.value);
//@ts-ignore
Comment on lines +146 to +153
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the issue with ts here?

htmlPreview.innerHTML = htmlContent;
}
});
Expand Down
Loading