Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 2, 2025

HN comments with multiple paragraphs were being collapsed into a single line. The htmlToText function used textContent which strips all HTML structure including <p> and <br> tags.

Changes

  • Preserve paragraph and line breaks: Convert </p> and <br> tags to \n before extracting text content
  • Handle tag variations: Regex matches <br>, <br/>, <br />, and non-standard </br>
  • Add test coverage: New test suite validates newline preservation across multiple HTML structures
// Before: collapsed all HTML structure
const htmlToText = (html) => {
  const div = document.createElement('div');
  div.innerHTML = html;
  return div.textContent.trim();
};

// After: preserve paragraph breaks
const htmlToText = (html) => {
  if (!html) return '';
  let text = html
    .replace(/<\/p>/gi, '\n')
    .replace(/<p>/gi, '')
    .replace(/<br\s*\/?>|<\/br>/gi, '\n');
  const div = document.createElement('div');
  div.innerHTML = text;
  return (div.textContent || div.innerText || '').trim();
};

Before/After

Screenshot showing collapsed text vs preserved newlines

Top textarea shows original behavior (all paragraphs run together). Bottom shows fixed behavior (each paragraph on its own line).

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • hn.algolia.com
    • Triggering command: `python3 -c
      import json
      import urllib.request

Fetch a sample comment from HN API

url = 'REDACTED,author_pg&hitsPerPage=1'
with urllib.request.urlopen(url) as response:
data = json.loads(response.read())
if data['hits']:
comment = data['hits'][0]
print('Sample comment HTML:')
print('='*60)
print(comment['comment_text'][:500])
print('='*60)` (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>hn-comments-for-user removes all newlines from comments</issue_title>
<issue_description>It currently collapses newlines down to not even a space character.</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Nov 2, 2025

Deploying tools with  Cloudflare Pages  Cloudflare Pages

Latest commit: 6131198
Status: ✅  Deploy successful!
Preview URL: https://b20647ef.tools-b1q.pages.dev
Branch Preview URL: https://copilot-fix-hn-comments-newl.tools-b1q.pages.dev

View logs

Copilot AI changed the title [WIP] Fix hn-comments-for-user to preserve newlines in comments Fix hn-comments-for-user removing newlines from comments Nov 2, 2025
Copilot AI requested a review from simonw November 2, 2025 21:04
Copilot finished work on behalf of simonw November 2, 2025 21:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

hn-comments-for-user removes all newlines from comments

2 participants