|
|
@@ -10,7 +10,9 @@ export class ContentExtractor {
|
|
|
constructor() {
|
|
|
this.turndownService = new TurndownService({
|
|
|
headingStyle: 'atx',
|
|
|
- codeBlockStyle: 'fenced'
|
|
|
+ codeBlockStyle: 'fenced',
|
|
|
+ hr: '---',
|
|
|
+ bulletListMarker: '-'
|
|
|
});
|
|
|
}
|
|
|
|
|
|
@@ -103,25 +105,38 @@ export class ContentExtractor {
|
|
|
*/
|
|
|
private cleanupContent(content: string): string {
|
|
|
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)
|
|
|
.replace(/\n{3,}/g, '\n\n')
|
|
|
// Remove trailing spaces and tabs from lines
|
|
|
.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)
|
|
|
.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
|
|
|
.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(/(#{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
|
|
|
.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
|
|
|
.trim();
|
|
|
}
|