Frontend School

Simple Markdown-like Text Editor

Machine Coding
medium

Simple Markdown-like Text Editor

Medium
Rich Text Editing
Frontend
React
JavaScript
String Manipulation
UI State Management

Overview

Flipkart product listings often require rich descriptions to highlight features and benefits. This problem simulates building a simplified text editor component that allows sellers to easily format their product descriptions using common styling options. The goal is to provide a basic, intuitive interface for content creation.

What you need to do / Task

Your task is to build a React component that functions as a basic text editor with markdown-like formatting capabilities. Specifically, you need to:

  1. Text Input Area: Create a primary input area (e.g., a textarea) where users can type and select text.
  2. Formatting Buttons: Implement three buttons in a toolbar above the input area: 'Bold', 'Italic', and 'Underline'.
  3. Apply Formatting: When a user selects text in the input area and clicks one of the formatting buttons, apply the corresponding markdown-like syntax:
    • 'Bold' should wrap selected text with double asterisks: **selected text**
    • 'Italic' should wrap selected text with single asterisks: *selected text*
    • 'Underline' should wrap selected text with underscores: _selected text_
  4. Toggle Formatting: If the selected text (or the text immediately surrounding it) is already formatted with the clicked button's style, clicking the button again should remove that formatting.
  5. Live Preview: Display a live preview of the content below the editor. This preview should render the markdown-like syntax as actual HTML (e.g., **text** becomes <strong>text</strong>).

UI/UX expectations

  • The editor should clearly separate the input area, the toolbar, and the preview area.
  • Buttons in the toolbar should be easily clickable and visually distinct.
  • The input area should allow standard text input and selection.
  • The preview area should dynamically update as the user types or applies formatting.
  • After applying/removing formatting, the editor should ideally maintain the user's cursor position or selection range for a smooth editing experience.

Acceptance criteria

  • User can type text into the editor.
  • Selecting text and clicking 'Bold' correctly applies ** formatting.
  • Selecting text and clicking 'Italic' correctly applies * formatting.
  • Selecting text and clicking 'Underline' correctly applies _ formatting.
  • Clicking a formatting button on already formatted selected text removes the corresponding formatting.
  • The preview area correctly renders **text** as bold, *text* as italic, and _text_ as underlined HTML.
  • The editor's state (text content, formatting) is managed efficiently using React's capabilities.

Constraints

Constraints & assumptions

  • The solution should be implemented using React and basic JavaScript/TypeScript.
  • No external rich text editor libraries (e.g., TinyMCE, Quill) are allowed. The focus is on implementing the core logic.
  • The input text size will be relatively small (e.g., a few paragraphs), so performance for very large texts is not a primary concern.
  • Only the three specified formatting options (Bold, Italic, Underline) need to be implemented.
  • Assume single-level formatting for removal logic (e.g., **text** is handled, but complex nested scenarios like **_text_** where only ** needs to be removed are not strictly required to be perfectly parsed for all edge cases).

Out of scope

  • Complex markdown parsing (e.g., headings, lists, links, images).
  • Real-time collaborative editing features.
  • Undo/Redo functionality.
  • Accessibility beyond basic button and input element usage.

What interviewers evaluate

  • Functional Correctness: Does the editor correctly apply and toggle all specified formatting options?
  • Code Structure & Readability: Is the code well-organized, modular, and easy to understand, following React best practices?
  • State Management: Is React state managed efficiently and correctly for the editor's content and UI interactions?
  • UI/UX Responsiveness: Does the application provide a basic but intuitive user experience, including maintaining cursor position after actions?

Loading editor...