harrison

Finished
1function wrap(text, tag) {
2 return `<${tag}>${text}</${tag}>`;
3}
4
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}>`;
12 return markdown.replace(pattern, replacement);
13}
14
15function parse__(markdown) {
16 return parser(markdown, '__', 'strong');
17}
18
19function parse_(markdown) {
20 return parser(markdown, '_', 'em');
21}
22
23function parseText(markdown, list) {
24 const parsedText = parse_(parse__(markdown));
25 if (list) {
26 return parsedText;
27 } else {
28 return wrap(parsedText, 'p');
29 }
30}
31
32function parseHeader(markdown, list) {
33 let count = 0;
34 for (let i = 0; i < markdown.length; i++) {
35 if (markdown[i] === '#') {
36 count += 1;
37 } else {
38 break;
1/**
2 * Parses Markdown syntax and returns equivalent HTML
3 * @param {string} markdown - The markdown text to parse
4 * @returns {string} - The equivalent HTML
5 */
6export function parse(markdown) {
7 const lines = markdown.split('\n');
8 let html = '';
9 let inList = false;
10
11 for (const line of lines) {
12 let lineHtml = '';
13 let currentlyInList = inList;
14
15 // Check for headers (#, ##, ###, etc.)
16 const headerMatch = line.match(/^(#{1,6})\s+(.*)$/);
17 if (headerMatch) {
18 const level = headerMatch[1].length;
19 const content = headerMatch[2];
20 const headerTag = `h${level}`;
21
22 // Close list if we were in one
23 if (inList) {
24 lineHtml += '</ul>';
25 currentlyInList = false;
26 }
27
28 lineHtml += `<${headerTag}>${parseInlineFormatting(content)}</${headerTag}>`;
3929 }
40 }
41 if (count === 0 || count > 6) {
42 return [null, list];
43 }
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
53function 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];
30 // Check for list items (* item)
31 else if (line.startsWith('* ')) {
32 const content = line.substring(2);
33
34 // Start list if not already in one
35 if (!inList) {
36 lineHtml += '<ul>';
37 currentlyInList = true;
38 }
39
40 lineHtml += `<li>${parseInlineFormatting(content)}</li>`;
6041 }
42 // Regular paragraph
43 else {
44 // Close list if we were in one
45 if (inList) {
46 lineHtml += '</ul>';
47 currentlyInList = false;
48 }
49
50 lineHtml += `<p>${parseInlineFormatting(line)}</p>`;
51 }
52
53 html += lineHtml;
54 inList = currentlyInList;
6155 }
62 return [null, list];
63}
64
65function parseParagraph(markdown, list) {
66 if (!list) {
67 return [parseText(markdown, false), false];
68 } else {
69 return [`</ul>${parseText(markdown, false)}`, false];
70 }
71}
72
73function parseLine(markdown, list) {
74 let [result, inListAfter] = parseHeader(markdown, list);
75 if (result === null) {
76 [result, inListAfter] = parseLineItem(markdown, list);
77 }
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');
56
57 // Close any open list at the end
58 if (inList) {
59 html += '</ul>';
8360 }
84 return [result, inListAfter];
61
62 return html;
8563}
8664
8765/**
88 * @param {string} markdown
89 * @returns {string}
66 * Parses inline formatting like bold (__text__) and italic (_text_)
67 * @param {string} text - The text to parse
68 * @returns {string} - The text with inline formatting converted to HTML
9069 */
91export function parse(markdown) {
92 const lines = markdown.split('\n');
93 let result = '';
94 let list = false;
95 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;
104 }
70function parseInlineFormatting(text) {
71 // Parse bold (__text__) - use non-greedy matching and word boundaries
72 text = text.replace(/__(.+?)__/g, '<strong>$1</strong>');
73 // Parse italic (_text_) - use non-greedy matching and word boundaries
74 text = text.replace(/_(.+?)_/g, '<em>$1</em>');
75
76 return text;
10577}
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.