* 解析内容为HTML
(content: string)
| 840 | * 解析内容为HTML |
| 841 | */ |
| 842 | private parseContentToHTML(content: string): string { |
| 843 | // 简单的文本到HTML转换 |
| 844 | return content |
| 845 | .split('\n\n') |
| 846 | .map(paragraph => { |
| 847 | const trimmed = paragraph.trim(); |
| 848 | if (!trimmed) return ''; |
| 849 | |
| 850 | // 检查是否是标题 |
| 851 | if (trimmed.startsWith('# ')) { |
| 852 | return `<h1>${this.escapeHtml(trimmed.substring(2))}</h1>`; |
| 853 | } else if (trimmed.startsWith('## ')) { |
| 854 | return `<h2>${this.escapeHtml(trimmed.substring(3))}</h2>`; |
| 855 | } else if (trimmed.startsWith('### ')) { |
| 856 | return `<h3>${this.escapeHtml(trimmed.substring(4))}</h3>`; |
| 857 | } else if (trimmed.startsWith('- ') || trimmed.startsWith('* ')) { |
| 858 | // 简单的列表处理 |
| 859 | const items = trimmed |
| 860 | .split('\n') |
| 861 | .map(line => { |
| 862 | if (line.startsWith('- ') || line.startsWith('* ')) { |
| 863 | return `<li>${this.escapeHtml(line.substring(2))}</li>`; |
| 864 | } |
| 865 | return this.escapeHtml(line); |
| 866 | }) |
| 867 | .join('\n'); |
| 868 | return `<ul>\n${items}\n</ul>`; |
| 869 | } else { |
| 870 | return `<p>${this.escapeHtml(trimmed).replace(/\n/g, '<br>')}</p>`; |
| 871 | } |
| 872 | }) |
| 873 | .filter(p => p) |
| 874 | .join('\n'); |
| 875 | } |
| 876 | |
| 877 | /** |
| 878 | * 将内容分割成适合PDF的行,支持标题识别 |
no test coverage detected