armor

Finished
1// =============================================================================
2// UTILITY FUNCTIONS
3// =============================================================================
4
5/**
6 * Wraps text in an HTML tag
7 * @param {string} text - The text to wrap
8 * @param {string} tag - The HTML tag name
9 * @returns {string} The wrapped text
10 */
111function wrap(text, tag) {
212 return `<${tag}>${text}</${tag}>`;
313}
414
15/**
16 * Checks if text starts with a specific HTML tag
17 * @param {string} text - The text to check
18 * @param {string} tag - The tag name to check for
19 * @returns {boolean} True if text starts with the tag
20 */
521function isTag(text, tag) {
622 return text.startsWith(`<${tag}>`);
723}
824
925function parser(markdown, delimiter, tag) {
10 const pattern = new RegExp(`${delimiter}(.+)${delimiter}`);
26 // Use non-greedy matching and word boundaries to avoid over-matching
27 const pattern = new RegExp(`${delimiter}([^${delimiter}]+)${delimiter}`, 'g');
1128 const replacement = `<${tag}>$1</${tag}>`;
1229 return markdown.replace(pattern, replacement);
1330}
1431
15function parse__(markdown) {
32function parseAsteriskBold(markdown) {
33 // Parse **bold** text
34 return parser(markdown, '\\\*\\\*', 'strong');
35}
36
37function parseAsteriskItalic(markdown) {
38 // Parse *italic* text
39 return parser(markdown, '\\\*', 'em');
40}
41
42function parseUnderscoreBold(markdown) {
43 // Parse __bold__ text
1644 return parser(markdown, '__', 'strong');
1745}
1846
19function parse_(markdown) {
47function parseUnderscoreItalic(markdown) {
48 // Parse _italic_ text
2049 return parser(markdown, '_', 'em');
2150}
2251
2352function parseText(markdown, list) {
24 const parsedText = parse_(parse__(markdown));
53 // Parse emphasis and bold formatting
54 // Order matters: parse bold before italic to handle nested formatting correctly
55 let parsedText = markdown;
56
57 // Parse bold formatting first (** and __)
58 parsedText = parseAsteriskBold(parsedText);
59 parsedText = parseUnderscoreBold(parsedText);
60
61 // Then parse italic formatting (* and _)
62 parsedText = parseAsteriskItalic(parsedText);
63 parsedText = parseUnderscoreItalic(parsedText);
64
2565 if (list) {
2666 return parsedText;
2767 } else {
5191}
5292
5393function parseLineItem(markdown, list) {
94 // Original behavior: treat any line starting with '*' as a list item
95 // This is maintained for backward compatibility with the original implementation
5496 if (markdown.startsWith('*')) {
5597 const innerHtml = wrap(parseText(markdown.substring(2), true), 'li');
5698 if (list) {
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.