agent

Finished
1212 return markdown.replace(pattern, replacement);
1313}
1414
15function parse__(markdown) {
15// REFACTORED: Renamed from parse__ to parseBold for clarity
16function parseBold(markdown) {
1617 return parser(markdown, '__', 'strong');
1718}
1819
19function parse_(markdown) {
20// REFACTORED: Renamed from parse_ to parseItalic for clarity
21function parseItalic(markdown) {
2022 return parser(markdown, '_', 'em');
2123}
2224
25// REFACTORED: Improved function to use renamed functions and clearer variable names
2326function parseText(markdown, list) {
24 const parsedText = parse_(parse__(markdown));
27 const parsedText = parseItalic(parseBold(markdown));
2528 if (list) {
2629 return parsedText;
2730 } else {
2932 }
3033}
3134
35// REFACTORED: Improved variable names and added comments for clarity
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 // Invalid header if no # or more than 6 # characters
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 // Close any open list before adding header
4658 if (list) {
4759 return [`</ul>${headerHtml}`, false];
4860 } else {
5062 }
5163}
5264
65// REFACTORED: Improved variable names and added comments for clarity
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 return [listItemHtml, true];
5873 } else {
59 return [`<ul>${innerHtml}`, true];
74 return [`<ul>${listItemHtml}`, true];
6075 }
6176 }
6277 return [null, list];
6378}
6479
80// REFACTORED: Improved variable names and added comments for clarity
6581function parseParagraph(markdown, list) {
82 const paragraphHtml = parseText(markdown, false);
83
6684 if (!list) {
67 return [parseText(markdown, false), false];
85 return [paragraphHtml, false];
6886 } else {
69 return [`</ul>${parseText(markdown, false)}`, false];
87 // Close any open list before adding paragraph
88 return [`</ul>${paragraphHtml}`, false];
7089 }
7190}
7291
92// REFACTORED: Improved variable names and added comments for clarity
7393function parseLine(markdown, list) {
74 let [result, inListAfter] = parseHeader(markdown, list);
75 if (result === null) {
76 [result, inListAfter] = parseLineItem(markdown, list);
94 let [htmlResult, isInListAfter] = parseHeader(markdown, list);
95
96 // Try to parse as list item if not a header
97 if (htmlResult === null) {
98 [htmlResult, isInListAfter] = parseLineItem(markdown, list);
7799 }
78 if (result === null) {
79 [result, inListAfter] = parseParagraph(markdown, list);
100
101 // Try to parse as paragraph if not header or list item
102 if (htmlResult === null) {
103 [htmlResult, isInListAfter] = parseParagraph(markdown, list);
80104 }
81 if (result === null) {
82 throw new Error('Remove this line and implement the function');
105
106 // This should never happen as one of the parsers should always succeed
107 if (htmlResult === null) {
108 throw new Error('Unable to parse markdown line');
83109 }
84 return [result, inListAfter];
110
111 return [htmlResult, isInListAfter];
85112}
86113
87114/**
88 * @param {string} markdown
89 * @returns {string}
115 * Parse markdown text and convert it to HTML
116 * @param {string} markdown - The markdown text to parse
117 * @returns {string} - The resulting HTML
90118 */
91119export function parse(markdown) {
92120 const lines = markdown.split('\n');
93 let result = '';
94 let list = false;
121 let htmlResult = '';
122 let isInList = false;
123
95124 for (let i = 0; i < lines.length; i++) {
96 let [lineResult, newList] = parseLine(lines[i], list);
97 result += lineResult;
98 list = newList;
125 let [lineHtml, newListState] = parseLine(lines[i], isInList);
126 htmlResult += lineHtml;
127 isInList = newListState;
99128 }
100 if (list) {
101 return result + '</ul>';
129
130 // Close any open list at the end of the document
131 if (isInList) {
132 return htmlResult + '</ul>';
102133 } else {
103 return result;
134 return htmlResult;
104135 }
105136}
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.