Skip to content

Commit

Permalink
chore(style): [ci] format
Browse files Browse the repository at this point in the history
  • Loading branch information
theisel authored and github-actions[bot] committed Oct 29, 2022
1 parent 7e04af9 commit 8300fe6
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 53 deletions.
135 changes: 83 additions & 52 deletions astro-portabletext/components/PortableText.astro
Original file line number Diff line number Diff line change
Expand Up @@ -139,74 +139,105 @@ const prepareForRender = (props: ComponentProps) => {
const { node } = props;
const $: (
nodeSpec: {nodeType: string, type: string | null},
nodeSpec: { nodeType: string; type: string | null },
component: Component | string,
children?: ComponentProps[],
) => [typeof component, ComponentProps[]] = (nodeSpec, component, children) => {
children?: ComponentProps[]
) => [typeof component, ComponentProps[]] = (
nodeSpec,
component,
children
) => {
Object.defineProperty(props.node, nodeTypeRef, {
value: nodeSpec,
});
return [ component, children ?? [] ];
return [component, children ?? []];
};
return isPortableTextToolkitList(node)
? use({nodeType: "list" as keyof typeof components, type: node.listItem}, (spec) => {
return $(
spec,
provideComponent(spec.nodeType, spec.type) ?? components.unknownList,
serializeChildren(node, false)
? use(
{ nodeType: "list" as keyof typeof components, type: node.listItem },
(spec) => {
return $(
spec,
provideComponent(spec.nodeType, spec.type) ??
components.unknownList,
serializeChildren(node, false)
);
}
)
})
: isPortableTextListItemBlock(node)
? use({nodeType: "listItem" as keyof typeof components, type: node.listItem}, (spec) => {
return $(
spec,
provideComponent(spec.nodeType, spec.type) ??
components.unknownListItem,
serializeMarksTree(node).map((children) => {
if (node.style !== "normal") {
const { listItem, ...blockNode } = node;
children = serializeNode(false)(blockNode, 0);
}
return children;
})
)
})
? use(
{
nodeType: "listItem" as keyof typeof components,
type: node.listItem,
},
(spec) => {
return $(
spec,
provideComponent(spec.nodeType, spec.type) ??
components.unknownListItem,
serializeMarksTree(node).map((children) => {
if (node.style !== "normal") {
const { listItem, ...blockNode } = node;
children = serializeNode(false)(blockNode, 0);
}
return children;
})
);
}
)
: isPortableTextToolkitSpan(node)
? use({nodeType: "mark" as keyof typeof components, type: node.markType}, (spec) => {
return $(
spec,
provideComponent(spec.nodeType, spec.type) ?? components.unknownMark,
serializeChildren(node, true)
? use(
{ nodeType: "mark" as keyof typeof components, type: node.markType },
(spec) => {
return $(
spec,
provideComponent(spec.nodeType, spec.type) ??
components.unknownMark,
serializeChildren(node, true)
);
}
)
})
: isPortableTextBlock(node)
? use({nodeType: "block" as keyof typeof components, type: node.style ?? "normal"}, (spec) => {
props.node = { style: spec.type, ...node };
? use(
{
nodeType: "block" as keyof typeof components,
type: node.style ?? "normal",
},
(spec) => {
props.node = { style: spec.type, ...node };
return $(
spec,
provideComponent(spec.nodeType, spec.type) ?? components.unknownBlock,
serializeMarksTree(node)
return $(
spec,
provideComponent(spec.nodeType, spec.type) ??
components.unknownBlock,
serializeMarksTree(node)
);
}
)
})
: isPortableTextToolkitTextNode(node)
? $(
{nodeType: "text", type: null},
{ nodeType: "text", type: null },
"\n" === node.text && isComponent(components.hardBreak)
? components.hardBreak
: node.text
)
: use({nodeType: "type" as keyof typeof components, type: node._type}, (spec) => {
return $(spec, provideComponent(spec.nodeType, spec.type) ?? components.unknownType)
});
: use(
{ nodeType: "type" as keyof typeof components, type: node._type },
(spec) => {
return $(
spec,
provideComponent(spec.nodeType, spec.type) ?? components.unknownType
);
}
);
};
// Set `provideDefaultComponent` on context
context.provideDefaultComponent = (node: TypedObject): any => {
const { nodeType } = (node as any)[nodeTypeRef];
return nodeType === "list" || isPortableTextToolkitList(node)
? List
: nodeType === "listItem" || isPortableTextListItemBlock(node)
Expand Down Expand Up @@ -257,7 +288,7 @@ context.provideWarningMessage = (node: TypedObject) => {
context.provideMissingComponentHandler = missingComponentHandler;
// Using a generator to avoid creating a new array
function * renderBlocks() {
function* renderBlocks() {
for (const it of nestLists(blocks, listNestingMode)) {
yield asComponentProps(it, 0, false);
}
Expand All @@ -266,12 +297,12 @@ function * renderBlocks() {

{
[...renderBlocks()].map(function render(props) {
const [Cmp, children] = prepareForRender(props);
const [Cmp, children] = prepareForRender(props);

return !isComponent(Cmp) ? (
<Fragment set:text={Cmp} />
) : (
<Cmp {...props}>{children.map(render)}</Cmp>
);
})
return !isComponent(Cmp) ? (
<Fragment set:text={Cmp} />
) : (
<Cmp {...props}>{children.map(render)}</Cmp>
);
})
}
3 changes: 2 additions & 1 deletion astro-portabletext/components/UnknownType.astro
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { usePortableText } from "../lib/utils";
export type Props = ComponentProps;
const { node, isInline } = Astro.props;
const { getWarningMessage, notifyMissingComponentHandler } = usePortableText(node);
const { getWarningMessage, notifyMissingComponentHandler } =
usePortableText(node);
const warning = getWarningMessage();
notifyMissingComponentHandler(warning);
Expand Down

0 comments on commit 8300fe6

Please sign in to comment.