Problem · String

Render a Source-Backed Markdown Subset

Learn this problem
MediumSamsara logoSamsaraFULLTIMEPHONE SCREEN

Problem statement

Convert a string containing a bounded Markdown subset to HTML.

  • One or more blank lines separate blocks.
  • Consecutive ordinary nonblank lines form one <p> block; a single newline inside it becomes <br>.
  • Consecutive nonblank lines beginning with > form one <blockquote>; remove each prefix and join its lines with <br>.
  • Within one block, paired **...** becomes <strong>...</strong> and paired ~~...~~ becomes <del>...</del>.
  • Inline formatting may span a single newline within its block, but never a blank-line block boundary. Malformed or unmatched delimiters remain literal. Preserve all other punctuation and spaces.

Return the concatenated HTML blocks without extra separators.

Function

renderLightweightMarkup(text: String) → String

Examples

Example 1

text = "Hello, **world**!\nNext line.\n\n> quoted\n> ~~old~~"return = "<p>Hello, <strong>world</strong>!<br>Next line.</p><blockquote>quoted<br><del>old</del></blockquote>"

The ordinary lines form one paragraph, the blank line starts a blockquote, and both inline forms are rendered.

Example 2

text = "Keep **open\n\nNew ~~text~~."return = "<p>Keep **open</p><p>New <del>text</del>.</p>"

The unmatched strong delimiter cannot cross the paragraph boundary and stays literal.

Constraints

  • 0 <= text.length <= 200000.
  • text contains ordinary characters and newline characters.

More Samsara problems

drafts saved locally
public String renderLightweightMarkup(String text) {
    // Write your code here.
}
text"Hello, **world**!\nNext line.\n\n> quoted\n> ~~old~~"
expected"<p>Hello, <strong>world</strong>!<br>Next line.</p><blockquote>quoted<br><del>old</del></blockquote>"
checking account