Browse Source

fix: improve markdown cleanup for HTML to markdown conversion

- Normalize horizontal rules to consistent --- format
- Remove redundant bold/italic formatting from headers
- Fix empty bold pattern to not span across newlines
- Preserve list indentation when removing leading spaces
- Configure Turndown with consistent hr and bullet markers

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Fszontagh 8 months ago
parent
commit
1be68acf68
1 changed files with 24 additions and 9 deletions
  1. 24 9
      src/scraper/ContentExtractor.ts

+ 24 - 9
src/scraper/ContentExtractor.ts

@@ -10,7 +10,9 @@ export class ContentExtractor {
   constructor() {
   constructor() {
     this.turndownService = new TurndownService({
     this.turndownService = new TurndownService({
       headingStyle: 'atx',
       headingStyle: 'atx',
-      codeBlockStyle: 'fenced'
+      codeBlockStyle: 'fenced',
+      hr: '---',
+      bulletListMarker: '-'
     });
     });
   }
   }
 
 
@@ -103,25 +105,38 @@ export class ContentExtractor {
    */
    */
   private cleanupContent(content: string): string {
   private cleanupContent(content: string): string {
     return content
     return content
+      // Normalize horizontal rules first (Turndown may produce * * *, - - -, or ___ variants)
+      .replace(/^[\s]*[\*\-_][\s]*[\*\-_][\s]*[\*\-_][\s]*$/gm, '\n---\n')
+      // Fix malformed horizontal rules that got mixed with list markers (e.g., "- * *")
+      .replace(/^-\s*\*\s*\*\s*$/gm, '\n---\n')
+      .replace(/^\*\s*\*\s*\*\s*$/gm, '\n---\n')
       // Remove excessive newlines (3 or more becomes 2)
       // Remove excessive newlines (3 or more becomes 2)
       .replace(/\n{3,}/g, '\n\n')
       .replace(/\n{3,}/g, '\n\n')
       // Remove trailing spaces and tabs from lines
       // Remove trailing spaces and tabs from lines
       .replace(/[ \t]+$/gm, '')
       .replace(/[ \t]+$/gm, '')
-      // Remove leading spaces/tabs from lines (but preserve markdown indentation)
-      .replace(/^[ \t]+/gm, '')
+      // Remove leading spaces/tabs from lines (but preserve markdown indentation for lists)
+      .replace(/^[ \t]+(?![-*+\d])/gm, '')
       // Remove multiple spaces within text (but preserve intentional spacing)
       // Remove multiple spaces within text (but preserve intentional spacing)
       .replace(/[ \t]{2,}/g, ' ')
       .replace(/[ \t]{2,}/g, ' ')
-      // Clean up markdown list formatting
-      .replace(/\n\s*\n\s*[-\*\+]/g, '\n\n- ')
+      // Fix bold/italic text that lost its opening asterisks after list markers
+      // Pattern: "- *text:**" should become "- **text:**"
+      .replace(/^(-\s+)\*([^*\s][^*]*)\*\*$/gm, '$1**$2**')
+      // Clean up markdown list formatting - be more careful to not eat asterisks
+      .replace(/\n\s*\n\s*(-)\s+/g, '\n\n$1 ')
       // Clean up excessive spaces around punctuation
       // Clean up excessive spaces around punctuation
       .replace(/\s+([,.;:!?])/g, '$1')
       .replace(/\s+([,.;:!?])/g, '$1')
-      // Normalize line breaks around headers
+      // Normalize line breaks around headers - ensure blank line before headers
       .replace(/\n+(#{1,6}\s)/g, '\n\n$1')
       .replace(/\n+(#{1,6}\s)/g, '\n\n$1')
-      .replace(/(#{1,6}[^\n]*)\n+/g, '$1\n\n')
+      // Remove bold/italic formatting from headers (redundant - headers are already emphasized)
+      .replace(/^(#{1,6}\s+)\*\*(.+?)\*\*\s*$/gm, '$1$2')
+      .replace(/^(#{1,6}\s+)\*(.+?)\*\s*$/gm, '$1$2')
+      .replace(/^(#{1,6}\s+)__(.+?)__\s*$/gm, '$1$2')
+      .replace(/^(#{1,6}\s+)_(.+?)_\s*$/gm, '$1$2')
       // Remove empty markdown links and formatting
       // Remove empty markdown links and formatting
       .replace(/\[\]\([^)]*\)/g, '')
       .replace(/\[\]\([^)]*\)/g, '')
-      .replace(/\*\*\s*\*\*/g, '')
-      .replace(/__\s*__/g, '')
+      // Only remove empty bold/underline on same line (not spanning newlines)
+      .replace(/\*\*[ \t]*\*\*/g, '')
+      .replace(/__[ \t]*__/g, '')
       // Final trim
       // Final trim
       .trim();
       .trim();
   }
   }