Skip to content

Commit

Permalink
fix(PrismicRichText): prevent extra whitespace between text (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed Aug 30, 2023
1 parent 244b39c commit 1502f92
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
6 changes: 2 additions & 4 deletions src/PrismicRichText/DefaultComponent.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@
<span class={node.data.label}><slot /></span>
{:else}
{#each node.text.split("\n") as line, index}
{#if index > 0}
<br />
{/if}
{line}
<!-- This formatting is intentional to prevent unwanted whitespace between elements. -->
{#if index > 0}<br />{/if}{line}
{/each}
{/if}
34 changes: 17 additions & 17 deletions src/PrismicRichText/Serialize.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<script lang="ts">
import type { RichTextNodeType } from "@prismicio/client";
import type { asTree } from "@prismicio/richtext";
import type { SvelteRichTextSerializer } from "../types";
Expand All @@ -9,28 +8,29 @@
export let components: SvelteRichTextSerializer = {};
export let children: ReturnType<typeof asTree>["children"];
const rewrittenNodeTypes: Partial<
Record<
(typeof RichTextNodeType)[keyof typeof RichTextNodeType],
keyof typeof RichTextNodeType
>
> = {
const CHILD_TYPE_RENAMES = {
"list-item": "listItem",
"o-list-item": "oListItem",
"group-list-item": "list",
"group-o-list-item": "oList",
};
} as const;
function getComponent(child: ReturnType<typeof asTree>["children"][number]) {
return (
components[
CHILD_TYPE_RENAMES[child.type as keyof typeof CHILD_TYPE_RENAMES] ||
(child.type as keyof typeof components)
] || DefaultComponent
);
}
</script>

{#each children as child}
<svelte:component
this={components[rewrittenNodeTypes[child.type] || child.type] ||
DefaultComponent}
node={child.node}
key={child.key}
<svelte:component this={getComponent(child)} node={child.node}>
<!-- This formatting is intentional to prevent unwanted whitespace between elements. -->
{#if child.children.length > 0}<svelte:self
children={child.children}
{components}
/>{/if}</svelte:component
>
{#if child.children.length > 0}
<svelte:self children={child.children} {components} />
{/if}
</svelte:component>
{/each}

0 comments on commit 1502f92

Please sign in to comment.