MediumParsingPython 3
Streaming Markdown Parser
Parse streamed Markdown chunks into text, inline-code, and fenced-code tokens while preserving delimiter state.
35m2 sample tests4 hidden tests
Implement parse_markdown_chunks(chunks), a small parser for streamed Markdown text.
Requirements
- Input is a list of string chunks.
- Return a list of
(kind, value)tokens. - Token kinds are
"text","inline_code", and"fenced_code". - Single backticks toggle inline code unless the parser is inside a fenced code block.
- Triple backticks toggle fenced code.
- Delimiters may be split across chunks.
- Preserve text order and flush the final token at end of stream.
Example
python
1fence = chr(96) * 3
2chunks = ["Use `he", "apq` and " + fence + "py", "thon\\nprint(1)\\n" + fence + " now"]
3assert parse_markdown_chunks(chunks) == [
4 ("text", "Use "),
5 ("inline_code", "heapq"),
6 ("text", " and "),
7 ("fenced_code", "python\\nprint(1)\\n"),
8 ("text", " now"),
9]Constraints
- Handle only backtick inline code and fenced code blocks.
- Don't render HTML.
- Don't use a Markdown library.
Editor