From 1d4ab06af160bc6e248d93d86449962d93a291a9 Mon Sep 17 00:00:00 2001 From: Gokalp Sen Date: Fri, 6 Dec 2024 23:09:51 +0300 Subject: [PATCH] Initial release --- MDRender.code-workspace | 8 ++ README.md | 181 ++++++++++++++++++++++++++++++++++++++++ css/markdown.css | 114 +++++++++++++++++++++++++ css/style.css | 176 ++++++++++++++++++++++++++++++++++++++ index.html | 51 +++++++++++ js/renderer.js | 124 +++++++++++++++++++++++++++ js/script.js | 70 ++++++++++++++++ 7 files changed, 724 insertions(+) create mode 100644 MDRender.code-workspace create mode 100644 README.md create mode 100644 css/markdown.css create mode 100644 css/style.css create mode 100644 index.html create mode 100644 js/renderer.js create mode 100644 js/script.js diff --git a/MDRender.code-workspace b/MDRender.code-workspace new file mode 100644 index 0000000..876a149 --- /dev/null +++ b/MDRender.code-workspace @@ -0,0 +1,8 @@ +{ + "folders": [ + { + "path": "." + } + ], + "settings": {} +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..15a25a2 --- /dev/null +++ b/README.md @@ -0,0 +1,181 @@ +# MDRender + +A lightweight, customizable markdown renderer for web applications. MDRender converts markdown text into styled HTML with support for dark mode and custom theming. + +## Features + +- 📝 Full markdown support including: + - Headings (h1-h6) + - Bold and italic text + - Code blocks with language highlighting + - Inline code + - Lists (ordered and unordered) + - Blockquotes + - Links + - Tables + - Images +- 🎨 Customizable themes via CSS variables +- 🌙 Built-in dark mode support +- 🎯 Zero dependencies +- 📱 Responsive design +- 🚀 Simple API + +## Getting Started + +### 1. Include the Required Files + +Copy `renderer.js` and `markdown.css` to your project and include them in your HTML: + +```html + + +``` + +### 2. Initialize the Renderer + +```javascript +const renderer = new MDRenderer(); +``` + +### 3. Convert Markdown to HTML + +```javascript +const markdown = "# Hello World\nThis is **markdown**!"; +const html = renderer.render(markdown); +// Output:

Hello World

This is markdown!

+``` + +### 4. Display the Result + +```javascript +document.getElementById('output').innerHTML = html; +``` + +## Customizing Styles + +### Basic Theme Customization + +Override the default colors by setting CSS variables: + +```css +.mdrender { + --md-text: #333; /* Main text color */ + --md-header: #2c3e50; /* Heading color */ + --md-code-bg: #f8f9fa; /* Code background */ + --md-blockquote: #666; /* Blockquote text color */ + --md-link: #3498db; /* Link color */ + --md-border: #ddd; /* Border color */ +} +``` + +### Dark Mode Support + +Add dark mode styles by using the `data-theme` attribute: + +```css +[data-theme="dark"] .mdrender { + --md-text: #e0e0e0; + --md-header: #60a5fa; + --md-code-bg: #2d2d2d; + --md-blockquote: #a0a0a0; + --md-link: #60a5fa; + --md-border: #404040; +} +``` + +Toggle dark mode with JavaScript: + +```javascript +// Enable dark mode +document.documentElement.setAttribute('data-theme', 'dark'); + +// Enable light mode +document.documentElement.setAttribute('data-theme', 'light'); +``` + +## Complete Example + +Here's a complete example showing how to use MDRender in your project: + +```html + + + + + MDRender Example + + + +
+ + + + + +``` + +## Supported Markdown Syntax + +- Headings: `# H1` through `###### H6` +- Bold: `**bold text**` +- Italic: `*italic text*` +- Code blocks: ````javascript code here``` +- Inline code: `` `code` `` +- Lists: + - Unordered: `- item` + - Ordered: `1. item` +- Blockquotes: `> quote` +- Links: `[text](url)` +- Images: `![alt](url)` +- Tables: + ``` + | Header | Header | + |--------|--------| + | Cell | Cell | + ``` + +## Browser Support + +MDRender works in all modern browsers: +- Chrome +- Firefox +- Safari +- Edge + +## Contributing + +Feel free to submit issues and enhancement requests! + +## License + +MIT License - feel free to use this in your own projects! diff --git a/css/markdown.css b/css/markdown.css new file mode 100644 index 0000000..f236d44 --- /dev/null +++ b/css/markdown.css @@ -0,0 +1,114 @@ +/* MDRender Markdown Styles */ +.mdrender { + 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; +} + +.mdrender h1, +.mdrender h2, +.mdrender h3, +.mdrender h4, +.mdrender h5, +.mdrender h6 { + margin: 1.5rem 0 1rem; + color: var(--md-header, #2c3e50); + line-height: 1.25; +} + +.mdrender h1 { font-size: 2em; } +.mdrender h2 { font-size: 1.5em; } +.mdrender h3 { font-size: 1.25em; } +.mdrender h4 { font-size: 1em; } +.mdrender h5 { font-size: 0.875em; } +.mdrender h6 { font-size: 0.85em; } + +.mdrender p { + margin-bottom: 1rem; +} + +.mdrender code { + background: var(--md-code-bg, #f8f9fa); + padding: 0.2rem 0.4rem; + border-radius: 3px; + font-family: 'Courier New', Courier, monospace; + font-size: 0.9em; +} + +.mdrender pre { + background: var(--md-code-bg, #f8f9fa); + padding: 1rem; + border-radius: 4px; + overflow-x: auto; + margin: 1rem 0; +} + +.mdrender pre code { + background: none; + padding: 0; + font-size: 0.9em; +} + +.mdrender blockquote { + border-left: 4px solid var(--md-header, #2c3e50); + padding-left: 1rem; + margin: 1rem 0; + color: var(--md-blockquote, #666); +} + +.mdrender ul, +.mdrender ol { + margin: 1rem 0; + padding-left: 2rem; +} + +.mdrender li { + margin-bottom: 0.5rem; +} + +.mdrender a { + color: var(--md-link, #3498db); + text-decoration: none; +} + +.mdrender a:hover { + text-decoration: underline; +} + +.mdrender img { + max-width: 100%; + height: auto; + margin: 1rem 0; +} + +.mdrender hr { + border: none; + border-top: 2px solid var(--md-border, #ddd); + margin: 2rem 0; +} + +.mdrender table { + border-collapse: collapse; + width: 100%; + margin: 1rem 0; +} + +.mdrender th, +.mdrender td { + border: 1px solid var(--md-border, #ddd); + padding: 0.5rem; +} + +.mdrender th { + background: var(--md-code-bg, #f8f9fa); +} + +/* Dark theme support */ +[data-theme="dark"] .mdrender { + --md-text: #e0e0e0; + --md-header: #60a5fa; + --md-code-bg: #2d2d2d; + --md-blockquote: #a0a0a0; + --md-link: #60a5fa; + --md-border: #404040; +} diff --git a/css/style.css b/css/style.css new file mode 100644 index 0000000..b29c20e --- /dev/null +++ b/css/style.css @@ -0,0 +1,176 @@ +@import 'markdown.css'; + +:root { + --bg-color: #f5f5f5; + --text-color: #333; + --header-color: #2c3e50; + --border-color: #dddddd; + --code-bg: #f8f9fa; + --button-bg: #2c3e50; + --button-hover: #34495e; + --blockquote-color: #666; + --link-color: #3498db; + --preview-bg: white; +} + +[data-theme="dark"] { + --bg-color: #1a1a1a; + --text-color: #e0e0e0; + --header-color: #60a5fa; + --border-color: #404040; + --code-bg: #2d2d2d; + --button-bg: #60a5fa; + --button-hover: #3b82f6; + --blockquote-color: #a0a0a0; + --link-color: #60a5fa; + --preview-bg: #2d2d2d; +} + +* { + margin: 0; + padding: 0; + box-sizing: border-box; +} + +body { + font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; + line-height: 1.6; + color: var(--text-color); + background-color: var(--bg-color); + transition: background-color 0.3s ease, color 0.3s ease; +} + +.container { + max-width: 1200px; + margin: 0 auto; + padding: 2rem; +} + +header { + text-align: center; + margin-bottom: 2rem; + display: flex; + justify-content: center; + align-items: center; + gap: 1rem; +} + +.header-content { + text-align: center; +} + +header h1 { + color: var(--header-color); + margin-bottom: 0.5rem; +} + +#theme-toggle { + background: none; + border: none; + cursor: pointer; + padding: 0.5rem; + color: var(--text-color); + border-radius: 50%; + width: 40px; + height: 40px; + display: flex; + align-items: center; + justify-content: center; +} + +#theme-toggle:hover { + background: var(--border-color); +} + +.sun-icon { + display: none; +} + +[data-theme="dark"] .sun-icon { + display: block; +} + +[data-theme="dark"] .moon-icon { + display: none; +} + +main { + display: grid; + grid-template-columns: 1fr 1fr; + gap: 2rem; +} + +.editor-section { + grid-column: 1; +} + +.preview-section { + grid-column: 2; +} + +.html-section { + grid-column: 1 / -1; + margin-top: 2rem; +} + +h2 { + color: var(--header-color); + margin-bottom: 1rem; +} + +textarea { + width: 100%; + height: 400px; + padding: 1rem; + border: 1px solid var(--border-color); + border-radius: 4px; + font-family: 'Courier New', Courier, monospace; + font-size: 14px; + resize: vertical; + background-color: var(--preview-bg); + color: var(--text-color); +} + +#preview { + background: var(--preview-bg); + padding: 1rem; + border: 1px solid var(--border-color); + border-radius: 4px; + min-height: 400px; +} + +button { + padding: 0.5rem 1rem; + background: var(--button-bg); + color: white; + border: none; + border-radius: 4px; + cursor: pointer; + margin-bottom: 1rem; +} + +button:hover { + background: var(--button-hover); +} + +#html-output { + background: var(--preview-bg); + padding: 2px; + border: 1px solid var(--border-color); + border-radius: 4px; + overflow-x: auto; + font-family: 'Courier New', Courier, monospace; + font-size: 14px; + color: var(--text-color); +} + +@media (max-width: 768px) { + main { + grid-template-columns: 1fr; + } + + .editor-section, + .preview-section { + grid-column: 1; + } +} \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..8140262 --- /dev/null +++ b/index.html @@ -0,0 +1,51 @@ + + + + + + MDRender + + + + + +
+
+
+

MDRender

+

A simple markdown renderer

+
+ +
+ +
+
+

Markdown Input

+ +
+ +
+

Preview

+
+
+ +
+

HTML Output

+ + +
+
+
+ + + + + \ No newline at end of file diff --git a/js/renderer.js b/js/renderer.js new file mode 100644 index 0000000..6b8235e --- /dev/null +++ b/js/renderer.js @@ -0,0 +1,124 @@ +class MDRenderer { + constructor() { + this.rules = { + heading: /^(#{1,6})\s+(.+)$/gm, + paragraph: /^(?!#|>|\s*\d+\.|```|\s*-\s)(.+)$/gm, + bold: /\*\*(.+?)\*\*/g, + italic: /\*(.+?)\*/g, + code: /`(.+?)`/g, + codeblock: /```(\w*)\n([\s\S]*?)```/g, + link: /\[(.+?)\]\((.+?)\)/g, + list: /^(\s*)-\s(.+)$/gm, + orderedList: /^(\s*)\d+\.\s(.+)$/gm, + blockquote: /^>\s(.+)$/gm, + }; + } + + render(markdown) { + let html = markdown; + + // Convert code blocks first to prevent interference + html = this.renderCodeBlocks(html); + + // Convert block elements + html = this.renderBlockquotes(html); + html = this.renderHeadings(html); + html = this.renderLists(html); + html = this.renderOrderedLists(html); + html = this.renderParagraphs(html); + + // Convert inline elements + html = this.renderBold(html); + html = this.renderItalic(html); + html = this.renderInlineCode(html); + html = this.renderLinks(html); + + // Wrap the content in a div with the mdrender class + return `
${html}
`; + } + + renderHeadings(text) { + return text.replace(this.rules.heading, (match, level, content) => { + const headingLevel = level.length; + return `${content}`; + }); + } + + renderParagraphs(text) { + return text.replace(this.rules.paragraph, (match, content) => { + return `

${content}

`; + }); + } + + renderBold(text) { + return text.replace(this.rules.bold, '$1'); + } + + renderItalic(text) { + return text.replace(this.rules.italic, '$1'); + } + + renderInlineCode(text) { + return text.replace(this.rules.code, '$1'); + } + + renderCodeBlocks(text) { + return text.replace(this.rules.codeblock, (match, language, code) => { + return `
${code.trim()}
`; + }); + } + + renderLinks(text) { + return text.replace(this.rules.link, '$1'); + } + + renderLists(text) { + const items = []; + let lastIndentLevel = 0; + let html = text.replace(this.rules.list, (match, indent, content) => { + const indentLevel = indent.length; + if (indentLevel > lastIndentLevel) { + items.push(''); + } + lastIndentLevel = indentLevel; + items.push(`
  • ${content}
  • `); + return ''; + }); + + while (lastIndentLevel > 0) { + items.push(''); + lastIndentLevel--; + } + + return html + items.join(''); + } + + renderOrderedLists(text) { + const items = []; + let lastIndentLevel = 0; + let html = text.replace(this.rules.orderedList, (match, indent, content) => { + const indentLevel = indent.length; + if (indentLevel > lastIndentLevel) { + items.push('
      '); + } else if (indentLevel < lastIndentLevel) { + items.push('
    '); + } + lastIndentLevel = indentLevel; + items.push(`
  • ${content}
  • `); + return ''; + }); + + while (lastIndentLevel > 0) { + items.push(''); + lastIndentLevel--; + } + + return html + items.join(''); + } + + renderBlockquotes(text) { + return text.replace(this.rules.blockquote, '
    $1
    '); + } +} \ No newline at end of file diff --git a/js/script.js b/js/script.js new file mode 100644 index 0000000..0fd0d8f --- /dev/null +++ b/js/script.js @@ -0,0 +1,70 @@ +document.addEventListener('DOMContentLoaded', () => { + const renderer = new MDRenderer(); + const editor = document.getElementById('markdown-input'); + const preview = document.getElementById('preview'); + const copyButton = document.getElementById('copy-html'); + const themeToggle = document.getElementById('theme-toggle'); + + // Theme handling + const theme = localStorage.getItem('theme') || 'light'; + document.documentElement.setAttribute('data-theme', theme); + + themeToggle.addEventListener('click', () => { + const currentTheme = document.documentElement.getAttribute('data-theme'); + const newTheme = currentTheme === 'light' ? 'dark' : 'light'; + document.documentElement.setAttribute('data-theme', newTheme); + localStorage.setItem('theme', newTheme); + }); + + function updatePreview() { + const markdown = editor.value; + const html = renderer.render(markdown); + preview.innerHTML = html; + document.getElementById('html-output').textContent = html; + } + + editor.addEventListener('input', updatePreview); + + copyButton.addEventListener('click', () => { + const htmlOutput = document.getElementById('html-output'); + navigator.clipboard.writeText(htmlOutput.textContent) + .then(() => { + copyButton.textContent = 'Copied!'; + setTimeout(() => { + copyButton.textContent = 'Copy HTML'; + }, 2000); + }) + .catch(err => { + console.error('Failed to copy:', err); + }); + }); + + // Load default example + editor.value = `# Welcome to MDRender + +This is a simple markdown renderer. Try editing this text! + +## Features: +- **Bold text** and *italic text* +- Lists (like this one!) +- Links: [Visit GitHub](https://github.com) +- Code: \`console.log('Hello World!')\` +- And more... + +### Code Blocks +\`\`\`javascript +function hello() { + console.log('Hello, World!'); +} +\`\`\` + +> Blockquotes are supported too! + +1. Ordered lists +2. Work as well +3. Try them out! +`; + + // Initial render + updatePreview(); +}); \ No newline at end of file