diff --git a/paste-html-subset.docs.md b/paste-html-subset.docs.md deleted file mode 100644 index 9ad384d9..00000000 --- a/paste-html-subset.docs.md +++ /dev/null @@ -1,3 +0,0 @@ -This tool converts rich text into a clean HTML subset by letting you paste formatted content from any source. Paste text into the editable area to generate sanitized HTML code that only includes supported elements like paragraphs, headings, links, and lists. The tool provides both a code view of the resulting HTML and a live preview window to verify the output appearance. - - \ No newline at end of file diff --git a/paste-html-subset.html b/paste-html-subset.html index b2207f54..ae64ee45 100644 --- a/paste-html-subset.html +++ b/paste-html-subset.html @@ -115,7 +115,8 @@

Supported HTML elements

  • strong - Bold text
  • em - Italic text
  • blockquote - Quoted text
  • -
  • code - Code snippets
  • +
  • code - Inline code snippets
  • +
  • pre - Preformatted text blocks
  • a - Links (href attribute is preserved)
  • h1-h6 - Headings
  • ul - Unordered lists
  • @@ -156,7 +157,7 @@

    Preview

    // 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' ]; @@ -239,15 +240,22 @@

    Preview

    // 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(' 0) { + preStack.pop(); + } indent = Math.max(0, indent - 1); // Prevent negative indent formatted += getIndent(indent) + part + '\n'; } @@ -258,6 +266,9 @@

    Preview

    // Opening tag else { formatted += getIndent(indent) + part + '\n'; + if (/^]*>/i)) { indent++; @@ -265,8 +276,11 @@

    Preview

    } } // 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'; + } } }