fistinz

Finished
1212 return markdown.replace(pattern, replacement);
1313}
1414
15function parse__(markdown) {
15// Refactored: Renamed parse__ to parseBold for clarity
16function parseBold(markdown) {
1617 return parser(markdown, '__', 'strong');
1718}
1819
19function parse_(markdown) {
20// Refactored: Renamed parse_ to parseItalic for clarity
21function parseItalic(markdown) {
2022 return parser(markdown, '_', 'em');
2123}
2224
25// Refactored: Improved parseText function with clearer variable names and logic
2326function parseText(markdown, list) {
24 const parsedText = parse_(parse__(markdown));
27 // Apply bold and italic parsing in the correct order
28 const parsedText = parseItalic(parseBold(markdown));
2529 if (list) {
2630 return parsedText;
2731 } else {
2933 }
3034}
3135
36// Refactored: Improved parseHeader function with clearer logic and comments
3237function parseHeader(markdown, list) {
33 let count = 0;
38 // Count consecutive # characters at the start of the line
39 let headerLevel = 0;
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 must have 1-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);
46 if (list) {
47 return [`</ul>${headerHtml}`, false];
48 } else {
49 return [headerHtml, false];
50 }
52
53 // Extract header content (skip the space after #)
54 const headerContent = markdown.substring(headerLevel + 1);
55 const headerTag = `h${headerLevel}`;
56 const headerHtml = wrap(headerContent, headerTag);
57
58 // Close any open list when transitioning to header
59 const inListAfter = false;
60 const resultHtml = list ? `</ul>${headerHtml}` : headerHtml;
61
62 return [resultHtml, inListAfter];
5163}
5264
65// Refactored: Improved parseLineItem function with clearer logic and comments
5366function parseLineItem(markdown, list) {
67 // Check if this line starts a list item
5468 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 }
69 // Extract the list item content (skip '* ')
70 const itemContent = markdown.substring(2);
71 // Parse the content and wrap it in li tags
72 const listItemHtml = wrap(parseText(itemContent, true), 'li');
73
74 // If we're already in a list, just add the item
75 // Otherwise, start a new list
76 const inListAfter = true;
77 const resultHtml = list ? listItemHtml : `<ul>${listItemHtml}`;
78
79 return [resultHtml, inListAfter];
6180 }
81
82 // Not a list item
6283 return [null, list];
6384}
6485
86// Refactored: Improved parseParagraph function with clearer logic and comments
6587function parseParagraph(markdown, list) {
66 if (!list) {
67 return [parseText(markdown, false), false];
68 } else {
69 return [`</ul>${parseText(markdown, false)}`, false];
70 }
88 // Parse the text as a paragraph
89 const paragraphHtml = parseText(markdown, false);
90
91 // If we're in a list, close it first
92 // Otherwise, just return the paragraph
93 const inListAfter = false;
94 const resultHtml = list ? `</ul>${paragraphHtml}` : paragraphHtml;
95
96 return [resultHtml, inListAfter];
7197}
7298
99// Refactored: Simplified parseLine function with explicit parsing order
73100function parseLine(markdown, list) {
101 // Try parsing as header first
74102 let [result, inListAfter] = parseHeader(markdown, list);
75 if (result === null) {
76 [result, inListAfter] = parseLineItem(markdown, list);
103 if (result !== null) {
104 return [result, inListAfter];
77105 }
78 if (result === null) {
79 [result, inListAfter] = parseParagraph(markdown, list);
80 }
81 if (result === null) {
82 throw new Error('Remove this line and implement the function');
106
107 // Try parsing as list item
108 [result, inListAfter] = parseLineItem(markdown, list);
109 if (result !== null) {
110 return [result, inListAfter];
83111 }
112
113 // Default to paragraph parsing
114 [result, inListAfter] = parseParagraph(markdown, list);
84115 return [result, inListAfter];
85116}
86117
87118/**
88 * @param {string} markdown
89 * @returns {string}
119 * Parse Markdown text and convert it to HTML
120 * @param {string} markdown - The Markdown text to parse
121 * @returns {string} - The resulting HTML
90122 */
91123export function parse(markdown) {
124 // Split the markdown into individual lines
92125 const lines = markdown.split('\n');
93 let result = '';
94 let list = false;
126 let htmlResult = '';
127 let isInList = false;
128
129 // Process each line individually
95130 for (let i = 0; i < lines.length; i++) {
96 let [lineResult, newList] = parseLine(lines[i], list);
97 result += lineResult;
98 list = newList;
131 const [lineHtml, newListState] = parseLine(lines[i], isInList);
132 htmlResult += lineHtml;
133 isInList = newListState;
99134 }
100 if (list) {
101 return result + '</ul>';
135
136 // Close any open list at the end of the document
137 if (isInList) {
138 return htmlResult + '</ul>';
102139 } else {
103 return result;
140 return htmlResult;
104141 }
105142}
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.