X OA 面试题解析:Markdown 转 HTML(段落、换行、引用、删除线)

16次阅读
没有评论

You need to parse these tags: <p/>, <br/>, <blockquote/>, and <del/>.

A sequence of two or more consecutive newline characters starts a new paragraph.

Other formatting commands do not cross paragraphs.

A single newline character is a soft line break, <br/>.

A blockquote is one or more consecutive lines where the first two characters on the line are > (a greater than sign followed by a space).

Text in strikethrough (<del></del>) is surrounded by a pair of tildes.

Example:

markdown_input = """This is a paragraph with a soft
line break.

This is another paragraph that has
> Some text that
> is in a
> block quote.

This is another paragraph with a ~~strikethrough~~ word."""
output = """<p>This is a paragraph with a soft<br />line break.</p>

<p>This is another paragraph that has<br />
<blockquote>Some text that<br />is in a<br />block quote.</blockquote>
</p>

<p>This is another paragraph with a <del>strikethrough</del> word.</p>"""

Note: It is not important to produce this specific output. We only care if the HTML is valid.

这道题要求把一段带有简单标记的 Markdown 文本转换成合法 HTML,核心是正确处理段落、软换行、引用和删除线。连续两个及以上换行表示新段落,单个换行要转换成 <code>&lt;br /&gt;</code>;以 <code>&gt; </code> 开头的连续行要为 blockquote;而 <code>~~text~~</code> 需要输出为 <code>&lt;del&gt;text&lt;/del&gt;</code>。实现时通常需要按行扫描并维护当前段落状态,注意这些格式规则不能跨段落生效。

正文完
 0