Skip to content

Commit

Permalink
refactor!: bring in consistent naming
Browse files Browse the repository at this point in the history
  • Loading branch information
theisel committed Sep 24, 2022
1 parent 11ffafc commit 9753535
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 40 deletions.
35 changes: 35 additions & 0 deletions .changeset/warm-trees-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
"astro-portabletext": minor
---

Brings in consistent naming, which reflects **Portable Text** node types.


**_T_ NodeType**

```diff
- type NodeType = "block" | "blockStyle" | "listStyle" | "listItemStyle" | "mark";
+ type NodeType = "type" | "block" | "list" | "listItem" | "mark";
```

**_T_ PortableTextComponents**

| Property | Counterpart |
| :--- | :--- |
| type | unknownType |
| block | ~~unknownBlockStyle~~ unknownBlock |
| list | unknownList |
| listItem | unknownListItem |
| mark | unknownMark |
| hardBreak | |

**PortableText**

```diff
<PortableText
components={{
- unknownBlockStyle: /* custom component */;
+ unknownBlock: /* custom component */;
}}
>
```
4 changes: 2 additions & 2 deletions component/components/Block.astro
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
---
import type { BlockStyle, Props as $ } from "../lib/types";
import { throwError } from "../lib/internal";
import { unknownBlockStyleWarning } from "../lib/warnings";
import { unknownBlockWarning } from "../lib/warnings";
export type Props = $<BlockStyle>;
const { node, index, isInline, ...attrs } = Astro.props;
const styleIs = (style: string) => style === node.style;
const err = unknownBlockStyleWarning(node.style);
const err = unknownBlockWarning(node.style);
---

{
Expand Down
32 changes: 16 additions & 16 deletions component/components/PortableText.astro
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import {
printWarning,
unknownTypeWarning,
unknownMarkWarning,
unknownBlockStyleWarning,
unknownListStyleWarning,
unknownListItemStyleWarning,
unknownBlockWarning,
unknownListWarning,
unknownListItemWarning,
} from "../lib/warnings";
import NodeRenderer from "./NodeRenderer.astro";
Expand All @@ -40,7 +40,7 @@ import List from "./List.astro";
import ListItem from "./ListItem.astro";
import Raw from "./Raw.astro";
import Mark from "./Mark.astro";
import UnknownBlockStyle from "./UnknownBlockStyle.astro";
import UnknownBlock from "./UnknownBlock.astro";
import UnknownList from "./UnknownList.astro";
import UnknownListItem from "./UnknownListItem.astro";
import UnknownMark from "./UnknownMark.astro";
Expand Down Expand Up @@ -70,7 +70,7 @@ const components = mergeComponents(
blockquote: Block,
normal: Block,
},
unknownBlockStyle: UnknownBlockStyle,
unknownBlock: UnknownBlock,
list: {
bullet: List,
number: List,
Expand Down Expand Up @@ -100,36 +100,36 @@ const missingComponentHandler = onMissingComponent || noop;
const emitter = mitt<{
unknownType: string;
unknownBlockStyle: string;
unknownBlock: string;
unknownList: string;
unknownListItem: string;
unknownMark: string;
}>();
emitter.on("unknownType", (type: string) => {
missingComponentHandler(unknownTypeWarning(type), {
nodeType: "block",
nodeType: "type",
type: type,
});
});
emitter.on("unknownBlockStyle", (style: string) => {
missingComponentHandler(unknownBlockStyleWarning(style), {
nodeType: "blockStyle",
emitter.on("unknownBlock", (style: string) => {
missingComponentHandler(unknownBlockWarning(style), {
nodeType: "block",
type: style,
});
});
emitter.on("unknownList", (listItem: string) => {
missingComponentHandler(unknownListStyleWarning(listItem), {
nodeType: "listStyle",
missingComponentHandler(unknownListWarning(listItem), {
nodeType: "list",
type: listItem,
});
});
emitter.on("unknownListItem", (listItem: string) => {
missingComponentHandler(unknownListItemStyleWarning(listItem), {
nodeType: "listItemStyle",
missingComponentHandler(unknownListItemWarning(listItem), {
nodeType: "listItem",
type: listItem,
});
});
Expand All @@ -145,7 +145,7 @@ const provideComponent = (
type: "type" | "block" | "list" | "listItem" | "mark",
fallback:
| "unknownType"
| "unknownBlockStyle"
| "unknownBlock"
| "unknownList"
| "unknownListItem"
| "unknownMark",
Expand Down Expand Up @@ -238,7 +238,7 @@ const asComponentProps = (
<NodeRenderer
component={provideComponent(
"block",
"unknownBlockStyle",
"unknownBlock",
node.style ?? "normal"
)}
node={{ style: "normal", ...node }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import type { Props as $, BlockStyle } from "../lib/types";
export type Props = $<BlockStyle>;
---

<p data-portabletext-unknown="blockstyle"><slot /></p>
<p data-portabletext-unknown="block"><slot /></p>
2 changes: 1 addition & 1 deletion component/docs/portabletext-component.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Override/extend the default components.
unknownType: Component,
// block style
block: { [style]: Component } | Component,
unknownBlockStyle: Component,
unknownBlock: Component,
// list
list: { [listItem]: Component } | Component,
unknownList: Component,
Expand Down
7 changes: 1 addition & 6 deletions component/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,4 @@ export type ComponentOrRecord<N extends TypedObject = ArbitraryTypedObject> =
/**
* @internal
*/
export type NodeType =
| "block"
| "blockStyle"
| "listStyle"
| "listItemStyle"
| "mark";
export type NodeType = "type" | "block" | "list" | "listItem" | "mark";
2 changes: 1 addition & 1 deletion component/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export interface PortableTextComponents {
/**
* Used when a `block` handler isn't found
*/
unknownBlockStyle: Component<BlockStyle> | Component<Block>;
unknownBlock: Component<BlockStyle> | Component<Block>;
/**
* How lists should be rendered
*/
Expand Down
6 changes: 3 additions & 3 deletions component/src/warnings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ export const unknownTypeWarning = (typeName: string): string =>
export const unknownMarkWarning = (markType: string): string =>
getTemplate(`mark type "${markType}"`, "mark");

export const unknownBlockStyleWarning = (blockStyle: string): string =>
export const unknownBlockWarning = (blockStyle: string): string =>
getTemplate(`block style "${blockStyle}"`, "block");

export const unknownListStyleWarning = (listStyle: string): string =>
export const unknownListWarning = (listStyle: string): string =>
getTemplate(`list style "${listStyle}"`, "list");

export const unknownListItemStyleWarning = (listStyle: string): string =>
export const unknownListItemWarning = (listStyle: string): string =>
getTemplate(`list item style "${listStyle}"`, "listItem");

export function printWarning(message: string): void {
Expand Down
2 changes: 1 addition & 1 deletion component/test/components/block.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ block("unknown", async () => {
const $el = $("[data-portabletext-unknown]");

assert.is($el.length, 1);
assert.is($el.attr("data-portabletext-unknown"), "blockstyle");
assert.is($el.attr("data-portabletext-unknown"), "block");
assert.is($el[0].name, "p");
});

Expand Down
18 changes: 9 additions & 9 deletions component/test/lib/warnings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import * as assert from "uvu/assert";
import {
unknownTypeWarning,
unknownMarkWarning,
unknownListStyleWarning,
unknownListItemStyleWarning,
unknownBlockStyleWarning,
unknownListWarning,
unknownListItemWarning,
unknownBlockWarning,
} from "../../src/warnings";

test("unknownTypeWarning", () => {
Expand All @@ -16,16 +16,16 @@ test("unknownMarkWarning", () => {
assert.type(unknownMarkWarning("em"), "string");
});

test("unknownListStyleWarning", () => {
assert.type(unknownListStyleWarning("bullet"), "string");
test("unknownListWarning", () => {
assert.type(unknownListWarning("bullet"), "string");
});

test("unknownListItemStyleWarning", () => {
assert.type(unknownListItemStyleWarning("bullet"), "string");
test("unknownListItemWarning", () => {
assert.type(unknownListItemWarning("bullet"), "string");
});

test("unknownBlockStyleWarning", () => {
assert.type(unknownBlockStyleWarning("normal"), "string");
test("unknownBlockWarning", () => {
assert.type(unknownBlockWarning("normal"), "string");
});

test.run();

0 comments on commit 9753535

Please sign in to comment.