Skip to content

Commit

Permalink
Allow to add HTML tags to cards (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubk authored Jan 9, 2024
1 parent 519a2da commit c2465fa
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 8 deletions.
141 changes: 136 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@supabase/supabase-js": "^2.38.0",
"@twa-dev/sdk": "^6.9.0",
"@types/luxon": "^3.3.2",
"@types/sanitize-html": "^2.9.5",
"colord": "^2.9.3",
"framer-motion": "^10.16.4",
"grammy": "^1.19.1",
Expand All @@ -45,6 +46,7 @@
"react-dom": "^18.2.0",
"react-hotkeys-hook": "^4.4.1",
"react-lines-ellipsis": "^0.15.3",
"sanitize-html": "^2.11.0",
"short-unique-id": "^5.0.3",
"supabase": "^1.110.1",
"zod": "^3.22.3"
Expand Down
10 changes: 10 additions & 0 deletions src/lib/sanitize-html/sanitize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import sanitizeHtml from "sanitize-html";

export const sanitize = (text: string) => {
return sanitizeHtml(text, {
allowedTags: ["small", "a", "big", "br", "b"],
allowedAttributes: {
a: ["href"],
},
});
};
13 changes: 13 additions & 0 deletions src/screens/deck-review/card-field-view.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { sanitize } from "../../lib/sanitize-html/sanitize.ts";
import React from "react";

export const CardFieldView = (props: { text: string }) => {
const { text } = props;
return (
<span
dangerouslySetInnerHTML={{
__html: sanitize(text)
}}
></span>
);
};
9 changes: 6 additions & 3 deletions src/screens/deck-review/card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { observer } from "mobx-react-lite";
import { CardUnderReviewStore } from "./store/card-under-review-store.ts";
import { HorizontalDivider } from "../../ui/horizontal-divider.tsx";
import { CardSpeaker } from "./card-speaker.tsx";
import { CardFieldView } from "./card-field-view.tsx";

export const cardSize = 310;

Expand Down Expand Up @@ -44,19 +45,21 @@ export const Card = observer(({ card, style, animate }: Props) => {
})}
>
<div>
{card.front} <CardSpeaker card={card} type={"front"} />
<CardFieldView text={card.front} />{" "}
<CardSpeaker card={card} type={"front"} />
</div>
{card.isOpened ? <HorizontalDivider /> : null}
{card.isOpened ? (
<div>
{card.back} <CardSpeaker card={card} type={"back"} />
<CardFieldView text={card.back} />{" "}
<CardSpeaker card={card} type={"back"} />
</div>
) : null}
{card.isOpened && card.example ? (
<div
className={css({ fontWeight: 400, fontSize: 14, paddingTop: 8 })}
>
{card.example}
<CardFieldView text={card.example} />
</div>
) : null}
</span>
Expand Down

0 comments on commit c2465fa

Please sign in to comment.