Skip to content

Commit

Permalink
md stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
targoninc-alex committed Apr 28, 2024
1 parent 9f18c7e commit 86dfe51
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
63 changes: 62 additions & 1 deletion ui/js/MarkdownProcessor.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,55 @@ export class MarkdownProcessor {
type: 'code',
text: line.replace(/```/, '')
});
} else if (line.match(/\*\*(.*?)\*\*/)) { // **bold** text
const text = line.match(/\*\*(.*?)\*\*/)[1];
elements.push({
type: 'bold',
text
});
} else if (line.match(/\*(.*?)\*/)) { // *italic* text
const text = line.match(/\*(.*?)\*/)[1];
elements.push({
type: 'italic',
text
});
} else if (line.match(/\*\*\*(.*?)\*\*\*/)) { // For ***bold and italic*** text
const text = line.match(/\*\*\*(.*?)\*\*\*/)[1];
elements.push({
type: 'boldItalic',
text
});
} else if (line.startsWith('>')) { // For quotes
const level = line.match(/>+/)[0].length;
const text = line.replace(/>+\s*/, '');
elements.push({
type: 'quote',
level,
text
});
} else if (line.startsWith('-')) { // For list item
const level = line.match(/-+/)[0].length;
const text = line.replace(/-+\s*/, '');
elements.push({
type: 'listItem',
level,
text
});
} else if (line.match(/\d\. (.*?)/)) { // For numbered list item
const number = line.match(/\d/)[0];
const text = line.replace(/\d\. /, '');
elements.push({
type: 'numberedList',
number,
text
});
} else {
elements.push({
type: 'paragraph',
text: line
});
}

}
return elements;
}
Expand All @@ -68,11 +111,29 @@ export class MarkdownProcessor {
case 'code':
nodes.push(MarkdownTemplates.code(element.text));
break;
case 'bold':
nodes.push(MarkdownTemplates.bold(element.text));
break;
case 'italic':
nodes.push(MarkdownTemplates.italic(element.text));
break;
case 'boldItalic':
nodes.push(MarkdownTemplates.boldItalic(element.text));
break;
case 'quote':
nodes.push(MarkdownTemplates.quote(element.text));
break;
case 'listItem':
nodes.push(MarkdownTemplates.listItem(element.text));
break;
case 'numberedList':
nodes.push(MarkdownTemplates.numberedListItem(element.text, element.number));
break;
default:
nodes.push(MarkdownTemplates.paragraph(element.text));
break;
}
}
return nodes;
}
}
}
42 changes: 42 additions & 0 deletions ui/templates/MarkdownTemplates.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,46 @@ export class MarkdownTemplates {
.text(text)
.build();
}

static bold(text) {
return create("strong")
.classes("markdown")
.text(text)
.build();
}

static italic(text) {
return create("em")
.classes("markdown")
.text(text)
.build();
}

static boldItalic(text) {
return create("strong")
.classes("markdown", "italic")
.text(text)
.build();
}

static quote(text) {
return create("blockquote")
.classes("markdown")
.text(text)
.build();
}

static listItem(text) {
return create("li")
.classes("markdown")
.text(text)
.build();
}

static numberedListItem(text, number) {
return create("li")
.classes("markdown")
.text(`${number}. ${text}`)
.build();
}
}

0 comments on commit 86dfe51

Please sign in to comment.