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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: markdowntosafeHtml refactor and fix the warning #18195

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions packages/features/bookings/Booker/components/EventMeta.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { EventMetaBlock } from "@calcom/features/bookings/components/event-meta/
import { useTimePreferences } from "@calcom/features/bookings/lib";
import type { BookerEvent } from "@calcom/features/bookings/types";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { markdownToSafeHTMLClient } from "@calcom/lib/markdownToSafeHTMLClient";
import { markdownToSafeHTML } from "@calcom/lib/markdownToSafeHTML";
import type { EventTypeTranslation } from "@calcom/prisma/client";
import { EventTypeAutoTranslatedField } from "@calcom/prisma/enums";

Expand Down Expand Up @@ -159,7 +159,7 @@ export const EventMeta = ({
<div
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{
__html: markdownToSafeHTMLClient(translatedDescription ?? event.description),
__html: markdownToSafeHTML(translatedDescription ?? event.description),
}}
/>
</EventMetaBlock>
Expand Down
6 changes: 3 additions & 3 deletions packages/features/form-builder/FormBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ZodError } from "zod";
import { classNames } from "@calcom/lib";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { md } from "@calcom/lib/markdownIt";
import { markdownToSafeHTMLClient } from "@calcom/lib/markdownToSafeHTMLClient";
import { markdownToSafeHTML } from "@calcom/lib/markdownToSafeHTML";
import turndown from "@calcom/lib/turndownService";
import {
Badge,
Expand Down Expand Up @@ -154,7 +154,7 @@ export const FormBuilder = function FormBuilder({
}

if (fieldsThatSupportLabelAsSafeHtml.includes(field.type)) {
field = { ...field, labelAsSafeHtml: markdownToSafeHTMLClient(field.label ?? "") };
field = { ...field, labelAsSafeHtml: markdownToSafeHTML(field.label ?? "") };
}
const numOptions = options?.length ?? 0;
const firstOptionInput =
Expand Down Expand Up @@ -736,7 +736,7 @@ function FieldLabel({ field }: { field: RhfFormField }) {
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{
// Derive from field.label because label might change in b/w and field.labelAsSafeHtml will not be updated.
__html: markdownToSafeHTMLClient(field.label || t(field.defaultLabel || "") || ""),
__html: markdownToSafeHTML(field.label || t(field.defaultLabel || "") || ""),
}}
/>
);
Expand Down
10 changes: 3 additions & 7 deletions packages/lib/markdownToSafeHTML.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import DOMPurify from "dompurify";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn’t we make this a dynamic import only on the client-side?

The comment in this file that was removed said it was done this way to avoid importing this dependency on the client-side since it’s costly.

import sanitizeHtml from "sanitize-html";

import { md } from "@calcom/lib/markdownIt";

if (typeof window !== "undefined") {
// This file imports markdown parser which is a costly dependency, so we want to make sure it's not imported on the client side.
// It is still imported at some places on client in non-booker pages, we can gradually remove it from there and then convert it into an error
console.warn("`markdownToSafeHTML` should not be imported on the client side.");
}

export function markdownToSafeHTML(markdown: string | null) {
if (!markdown) return "";

const html = md.render(markdown);

const safeHTML = sanitizeHtml(html);
// Sanitize HTML using the appropriate library based on environment
const safeHTML = typeof window === "undefined" ? sanitizeHtml(html) : DOMPurify.sanitize(html);

const safeHTMLWithListFormatting = safeHTML
.replace(
Expand Down
30 changes: 0 additions & 30 deletions packages/lib/markdownToSafeHTMLClient.ts

This file was deleted.

Loading