Frontend School

Rich Text Editor: Smart Link Highlighting

Machine Coding
hard

Rich Text Editor: Smart Link Highlighting

Hard
Rich Text Editing
Frontend
React
DOM Manipulation
Selection API

Overview

At Cred, clear and concise communication is paramount. Our product and content teams frequently draft internal documents, FAQs, and marketing copy that contain specific product features, financial terms, or company names. To enhance readability and provide quick context, we need a simple rich text editor component capable of highlighting these key terms as "smart links". These smart links are not just regular hyperlinks; they are visually distinct and can be automatically applied, streamlining the content creation process. This problem challenges your ability to build a robust rich text editing experience by directly manipulating the DOM.

What you need to do / Task

Your goal is to build a React component that functions as a rich text editor with specific "smart link" capabilities:

  1. Editable Content Area: Implement a contentEditable div where users can type and edit text.
  2. Manual Smart Link Application: Add a toolbar button labeled "Smart Link". When text is selected in the editor and this button is clicked, the selected text should be wrapped in a special "smart link" style. If the selected text (or part of it) is already a smart link, clicking the button should remove the smart link style.
  3. Automatic Smart Link Conversion: As the user types, specific keywords should be automatically converted into "smart links" as soon as they are typed and followed by a space or punctuation. The keywords are "Cred", "Pay", and "Rewards" (case-insensitive). This automatic conversion should only apply if the keyword is not already part of an existing smart link.
  4. Visual Distinction: "Smart links" must be visually distinct from regular text (e.g., a unique background color, text color, and border-radius, making them appear as "pills" or "tags").
  5. Cursor & Selection Management: Ensure that cursor position and text selection behave correctly and are maintained after both manual application and automatic conversion of smart links.

UI/UX expectations

  • Layout: A main editable area with a simple toolbar positioned directly above it.
  • Toolbar: The toolbar should contain at least one button for "Smart Link".
  • Smart Link Appearance: Smart links should be rendered as visually distinct elements, resembling a tag or pill. For example, background-color: #e0f2f7; padding: 2px 6px; border-radius: 4px; color: #007bff;.
  • Interactions: Standard text editing interactions (typing, selecting, backspacing) should function smoothly.

Acceptance criteria

  • User can type and edit arbitrary text within the contentEditable area.
  • Selecting text and clicking the "Smart Link" button correctly applies the smart link style.
  • Clicking the "Smart Link" button on already-styled text (or a selection within it) correctly removes the style.
  • Typing any of "Cred", "Pay", or "Rewards" (case-insensitive) followed by a space or punctuation automatically converts the word into a smart link.
  • Automatic conversion does not occur if the keyword is already part of an existing smart link.
  • The cursor's position and current text selection are accurately maintained after both manual style application/removal and automatic keyword conversion.
  • Smart links are rendered with the specified distinct visual styling.

Constraints

Constraints & assumptions

  • You must use React for the UI.
  • Direct DOM manipulation using browser's Selection and Range APIs is expected. Do not use external rich text editor libraries (e.g., Draft.js, Slate.js).
  • The keywords for automatic conversion are "Cred", "Pay", and "Rewards" (case-insensitive).
  • Assume a relatively flat text structure. You do not need to handle complex nested HTML elements (e.g., bold text inside a smart link, or smart links spanning multiple block elements like paragraphs).
  • Focus on modern browser environments.

Out of scope

  • Full-fledged undo/redo stack.
  • Handling complex copy-paste scenarios (e.g., pasting rich HTML).
  • Persistence of editor content.
  • Other text formatting options (bold, italic, underline, etc.).

What interviewers evaluate

  • DOM Manipulation & APIs: Correct and efficient use of Selection, Range, and other DOM APIs for rich text editing.
  • React Component Design: Effective use of React hooks, state, and component structure for managing the editor.
  • Robustness & Edge Cases: Handling of cursor placement, selection persistence, and preventing unintended formatting (e.g., re-wrapping existing smart links).
  • Code Quality: Readability, maintainability, and clarity of the implementation.

Loading editor...