From c2c96848c4f1ab356d94aed56d52706a410802d4 Mon Sep 17 00:00:00 2001 From: Gokalp Sen Date: Fri, 6 Dec 2024 23:47:52 +0300 Subject: [PATCH] Fix stuff --- css/markdown.css | 55 ++++++++++++++++---- js/renderer.js | 132 ++++++++++++++++++++++++++++++++++++++++++----- js/script.js | 20 +++---- 3 files changed, 176 insertions(+), 31 deletions(-) diff --git a/css/markdown.css b/css/markdown.css index 77fd681..0abf070 100644 --- a/css/markdown.css +++ b/css/markdown.css @@ -3,6 +3,8 @@ color: var(--md-text, #333); line-height: 1.6; 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, @@ -51,10 +53,24 @@ } .mdrender blockquote { - border-left: 4px solid var(--md-header, #2c3e50); - padding-left: 1rem; - margin: 1rem 0; - color: var(--md-blockquote, #666); + border-left: 4px solid var(--md-blockquote-border, #ddd); + margin: 1em 0; + padding: 0.5em 1em; + 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, @@ -114,13 +130,34 @@ } /* Task Lists */ -.mdrender .task-list-item { - list-style-type: none; - margin-left: -1.5rem; +.mdrender ul.task-list { + list-style: none; + padding-left: 0; } -.mdrender .task-list-item input[type="checkbox"] { - margin-right: 0.5rem; +.mdrender .task-list-item { + 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 */ diff --git a/js/renderer.js b/js/renderer.js index 8cc963d..14479ec 100644 --- a/js/renderer.js +++ b/js/renderer.js @@ -12,11 +12,11 @@ class MDRenderer { image: /!\[(.+?)\]\((.+?)(?:\s+"(.+?)")?\)/g, list: /^(\s*)[*-]\s(.+)$/gm, orderedList: /^(\s*)\d+\.\s(.+)$/gm, - blockquote: /^>\s(.+)$/gm, + blockquote: /^((?:\s*>+\s*)+)(.+)$/gm, horizontalRule: /^([-*_])\1{2,}$/gm, table: /^\|(.+)\|$/gm, tableDelimiter: /^\|(?:[-:]+[-| :]*)\|$/gm, - taskList: /^(\s*)-\s\[([ x])\]\s(.+)$/gm, + taskList: /^(\s*)-\s+\[([ x])\]\s+(.+)$/gm, superscript: /\^(.+?)\^/g, subscript: /~(.+?)~/g, highlight: /==(.+?)==/g, @@ -56,12 +56,16 @@ class MDRenderer { // Convert code blocks first to prevent interference 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 html = this.renderHorizontalRules(html); - html = this.renderBlockquotes(html); html = this.renderHeadings(html); html = this.renderTables(html); - html = this.renderTaskLists(html); html = this.renderLists(html); html = this.renderOrderedLists(html); html = this.renderDefinitions(html); @@ -186,7 +190,118 @@ class MDRenderer { } renderBlockquotes(text) { - return text.replace(this.rules.blockquote, '
$1
'); + 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 = `
${html}
`; + } + 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 = ``; + const listItem = `
  • ${checkbox}${content}
  • `; + + if (currentList === null) { + // Start a new list + currentList = indentLevel; + currentIndent = indentLevel; + processedLines.push(`'.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(''.repeat(closeLevels)); + } + + return processedLines.join('\n'); } renderHorizontalRules(text) { @@ -257,13 +372,6 @@ class MDRenderer { return newLines.join('\n'); } - renderTaskLists(text) { - return text.replace(this.rules.taskList, (match, indent, checked, content) => { - const checkbox = ``; - return `${indent}
  • ${checkbox} ${content}
  • `; - }); - } - renderSuperscript(text) { return text.replace(this.rules.superscript, '$1'); } diff --git a/js/script.js b/js/script.js index 835c80b..4650c94 100644 --- a/js/script.js +++ b/js/script.js @@ -327,16 +327,16 @@ ___ :coffee: :pizza: :cake: :apple: ## 12. Special Characters & Escaping -\\* Escaped asterisk -\\_ Escaped underscore -\\# Escaped hash -\\[ Escaped bracket -\\] Escaped bracket -\\ Escaped backtick (Cannot show here, as it would be a JS syntax error) -\\( Escaped parenthesis -\\) Escaped parenthesis -\\~ Escaped tilde -\\: Escaped colon +\* Escaped asterisk +\_ Escaped underscore +\# Escaped hash +\[ Escaped bracket +\] Escaped bracket +\` Escaped backtick +\( Escaped parenthesis +\) Escaped parenthesis +\~ Escaped tilde +\: Escaped colon ## 13. HTML Support