Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(PrismicRichText): prevent extra whitespace between text #13

Merged
merged 1 commit into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}