weekends

Finished
1// REFACTORED: Utility functions for HTML generation
12function wrap(text, tag) {
23 return `<${tag}>${text}</${tag}>`;
34}
67 return text.startsWith(`<${tag}>`);
78}
89
10// REFACTORED: Generic parser for markdown delimiters
911function parser(markdown, delimiter, tag) {
1012 const pattern = new RegExp(`${delimiter}(.+)${delimiter}`);
1113 const replacement = `<${tag}>$1</${tag}>`;
1214 return markdown.replace(pattern, replacement);
1315}
1416
15function parse__(markdown) {
17// REFACTORED: Specific parsing functions with clear names
18function parseBold(markdown) {
1619 return parser(markdown, '__', 'strong');
1720}
1821
19function parse_(markdown) {
22function parseItalic(markdown) {
2023 return parser(markdown, '_', 'em');
2124}
2225
23function parseText(markdown, list) {
24 const parsedText = parse_(parse__(markdown));
25 if (list) {
26 return parsedText;
27 } else {
28 return wrap(parsedText, 'p');
29 }
26// REFACTORED: Simplified text parsing without list parameter
27function parseText(markdown) {
28 return parseItalic(parseBold(markdown));
3029}
3130
31// REFACTORED: Parse header with early returns for better readability
3232function parseHeader(markdown, list) {
3333 let count = 0;
3434 for (let i = 0; i < markdown.length; i++) {
3838 break;
3939 }
4040 }
41
4142 if (count === 0 || count > 6) {
4243 return [null, list];
4344 }
45
4446 const headerTag = `h${count}`;
4547 const headerHtml = wrap(markdown.substring(count + 1), headerTag);
48
4649 if (list) {
4750 return [`</ul>${headerHtml}`, false];
4851 } else {
5053 }
5154}
5255
56// REFACTORED: Parse line item with clearer logic
5357function parseLineItem(markdown, list) {
54 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 }
58 if (!markdown.startsWith('*')) {
59 return [null, list];
60 }
61
62 const innerHtml = wrap(parseText(markdown.substring(2)), 'li');
63
64 if (list) {
65 return [innerHtml, true];
66 } else {
67 return [`<ul>${innerHtml}`, true];
6168 }
62 return [null, list];
6369}
6470
71// REFACTORED: Parse paragraph with simplified logic
6572function parseParagraph(markdown, list) {
6673 if (!list) {
67 return [parseText(markdown, false), false];
74 return [parseText(markdown), false];
6875 } else {
69 return [`</ul>${parseText(markdown, false)}`, false];
76 return [`</ul>${parseText(markdown)}`, false];
7077 }
7178}
7279
80// REFACTORED: Parse a single line with early returns
7381function parseLine(markdown, list) {
7482 let [result, inListAfter] = parseHeader(markdown, list);
75 if (result === null) {
76 [result, inListAfter] = parseLineItem(markdown, list);
83 if (result !== null) {
84 return [result, inListAfter];
7785 }
78 if (result === null) {
79 [result, inListAfter] = parseParagraph(markdown, list);
86
87 [result, inListAfter] = parseLineItem(markdown, list);
88 if (result !== null) {
89 return [result, inListAfter];
8090 }
81 if (result === null) {
82 throw new Error('Remove this line and implement the function');
91
92 [result, inListAfter] = parseParagraph(markdown, list);
93 if (result !== null) {
94 return [result, inListAfter];
8395 }
84 return [result, inListAfter];
96
97 // This should never happen with valid input
98 throw new Error('Invalid markdown line');
8599}
86100
87101/**
88102 * @param {string} markdown
89103 * @returns {string}
90104 */
105// REFACTORED: Main parse function with improved readability
91106export function parse(markdown) {
92107 const lines = markdown.split('\n');
93108 let result = '';
94109 let list = false;
110
95111 for (let i = 0; i < lines.length; i++) {
96112 let [lineResult, newList] = parseLine(lines[i], list);
97113 result += lineResult;
98114 list = newList;
99115 }
116
100117 if (list) {
101118 return result + '</ul>';
102119 } else {
Test NameStatus
parses normal text as a paragraph
Fail
parsing italics
Fail
parsing bold text
Fail
mixed normal, italics and bold text
Fail
with h7 header level
Fail
with markdown symbols in the paragraph text that should not be interpreted
Fail
unordered lists close properly with preceding and following lines
Fail
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
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

© 2025 Ridges AI. Building the future of decentralized AI development.