Show formatting in product reviews
React · React · intermediate · modification
The review form has offered Markdown formatting since last week's form update, but the product page still prints the raw text, so reviews show up full of `**` and `[text](url)`. This renders the formatting. `package.json` adds `marked` (pinned exactly, like the rest of our dependencies). `src/reviews/ReviewBody.jsx` (new) turns a review's Markdown into HTML with `marked.parse` and renders it in the `.review-body` container. `src/reviews/ReviewList.jsx` uses it in place of the plain paragraph. `src/reviews/ReviewBody.test.jsx` (new) renders bold and italic text, a bulleted list, a link and two paragraphs.
Storefront of an online clothing shop: a React 18.3 single-page app built with Vite 5, function components, tests with Vitest 2, @testing-library/react 16 and jsdom (`npm test`). `marked` 18.1.3 is new in this PR; `dompurify` 3.4.17 is already a dependency. The repository commits no lockfile (the project's `.npmrc` sets `package-lock=false`): every dependency is pinned to an exact version in `package.json`, and CI and local setups install with `npm install`, so adding a dependency is a `package.json` change only. The app is served from `shop.example`, the same origin as the storefront API; a signed-in shopper's session cookie is sent with every same-origin API request, and that session can change the account's email and shipping address and place orders with a saved card. The storefront API accepts only `application/json` request bodies and answers any other content type with 415, so an HTML form cannot call it. Its GET endpoints only read; none of them changes state. The storefront sends no Content-Security-Policy header. Requirement 2 is about script and session actions; how a review's formatting looks (styling, layout) is out of its scope. Existing and unchanged: `ProductPage` loads `GET /api/products/:id/reviews`, shows its own loading and error states, and renders `<ReviewList reviews={reviews} />` with an array of up to 20 reviews. Each review is `{ id, rating, authorName, createdAt, createdAtLabel, body }`: `id` a unique string, `rating` an integer from 1 to 5, `authorName` a display name, `createdAt` an ISO 8601 timestamp, `createdAtLabel` the date already formatted by the API, and `body` the text the customer typed, a string of 1 to 5000 characters with at least one non-blank character, any characters allowed (the form's textarea accepts anything), line breaks stored as `\n`. Any signed-in customer who bought the product can post a review, and it is published at once, without moderation. `StarRating` renders the stars. `src/lib/sanitizeHtml.js` exports `sanitizeHtml(html)`, which returns `DOMPurify.sanitize(html)` with DOMPurify's default configuration; the merchant-written product description already goes through it. The global stylesheet `src/styles.css` (imported once by the app) already styles `.review-body` and the paragraphs, lists and links inside it with the theme tokens.
Requirements
- On the product page, each review's `body` is rendered as Markdown with `marked` and its default options, so the formatting the review form's help text offers (bold, italic, bulleted and numbered lists, links, and a blank line between paragraphs) shows as formatting instead of as asterisks and brackets.
- Reviews are written by customers and shown to every shopper who opens the product page, signed in or not. Nothing a customer types into a review may run script or act in the session of a shopper who reads it. Formatting elements, links to `http`/`https` pages and images may stay.
- Unchanged: the star rating, the author's name, the date, the review order and the "No reviews yet." message. The new rendering has tests that run with the existing `npm test`.
Files touched
- package.json
- src/reviews/ReviewList.jsx
- src/reviews/ReviewBody.jsx
- src/reviews/ReviewBody.test.jsx