agent

Finished
1212 return markdown.replace(pattern, replacement);
1313}
1414
15function parse__(markdown) {
15// REFACTORED: Renamed parse__ to parseBoldText for clarity
16function parseBoldText(markdown) {
1617 return parser(markdown, '__', 'strong');
1718}
1819
19function parse_(markdown) {
20// REFACTORED: Renamed parse_ to parseItalicText for clarity
21function parseItalicText(markdown) {
2022 return parser(markdown, '_', 'em');
2123}
2224
25// REFACTORED: Updated to use renamed functions and improved clarity
2326function parseText(markdown, list) {
24 const parsedText = parse_(parse__(markdown));
27 const parsedText = parseItalicText(parseBoldText(markdown));
2528 if (list) {
2629 return parsedText;
2730 } else {
2932 }
3033}
3134
35// REFACTORED: Improved variable names and added clarity for header parsing
3236function parseHeader(markdown, list) {
33 let count = 0;
37 let headerLevel = 0;
38
39 // Count consecutive # characters to determine header level
3440 for (let i = 0; i < markdown.length; i++) {
3541 if (markdown[i] === '#') {
36 count += 1;
42 headerLevel += 1;
3743 } else {
3844 break;
3945 }
4046 }
41 if (count === 0 || count > 6) {
47
48 // Valid headers are 1-6 levels
49 if (headerLevel === 0 || headerLevel > 6) {
4250 return [null, list];
4351 }
44 const headerTag = `h${count}`;
45 const headerHtml = wrap(markdown.substring(count + 1), headerTag);
52
53 const headerTag = `h${headerLevel}`;
54 const headerContent = markdown.substring(headerLevel + 1).trim();
55 const headerHtml = wrap(headerContent, headerTag);
56
57 // If we're in a list, close it before adding header
4658 if (list) {
4759 return [`</ul>${headerHtml}`, false];
4860 } else {
5062 }
5163}
5264
65// REFACTORED: Improved clarity for list item parsing
5366function parseLineItem(markdown, list) {
5467 if (markdown.startsWith('*')) {
55 const innerHtml = wrap(parseText(markdown.substring(2), true), 'li');
68 const listItemContent = markdown.substring(2).trim();
69 const listItemHtml = wrap(parseText(listItemContent, true), 'li');
70
5671 if (list) {
57 return [innerHtml, true];
72 // Continue existing list
73 return [listItemHtml, true];
5874 } else {
59 return [`<ul>${innerHtml}`, true];
75 // Start new list
76 return [`<ul>${listItemHtml}`, true];
6077 }
6178 }
6279 return [null, list];
6380}
6481
82// REFACTORED: Improved clarity for paragraph parsing
6583function parseParagraph(markdown, list) {
6684 if (!list) {
85 // Not in a list, wrap in paragraph tags
6786 return [parseText(markdown, false), false];
6887 } else {
88 // In a list, close it and start a new paragraph
6989 return [`</ul>${parseText(markdown, false)}`, false];
7090 }
7191}
7292
93// REFACTORED: Simplified control flow with clearer pattern matching
7394function parseLine(markdown, list) {
95 // Try parsing as header first
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 parsing 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 // Default to paragraph
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 * Parses markdown text and converts it to HTML
119 * @param {string} markdown - The markdown text to parse
120 * @returns {string} - The resulting HTML
90121 */
122// REFACTORED: Improved variable names and added clarity to main parsing loop
91123export function parse(markdown) {
92124 const lines = markdown.split('\n');
93 let result = '';
94 let list = false;
125 let htmlResult = '';
126 let isInList = 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 const [lineHtml, newInListState] = parseLine(lines[i], isInList);
131 htmlResult += lineHtml;
132 isInList = newInListState;
99133 }
100 if (list) {
101 return result + '</ul>';
134
135 // Close any unclosed list
136 if (isInList) {
137 return htmlResult + '</ul>';
102138 } else {
103 return result;
139 return htmlResult;
104140 }
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.