Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions paste-html-subset.docs.md

This file was deleted.

30 changes: 22 additions & 8 deletions paste-html-subset.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ <h3>Supported HTML elements</h3>
<li><strong>strong</strong> - Bold text</li>
<li><strong>em</strong> - Italic text</li>
<li><strong>blockquote</strong> - Quoted text</li>
<li><strong>code</strong> - Code snippets</li>
<li><strong>code</strong> - Inline code snippets</li>
<li><strong>pre</strong> - Preformatted text blocks</li>
<li><strong>a</strong> - Links (href attribute is preserved)</li>
<li><strong>h1-h6</strong> - Headings</li>
<li><strong>ul</strong> - Unordered lists</li>
Expand Down Expand Up @@ -156,7 +157,7 @@ <h2>Preview</h2>
// Function to sanitize HTML and keep only allowed tags and attributes
function sanitizeHtml(html) {
const allowedTags = [
'p', 'strong', 'em', 'blockquote', 'code', 'a',
'p', 'strong', 'em', 'blockquote', 'code', 'pre', 'a',
'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
'ul', 'ol', 'li', 'dl', 'dt', 'dd'
];
Expand Down Expand Up @@ -239,15 +240,22 @@ <h2>Preview</h2>

// Split HTML by tags
const parts = html.split(/(<\/?[^>]+>)/);

const preStack = [];

for (let i = 0; i < parts.length; i++) {
const part = parts[i].trim();
if (!part) continue;

const rawPart = parts[i];
if (!rawPart) continue;

const part = rawPart.trim();
if (!part && preStack.length === 0) continue;

// Check if it's a tag
if (part.startsWith('<')) {
// Closing tag
if (part.startsWith('</')) {
if (/^<\/pre\b/i.test(part) && preStack.length > 0) {
preStack.pop();
}
indent = Math.max(0, indent - 1); // Prevent negative indent
formatted += getIndent(indent) + part + '\n';
}
Expand All @@ -258,15 +266,21 @@ <h2>Preview</h2>
// Opening tag
else {
formatted += getIndent(indent) + part + '\n';
if (/^<pre\b/i.test(part)) {
preStack.push('pre');
}
// Don't increase indent for inline elements
if (!part.match(/<(strong|em|code|a)[^>]*>/i)) {
indent++;
}
}
}
// Content
else if (part) {
formatted += getIndent(indent) + part + '\n';
else {
const content = preStack.length > 0 ? rawPart : part;
if (content || preStack.length > 0) {
formatted += getIndent(indent) + content + '\n';
}
}
}

Expand Down