fishlium

Finished
1// Helper function to wrap text in HTML tags
12function wrap(text, tag) {
23 return `<${tag}>${text}</${tag}>`;
34}
45
5function isTag(text, tag) {
6 return text.startsWith(`<${tag}>`);
7}
8
9function parser(markdown, delimiter, tag) {
10 const pattern = new RegExp(`${delimiter}(.+)${delimiter}`);
11 const replacement = `<${tag}>$1</${tag}>`;
6// Parse bold text (double underscores)
7function parseBold(markdown) {
8 const pattern = new RegExp('__(.+)__');
9 const replacement = '<strong>$1</strong>';
1210 return markdown.replace(pattern, replacement);
1311}
1412
15function parse__(markdown) {
16 return parser(markdown, '__', 'strong');
13// Parse italic text (single underscores)
14function parseItalic(markdown) {
15 const pattern = new RegExp('_(.+)_');
16 const replacement = '<em>$1</em>';
17 return markdown.replace(pattern, replacement);
1718}
1819
19function parse_(markdown) {
20 return parser(markdown, '_', 'em');
20// Parse inline formatting (bold and italic)
21function parseInlineFormatting(markdown) {
22 return parseItalic(parseBold(markdown));
2123}
2224
23function parseText(markdown, list) {
24 const parsedText = parse_(parse__(markdown));
25 if (list) {
25// Parse text with appropriate wrapper tags
26function parseText(markdown, inList) {
27 const parsedText = parseInlineFormatting(markdown);
28 if (inList) {
2629 return parsedText;
2730 } else {
2831 return wrap(parsedText, 'p');
2932 }
3033}
3134
32function parseHeader(markdown, list) {
35// Parse header elements (#, ##, ###, etc.)
36function parseHeader(markdown, inList) {
3337 let count = 0;
3438 for (let i = 0; i < markdown.length; i++) {
3539 if (markdown[i] === '#') {
3842 break;
3943 }
4044 }
45
46 // Headers must be between 1 and 6 # characters
4147 if (count === 0 || count > 6) {
42 return [null, list];
48 return [null, inList];
4349 }
50
4451 const headerTag = `h${count}`;
45 const headerHtml = wrap(markdown.substring(count + 1), headerTag);
46 if (list) {
52 const headerContent = markdown.substring(count + 1);
53 const headerHtml = wrap(headerContent, headerTag);
54
55 // If we were in a list, close it before the header
56 if (inList) {
4757 return [`</ul>${headerHtml}`, false];
4858 } else {
4959 return [headerHtml, false];
5060 }
5161}
5262
53function parseLineItem(markdown, list) {
63// Parse list items (lines starting with *)
64function parseListItem(markdown, inList) {
5465 if (markdown.startsWith('*')) {
55 const innerHtml = wrap(parseText(markdown.substring(2), true), 'li');
56 if (list) {
66 const itemContent = markdown.substring(2); // Skip "* "
67 const innerHtml = wrap(parseText(itemContent, true), 'li');
68
69 if (inList) {
70 // Continue existing list
5771 return [innerHtml, true];
5872 } else {
73 // Start new list
5974 return [`<ul>${innerHtml}`, true];
6075 }
6176 }
62 return [null, list];
77 return [null, inList];
6378}
6479
65function parseParagraph(markdown, list) {
66 if (!list) {
67 return [parseText(markdown, false), false];
68 } else {
80// Parse paragraph elements (default case)
81function parseParagraph(markdown, inList) {
82 if (inList) {
83 // If we were in a list, close it before the paragraph
6984 return [`</ul>${parseText(markdown, false)}`, false];
85 } else {
86 return [parseText(markdown, false), false];
7087 }
7188}
7289
73function parseLine(markdown, list) {
74 let [result, inListAfter] = parseHeader(markdown, list);
90// Parse a single line of markdown
91function parseLine(markdown, inList) {
92 // Try to parse as header first
93 let [result, newList] = parseHeader(markdown, inList);
94
95 // If not a header, try to parse as list item
7596 if (result === null) {
76 [result, inListAfter] = parseLineItem(markdown, list);
97 [result, newList] = parseListItem(markdown, inList);
7798 }
99
100 // If not a list item, parse as paragraph
78101 if (result === null) {
79 [result, inListAfter] = parseParagraph(markdown, list);
102 [result, newList] = parseParagraph(markdown, inList);
80103 }
104
105 // This should never happen with valid input
81106 if (result === null) {
82 throw new Error('Remove this line and implement the function');
107 throw new Error('Unable to parse line: ' + markdown);
83108 }
84 return [result, inListAfter];
109
110 return [result, newList];
85111}
86112
87113/**
88 * @param {string} markdown
89 * @returns {string}
114 * Parse markdown text and convert to HTML
115 * @param {string} markdown - The markdown text to parse
116 * @returns {string} - The resulting HTML
90117 */
91118export function parse(markdown) {
92119 const lines = markdown.split('\n');
93120 let result = '';
94 let list = false;
95 for (let i = 0; i < lines.length; i++) {
96 let [lineResult, newList] = parseLine(lines[i], list);
121 let inList = false;
122
123 for (const line of lines) {
124 let [lineResult, newList] = parseLine(line, inList);
97125 result += lineResult;
98 list = newList;
126 inList = newList;
99127 }
100 if (list) {
101 return result + '</ul>';
102 } else {
103 return result;
128
129 // Close any open list at the end
130 if (inList) {
131 result += '</ul>';
104132 }
133
134 return result;
105135}
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.