Skip to content

Commit

Permalink
feat: components to throw error
Browse files Browse the repository at this point in the history
  • Loading branch information
theisel committed Jul 20, 2022
1 parent 74574c4 commit 7c52ca2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/kind-hornets-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro-portabletext": minor
---

Feat: Unhandled `style` in `Block` component and unhandled `markType` in `Mark` component now throws an error
10 changes: 6 additions & 4 deletions component/lib/components/Block.astro
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
---
import type { PtBlockComponentProps } from "../types";
import UnknownBlockStyle from "./UnknownBlockStyle.astro";
import type { BlockStyle, Props } from "../types";
import { throwError } from "../internal";
import { unknownBlockStyleWarning } from "../warnings";
const { node, astroClass, ...props } = Astro.props as PtBlockComponentProps;
const { node, astroClass } = Astro.props as Props<BlockStyle>;
const attrs = { class: astroClass };
const styleIs = (style: string) => style === node.style;
const err = unknownBlockStyleWarning(node.style);
---

{styleIs("h1") ? (
Expand All @@ -24,5 +26,5 @@ const styleIs = (style: string) => style === node.style;
) : styleIs("normal") ? (
<p {...attrs}><slot /></p>
) : (
<UnknownBlockStyle node={node} astroClass={astroClass} {...props} />
throwError(err)
)}
11 changes: 7 additions & 4 deletions component/lib/components/Mark.astro
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
---
import UnknownMark from "./UnknownMark.astro";
import { Mark, Props } from "../types";
import { throwError } from "../internal";
import { unknownMarkWarning } from "../warnings";
const { node, astroClass, ...props } = Astro.props;
const { node, astroClass } = Astro.props as Props<Mark>;
const attrs = { class: astroClass };
const markTypeIs = (markType: string) => markType === node.markType;
const err = unknownMarkWarning(node.markType);
---

{markTypeIs("code") ? (
<code {...attrs}><slot /></code>
) : markTypeIs("em") ? (
<em {...attrs}><slot /></em>
) : markTypeIs("link") ? (
<a href={node.markDef?.href} {...attrs}><slot /></a>
<a href={(node as unknown as Mark<{ href: string }>).markDef.href} {...attrs}><slot /></a>
) : markTypeIs("strike-through") ? (
<del {...attrs}><slot /></del>
) : markTypeIs("strong") ? (
<strong {...attrs}><slot /></strong>
) : markTypeIs("underline") ? (
<span style="text-decoration: underline;" {...attrs}><slot /></span>
) : (
<UnknownMark node={node} astroClass={astroClass} {...props} />
throwError(err)
)}

0 comments on commit 7c52ca2

Please sign in to comment.