import { renderTablesAsParagraphRows } from '../../../../../packages/publisher-platforms/src/baijiahao' export function prepareDongchediArticleHtml(html: string, title = ''): string { let next = renderTablesAsParagraphRows(html).trim() next = next.replace(/
${text}
` : '' }) next = next.replace(/${text}
` : '' }) next = next.replace(/<\/?(?:ol|ul)\b[^>]*>/gi, '') next = next.replace(/]*>/gi, '
')
next = next.replace(/<(strong|b)\b[^>]*>/gi, '').replace(/<\/b>/gi, '')
next = next.replace(/
/gi, ' ')
next = next.replace(/<(?!\/?(?:p|strong|img)\b)[^>]+>/gi, '')
next = next.replace(/
\s*<\/p>/gi, '')
next = next.replace(/\s+/g, ' ')
next = next.replace(/>\s+<')
next = stripLeadingDuplicateTitleParagraph(next, title)
return next.trim()
}
export function countDongchediContentWordCount(html: string): number {
const text = decodeDongchediHtmlEntities(html)
.replace(/<[^>]*>/g, '')
.replace(/\s+/g, '')
.trim()
return [...text].length
}
function decodeDongchediHtmlEntities(value: string): string {
return value
.replace(/ /gi, ' ')
.replace(/&/gi, '&')
.replace(/</gi, '<')
.replace(/>/gi, '>')
.replace(/"/gi, '"')
.replace(/'|'/gi, "'")
.replace(/(\d+);/g, (_match, code: string) => {
const value = Number.parseInt(code, 10)
return Number.isFinite(value) && value >= 0 && value <= 0x10ffff
? String.fromCodePoint(value)
: ''
})
.replace(/([0-9a-f]+);/gi, (_match, code: string) => {
const value = Number.parseInt(code, 16)
return Number.isFinite(value) && value >= 0 && value <= 0x10ffff
? String.fromCodePoint(value)
: ''
})
}
function normalizeDongchediImageTags(html: string): string {
return html.replace(/]*>/gi, (imageTag: string) => {
const src = extractAttribute(imageTag, 'src')
if (!src) {
return ''
}
const alt = extractAttribute(imageTag, 'alt') ?? ''
return `
`
})
}
function normalizeInlineContent(value: string): string {
return value
.replace(/
/gi, ' ')
.replace(
/<\/?(?:p|div|section|article|header|footer|ol|ul|li|blockquote|h[1-6])\b[^>]*>/gi,
' ',
)
.replace(/\s+/g, ' ')
.trim()
}
function stripLeadingDuplicateTitleParagraph(html: string, title: string): string {
const normalizedTitle = normalizeComparableText(title)
if (!normalizedTitle) {
return html
}
return html.replace(/^\s*
([\s\S]*?)<\/p>/i, (match, paragraphContent: string) => { return normalizeComparableText(paragraphContent) === normalizedTitle ? '' : match }) } function normalizeComparableText(value: string): string { return decodeDongchediHtmlEntities(value) .replace(/<[^>]*>/g, '') .replace(/\s+/g, '') .trim() } function extractAttribute(sourceTag: string, attributeName: string): string | null { const matcher = new RegExp( `\\s${attributeName}\\s*=\\s*(?:"([^"]*)"|'([^']*)'|([^\\s"'>]+))`, 'i', ) const match = matcher.exec(sourceTag) const value = match?.[1] ?? match?.[2] ?? match?.[3] ?? '' const trimmed = value.trim() return trimmed ? trimmed : null } function escapeAttribute(value: string): string { return value .replace(/&/g, '&') .replace(/"/g, '"') .replace(//g, '>') }