agent

Finished
1212 return markdown.replace(pattern, replacement);
1313}
1414
15function parse__(markdown) {
15// REFACTORED: Renamed parse__ to parseStrong for clarity
16function parseStrong(markdown) {
1617 return parser(markdown, '__', 'strong');
1718}
1819
19function parse_(markdown) {
20// REFACTORED: Renamed parse_ to parseEmphasis for clarity
21function parseEmphasis(markdown) {
2022 return parser(markdown, '_', 'em');
2123}
2224
25// REFACTORED: Simplified parseText function with clearer logic
2326function parseText(markdown, list) {
24 const parsedText = parse_(parse__(markdown));
25 if (list) {
26 return parsedText;
27 } else {
28 return wrap(parsedText, 'p');
29 }
27 // Apply emphasis and strong formatting in order
28 const parsedText = parseEmphasis(parseStrong(markdown));
29
30 // If we're inside a list, return the text as-is
31 // Otherwise, wrap it in a paragraph tag
32 return list ? parsedText : wrap(parsedText, 'p');
3033}
3134
35// REFACTORED: Improved readability of header parsing with clearer variable names and comments
3236function parseHeader(markdown, list) {
33 let count = 0;
37 // Count the number of consecutive '#' characters at the start of the line
38 let headerLevel = 0;
3439 for (let i = 0; i < markdown.length; i++) {
3540 if (markdown[i] === '#') {
36 count += 1;
41 headerLevel += 1;
3742 } else {
3843 break;
3944 }
4045 }
41 if (count === 0 || count > 6) {
46
47 // Valid headers have 1-6 '#' characters
48 if (headerLevel === 0 || headerLevel > 6) {
4249 return [null, list];
4350 }
44 const headerTag = `h${count}`;
45 const headerHtml = wrap(markdown.substring(count + 1), headerTag);
46 if (list) {
47 return [`</ul>${headerHtml}`, false];
48 } else {
49 return [headerHtml, false];
50 }
51
52 // Create the appropriate header tag (h1-h6)
53 const headerTag = `h${headerLevel}`;
54
55 // Extract the header text (skip the '#' characters and the following space)
56 const headerText = markdown.substring(headerLevel + 1);
57 const headerHtml = wrap(headerText, headerTag);
58
59 // If we were in a list, close it before the header
60 // Otherwise, just return the header
61 return list ? [`</ul>${headerHtml}`, false] : [headerHtml, false];
5162}
5263
64// REFACTORED: Improved list item parsing with clearer logic and variable names
5365function parseLineItem(markdown, list) {
66 // Check if this line starts a list item
5467 if (markdown.startsWith('*')) {
55 const innerHtml = wrap(parseText(markdown.substring(2), true), 'li');
56 if (list) {
57 return [innerHtml, true];
58 } else {
59 return [`<ul>${innerHtml}`, true];
60 }
68 // Extract the list item content (skip '* ')
69 const listItemContent = markdown.substring(2);
70
71 // Parse the content and wrap it in li tags
72 const listItemHtml = wrap(parseText(listItemContent, true), 'li');
73
74 // If we're already in a list, just add the item
75 // Otherwise, start a new list with this item
76 return list ? [listItemHtml, true] : [`<ul>${listItemHtml}`, true];
6177 }
78
79 // Not a list item
6280 return [null, list];
6381}
6482
83// REFACTORED: Simplified paragraph parsing with clearer conditional logic
6584function parseParagraph(markdown, list) {
66 if (!list) {
67 return [parseText(markdown, false), false];
68 } else {
69 return [`</ul>${parseText(markdown, false)}`, false];
70 }
85 // Parse the text content
86 const parsedText = parseText(markdown, false);
87
88 // If we were in a list, close it before starting the paragraph
89 // Otherwise, just return the parsed text
90 return list ? [`</ul>${parsedText}`, false] : [parsedText, false];
7191}
7292
93// REFACTORED: Simplified line parsing with explicit conditional logic instead of sequential null checks
7394function parseLine(markdown, list) {
95 // Try to parse as a header first
7496 let [result, inListAfter] = parseHeader(markdown, list);
97
98 // If it's not a header, try to parse as a list item
7599 if (result === null) {
76100 [result, inListAfter] = parseLineItem(markdown, list);
77101 }
102
103 // If it's neither a header nor a list item, parse as a paragraph
78104 if (result === null) {
79105 [result, inListAfter] = parseParagraph(markdown, list);
80106 }
107
108 // This should never happen with valid input
81109 if (result === null) {
82 throw new Error('Remove this line and implement the function');
110 throw new Error('Unable to parse line: ' + markdown);
83111 }
112
84113 return [result, inListAfter];
85114}
86115
87116/**
88 * @param {string} markdown
89 * @returns {string}
117 * REFACTORED: Main markdown parser with improved readability
118 * @param {string} markdown - The markdown string to parse
119 * @returns {string} - The resulting HTML
90120 */
91121export function parse(markdown) {
122 // Split markdown into individual lines
92123 const lines = markdown.split('\n');
93 let result = '';
94 let list = false;
124 let htmlResult = '';
125 let isInList = false;
126
127 // Process each line sequentially
95128 for (let i = 0; i < lines.length; i++) {
96 let [lineResult, newList] = parseLine(lines[i], list);
97 result += lineResult;
98 list = newList;
99 }
100 if (list) {
101 return result + '</ul>';
102 } else {
103 return result;
129 const currentLine = lines[i];
130
131 // Parse the current line and get the result plus list state
132 let [lineHtml, newInListState] = parseLine(currentLine, isInList);
133
134 // Append the HTML for this line
135 htmlResult += lineHtml;
136
137 // Update the list state for the next iteration
138 isInList = newInListState;
104139 }
140
141 // If we ended while still in a list, close it
142 return isInList ? htmlResult + '</ul>' : htmlResult;
105143}
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.