Fix stuff

This commit is contained in:
Gokalp Sen 2024-12-06 23:47:52 +03:00
parent 1e09e9488a
commit c2c96848c4
3 changed files with 176 additions and 31 deletions

View file

@ -3,6 +3,8 @@
color: var(--md-text, #333); color: var(--md-text, #333);
line-height: 1.6; line-height: 1.6;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
--checkbox-size: 1.2em;
--checkbox-margin: 0.5em;
} }
.mdrender h1, .mdrender h1,
@ -51,10 +53,24 @@
} }
.mdrender blockquote { .mdrender blockquote {
border-left: 4px solid var(--md-header, #2c3e50); border-left: 4px solid var(--md-blockquote-border, #ddd);
padding-left: 1rem; margin: 1em 0;
margin: 1rem 0; padding: 0.5em 1em;
color: var(--md-blockquote, #666); color: var(--md-blockquote-text, #666);
background-color: var(--md-blockquote-bg, rgba(0, 0, 0, 0.05));
}
.mdrender blockquote blockquote {
margin-left: 0.5em;
border-left-color: var(--md-blockquote-nested-border, #999);
}
.mdrender blockquote blockquote blockquote {
border-left-color: var(--md-blockquote-nested-2-border, #666);
}
.mdrender blockquote blockquote blockquote blockquote {
border-left-color: var(--md-blockquote-nested-3-border, #333);
} }
.mdrender ul, .mdrender ul,
@ -114,13 +130,34 @@
} }
/* Task Lists */ /* Task Lists */
.mdrender .task-list-item { .mdrender ul.task-list {
list-style-type: none; list-style: none;
margin-left: -1.5rem; padding-left: 0;
} }
.mdrender .task-list-item input[type="checkbox"] { .mdrender .task-list-item {
margin-right: 0.5rem; display: flex;
align-items: flex-start;
margin: 0.5em 0;
line-height: 1.4;
}
.mdrender .task-list-item-checkbox {
width: var(--checkbox-size);
height: var(--checkbox-size);
margin: 0.1em var(--checkbox-margin) 0 0;
flex-shrink: 0;
}
.mdrender .task-list-item-text {
flex: 1;
min-width: 0;
}
/* Nested task lists */
.mdrender .task-list .task-list {
margin-top: 0.5em;
margin-left: var(--checkbox-size);
} }
/* Footnotes */ /* Footnotes */

View file

@ -12,11 +12,11 @@ class MDRenderer {
image: /!\[(.+?)\]\((.+?)(?:\s+"(.+?)")?\)/g, image: /!\[(.+?)\]\((.+?)(?:\s+"(.+?)")?\)/g,
list: /^(\s*)[*-]\s(.+)$/gm, list: /^(\s*)[*-]\s(.+)$/gm,
orderedList: /^(\s*)\d+\.\s(.+)$/gm, orderedList: /^(\s*)\d+\.\s(.+)$/gm,
blockquote: /^>\s(.+)$/gm, blockquote: /^((?:\s*>+\s*)+)(.+)$/gm,
horizontalRule: /^([-*_])\1{2,}$/gm, horizontalRule: /^([-*_])\1{2,}$/gm,
table: /^\|(.+)\|$/gm, table: /^\|(.+)\|$/gm,
tableDelimiter: /^\|(?:[-:]+[-| :]*)\|$/gm, tableDelimiter: /^\|(?:[-:]+[-| :]*)\|$/gm,
taskList: /^(\s*)-\s\[([ x])\]\s(.+)$/gm, taskList: /^(\s*)-\s+\[([ x])\]\s+(.+)$/gm,
superscript: /\^(.+?)\^/g, superscript: /\^(.+?)\^/g,
subscript: /~(.+?)~/g, subscript: /~(.+?)~/g,
highlight: /==(.+?)==/g, highlight: /==(.+?)==/g,
@ -56,12 +56,16 @@ class MDRenderer {
// Convert code blocks first to prevent interference // Convert code blocks first to prevent interference
html = this.renderCodeBlocks(html); html = this.renderCodeBlocks(html);
// Process blockquotes first to handle nesting
html = this.renderBlockquotes(html);
// Then process task lists
html = this.renderTaskLists(html);
// Convert block elements // Convert block elements
html = this.renderHorizontalRules(html); html = this.renderHorizontalRules(html);
html = this.renderBlockquotes(html);
html = this.renderHeadings(html); html = this.renderHeadings(html);
html = this.renderTables(html); html = this.renderTables(html);
html = this.renderTaskLists(html);
html = this.renderLists(html); html = this.renderLists(html);
html = this.renderOrderedLists(html); html = this.renderOrderedLists(html);
html = this.renderDefinitions(html); html = this.renderDefinitions(html);
@ -186,7 +190,118 @@ class MDRenderer {
} }
renderBlockquotes(text) { renderBlockquotes(text) {
return text.replace(this.rules.blockquote, '<blockquote>$1</blockquote>'); const lines = text.split('\n');
const result = [];
let inBlockquote = false;
let currentContent = [];
let currentLevel = 0;
function processBlockquote(content, level) {
if (content.length === 0) return '';
const innerContent = content.join('\n').trim();
let html = innerContent;
for (let i = 0; i < level; i++) {
html = `<blockquote>${html}</blockquote>`;
}
return html;
}
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const match = line.match(/^(>+)?\s*(.*)$/);
if (match) {
const [, quotes, content] = match;
const level = quotes ? quotes.length : 0;
if (level > 0) {
if (!inBlockquote) {
inBlockquote = true;
currentLevel = level;
}
if (level !== currentLevel) {
// Process previous blockquote
result.push(processBlockquote(currentContent, currentLevel));
currentContent = [];
currentLevel = level;
}
currentContent.push(content.trim());
} else {
if (inBlockquote) {
// End of blockquote
result.push(processBlockquote(currentContent, currentLevel));
currentContent = [];
inBlockquote = false;
}
result.push(line);
}
}
}
// Process any remaining blockquote
if (inBlockquote && currentContent.length > 0) {
result.push(processBlockquote(currentContent, currentLevel));
}
return result.join('\n');
}
renderTaskLists(text) {
const processedLines = [];
const lines = text.split('\n');
let currentList = null;
let currentIndent = 0;
for (let line of lines) {
const taskMatch = line.match(/^(\s*)-\s+\[([ x])\]\s+(.+)$/);
if (taskMatch) {
const [, indent, checked, content] = taskMatch;
const indentLevel = indent.length;
const checkbox = `<input type="checkbox" class="task-list-item-checkbox" ${checked === 'x' ? 'checked' : ''} disabled>`;
const listItem = `<li class="task-list-item">${checkbox}<span class="task-list-item-text">${content}</span></li>`;
if (currentList === null) {
// Start a new list
currentList = indentLevel;
currentIndent = indentLevel;
processedLines.push(`<ul class="task-list">${listItem}`);
} else if (indentLevel > currentIndent) {
// Nested list
processedLines.push(`<ul class="task-list">${listItem}`);
currentIndent = indentLevel;
} else if (indentLevel < currentIndent) {
// Close nested lists and start new item
const closeLevels = Math.floor((currentIndent - indentLevel) / 2);
processedLines.push('</ul>'.repeat(closeLevels));
processedLines.push(listItem);
currentIndent = indentLevel;
} else {
// Same level
processedLines.push(listItem);
}
} else {
if (currentList !== null) {
// Close all open lists when encountering non-task-list content
const closeLevels = Math.floor(currentIndent / 2) + 1;
processedLines.push('</ul>'.repeat(closeLevels));
currentList = null;
currentIndent = 0;
}
processedLines.push(line);
}
}
// Close any remaining open lists
if (currentList !== null) {
const closeLevels = Math.floor(currentIndent / 2) + 1;
processedLines.push('</ul>'.repeat(closeLevels));
}
return processedLines.join('\n');
} }
renderHorizontalRules(text) { renderHorizontalRules(text) {
@ -257,13 +372,6 @@ class MDRenderer {
return newLines.join('\n'); return newLines.join('\n');
} }
renderTaskLists(text) {
return text.replace(this.rules.taskList, (match, indent, checked, content) => {
const checkbox = `<input type="checkbox" ${checked === 'x' ? 'checked' : ''} disabled>`;
return `${indent}<li class="task-list-item">${checkbox} ${content}</li>`;
});
}
renderSuperscript(text) { renderSuperscript(text) {
return text.replace(this.rules.superscript, '<sup>$1</sup>'); return text.replace(this.rules.superscript, '<sup>$1</sup>');
} }

View file

@ -327,16 +327,16 @@ ___
:coffee: :pizza: :cake: :apple: :coffee: :pizza: :cake: :apple:
## 12. Special Characters & Escaping ## 12. Special Characters & Escaping
\\* Escaped asterisk \* Escaped asterisk
\\_ Escaped underscore \_ Escaped underscore
\\# Escaped hash \# Escaped hash
\\[ Escaped bracket \[ Escaped bracket
\\] Escaped bracket \] Escaped bracket
\\ Escaped backtick (Cannot show here, as it would be a JS syntax error) \` Escaped backtick
\\( Escaped parenthesis \( Escaped parenthesis
\\) Escaped parenthesis \) Escaped parenthesis
\\~ Escaped tilde \~ Escaped tilde
\\: Escaped colon \: Escaped colon
## 13. HTML Support ## 13. HTML Support
<div style="color: blue;"> <div style="color: blue;">