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

refactor(parse and sanitize markdown on server) #1634

Merged
merged 21 commits into from
Jan 31, 2025
Merged

Conversation

Spencer6497
Copy link
Contributor

@Spencer6497 Spencer6497 commented Jan 20, 2025

This PR shifts the responsibility of parsing and sanitizing markdown from the <RichText/> component to the zod schema definitions via .transform(). To do this, I've added a new buildRichTextValidation zod schema type, and replaced all instances of zod.string() with it, where applicable. Further cleanup PR to come.

  • add new parseAndSanitize method, move sanitize to same file
  • Create new buildRichTextValidation() zod schema method

@Spencer6497
Copy link
Contributor Author

Tests are failing because at certain points in the CMS, we've nested list items (asterisks in markdown evaluate to <li> tags) inside of Mustache conditional statements. Since the markdown is now getting parsed + sanitized way before stringReplacements are being evaluated, we run into an issue where syntactically important DOM elements (like opening <ul> tags) get trapped inside {{ #conditional }} logic and get deleted when those conditions evaluate to false

@chohner
Copy link
Member

chohner commented Jan 23, 2025

Tests are failing because at certain points in the CMS, we've nested list items (asterisks in markdown evaluate to <li> tags) inside of Mustache conditional statements. Since the markdown is now getting parsed + sanitized way before stringReplacements are being evaluated, we run into an issue where syntactically important DOM elements (like opening <ul> tags) get trapped inside {{ #conditional }} logic and get deleted when those conditions evaluate to false

Nice find. Is that a sign of malformed content? I would've hoped that markdown conversion & template replacement to be commutative...

@Spencer6497
Copy link
Contributor Author

Tests are failing because at certain points in the CMS, we've nested list items (asterisks in markdown evaluate to <li> tags) inside of Mustache conditional statements. Since the markdown is now getting parsed + sanitized way before stringReplacements are being evaluated, we run into an issue where syntactically important DOM elements (like opening <ul> tags) get trapped inside {{ #conditional }} logic and get deleted when those conditions evaluate to false

Nice find. Is that a sign of malformed content? I would've hoped that markdown conversion & template replacement to be commutative...

Nope, unfortunately the content is correct :/ It would be commutative if we were only doing simply text replacements, but since we're executing IF-ELSE logic with conditionals, important HTML gets omitted after it's been parsed/sanitized from markdown

app/util/strings.ts Dismissed Show dismissed Hide dismissed
@Spencer6497 Spencer6497 marked this pull request as ready for review January 29, 2025 05:56
import { useField } from "remix-validated-form";
import TileTag, { type TileDescriptionProps } from "./TileTag";
import Image, { type ImageProps } from "../../Image";
import RichText from "../../RichText";

const paragraphRenderer: Partial<Renderer> = {
paragraph({ text }) {
return `<p class="ds-subhead">${text}</p>`;
Copy link
Contributor

Choose a reason for hiding this comment

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

can you check if this component renders the RichText with the paragraph with this className?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a new test suite, richtest.test.ts, as the rendering is occurring inside of buildRichTextValidation

@Spencer6497 Spencer6497 merged commit e2e50f7 into main Jan 31, 2025
22 checks passed
@Spencer6497 Spencer6497 deleted the markdown-on-server branch January 31, 2025 06:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants