great

Finished
1// Helper function to wrap text in HTML tags
12function wrap(text, tag) {
23 return `<${tag}>${text}</${tag}>`;
34}
45
5function isTag(text, tag) {
6 return text.startsWith(`<${tag}>`);
7}
8
9function parser(markdown, delimiter, tag) {
10 const pattern = new RegExp(`${delimiter}(.+)${delimiter}`);
11 const replacement = `<${tag}>$1</${tag}>`;
6// Parse bold text (__)
7function parseBold(markdown) {
8 const pattern = new RegExp('__([^_]+)__');
9 const replacement = '<strong>$1</strong>';
1210 return markdown.replace(pattern, replacement);
1311}
1412
15function parse__(markdown) {
16 return parser(markdown, '__', 'strong');
13// Parse italic text (_)
14function parseItalic(markdown) {
15 const pattern = new RegExp('_([^_]+)_');
16 const replacement = '<em>$1</em>';
17 return markdown.replace(pattern, replacement);
1718}
1819
19function parse_(markdown) {
20 return parser(markdown, '_', 'em');
20// Parse inline formatting (bold and italic)
21function parseInlineFormatting(markdown) {
22 return parseItalic(parseBold(markdown));
2123}
2224
23function parseText(markdown, list) {
24 const parsedText = parse_(parse__(markdown));
25 if (list) {
26 return parsedText;
25// Parse text with appropriate wrapper based on context
26function parseText(markdown, inList) {
27 const formattedText = parseInlineFormatting(markdown);
28 if (inList) {
29 return formattedText;
2730 } else {
28 return wrap(parsedText, 'p');
31 return wrap(formattedText, 'p');
2932 }
3033}
3134
32function parseHeader(markdown, list) {
35// Parse header elements (#, ##, ###, etc.)
36function parseHeader(markdown, inList) {
3337 let count = 0;
3438 for (let i = 0; i < markdown.length; i++) {
3539 if (markdown[i] === '#') {
3842 break;
3943 }
4044 }
45
46 // Headers must be between 1 and 6 # characters
4147 if (count === 0 || count > 6) {
42 return [null, list];
48 return [null, inList];
4349 }
50
4451 const headerTag = `h${count}`;
45 const headerHtml = wrap(markdown.substring(count + 1), headerTag);
46 if (list) {
52 const headerContent = markdown.substring(count + 1);
53 const headerHtml = wrap(headerContent, headerTag);
54
55 // If we were in a list, close it before starting the header
56 if (inList) {
4757 return [`</ul>${headerHtml}`, false];
4858 } else {
4959 return [headerHtml, false];
5060 }
5161}
5262
53function parseLineItem(markdown, list) {
63// Parse list items (*)
64function parseLineItem(markdown, inList) {
5465 if (markdown.startsWith('*')) {
55 const innerHtml = wrap(parseText(markdown.substring(2), true), 'li');
56 if (list) {
57 return [innerHtml, true];
66 const itemContent = markdown.substring(2);
67 const itemHtml = wrap(parseText(itemContent, true), 'li');
68
69 if (inList) {
70 // Continue existing list
71 return [itemHtml, true];
5872 } else {
59 return [`<ul>${innerHtml}`, true];
73 // Start new list
74 return [`<ul>${itemHtml}`, true];
6075 }
6176 }
62 return [null, list];
77 return [null, inList];
6378}
6479
65function parseParagraph(markdown, list) {
66 if (!list) {
67 return [parseText(markdown, false), false];
68 } else {
80// Parse paragraph elements (default case)
81function parseParagraph(markdown, inList) {
82 if (inList) {
83 // If we were in a list, close it before starting paragraph
6984 return [`</ul>${parseText(markdown, false)}`, false];
85 } else {
86 // Regular paragraph
87 return [parseText(markdown, false), false];
7088 }
7189}
7290
73function parseLine(markdown, list) {
74 let [result, inListAfter] = parseHeader(markdown, list);
91// Parse a single line of markdown
92function parseLine(markdown, inList) {
93 // Try parsing as header first
94 let [result, newListState] = parseHeader(markdown, inList);
7595 if (result === null) {
76 [result, inListAfter] = parseLineItem(markdown, list);
96 // Try parsing as list item
97 [result, newListState] = parseLineItem(markdown, inList);
7798 }
7899 if (result === null) {
79 [result, inListAfter] = parseParagraph(markdown, list);
100 // Default to paragraph
101 [result, newListState] = parseParagraph(markdown, inList);
80102 }
103
81104 if (result === null) {
82 throw new Error('Remove this line and implement the function');
105 throw new Error('Unable to parse line: ' + markdown);
83106 }
84 return [result, inListAfter];
107
108 return [result, newListState];
85109}
86110
87111/**
88 * @param {string} markdown
89 * @returns {string}
112 * Parse markdown text and convert it to HTML
113 * @param {string} markdown - The markdown text to parse
114 * @returns {string} - The resulting HTML
90115 */
91116export function parse(markdown) {
92117 const lines = markdown.split('\n');
93 let result = '';
94 let list = false;
118 let htmlResult = '';
119 let inList = false;
120
95121 for (let i = 0; i < lines.length; i++) {
96 let [lineResult, newList] = parseLine(lines[i], list);
97 result += lineResult;
98 list = newList;
122 let [lineHtml, newListState] = parseLine(lines[i], inList);
123 htmlResult += lineHtml;
124 inList = newListState;
99125 }
100 if (list) {
101 return result + '</ul>';
102 } else {
103 return result;
126
127 // Close any open list at the end
128 if (inList) {
129 htmlResult += '</ul>';
104130 }
131
132 return htmlResult;
105133}
Test NameStatus
parses normal text as a paragraph
Pass
parsing italics
Pass
parsing bold text
Pass
mixed normal, italics and bold text
Pass
with h1 header level
Pass
with h2 header level
Pass
with h3 header level
Pass
with h4 header level
Pass
with h5 header level
Pass
with h6 header level
Pass
with h7 header level
Pass
unordered lists
Pass
with a little bit of everything
Pass
with markdown symbols in the header text that should not be interpreted
Pass
with markdown symbols in the list item text that should not be interpreted
Pass
with markdown symbols in the paragraph text that should not be interpreted
Pass
unordered lists close properly with preceding and following lines
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.