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 8ee614b commit f204f69
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
28 changes: 21 additions & 7 deletions ui/js/MarkdownProcessor.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,23 @@ export class MarkdownProcessor {

static parseText(text) {
const elements = [];

// Create regular expressions for each formatting type
const formattingTypes = [
{name: 'boldItalic', regex: /\*\*\*(.*?)\*\*\*/g},
{name: 'bold', regex: /\*\*(.*?)\*\*/g},
{name: 'bold', regex: /\*\*(.*?)?\*\*/g},
{name: 'italic', regex: /\*(.*?)\*/g},
{name: 'link', regex: /\[(.*?)]\((.*?)\)/g},
{name: 'image', regex: /!\[(.*?)]\((.*?)\)/g},
];

// Keep examining the text until all formatting has been extracted
while (text.length > 0) {
// Find the nearest formatting
let nearest = {
index: text.length
};
let nearestType;

for (let type of formattingTypes) {
type.regex.lastIndex = 0; // Reset lastIndex to start search from beginning of string
const match = type.regex.exec(text);
if (match && match.index < nearest.index) {
console.log(match, type.name);
Expand All @@ -105,11 +103,9 @@ export class MarkdownProcessor {
}
elements.push(element);
}

// Remove the processed portion of the text
text = text.substring(nearest.index + (nearest[0] ? nearest[0].length : 0));
}

return elements;
}

Expand All @@ -120,7 +116,7 @@ export class MarkdownProcessor {
}

static generateHtml(elements) {
const nodes = [];
let nodes = [];
for (let element of elements) {
const parsedNodes = MarkdownProcessor.processText(element.text);
if (parsedNodes.length > 1) {
Expand Down Expand Up @@ -156,6 +152,24 @@ export class MarkdownProcessor {
break;
}
}
nodes = MarkdownProcessor.postProcessBlockQuotes(nodes);
return nodes;
}

static postProcessBlockQuotes(nodes) {
// if any blockquote is followed by another blockquote,
// merge their children into the first blockquote and remove the second blockquote
for (let i = 0; i < nodes.length - 1; i++) {
if (nodes[i].tagName === 'BLOCKQUOTE' && nodes[i + 1].tagName === 'BLOCKQUOTE') {
const firstBlockQuote = nodes[i];
const secondBlockQuote = nodes[i + 1];
for (let child of secondBlockQuote.children) {
firstBlockQuote.appendChild(child);
}
nodes.splice(i + 1, 1);
i--;
}
}
return nodes;
}

Expand Down
7 changes: 7 additions & 0 deletions ui/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,13 @@ button, input, textarea {
font-family: sans-serif;
}

blockquote {
background: var(--background);
margin: 0;
padding: var(--regular-padding);
border-radius: var(--border-radius);
}

input {
padding: var(--input-padding);
border-radius: var(--border-radius);
Expand Down
2 changes: 1 addition & 1 deletion ui/templates/MarkdownTemplates.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class MarkdownTemplates {

static quote(text) {
return create("blockquote")
.classes("markdown")
.classes("markdown", "flex-v")
.children(text)
.build();
}
Expand Down

0 comments on commit f204f69

Please sign in to comment.