Files
atlas-seller-kit/apps/extension/lib/description-format.ts
T

23 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 将采集到的描述规范为可阅读的纯文本。保留原有段落和列表边界;只有来源
* 已把结构压成一行时,才依据常见商品段落标题补出最小必要的换行。
*/
export function formatCollectedDescription(value: string): string {
const withoutMarkup = value
.replace(/\r\n?/g, '\n')
.replace(/<\s*br\s*\/?\s*>/gi, '\n')
.replace(/<\s*\/\s*(?:p|div|section|article|li|h[1-6])\s*>/gi, '\n')
.replace(/<\s*li\b[^>]*>/gi, '\n- ')
.replace(/<[^>]*>/g, '');
const formatted = withoutMarkup
.replace(
/\s*(Описание(?:\s+товара)?|Характеристики|Преимущества|Особенности|Комплектация|Технические\s+характеристики|Specifications?|Features?|Package\s+contents?|商品描述|产品描述|商品信息|规格参数|产品参数|产品特点|包装清单)\s*([::])\s*/gi,
'\n\n$1$2\n',
)
.replace(/([^\n])\s*[-•·]\s+(?=[\p{L}\p{N}])/gu, '$1\n- ')
.replace(/[\t\f\v ]*\n[\t\f\v ]*/g, '\n')
.replace(/\n{3,}/g, '\n\n')
.trim();
return formatted;
}