import sanitizeHtml from 'sanitize-html';
import { SITE_URL } from '../consts';
/** Sanitize и нормализация HTML тела поста для RSS ``. */
export function sanitizeForRss(html: string): string {
const absolutized = html.replace(/(src|href)=["']\/uploads\//g, `$1="${SITE_URL}/uploads/`);
return sanitizeHtml(absolutized, {
allowedTags: [
'p', 'br', 'strong', 'em', 'b', 'i', 'u', 's',
'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
'a', 'img',
'ul', 'ol', 'li',
'blockquote', 'pre', 'code',
'table', 'thead', 'tbody', 'tr', 'th', 'td',
'div', 'span', 'hr',
],
allowedAttributes: {
a: ['href', 'title', 'rel', 'target'],
img: ['src', 'alt', 'title', 'width', 'height'],
div: ['align'],
span: ['align'],
p: ['align'],
td: ['colspan', 'rowspan'],
th: ['colspan', 'rowspan'],
},
transformTags: {
a: (tagName, attribs) => ({
tagName,
attribs: {
...attribs,
target: '_blank',
rel: 'noopener noreferrer',
},
}),
},
allowedSchemes: ['http', 'https', 'mailto'],
});
}
/** Экранировать строку для XML-атрибутов и текстовых нод. */
export function xmlEscape(s: string): string {
return String(s)
.replace(/&/g, '&')
.replace(//g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
/** Завернуть в CDATA, нейтрализовав `]]>` внутри. */
export function cdata(s: string): string {
return `/g, ']]]]>')}]]>`;
}
/** Текстовое описание из HTML (для ``). */
export function plainTextExcerpt(html: string, max = 500): string {
const txt = html
.replace(/