Skip to content

Commit

Permalink
fix: fix TS errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasmerlin committed Mar 23, 2023
1 parent 9433e11 commit fee197a
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 73 deletions.
17 changes: 9 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"scripts": {
"cypress:open": "cypress open",
"dev": "astro dev",
"build": "astro build",
"build": "astro check && tsc --noEmit && astro build",
"start": "astro preview",
"format": "prettier -w ./src",
"check:format": "prettier -c ./src",
Expand All @@ -38,6 +38,7 @@
"cypress": "^12.8.1",
"prettier": "^2.8.6",
"prettier-plugin-astro": "^0.8.0",
"standard-version": "^9.5.0"
"standard-version": "^9.5.0",
"typescript": "^5.0.2"
}
}
101 changes: 44 additions & 57 deletions src/SEO.astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import OpenGraphOptionalTags from "./components/OpenGraphOptionalTags.astro";
import ExtendedTags from "./components/ExtendedTags.astro";
import TwitterTags from "./components/TwitterTags.astro";
export type TwitterCardType =
| "summary"
| "summary_large_image"
| "app"
| "player";
export type TwitterCardType = "summary" | "summary_large_image" | "app" | "player";
export interface Link extends HTMLLinkElement {
prefetch: boolean;
Expand Down Expand Up @@ -79,86 +75,77 @@ export interface Props {
surpressWarnings?: boolean;
}
const { props } = Astro;
const {
title,
titleTemplate,
titleDefault,
description,
canonical,
noindex,
nofollow,
charset,
surpressWarnings,
} = props;
function validateProps(props) {
const { openGraph, description } = props;
if (openGraph) {
Astro.props.surpressWarnings = true;
function validateProps(props: Props) {
if (props.openGraph) {
if (
!openGraph.basic ||
openGraph.basic.title == null ||
openGraph.basic.type == null ||
openGraph.basic.image == null
!props.openGraph.basic ||
props.openGraph.basic.title == null ||
props.openGraph.basic.type == null ||
props.openGraph.basic.image == null
) {
throw new Error(
"If you pass the openGraph prop, you have to at least define the title, type, and image basic properties!"
);
}
}
if (title && openGraph?.basic.title) {
if (title == openGraph.basic.title) {
if (!surpressWarnings) {
console.warn(
"WARNING(astro-seo): You passed the same value to `title` and `openGraph.optional.title`. This is most likely not what you want. See docs for more."
);
}
}
}
if (openGraph?.basic?.image && !openGraph?.image?.alt) {
if (!surpressWarnings) {
if (props.title && props.openGraph?.basic.title) {
if (props.title == props.openGraph.basic.title && !props.surpressWarnings) {
console.warn(
"WARNING(astro-seo): You defined `openGraph.basic.image`, but didn't define `openGraph.image.alt`. This is stongly discouraged.'"
"WARNING(astro-seo): You passed the same value to `title` and `openGraph.optional.title`. This is most likely not what you want. See docs for more."
);
}
}
if (
props.openGraph?.basic?.image &&
!props.openGraph?.image?.alt &&
!props.surpressWarnings
) {
console.warn(
"WARNING(astro-seo): You defined `openGraph.basic.image`, but didn't define `openGraph.image.alt`. This is stongly discouraged.'"
);
}
}
validateProps(props);
validateProps(Astro.props);
let updatedTitle = "";
if (title) {
updatedTitle = title;
if (titleTemplate) {
updatedTitle = titleTemplate.replace(/%s/g, updatedTitle);
if (Astro.props.title) {
updatedTitle = Astro.props.title;
if (Astro.props.titleTemplate) {
updatedTitle = Astro.props.titleTemplate.replace(/%s/g, updatedTitle);
}
} else if (titleDefault) {
updatedTitle = titleDefault;
} else if (Astro.props.titleDefault) {
updatedTitle = Astro.props.titleDefault;
}
---

{updatedTitle ? <title set:html={updatedTitle} /> : null}

{charset ? <meta charset={charset} /> : null}
{Astro.props.charset ? <meta charset={Astro.props.charset} /> : null}

<link rel="canonical" href={canonical || Astro.url.href} />
<link rel="canonical" href={Astro.props.canonical || Astro.url.href} />

{description ? <meta name="description" content={description} /> : null}
{
Astro.props.description ? (
<meta name="description" content={Astro.props.description} />
) : null
}

<meta
name="robots"
content={`${noindex ? "noindex" : "index"}, ${
nofollow ? "nofollow" : "follow"
content={`${Astro.props.noindex ? "noindex" : "index"}, ${
Astro.props.nofollow ? "nofollow" : "follow"
}`}
/>

{props.openGraph && <OpenGraphBasicTags {...props} />}
{props.openGraph?.optional && <OpenGraphOptionalTags {...props} />}
{props.openGraph?.image && <OpenGraphImageTags {...props} />}
{props.openGraph?.article && <OpenGraphArticleTags {...props} />}
{props.twitter && <TwitterTags {...props} />}
{props.extend && <ExtendedTags {...props} />}
{Astro.props.openGraph && <OpenGraphBasicTags {...Astro.props} />}
{Astro.props.openGraph?.optional && <OpenGraphOptionalTags {...Astro.props} />}
{Astro.props.openGraph?.image && <OpenGraphImageTags {...Astro.props} />}
{Astro.props.openGraph?.article && <OpenGraphArticleTags {...Astro.props} />}
{Astro.props.twitter && <TwitterTags {...Astro.props} />}
{Astro.props.extend && <ExtendedTags {...Astro.props} />}
10 changes: 9 additions & 1 deletion src/components/ExtendedTags.astro
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
---
import type { HTMLAttributes } from "astro/types";
const { props } = Astro;
type Link = HTMLAttributes<"link">;
---

{props.extend.link?.map((attributes) => <link {...attributes} />)}
{
props.extend.link?.map((attributes: Link) => (
<link {...attributes} />
))
}
{
props.extend.meta?.map(({ content, httpEquiv, name, property }) => (
<meta
Expand Down
4 changes: 2 additions & 2 deletions src/components/OpenGraphArticleTags.astro
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ const { publishedTime, modifiedTime, expirationTime, authors, section, tags } =
}
{
authors
? authors.map((author) => (
? authors.map((author: string) => (
<meta property="article:author" content={author} />
))
: null
}
{section ? <meta property="article:section" content={section} /> : null}
{tags ? tags.map((tag) => <meta property="article:tag" content={tag} />) : null}
{tags ? tags.map((tag: string) => <meta property="article:tag" content={tag} />) : null}
2 changes: 1 addition & 1 deletion src/components/OpenGraphImageTags.astro
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
const { image } = Astro.props.openGraph.basic;
const { url, secureUrl, type, width, height, alt } =
const { secureUrl, type, width, height, alt } =
Astro.props.openGraph.image;
---

Expand Down
2 changes: 1 addition & 1 deletion src/components/OpenGraphOptionalTags.astro
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const { optional } = Astro.props.openGraph;
) : null
}
{
optional.localeAlternate?.map((locale) => (
optional.localeAlternate?.map((locale: string) => (
<meta property="og:locale:alternate" content={locale} />
))
}
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-expect-error
export { default as SEO } from "./SEO.astro";

// @ts-expect-error
export * from "./SEO.astro";
5 changes: 4 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"compilerOptions": {
"importsNotUsedAsValues": "error",
}
}

0 comments on commit fee197a

Please sign in to comment.