Skip to content

Commit

Permalink
Super Script subscripts (#6)
Browse files Browse the repository at this point in the history
Co-authored-by: Bert Bengtson <[email protected]>
  • Loading branch information
bertybot and bertybot2 authored Dec 9, 2024
1 parent 2343829 commit 780fb83
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/unlucky-baboons-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rich-text-svelte-renderer": patch
---

Added support for superscript and subscript elements
6 changes: 6 additions & 0 deletions src/lib/RenderText.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
bold?: boolean;
italic?: boolean;
underline?: boolean;
superscript?: boolean;
subscript?: boolean;
code?: boolean;
shouldSerialize: boolean;
renderers: NodeRendererType | undefined;
Expand All @@ -18,6 +20,8 @@
italic = false,
underline = false,
code = false,
superscript = false,
subscript = false,
shouldSerialize,
renderers
}: Props = $props();
Expand All @@ -27,6 +31,8 @@
if (italic) return 'italic';
if (underline) return 'underline';
if (code) return 'code';
if (superscript) return 'superscript';
if (subscript) return 'subscript';
return null;
});
Expand Down
2 changes: 2 additions & 0 deletions src/lib/defaultElements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export const defaultElements: Required<NodeRendererType> = {
table: 'table',
bold: 'b',
italic: 'i',
superscript: 'sup',
subscript: 'sub',
underline: 'u',
code: 'code',
table_head: 'thead',
Expand Down
2 changes: 2 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export interface NodeRendererType {
blockquote?: Component<any> | keyof SvelteHTMLElements;
bold?: Component<any> | keyof SvelteHTMLElements;
italic?: Component<any> | keyof SvelteHTMLElements;
superscript?: Component<any> | keyof SvelteHTMLElements;
subscript?: Component<any> | keyof SvelteHTMLElements;
underline?: Component<any> | keyof SvelteHTMLElements;
code?: Component<any> | keyof SvelteHTMLElements;
code_block?: Component<any> | keyof SvelteHTMLElements;
Expand Down
14 changes: 13 additions & 1 deletion src/test/RichText.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
emptyContent,
embedAssetContent,
simpleH1Content,
tableContent
tableContent,
superScriptSubScriptContent
} from './content';
import TestP from './testComponents/TestP.svelte';
import TestBold from './testComponents/TestBold.svelte';
Expand Down Expand Up @@ -338,6 +339,17 @@ describe('rich-text-svelte-renderer', () => {

expect(document.body).toMatchSnapshot();
});

it('should render superscript and subscript elements', () => {
mount(RichText, {
target: document.body,
props: {
content: superScriptSubScriptContent
}
});

expect(document.body).toMatchSnapshot();
});
});

describe('custom embeds and assets', () => {
Expand Down
120 changes: 120 additions & 0 deletions src/test/__snapshots__/RichText.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2130,6 +2130,126 @@ exports[`rich-text-svelte-renderer > should render empty text spaces 1`] = `
</body>
`;

exports[`rich-text-svelte-renderer > should render superscript and subscript elements 1`] = `
<body>
<!---->
<!---->
<!---->
<!---->
<!---->
<!---->
<!---->
<!---->
<!---->
<!---->
<p>
<!---->
<!---->
<!---->
<!---->
<!---->
<!---->
TEST
<!---->
<!---->
<!---->
<!---->
<!---->
<!---->
<!---->
<sub>
<!---->
<!---->
T
</sub>
</p>
<!---->
<!---->
<!---->
<!---->
<!---->
<!---->
<!---->
<!---->
<!---->
<p>
<!---->
<!---->
<!---->
<!---->
<!---->
<!---->
TEST
<!---->
<!---->
<!---->
<!---->
<!---->
<!---->
<!---->
<sup>
<!---->
<!---->
2
</sup>
</p>
</body>
`;

Expand Down
27 changes: 27 additions & 0 deletions src/test/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,3 +452,30 @@ export const nestedEmbedAssetContent: RichTextContent = [
]
}
];

export const superScriptSubScriptContent: RichTextContent = [
{
type: 'paragraph',
children: [
{
text: 'TEST'
},
{
text: 'T',
subscript: true
}
]
},
{
type: 'paragraph',
children: [
{
text: 'TEST'
},
{
text: '2',
superscript: true
}
]
}
];

0 comments on commit 780fb83

Please sign in to comment.