Initial release
This commit is contained in:
commit
1d4ab06af1
7 changed files with 724 additions and 0 deletions
8
MDRender.code-workspace
Normal file
8
MDRender.code-workspace
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
{
|
||||||
|
"folders": [
|
||||||
|
{
|
||||||
|
"path": "."
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"settings": {}
|
||||||
|
}
|
||||||
181
README.md
Normal file
181
README.md
Normal file
|
|
@ -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
|
||||||
|
<link rel="stylesheet" href="path/to/markdown.css">
|
||||||
|
<script src="path/to/renderer.js"></script>
|
||||||
|
```
|
||||||
|
|
||||||
|
### 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: <div class="mdrender"><h1>Hello World</h1><p>This is <strong>markdown</strong>!</p></div>
|
||||||
|
```
|
||||||
|
|
||||||
|
### 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
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>MDRender Example</title>
|
||||||
|
<link rel="stylesheet" href="markdown.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="output"></div>
|
||||||
|
|
||||||
|
<script src="renderer.js"></script>
|
||||||
|
<script>
|
||||||
|
// Initialize renderer
|
||||||
|
const renderer = new MDRenderer();
|
||||||
|
|
||||||
|
// Your markdown content
|
||||||
|
const markdown = `
|
||||||
|
# Welcome to MDRender
|
||||||
|
|
||||||
|
This is a **bold** statement and this is *italic*.
|
||||||
|
|
||||||
|
## Code Example
|
||||||
|
\`\`\`javascript
|
||||||
|
function hello() {
|
||||||
|
console.log('Hello, World!');
|
||||||
|
}
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
> This is a blockquote
|
||||||
|
|
||||||
|
1. Ordered list item 1
|
||||||
|
2. Ordered list item 2
|
||||||
|
|
||||||
|
- Unordered list item
|
||||||
|
- Another item
|
||||||
|
|
||||||
|
[Visit our website](https://example.com)
|
||||||
|
`;
|
||||||
|
|
||||||
|
// Render and display
|
||||||
|
document.getElementById('output').innerHTML = renderer.render(markdown);
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
```
|
||||||
|
|
||||||
|
## 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: ``
|
||||||
|
- 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!
|
||||||
114
css/markdown.css
Normal file
114
css/markdown.css
Normal file
|
|
@ -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;
|
||||||
|
}
|
||||||
176
css/style.css
Normal file
176
css/style.css
Normal file
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
51
index.html
Normal file
51
index.html
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||||
|
<title>MDRender</title>
|
||||||
|
<meta name="description" content="Markdown Renderer">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<link rel="stylesheet" href="css/style.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<header>
|
||||||
|
<div class="header-content">
|
||||||
|
<h1>MDRender</h1>
|
||||||
|
<p>A simple markdown renderer</p>
|
||||||
|
</div>
|
||||||
|
<button id="theme-toggle" aria-label="Toggle dark mode">
|
||||||
|
<svg class="sun-icon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
||||||
|
<circle cx="12" cy="12" r="5"/>
|
||||||
|
<path d="M12 1v2M12 21v2M4.22 4.22l1.42 1.42M18.36 18.36l1.42 1.42M1 12h2M21 12h2M4.22 19.78l1.42-1.42M18.36 5.64l1.42-1.42"/>
|
||||||
|
</svg>
|
||||||
|
<svg class="moon-icon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
||||||
|
<path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/>
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
<main>
|
||||||
|
<div class="editor-section">
|
||||||
|
<h2>Markdown Input</h2>
|
||||||
|
<textarea id="markdown-input" placeholder="Enter your markdown here..."></textarea>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="preview-section">
|
||||||
|
<h2>Preview</h2>
|
||||||
|
<div id="preview"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="html-section">
|
||||||
|
<h2>HTML Output</h2>
|
||||||
|
<button id="copy-html">Copy HTML</button>
|
||||||
|
<code id="html-output"></code>
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script src="js/renderer.js"></script>
|
||||||
|
<script src="js/script.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
124
js/renderer.js
Normal file
124
js/renderer.js
Normal 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
70
js/script.js
Normal 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();
|
||||||
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue