Add more funcionality
This commit is contained in:
parent
1d4ab06af1
commit
1e09e9488a
5 changed files with 815 additions and 69 deletions
|
|
@ -14,6 +14,7 @@
|
||||||
margin: 1.5rem 0 1rem;
|
margin: 1.5rem 0 1rem;
|
||||||
color: var(--md-header, #2c3e50);
|
color: var(--md-header, #2c3e50);
|
||||||
line-height: 1.25;
|
line-height: 1.25;
|
||||||
|
scroll-margin-top: 1rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
.mdrender h1 { font-size: 2em; }
|
.mdrender h1 { font-size: 2em; }
|
||||||
|
|
@ -79,6 +80,7 @@
|
||||||
max-width: 100%;
|
max-width: 100%;
|
||||||
height: auto;
|
height: auto;
|
||||||
margin: 1rem 0;
|
margin: 1rem 0;
|
||||||
|
border-radius: 4px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.mdrender hr {
|
.mdrender hr {
|
||||||
|
|
@ -87,10 +89,13 @@
|
||||||
margin: 2rem 0;
|
margin: 2rem 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Tables */
|
||||||
.mdrender table {
|
.mdrender table {
|
||||||
border-collapse: collapse;
|
border-collapse: collapse;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
margin: 1rem 0;
|
margin: 1rem 0;
|
||||||
|
overflow-x: auto;
|
||||||
|
display: block;
|
||||||
}
|
}
|
||||||
|
|
||||||
.mdrender th,
|
.mdrender th,
|
||||||
|
|
@ -101,6 +106,74 @@
|
||||||
|
|
||||||
.mdrender th {
|
.mdrender th {
|
||||||
background: var(--md-code-bg, #f8f9fa);
|
background: var(--md-code-bg, #f8f9fa);
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mdrender tr:nth-child(even) {
|
||||||
|
background: var(--md-code-bg, #f8f9fa);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Task Lists */
|
||||||
|
.mdrender .task-list-item {
|
||||||
|
list-style-type: none;
|
||||||
|
margin-left: -1.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mdrender .task-list-item input[type="checkbox"] {
|
||||||
|
margin-right: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Footnotes */
|
||||||
|
.mdrender .footnotes {
|
||||||
|
margin-top: 2rem;
|
||||||
|
font-size: 0.9em;
|
||||||
|
color: var(--md-blockquote, #666);
|
||||||
|
}
|
||||||
|
|
||||||
|
.mdrender .footnotes hr {
|
||||||
|
margin: 1rem 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mdrender .footnote-ref {
|
||||||
|
font-size: 0.8em;
|
||||||
|
vertical-align: super;
|
||||||
|
line-height: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mdrender .footnote-backref {
|
||||||
|
font-size: 0.8em;
|
||||||
|
margin-left: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Definition Lists */
|
||||||
|
.mdrender dl {
|
||||||
|
margin: 1rem 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mdrender dt {
|
||||||
|
font-weight: bold;
|
||||||
|
margin-top: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mdrender dd {
|
||||||
|
margin-left: 2rem;
|
||||||
|
margin-top: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Text Formatting */
|
||||||
|
.mdrender mark {
|
||||||
|
background-color: #fff1a7;
|
||||||
|
padding: 0.1em 0.2em;
|
||||||
|
border-radius: 2px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mdrender del {
|
||||||
|
color: var(--md-blockquote, #666);
|
||||||
|
}
|
||||||
|
|
||||||
|
.mdrender sup,
|
||||||
|
.mdrender sub {
|
||||||
|
font-size: 0.8em;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Dark theme support */
|
/* Dark theme support */
|
||||||
|
|
@ -112,3 +185,12 @@
|
||||||
--md-link: #60a5fa;
|
--md-link: #60a5fa;
|
||||||
--md-border: #404040;
|
--md-border: #404040;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[data-theme="dark"] .mdrender mark {
|
||||||
|
background-color: #665c1e;
|
||||||
|
color: #fff;
|
||||||
|
}
|
||||||
|
|
||||||
|
[data-theme="dark"] .mdrender tr:nth-child(even) {
|
||||||
|
background: rgba(255, 255, 255, 0.05);
|
||||||
|
}
|
||||||
|
|
|
||||||
108
css/style.css
108
css/style.css
|
|
@ -44,24 +44,36 @@ body {
|
||||||
max-width: 1200px;
|
max-width: 1200px;
|
||||||
margin: 0 auto;
|
margin: 0 auto;
|
||||||
padding: 2rem;
|
padding: 2rem;
|
||||||
|
min-height: 100vh;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
}
|
}
|
||||||
|
|
||||||
header {
|
header {
|
||||||
text-align: center;
|
text-align: center;
|
||||||
margin-bottom: 2rem;
|
margin-bottom: 2rem;
|
||||||
display: flex;
|
display: flex;
|
||||||
justify-content: center;
|
justify-content: space-between;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
gap: 1rem;
|
gap: 1rem;
|
||||||
|
padding-bottom: 1rem;
|
||||||
|
border-bottom: 2px solid var(--border-color);
|
||||||
}
|
}
|
||||||
|
|
||||||
.header-content {
|
.header-content {
|
||||||
text-align: center;
|
text-align: left;
|
||||||
|
}
|
||||||
|
|
||||||
|
.header-controls {
|
||||||
|
display: flex;
|
||||||
|
gap: 1rem;
|
||||||
|
align-items: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
header h1 {
|
header h1 {
|
||||||
color: var(--header-color);
|
color: var(--header-color);
|
||||||
margin-bottom: 0.5rem;
|
margin-bottom: 0.5rem;
|
||||||
|
font-size: 2.5rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
#theme-toggle {
|
#theme-toggle {
|
||||||
|
|
@ -98,6 +110,24 @@ main {
|
||||||
display: grid;
|
display: grid;
|
||||||
grid-template-columns: 1fr 1fr;
|
grid-template-columns: 1fr 1fr;
|
||||||
gap: 2rem;
|
gap: 2rem;
|
||||||
|
flex-grow: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.section-header {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
margin-bottom: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.section-header h2 {
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.preview-controls,
|
||||||
|
.html-controls {
|
||||||
|
display: flex;
|
||||||
|
gap: 0.5rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
.editor-section {
|
.editor-section {
|
||||||
|
|
@ -146,31 +176,93 @@ button {
|
||||||
border: none;
|
border: none;
|
||||||
border-radius: 4px;
|
border-radius: 4px;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
margin-bottom: 1rem;
|
font-size: 0.9rem;
|
||||||
|
transition: background-color 0.2s ease;
|
||||||
}
|
}
|
||||||
|
|
||||||
button:hover {
|
button:hover {
|
||||||
background: var(--button-hover);
|
background: var(--button-hover);
|
||||||
}
|
}
|
||||||
|
|
||||||
#html-output {
|
#output-container {
|
||||||
|
border: 2px solid var(--border-color);
|
||||||
|
border-radius: 5px;
|
||||||
|
padding: 1rem;
|
||||||
background: var(--preview-bg);
|
background: var(--preview-bg);
|
||||||
padding: 2px;
|
max-height: 300px;
|
||||||
border: 1px solid var(--border-color);
|
overflow-y: auto;
|
||||||
border-radius: 4px;
|
}
|
||||||
overflow-x: auto;
|
|
||||||
|
#html-output {
|
||||||
font-family: 'Courier New', Courier, monospace;
|
font-family: 'Courier New', Courier, monospace;
|
||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
color: var(--text-color);
|
color: var(--text-color);
|
||||||
|
white-space: pre-wrap;
|
||||||
|
word-break: break-all;
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
padding: 0.5rem;
|
||||||
|
border: 1px solid var(--border-color);
|
||||||
|
border-radius: 4px;
|
||||||
|
background: var(--preview-bg);
|
||||||
|
color: var(--text-color);
|
||||||
|
font-size: 0.9rem;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
select:hover {
|
||||||
|
border-color: var(--button-bg);
|
||||||
|
}
|
||||||
|
|
||||||
|
footer {
|
||||||
|
margin-top: 2rem;
|
||||||
|
text-align: center;
|
||||||
|
padding-top: 1rem;
|
||||||
|
border-top: 2px solid var(--border-color);
|
||||||
|
}
|
||||||
|
|
||||||
|
footer a {
|
||||||
|
color: var(--link-color);
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
footer a:hover {
|
||||||
|
text-decoration: underline;
|
||||||
}
|
}
|
||||||
|
|
||||||
@media (max-width: 768px) {
|
@media (max-width: 768px) {
|
||||||
|
.container {
|
||||||
|
padding: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
header {
|
||||||
|
flex-direction: column;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.header-content {
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
main {
|
main {
|
||||||
grid-template-columns: 1fr;
|
grid-template-columns: 1fr;
|
||||||
|
gap: 1rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
.editor-section,
|
.editor-section,
|
||||||
.preview-section {
|
.preview-section {
|
||||||
grid-column: 1;
|
grid-column: 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.section-header {
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 0.5rem;
|
||||||
|
align-items: stretch;
|
||||||
|
}
|
||||||
|
|
||||||
|
.preview-controls,
|
||||||
|
.html-controls {
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
57
index.html
57
index.html
|
|
@ -15,34 +15,63 @@
|
||||||
<h1>MDRender</h1>
|
<h1>MDRender</h1>
|
||||||
<p>A simple markdown renderer</p>
|
<p>A simple markdown renderer</p>
|
||||||
</div>
|
</div>
|
||||||
<button id="theme-toggle" aria-label="Toggle dark mode">
|
<div class="header-controls">
|
||||||
<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">
|
<select id="example-select">
|
||||||
<circle cx="12" cy="12" r="5"/>
|
<option value="">Choose an example...</option>
|
||||||
<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"/>
|
<option value="basic">Basic Markdown</option>
|
||||||
</svg>
|
<option value="advanced">Advanced Features</option>
|
||||||
<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">
|
<option value="tables">Tables Example</option>
|
||||||
<path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/>
|
<option value="footnotes">Footnotes Example</option>
|
||||||
</svg>
|
</select>
|
||||||
</button>
|
<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>
|
||||||
|
</div>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
<main>
|
<main>
|
||||||
<div class="editor-section">
|
<div class="editor-section">
|
||||||
<h2>Markdown Input</h2>
|
<div class="section-header">
|
||||||
|
<h2>Markdown Input</h2>
|
||||||
|
<button id="clear-input">Clear</button>
|
||||||
|
</div>
|
||||||
<textarea id="markdown-input" placeholder="Enter your markdown here..."></textarea>
|
<textarea id="markdown-input" placeholder="Enter your markdown here..."></textarea>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="preview-section">
|
<div class="preview-section">
|
||||||
<h2>Preview</h2>
|
<div class="section-header">
|
||||||
|
<h2>Preview</h2>
|
||||||
|
<div class="preview-controls">
|
||||||
|
<button id="refresh-preview">Refresh</button>
|
||||||
|
<button id="toggle-preview">Toggle Preview</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
<div id="preview"></div>
|
<div id="preview"></div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="html-section">
|
<div class="html-section">
|
||||||
<h2>HTML Output</h2>
|
<div class="section-header">
|
||||||
<button id="copy-html">Copy HTML</button>
|
<h2>HTML Output</h2>
|
||||||
<code id="html-output"></code>
|
<div class="html-controls">
|
||||||
|
<button id="copy-html">Copy HTML</button>
|
||||||
|
<button id="format-html">Format HTML</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div id="output-container">
|
||||||
|
<code id="html-output"></code>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
|
<footer>
|
||||||
|
<p>Need help? Check out our <a href="https://github.com/tyuXX/MDRender" target="_blank">documentation</a></p>
|
||||||
|
</footer>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script src="js/renderer.js"></script>
|
<script src="js/renderer.js"></script>
|
||||||
|
|
|
||||||
212
js/renderer.js
212
js/renderer.js
|
|
@ -2,36 +2,88 @@ class MDRenderer {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.rules = {
|
this.rules = {
|
||||||
heading: /^(#{1,6})\s+(.+)$/gm,
|
heading: /^(#{1,6})\s+(.+)$/gm,
|
||||||
paragraph: /^(?!#|>|\s*\d+\.|```|\s*-\s)(.+)$/gm,
|
paragraph: /^(?!#|>|\s*\d+\.|```|\s*-\s|\s*\*\s|\||\s*\[\s*[x\s]\s*\]|\^\[\d+\]|\s*={3,}|\s*-{3,})(.+)$/gm,
|
||||||
bold: /\*\*(.+?)\*\*/g,
|
bold: /\*\*(.+?)\*\*/g,
|
||||||
italic: /\*(.+?)\*/g,
|
italic: /\*(.+?)\*/g,
|
||||||
|
strikethrough: /~~(.+?)~~/g,
|
||||||
code: /`(.+?)`/g,
|
code: /`(.+?)`/g,
|
||||||
codeblock: /```(\w*)\n([\s\S]*?)```/g,
|
codeblock: /```(\w*)\n([\s\S]*?)```/g,
|
||||||
link: /\[(.+?)\]\((.+?)\)/g,
|
link: /\[(.+?)\]\((.+?)(?:\s+"(.+?)")?\)/g,
|
||||||
list: /^(\s*)-\s(.+)$/gm,
|
image: /!\[(.+?)\]\((.+?)(?:\s+"(.+?)")?\)/g,
|
||||||
|
list: /^(\s*)[*-]\s(.+)$/gm,
|
||||||
orderedList: /^(\s*)\d+\.\s(.+)$/gm,
|
orderedList: /^(\s*)\d+\.\s(.+)$/gm,
|
||||||
blockquote: /^>\s(.+)$/gm,
|
blockquote: /^>\s(.+)$/gm,
|
||||||
|
horizontalRule: /^([-*_])\1{2,}$/gm,
|
||||||
|
table: /^\|(.+)\|$/gm,
|
||||||
|
tableDelimiter: /^\|(?:[-:]+[-| :]*)\|$/gm,
|
||||||
|
taskList: /^(\s*)-\s\[([ x])\]\s(.+)$/gm,
|
||||||
|
superscript: /\^(.+?)\^/g,
|
||||||
|
subscript: /~(.+?)~/g,
|
||||||
|
highlight: /==(.+?)==/g,
|
||||||
|
footnoteRef: /\[\^(\d+)\]/g,
|
||||||
|
footnote: /^\[\^(\d+)\]:\s(.+)$/gm,
|
||||||
|
abbreviation: /\*\[(.+?)\]:\s(.+)$/gm,
|
||||||
|
definition: /^:\s(.+?)\n\s{2,}(.+)$/gm,
|
||||||
|
emoji: /:([\w+-]+):/g,
|
||||||
|
};
|
||||||
|
|
||||||
|
this.footnotes = new Map();
|
||||||
|
this.abbreviations = new Map();
|
||||||
|
this.emojis = {
|
||||||
|
'smile': '😊',
|
||||||
|
'laugh': '😄',
|
||||||
|
'wink': '😉',
|
||||||
|
'heart': '❤️',
|
||||||
|
'check': '✅',
|
||||||
|
'x': '❌',
|
||||||
|
'warning': '⚠️',
|
||||||
|
'info': 'ℹ️',
|
||||||
|
// Add more emojis as needed
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
render(markdown) {
|
render(markdown) {
|
||||||
let html = markdown;
|
let html = markdown;
|
||||||
|
|
||||||
|
// Reset collections
|
||||||
|
this.footnotes.clear();
|
||||||
|
this.abbreviations.clear();
|
||||||
|
|
||||||
|
// Pre-process footnotes and abbreviations
|
||||||
|
html = this.collectFootnotes(html);
|
||||||
|
html = this.collectAbbreviations(html);
|
||||||
|
|
||||||
// Convert code blocks first to prevent interference
|
// Convert code blocks first to prevent interference
|
||||||
html = this.renderCodeBlocks(html);
|
html = this.renderCodeBlocks(html);
|
||||||
|
|
||||||
// Convert block elements
|
// Convert block elements
|
||||||
|
html = this.renderHorizontalRules(html);
|
||||||
html = this.renderBlockquotes(html);
|
html = this.renderBlockquotes(html);
|
||||||
html = this.renderHeadings(html);
|
html = this.renderHeadings(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.renderParagraphs(html);
|
html = this.renderParagraphs(html);
|
||||||
|
|
||||||
// Convert inline elements
|
// Convert inline elements
|
||||||
html = this.renderBold(html);
|
html = this.renderBold(html);
|
||||||
html = this.renderItalic(html);
|
html = this.renderItalic(html);
|
||||||
|
html = this.renderStrikethrough(html);
|
||||||
|
html = this.renderHighlight(html);
|
||||||
|
html = this.renderSuperscript(html);
|
||||||
|
html = this.renderSubscript(html);
|
||||||
html = this.renderInlineCode(html);
|
html = this.renderInlineCode(html);
|
||||||
|
html = this.renderImages(html);
|
||||||
html = this.renderLinks(html);
|
html = this.renderLinks(html);
|
||||||
|
html = this.renderFootnoteRefs(html);
|
||||||
|
html = this.renderEmojis(html);
|
||||||
|
|
||||||
|
// Add footnotes section if any exist
|
||||||
|
if (this.footnotes.size > 0) {
|
||||||
|
html += this.renderFootnotesSection();
|
||||||
|
}
|
||||||
|
|
||||||
// Wrap the content in a div with the mdrender class
|
// Wrap the content in a div with the mdrender class
|
||||||
return `<div class="mdrender">${html}</div>`;
|
return `<div class="mdrender">${html}</div>`;
|
||||||
|
|
@ -40,7 +92,8 @@ class MDRenderer {
|
||||||
renderHeadings(text) {
|
renderHeadings(text) {
|
||||||
return text.replace(this.rules.heading, (match, level, content) => {
|
return text.replace(this.rules.heading, (match, level, content) => {
|
||||||
const headingLevel = level.length;
|
const headingLevel = level.length;
|
||||||
return `<h${headingLevel}>${content}</h${headingLevel}>`;
|
const id = content.toLowerCase().replace(/[^\w]+/g, '-');
|
||||||
|
return `<h${headingLevel} id="${id}">${content}</h${headingLevel}>`;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -58,6 +111,10 @@ class MDRenderer {
|
||||||
return text.replace(this.rules.italic, '<em>$1</em>');
|
return text.replace(this.rules.italic, '<em>$1</em>');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
renderStrikethrough(text) {
|
||||||
|
return text.replace(this.rules.strikethrough, '<del>$1</del>');
|
||||||
|
}
|
||||||
|
|
||||||
renderInlineCode(text) {
|
renderInlineCode(text) {
|
||||||
return text.replace(this.rules.code, '<code>$1</code>');
|
return text.replace(this.rules.code, '<code>$1</code>');
|
||||||
}
|
}
|
||||||
|
|
@ -69,7 +126,17 @@ class MDRenderer {
|
||||||
}
|
}
|
||||||
|
|
||||||
renderLinks(text) {
|
renderLinks(text) {
|
||||||
return text.replace(this.rules.link, '<a href="$2">$1</a>');
|
return text.replace(this.rules.link, (match, text, url, title) => {
|
||||||
|
const titleAttr = title ? ` title="${title}"` : '';
|
||||||
|
return `<a href="${url}"${titleAttr}>${text}</a>`;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
renderImages(text) {
|
||||||
|
return text.replace(this.rules.image, (match, alt, url, title) => {
|
||||||
|
const titleAttr = title ? ` title="${title}"` : '';
|
||||||
|
return `<img src="${url}" alt="${alt}"${titleAttr}>`;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
renderLists(text) {
|
renderLists(text) {
|
||||||
|
|
@ -121,4 +188,139 @@ class MDRenderer {
|
||||||
renderBlockquotes(text) {
|
renderBlockquotes(text) {
|
||||||
return text.replace(this.rules.blockquote, '<blockquote>$1</blockquote>');
|
return text.replace(this.rules.blockquote, '<blockquote>$1</blockquote>');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
renderHorizontalRules(text) {
|
||||||
|
return text.replace(this.rules.horizontalRule, '<hr>');
|
||||||
|
}
|
||||||
|
|
||||||
|
renderTables(text) {
|
||||||
|
let html = text;
|
||||||
|
let inTable = false;
|
||||||
|
let alignment = [];
|
||||||
|
|
||||||
|
// Split into lines
|
||||||
|
const lines = html.split('\n');
|
||||||
|
const newLines = [];
|
||||||
|
|
||||||
|
for (let i = 0; i < lines.length; i++) {
|
||||||
|
const line = lines[i];
|
||||||
|
|
||||||
|
if (this.rules.tableDelimiter.test(line)) {
|
||||||
|
// Get alignment info
|
||||||
|
alignment = line.split('|').slice(1, -1).map(cell => {
|
||||||
|
cell = cell.trim();
|
||||||
|
if (cell.startsWith(':') && cell.endsWith(':')) return 'center';
|
||||||
|
if (cell.endsWith(':')) return 'right';
|
||||||
|
return 'left';
|
||||||
|
});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.rules.table.test(line)) {
|
||||||
|
const cells = line.split('|').slice(1, -1);
|
||||||
|
|
||||||
|
if (!inTable) {
|
||||||
|
inTable = true;
|
||||||
|
newLines.push('<table>');
|
||||||
|
newLines.push('<thead>');
|
||||||
|
newLines.push('<tr>');
|
||||||
|
cells.forEach((cell, index) => {
|
||||||
|
const align = alignment[index] ? ` style="text-align: ${alignment[index]}"` : '';
|
||||||
|
newLines.push(`<th${align}>${cell.trim()}</th>`);
|
||||||
|
});
|
||||||
|
newLines.push('</tr>');
|
||||||
|
newLines.push('</thead>');
|
||||||
|
newLines.push('<tbody>');
|
||||||
|
} else {
|
||||||
|
newLines.push('<tr>');
|
||||||
|
cells.forEach((cell, index) => {
|
||||||
|
const align = alignment[index] ? ` style="text-align: ${alignment[index]}"` : '';
|
||||||
|
newLines.push(`<td${align}>${cell.trim()}</td>`);
|
||||||
|
});
|
||||||
|
newLines.push('</tr>');
|
||||||
|
}
|
||||||
|
} else if (inTable) {
|
||||||
|
inTable = false;
|
||||||
|
newLines.push('</tbody>');
|
||||||
|
newLines.push('</table>');
|
||||||
|
newLines.push(line);
|
||||||
|
} else {
|
||||||
|
newLines.push(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (inTable) {
|
||||||
|
newLines.push('</tbody>');
|
||||||
|
newLines.push('</table>');
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
return text.replace(this.rules.superscript, '<sup>$1</sup>');
|
||||||
|
}
|
||||||
|
|
||||||
|
renderSubscript(text) {
|
||||||
|
return text.replace(this.rules.subscript, '<sub>$1</sub>');
|
||||||
|
}
|
||||||
|
|
||||||
|
renderHighlight(text) {
|
||||||
|
return text.replace(this.rules.highlight, '<mark>$1</mark>');
|
||||||
|
}
|
||||||
|
|
||||||
|
collectFootnotes(text) {
|
||||||
|
return text.replace(this.rules.footnote, (match, ref, content) => {
|
||||||
|
this.footnotes.set(ref, content);
|
||||||
|
return '';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
renderFootnoteRefs(text) {
|
||||||
|
return text.replace(this.rules.footnoteRef, (match, ref) => {
|
||||||
|
if (this.footnotes.has(ref)) {
|
||||||
|
return `<sup class="footnote-ref"><a href="#fn${ref}" id="fnref${ref}">${ref}</a></sup>`;
|
||||||
|
}
|
||||||
|
return match;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
renderFootnotesSection() {
|
||||||
|
if (this.footnotes.size === 0) return '';
|
||||||
|
|
||||||
|
let html = '<section class="footnotes"><hr><ol>';
|
||||||
|
for (const [ref, content] of this.footnotes) {
|
||||||
|
html += `
|
||||||
|
<li id="fn${ref}">
|
||||||
|
${content}
|
||||||
|
<a href="#fnref${ref}" class="footnote-backref">↩</a>
|
||||||
|
</li>`;
|
||||||
|
}
|
||||||
|
html += '</ol></section>';
|
||||||
|
return html;
|
||||||
|
}
|
||||||
|
|
||||||
|
collectAbbreviations(text) {
|
||||||
|
return text.replace(this.rules.abbreviation, (match, abbr, definition) => {
|
||||||
|
this.abbreviations.set(abbr, definition);
|
||||||
|
return '';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
renderDefinitions(text) {
|
||||||
|
return text.replace(this.rules.definition, '<dl><dt>$1</dt><dd>$2</dd></dl>');
|
||||||
|
}
|
||||||
|
|
||||||
|
renderEmojis(text) {
|
||||||
|
return text.replace(this.rules.emoji, (match, name) => {
|
||||||
|
return this.emojis[name] || match;
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
423
js/script.js
423
js/script.js
|
|
@ -1,9 +1,15 @@
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
document.addEventListener('DOMContentLoaded', function() {
|
||||||
const renderer = new MDRenderer();
|
const renderer = new MDRenderer();
|
||||||
const editor = document.getElementById('markdown-input');
|
const input = document.getElementById('markdown-input');
|
||||||
const preview = document.getElementById('preview');
|
const preview = document.getElementById('preview');
|
||||||
const copyButton = document.getElementById('copy-html');
|
const htmlOutput = document.getElementById('html-output');
|
||||||
const themeToggle = document.getElementById('theme-toggle');
|
const themeToggle = document.getElementById('theme-toggle');
|
||||||
|
const copyHtmlBtn = document.getElementById('copy-html');
|
||||||
|
const clearInputBtn = document.getElementById('clear-input');
|
||||||
|
const refreshPreviewBtn = document.getElementById('refresh-preview');
|
||||||
|
const togglePreviewBtn = document.getElementById('toggle-preview');
|
||||||
|
const formatHtmlBtn = document.getElementById('format-html');
|
||||||
|
const exampleSelect = document.getElementById('example-select');
|
||||||
|
|
||||||
// Theme handling
|
// Theme handling
|
||||||
const theme = localStorage.getItem('theme') || 'light';
|
const theme = localStorage.getItem('theme') || 'light';
|
||||||
|
|
@ -16,55 +22,390 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||||
localStorage.setItem('theme', newTheme);
|
localStorage.setItem('theme', newTheme);
|
||||||
});
|
});
|
||||||
|
|
||||||
function updatePreview() {
|
// Example markdown templates
|
||||||
const markdown = editor.value;
|
const examples = {
|
||||||
const html = renderer.render(markdown);
|
basic: `# Basic Markdown Example
|
||||||
preview.innerHTML = html;
|
## Headers
|
||||||
document.getElementById('html-output').textContent = html;
|
|
||||||
}
|
|
||||||
|
|
||||||
editor.addEventListener('input', updatePreview);
|
You can write headers from h1 to h6 using # symbols.
|
||||||
|
|
||||||
copyButton.addEventListener('click', () => {
|
## Text Formatting
|
||||||
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
|
*This text is italic*
|
||||||
editor.value = `# Welcome to MDRender
|
**This text is bold**
|
||||||
|
***This text is bold and italic***
|
||||||
|
~~This text is strikethrough~~
|
||||||
|
==This text is highlighted==
|
||||||
|
|
||||||
This is a simple markdown renderer. Try editing this text!
|
## Lists
|
||||||
|
|
||||||
## Features:
|
### Unordered List
|
||||||
- **Bold text** and *italic text*
|
- Item 1
|
||||||
- Lists (like this one!)
|
- Item 2
|
||||||
- Links: [Visit GitHub](https://github.com)
|
- Nested item
|
||||||
- Code: \`console.log('Hello World!')\`
|
- Another nested item
|
||||||
- And more...
|
- Item 3
|
||||||
|
|
||||||
|
### Ordered List
|
||||||
|
1. First item
|
||||||
|
2. Second item
|
||||||
|
1. Nested item
|
||||||
|
2. Another nested item
|
||||||
|
3. Third item
|
||||||
|
|
||||||
|
## Links and Images
|
||||||
|
|
||||||
|
[Visit MDRender](https://github.com/yourusername/MDRender)
|
||||||
|

|
||||||
|
|
||||||
|
## Code
|
||||||
|
|
||||||
|
Inline \`code\` looks like this.
|
||||||
|
|
||||||
### Code Blocks
|
|
||||||
\`\`\`javascript
|
\`\`\`javascript
|
||||||
|
// Code block
|
||||||
function hello() {
|
function hello() {
|
||||||
console.log('Hello, World!');
|
console.log("Hello, world!");
|
||||||
}
|
}
|
||||||
\`\`\`
|
\`\`\`
|
||||||
|
|
||||||
> Blockquotes are supported too!
|
## Blockquotes
|
||||||
|
|
||||||
1. Ordered lists
|
> This is a blockquote
|
||||||
2. Work as well
|
> It can span multiple lines
|
||||||
3. Try them out!
|
>> And can be nested`,
|
||||||
`;
|
|
||||||
|
|
||||||
// Initial render
|
advanced: `# Advanced Features
|
||||||
updatePreview();
|
|
||||||
|
## Task Lists
|
||||||
|
- [x] Completed task
|
||||||
|
- [ ] Incomplete task
|
||||||
|
- [x] Another completed task
|
||||||
|
|
||||||
|
## Tables
|
||||||
|
| Header 1 | Header 2 | Header 3 |
|
||||||
|
|:---------|:--------:|---------:|
|
||||||
|
| Left | Center | Right |
|
||||||
|
| aligned | aligned | aligned |
|
||||||
|
|
||||||
|
## Footnotes
|
||||||
|
Here's a sentence with a footnote[^1].
|
||||||
|
|
||||||
|
[^1]: This is the footnote content.
|
||||||
|
|
||||||
|
## Definition Lists
|
||||||
|
Term 1
|
||||||
|
: Definition 1
|
||||||
|
: Another definition
|
||||||
|
|
||||||
|
Term 2
|
||||||
|
: Definition 2
|
||||||
|
|
||||||
|
## Abbreviations
|
||||||
|
*[HTML]: HyperText Markup Language
|
||||||
|
*[CSS]: Cascading Style Sheets
|
||||||
|
|
||||||
|
HTML and CSS are web technologies.
|
||||||
|
|
||||||
|
## Superscript and Subscript
|
||||||
|
E = mc^2^
|
||||||
|
H~2~O
|
||||||
|
|
||||||
|
## Emojis
|
||||||
|
:smile: :heart: :thumbsup: :rocket:`,
|
||||||
|
|
||||||
|
tables: `# Table Examples
|
||||||
|
|
||||||
|
## Simple Table
|
||||||
|
| Name | Age | City |
|
||||||
|
|---------|-----|----------|
|
||||||
|
| John | 25 | New York |
|
||||||
|
| Alice | 30 | London |
|
||||||
|
| Bob | 28 | Paris |
|
||||||
|
|
||||||
|
## Table with Alignment
|
||||||
|
| Left | Center | Right |
|
||||||
|
|:---------|:--------:|---------:|
|
||||||
|
| Text | Text | Text |
|
||||||
|
| Left | Center | Right |
|
||||||
|
|
||||||
|
## Complex Table
|
||||||
|
| Feature | Basic | Pro | Enterprise |
|
||||||
|
|:--------|:------|:---:|----------:|
|
||||||
|
| Users | 10 | 100 | Unlimited |
|
||||||
|
| Storage | 5GB | 50GB| 500GB |
|
||||||
|
| Support | Email | 24/7| Priority |
|
||||||
|
| Price | Free | $10 | Custom |`,
|
||||||
|
|
||||||
|
footnotes: `# Document with Footnotes
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
This is a sample document[^1] that demonstrates the use of footnotes[^2].
|
||||||
|
|
||||||
|
You can use footnotes to add additional information[^details] or references[^ref].
|
||||||
|
|
||||||
|
## Main Content
|
||||||
|
The content continues here with another footnote[^3].
|
||||||
|
|
||||||
|
[^1]: This is the first footnote.
|
||||||
|
[^2]: Footnotes can contain multiple lines
|
||||||
|
and even include code blocks.
|
||||||
|
[^details]: You can use any text as the footnote identifier.
|
||||||
|
[^ref]: Reference: *Markdown Guide*, 2021
|
||||||
|
[^3]: This is the last footnote.
|
||||||
|
|
||||||
|
## Using Named Footnotes
|
||||||
|
You can refer to the same footnote[^details] multiple times.`,
|
||||||
|
|
||||||
|
default: `# MDRender Feature Showcase
|
||||||
|
Welcome to MDRender! This example demonstrates all supported features.
|
||||||
|
|
||||||
|
## 1. Text Formatting
|
||||||
|
|
||||||
|
### Basic Formatting
|
||||||
|
Regular text
|
||||||
|
*Italic text* or _italic text_
|
||||||
|
**Bold text** or __bold text__
|
||||||
|
***Bold and italic*** or ___bold and italic___
|
||||||
|
~~Strikethrough text~~
|
||||||
|
==Highlighted text==
|
||||||
|
|
||||||
|
### Superscript and Subscript
|
||||||
|
E = mc^2^
|
||||||
|
H~2~O
|
||||||
|
X^2^ + Y^2^ = Z^2^
|
||||||
|
|
||||||
|
## 2. Headers
|
||||||
|
# H1 Header
|
||||||
|
## H2 Header
|
||||||
|
### H3 Header
|
||||||
|
#### H4 Header
|
||||||
|
##### H5 Header
|
||||||
|
###### H6 Header
|
||||||
|
|
||||||
|
## 3. Lists
|
||||||
|
|
||||||
|
### Unordered Lists
|
||||||
|
- First level
|
||||||
|
- Second level
|
||||||
|
- Third level
|
||||||
|
- Fourth level
|
||||||
|
- Another item
|
||||||
|
* Mixed markers
|
||||||
|
+ Are supported
|
||||||
|
|
||||||
|
### Ordered Lists
|
||||||
|
1. First item
|
||||||
|
2. Second item
|
||||||
|
1. Nested item
|
||||||
|
2. Another nested
|
||||||
|
1. Deep nested
|
||||||
|
3. Third item
|
||||||
|
|
||||||
|
### Task Lists
|
||||||
|
- [x] Completed task
|
||||||
|
- [ ] Incomplete task
|
||||||
|
- [x] Another completed task
|
||||||
|
- [ ] Nested task
|
||||||
|
- [x] Completed nested task
|
||||||
|
|
||||||
|
### Definition Lists
|
||||||
|
Term 1
|
||||||
|
: First definition
|
||||||
|
: Second definition
|
||||||
|
|
||||||
|
Complex Term
|
||||||
|
: Definition with *inline* formatting
|
||||||
|
: Another definition with **bold** text
|
||||||
|
|
||||||
|
## 4. Code
|
||||||
|
|
||||||
|
### Inline Code
|
||||||
|
Use \`const x = 42;\` for inline code.
|
||||||
|
|
||||||
|
### Code Blocks
|
||||||
|
\`\`\`javascript
|
||||||
|
// JavaScript code
|
||||||
|
function greet(name) {
|
||||||
|
console.log(\`Hello, \${name}!\`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call the function
|
||||||
|
greet('World');
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
\`\`\`python
|
||||||
|
# Python code
|
||||||
|
def factorial(n):
|
||||||
|
return 1 if n <= 1 else n * factorial(n - 1)
|
||||||
|
|
||||||
|
print(factorial(5)) # Output: 120
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
## 5. Blockquotes
|
||||||
|
|
||||||
|
> Single level quote
|
||||||
|
> Multiple lines
|
||||||
|
> In the same quote
|
||||||
|
|
||||||
|
> Nested quotes
|
||||||
|
>> Second level
|
||||||
|
>>> Third level
|
||||||
|
>>>> Fourth level
|
||||||
|
|
||||||
|
## 6. Links and Images
|
||||||
|
|
||||||
|
### Links
|
||||||
|
[Basic link](https://example.com)
|
||||||
|
[Link with title](https://example.com "Visit example.com")
|
||||||
|
[Reference link][ref1]
|
||||||
|
[Another reference][ref2]
|
||||||
|
|
||||||
|
[ref1]: https://example.com "Reference 1"
|
||||||
|
[ref2]: https://example.com "Reference 2"
|
||||||
|
|
||||||
|
### Images
|
||||||
|

|
||||||
|

|
||||||
|
[](https://example.com)
|
||||||
|
|
||||||
|
## 7. Tables
|
||||||
|
|
||||||
|
### Basic Table
|
||||||
|
| Header 1 | Header 2 | Header 3 |
|
||||||
|
|----------|----------|----------|
|
||||||
|
| Cell 1 | Cell 2 | Cell 3 |
|
||||||
|
| Cell 4 | Cell 5 | Cell 6 |
|
||||||
|
|
||||||
|
### Aligned Table
|
||||||
|
| Left | Center | Right |
|
||||||
|
|:-----|:------:|------:|
|
||||||
|
| 1 | 2 | 3 |
|
||||||
|
| 4 | 5 | 6 |
|
||||||
|
|
||||||
|
### Complex Table
|
||||||
|
| Feature | Basic | Pro | Enterprise |
|
||||||
|
|:--------|:------|:---:|----------:|
|
||||||
|
| Users | 10 | 100 | Unlimited |
|
||||||
|
| Storage | 5GB | 50GB| 500GB |
|
||||||
|
| Support | Email | 24/7| Priority |
|
||||||
|
| Price | Free | $10 | Custom |
|
||||||
|
|
||||||
|
## 8. Footnotes
|
||||||
|
Here's a sentence with a footnote[^1].
|
||||||
|
Here's another with a named footnote[^note].
|
||||||
|
And one more[^2].
|
||||||
|
|
||||||
|
[^1]: Basic footnote content.
|
||||||
|
[^note]: Named footnote with multiple paragraphs.
|
||||||
|
|
||||||
|
Indent paragraphs to include them in the footnote.
|
||||||
|
[^2]: Another simple footnote.
|
||||||
|
|
||||||
|
## 9. Abbreviations
|
||||||
|
HTML and CSS are web technologies.
|
||||||
|
The WHO was founded in 1948.
|
||||||
|
|
||||||
|
*[HTML]: HyperText Markup Language
|
||||||
|
*[CSS]: Cascading Style Sheets
|
||||||
|
*[WHO]: World Health Organization
|
||||||
|
|
||||||
|
## 10. Horizontal Rules
|
||||||
|
|
||||||
|
Three or more hyphens:
|
||||||
|
---
|
||||||
|
|
||||||
|
Three or more asterisks:
|
||||||
|
***
|
||||||
|
|
||||||
|
Three or more underscores:
|
||||||
|
___
|
||||||
|
|
||||||
|
## 11. Emojis
|
||||||
|
:smile: :heart: :thumbsup: :rocket:
|
||||||
|
:star: :fire: :sunny: :moon:
|
||||||
|
: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
|
||||||
|
|
||||||
|
## 13. HTML Support
|
||||||
|
<div style="color: blue;">
|
||||||
|
This is <em>HTML</em> content with <strong>inline styles</strong>.
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Click to expand</summary>
|
||||||
|
This is expandable content using HTML tags.
|
||||||
|
</details>
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
That's all! This document showcases every feature supported by MDRender. Feel free to edit and experiment with the content!`
|
||||||
|
};
|
||||||
|
|
||||||
|
// Update preview and HTML output
|
||||||
|
function updateOutput() {
|
||||||
|
const markdown = input.value;
|
||||||
|
const html = renderer.render(markdown);
|
||||||
|
preview.innerHTML = html;
|
||||||
|
htmlOutput.textContent = html;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Event listeners
|
||||||
|
input.addEventListener('input', updateOutput);
|
||||||
|
|
||||||
|
copyHtmlBtn.addEventListener('click', () => {
|
||||||
|
navigator.clipboard.writeText(htmlOutput.textContent)
|
||||||
|
.then(() => {
|
||||||
|
const originalText = copyHtmlBtn.textContent;
|
||||||
|
copyHtmlBtn.textContent = 'Copied!';
|
||||||
|
setTimeout(() => {
|
||||||
|
copyHtmlBtn.textContent = originalText;
|
||||||
|
}, 2000);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
clearInputBtn.addEventListener('click', () => {
|
||||||
|
input.value = '';
|
||||||
|
updateOutput();
|
||||||
|
});
|
||||||
|
|
||||||
|
refreshPreviewBtn.addEventListener('click', updateOutput);
|
||||||
|
|
||||||
|
togglePreviewBtn.addEventListener('click', () => {
|
||||||
|
preview.style.display = preview.style.display === 'none' ? 'block' : 'none';
|
||||||
|
togglePreviewBtn.textContent = preview.style.display === 'none' ? 'Show Preview' : 'Hide Preview';
|
||||||
|
});
|
||||||
|
|
||||||
|
formatHtmlBtn.addEventListener('click', () => {
|
||||||
|
const formatted = html_beautify(htmlOutput.textContent, {
|
||||||
|
indent_size: 2,
|
||||||
|
wrap_line_length: 80,
|
||||||
|
preserve_newlines: true,
|
||||||
|
max_preserve_newlines: 2
|
||||||
|
});
|
||||||
|
htmlOutput.textContent = formatted;
|
||||||
|
});
|
||||||
|
|
||||||
|
exampleSelect.addEventListener('change', () => {
|
||||||
|
const selected = exampleSelect.value;
|
||||||
|
if (selected && examples[selected]) {
|
||||||
|
input.value = examples[selected];
|
||||||
|
updateOutput();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Initial render with default content
|
||||||
|
input.value = examples.default;
|
||||||
|
updateOutput();
|
||||||
});
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue