Convert Email Text to Structured Markup
Problem statement
Convert a plain-text email message into a compact markup string made from <Paragraph>, <Quote>, and <Break /> components.
- One or more empty lines end the current paragraph or quote block.
- A non-empty line with no leading
>belongs to a paragraph. Consecutive paragraph lines are joined with<Break />. - A line beginning with one or more
>characters belongs to a quote. The number of leading markers is its quote depth, and those markers are removed from the rendered text. - Quote depth may increase or decrease between consecutive quote lines. Increasing the depth opens nested
<Quote>components. Decreasing it closes components until the requested depth remains. - Consecutive text lines at the same quote depth are joined with
<Break />.
Return all rendered components concatenated without extra whitespace.
Function
renderEmailMarkup(message: String) → StringExamples
Example 1
message = "Please tag @Bob Jones for approval!\n\nHere's the max amount we were allotted:\n\n>Max spend: $5000\n>Max spend per user: $12\n>Target spend: $3000\n\nDo you think it will be ok?\nOtherwise let's consider a new vendor."return = "<Paragraph>Please tag @Bob Jones for approval!</Paragraph><Paragraph>Here's the max amount we were allotted:</Paragraph><Quote>Max spend: $5000<Break />Max spend per user: $12<Break />Target spend: $3000</Quote><Paragraph>Do you think it will be ok?<Break />Otherwise let's consider a new vendor.</Paragraph>"Blank lines split the two introductory paragraphs, the three > lines form one quote, and the final single newline becomes a break inside one paragraph.
Example 2
message = "Top\n\n>One\n>>Two\n>Three\n\nEnd"return = "<Paragraph>Top</Paragraph><Quote>One<Quote>Two</Quote><Break />Three</Quote><Paragraph>End</Paragraph>"The second quote line opens a nested quote. Returning to depth one closes that nested component before adding a break and the next outer-quote line.
Example 3
message = "alpha\nbeta"return = "<Paragraph>alpha<Break />beta</Paragraph>"Without a blank separator or quote marker, both lines belong to one paragraph.
Constraints
0 <= message.length <= 100000.- Line endings use
\n. - Text contains printable ASCII characters. Outside leading quote markers, it does not contain
<,>, or&. - A non-empty quote line contains at least one text character after its leading markers.