#P2395. BBCode to Markdown Converter
BBCode to Markdown Converter
BBCode to Markdown Converter
You are given a string containing BBCode. Your task is to convert the BBCode into Markdown format following the mapping below and to ensure that the BBCode is valid. If the input contains an error (such as unclosed tags, mismatched tag pairs, or incorrect nesting), output the string error
.
The BBCode tags that may appear and their corresponding Markdown formats are:
[h1]...[/h1]
converts to a level‐1 heading:# ... #
[h2]...[/h2]
converts to a level‐2 heading:## ... ##
[b]...[/b]
converts to bold text: wrap the content with double underscores:__...__
[i]...[/i]
converts to italic text: wrap the content with single asterisks:*...*
[quote]...[/quote]
is a special tag. Its content should not be parsed as BBCode. Instead, treat the entire content inside as raw text and output it in Markdown blockquote format. To do so, prefix each line with>
without applying any further BBCode conversion.
Important:
- The opening and closing tags must match exactly. For example, the following are invalid:
[h1]Hello World! // tag not closed [h1]Hello World![/h2] // mismatched tags [h1][i]Hello World![/h1][/i] // incorrect nesting order
- All tags other than the ones listed will not appear.
Input: A single string representing the BBCode.
Output: If the input BBCode is valid, output the converted Markdown string. If it is invalid, output error
.
For example:
Input: [h1]Hello World![/h1] Output: # Hello World! # Input: [b]This is [i]cool[/i] text[/b] Output: __This is *cool* text__ Input: [quote]Here is [h1]ignored header[/h1] text[/quote] Output: > Here is [h1]ignored header[/h1] text
inputFormat
A single line containing the BBCode string.
outputFormat
A single line which is the converted Markdown string, or error
if the BBCode is invalid.
sample
[h1]Hello World![/h1]
# Hello World! #