Initial release

This commit is contained in:
Gokalp Sen 2024-12-06 23:09:51 +03:00
commit 1d4ab06af1
7 changed files with 724 additions and 0 deletions

124
js/renderer.js Normal file
View file

@ -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 `<div class="mdrender">${html}</div>`;
}
renderHeadings(text) {
return text.replace(this.rules.heading, (match, level, content) => {
const headingLevel = level.length;
return `<h${headingLevel}>${content}</h${headingLevel}>`;
});
}
renderParagraphs(text) {
return text.replace(this.rules.paragraph, (match, content) => {
return `<p>${content}</p>`;
});
}
renderBold(text) {
return text.replace(this.rules.bold, '<strong>$1</strong>');
}
renderItalic(text) {
return text.replace(this.rules.italic, '<em>$1</em>');
}
renderInlineCode(text) {
return text.replace(this.rules.code, '<code>$1</code>');
}
renderCodeBlocks(text) {
return text.replace(this.rules.codeblock, (match, language, code) => {
return `<pre><code class="language-${language}">${code.trim()}</code></pre>`;
});
}
renderLinks(text) {
return text.replace(this.rules.link, '<a href="$2">$1</a>');
}
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('<ul>');
} else if (indentLevel < lastIndentLevel) {
items.push('</ul>');
}
lastIndentLevel = indentLevel;
items.push(`<li>${content}</li>`);
return '';
});
while (lastIndentLevel > 0) {
items.push('</ul>');
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('<ol>');
} else if (indentLevel < lastIndentLevel) {
items.push('</ol>');
}
lastIndentLevel = indentLevel;
items.push(`<li>${content}</li>`);
return '';
});
while (lastIndentLevel > 0) {
items.push('</ol>');
lastIndentLevel--;
}
return html + items.join('');
}
renderBlockquotes(text) {
return text.replace(this.rules.blockquote, '<blockquote>$1</blockquote>');
}
}

70
js/script.js Normal file
View file

@ -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();
});