| 1 | // Helper function to wrap text in HTML tags |
| 1 | 2 | function wrap(text, tag) { |
| 2 | 3 | return `<${tag}>${text}</${tag}>`; |
| 3 | 4 | } |
| 4 | 5 | |
| 5 | | function isTag(text, tag) { |
| 6 | | return text.startsWith(`<${tag}>`); |
| 7 | | } |
| 8 | | |
| 9 | | function parser(markdown, delimiter, tag) { |
| 10 | | const pattern = new RegExp(`${delimiter}(.+)${delimiter}`); |
| 11 | | const replacement = `<${tag}>$1</${tag}>`; |
| 6 | // Parse bold text (__text__ -> <strong>text</strong>) |
| 7 | function parseBold(markdown) { |
| 8 | const pattern = new RegExp('__(.+)__'); |
| 9 | const replacement = '<strong>$1</strong>'; |
| 12 | 10 | return markdown.replace(pattern, replacement); |
| 13 | 11 | } |
| 14 | 12 | |
| 15 | | function parse__(markdown) { |
| 16 | | return parser(markdown, '__', 'strong'); |
| 17 | | } |
| 18 | | |
| 19 | | function parse_(markdown) { |
| 20 | | return parser(markdown, '_', 'em'); |
| 13 | // Parse italic text (_text_ -> <em>text</em>) |
| 14 | function parseItalic(markdown) { |
| 15 | const pattern = new RegExp('_(.+)_'); |
| 16 | const replacement = '<em>$1</em>'; |
| 17 | return markdown.replace(pattern, replacement); |
| 21 | 18 | } |
| 22 | 19 | |
| 23 | | function parseText(markdown, list) { |
| 24 | | const parsedText = parse_(parse__(markdown)); |
| 25 | | if (list) { |
| 26 | | return parsedText; |
| 27 | | } else { |
| 28 | | return wrap(parsedText, 'p'); |
| 29 | | } |
| 20 | // Parse inline formatting (bold and italic) |
| 21 | function parseInlineFormatting(markdown) { |
| 22 | // Parse bold first, then italic |
| 23 | return parseItalic(parseBold(markdown)); |
| 30 | 24 | } |
| 31 | 25 | |
| 32 | | function parseHeader(markdown, list) { |
| 26 | // Parse header (# Header -> <h1>Header</h1>) |
| 27 | function parseHeader(markdown, inList) { |
| 33 | 28 | let count = 0; |
| 34 | 29 | for (let i = 0; i < markdown.length; i++) { |
| 35 | 30 | if (markdown[i] === '#') { |
| 38 | 33 | break; |
| 39 | 34 | } |
| 40 | 35 | } |
| 36 | |
| 41 | 37 | if (count === 0 || count > 6) { |
| 42 | | return [null, list]; |
| 38 | return [null, inList]; |
| 43 | 39 | } |
| 40 | |
| 44 | 41 | const headerTag = `h${count}`; |
| 45 | | const headerHtml = wrap(markdown.substring(count + 1), headerTag); |
| 46 | | if (list) { |
| 42 | const headerContent = markdown.substring(count + 1); |
| 43 | const headerHtml = wrap(headerContent, headerTag); |
| 44 | |
| 45 | if (inList) { |
| 47 | 46 | return [`</ul>${headerHtml}`, false]; |
| 48 | 47 | } else { |
| 49 | 48 | return [headerHtml, false]; |
| 50 | 49 | } |
| 51 | 50 | } |
| 52 | 51 | |
| 53 | | function parseLineItem(markdown, list) { |
| 52 | // Parse list item (* item -> <li>item</li>) |
| 53 | function parseListItem(markdown, inList) { |
| 54 | 54 | if (markdown.startsWith('*')) { |
| 55 | | const innerHtml = wrap(parseText(markdown.substring(2), true), 'li'); |
| 56 | | if (list) { |
| 57 | | return [innerHtml, true]; |
| 55 | const itemContent = markdown.substring(2); |
| 56 | const formattedContent = parseInlineFormatting(itemContent); |
| 57 | const listItemHtml = wrap(formattedContent, 'li'); |
| 58 | |
| 59 | if (inList) { |
| 60 | return [listItemHtml, true]; |
| 58 | 61 | } else { |
| 59 | | return [`<ul>${innerHtml}`, true]; |
| 62 | return [`<ul>${listItemHtml}`, true]; |
| 60 | 63 | } |
| 61 | 64 | } |
| 62 | | return [null, list]; |
| 65 | return [null, inList]; |
| 63 | 66 | } |
| 64 | 67 | |
| 65 | | function parseParagraph(markdown, list) { |
| 66 | | if (!list) { |
| 67 | | return [parseText(markdown, false), false]; |
| 68 | // Parse paragraph |
| 69 | function parseParagraph(markdown, inList) { |
| 70 | const formattedText = parseInlineFormatting(markdown); |
| 71 | |
| 72 | if (inList) { |
| 73 | return [`</ul>${wrap(formattedText, 'p')}`, false]; |
| 68 | 74 | } else { |
| 69 | | return [`</ul>${parseText(markdown, false)}`, false]; |
| 75 | return [wrap(formattedText, 'p'), false]; |
| 70 | 76 | } |
| 71 | 77 | } |
| 72 | 78 | |
| 73 | | function parseLine(markdown, list) { |
| 74 | | let [result, inListAfter] = parseHeader(markdown, list); |
| 79 | // Parse a single line of markdown |
| 80 | function parseLine(markdown, inList) { |
| 81 | // Try to parse as header |
| 82 | let [result, newList] = parseHeader(markdown, inList); |
| 83 | |
| 84 | // If not a header, try to parse as list item |
| 75 | 85 | if (result === null) { |
| 76 | | [result, inListAfter] = parseLineItem(markdown, list); |
| 86 | [result, newList] = parseListItem(markdown, inList); |
| 77 | 87 | } |
| 88 | |
| 89 | // If not a list item, parse as paragraph |
| 78 | 90 | if (result === null) { |
| 79 | | [result, inListAfter] = parseParagraph(markdown, list); |
| 91 | [result, newList] = parseParagraph(markdown, inList); |
| 80 | 92 | } |
| 93 | |
| 94 | // This should never happen if implementation is complete |
| 81 | 95 | if (result === null) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 96 | throw new Error('Invalid markdown line'); |
| 83 | 97 | } |
| 84 | | return [result, inListAfter]; |
| 98 | |
| 99 | return [result, newList]; |
| 85 | 100 | } |
| 86 | 101 | |
| 87 | 102 | /** |
| 103 | * Parse markdown text and convert to HTML |
| 88 | 104 | * @param {string} markdown |
| 89 | 105 | * @returns {string} |
| 90 | 106 | */ |
| 91 | 107 | export function parse(markdown) { |
| 92 | 108 | const lines = markdown.split('\n'); |
| 93 | | let result = ''; |
| 94 | | let list = false; |
| 95 | | for (let i = 0; i < lines.length; i++) { |
| 96 | | let [lineResult, newList] = parseLine(lines[i], list); |
| 97 | | result += lineResult; |
| 98 | | list = newList; |
| 109 | let html = ''; |
| 110 | let inList = false; |
| 111 | |
| 112 | for (const line of lines) { |
| 113 | let [lineHtml, newListState] = parseLine(line, inList); |
| 114 | html += lineHtml; |
| 115 | inList = newListState; |
| 99 | 116 | } |
| 100 | | if (list) { |
| 101 | | return result + '</ul>'; |
| 102 | | } else { |
| 103 | | return result; |
| 117 | |
| 118 | // Close any open list tag |
| 119 | if (inList) { |
| 120 | html += '</ul>'; |
| 104 | 121 | } |
| 122 | |
| 123 | return html; |
| 105 | 124 | } |