agent

Finished
1212 return markdown.replace(pattern, replacement);
1313}
1414
15function parse__(markdown) {
15// Parse bold text (double underscore syntax)
16function parseBold(markdown) {
1617 return parser(markdown, '__', 'strong');
1718}
1819
19function parse_(markdown) {
20// Parse italic text (single underscore syntax)
21function parseItalic(markdown) {
2022 return parser(markdown, '_', 'em');
2123}
2224
25// Parse text content with emphasis (bold/italic) and wrap in paragraph if not in list
2326function parseText(markdown, list) {
24 const parsedText = parse_(parse__(markdown));
27 const parsedText = parseItalic(parseBold(markdown));
2528 if (list) {
2629 return parsedText;
2730 } else {
2831 return wrap(parsedText, 'p');
2932 }
30}
33 }
34
35// Maximum header level supported
36const MAX_HEADER_LEVEL = 6;
3137
38// Parse header markdown (#, ##, ###, etc.) and convert to HTML
3239function parseHeader(markdown, list) {
3340 let count = 0;
3441 for (let i = 0; i < markdown.length; i++) {
3845 break;
3946 }
4047 }
41 if (count === 0 || count > 6) {
48
49 // Invalid header: no # or more than 6 # characters
50 if (count === 0 || count > MAX_HEADER_LEVEL) {
4251 return [null, list];
4352 }
53
4454 const headerTag = `h${count}`;
4555 const headerHtml = wrap(markdown.substring(count + 1), headerTag);
56
57 // Close list if we're currently in one
4658 if (list) {
4759 return [`</ul>${headerHtml}`, false];
4860 } else {
5062 }
5163}
5264
65// Parse list item markdown (starting with *) and convert to HTML
5366function parseLineItem(markdown, list) {
5467 if (markdown.startsWith('*')) {
55 const innerHtml = wrap(parseText(markdown.substring(2), true), 'li');
68 const itemContent = markdown.substring(2); // Remove "* " prefix
69 const innerHtml = wrap(parseText(itemContent, true), 'li');
70
5671 if (list) {
72 // Continue existing list
5773 return [innerHtml, true];
5874 } else {
75 // Start new list
5976 return [`<ul>${innerHtml}`, true];
6077 }
6178 }
6279 return [null, list];
6380}
6481
82// Parse regular paragraph text and convert to HTML
6583function parseParagraph(markdown, list) {
6684 if (!list) {
85 // Regular paragraph
6786 return [parseText(markdown, false), false];
6887 } else {
88 // Close list and start paragraph
6989 return [`</ul>${parseText(markdown, false)}`, false];
7090 }
7191}
7292
93// Parse a single line of markdown and convert to HTML
7394function parseLine(markdown, list) {
95 // Try to parse as header
7496 let [result, inListAfter] = parseHeader(markdown, list);
75 if (result === null) {
76 [result, inListAfter] = parseLineItem(markdown, list);
97 if (result !== null) {
98 return [result, inListAfter];
7799 }
78 if (result === null) {
79 [result, inListAfter] = parseParagraph(markdown, list);
100
101 // Try to parse as list item
102 [result, inListAfter] = parseLineItem(markdown, list);
103 if (result !== null) {
104 return [result, inListAfter];
80105 }
81 if (result === null) {
82 throw new Error('Remove this line and implement the function');
106
107 // Parse as regular paragraph (fallback)
108 [result, inListAfter] = parseParagraph(markdown, list);
109 if (result !== null) {
110 return [result, inListAfter];
83111 }
84 return [result, inListAfter];
112
113 // This should never happen with valid input
114 throw new Error('Invalid markdown line');
85115}
86116
87117/**
88 * @param {string} markdown
89 * @returns {string}
118 * Parse markdown text and convert to HTML
119 * Supports headers (#, ##, ###, etc.), bold (__text__), italic (_text_), and lists (* item)
120 * @param {string} markdown - The markdown text to parse
121 * @returns {string} - The resulting HTML
90122 */
91123export function parse(markdown) {
92124 const lines = markdown.split('\n');
93 let result = '';
94 let list = false;
125 let html = '';
126 let inList = false;
127
128 // Process each line of markdown
95129 for (let i = 0; i < lines.length; i++) {
96 let [lineResult, newList] = parseLine(lines[i], list);
97 result += lineResult;
98 list = newList;
130 let [lineHtml, newListState] = parseLine(lines[i], inList);
131 html += lineHtml;
132 inList = newListState;
99133 }
100 if (list) {
101 return result + '</ul>';
102 } else {
103 return result;
134
135 // Close any open list at the end
136 if (inList) {
137 html += '</ul>';
104138 }
139
140 return html;
105141}
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.