選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 

46 行
1.3 KiB

  1. import sanitizeHtml from 'sanitize-html';
  2. const CARD_HTML_CONFIG: sanitizeHtml.IOptions = {
  3. allowedTags: [
  4. 'p', 'br', 'strong', 'em', 'b', 'i', 'u',
  5. 'ul', 'ol', 'li',
  6. 'a',
  7. 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
  8. 'blockquote',
  9. 'span',
  10. ],
  11. allowedAttributes: {
  12. a: ['href', 'title', 'target', 'rel'],
  13. span: ['class'],
  14. '*': [],
  15. },
  16. allowedSchemes: ['http', 'https', 'mailto', 'tel'],
  17. allowedSchemesAppliedToAttributes: ['href'],
  18. transformTags: {
  19. a: sanitizeHtml.simpleTransform('a', { rel: 'noopener noreferrer', target: '_blank' }),
  20. },
  21. disallowedTagsMode: 'discard',
  22. };
  23. export function sanitizeCardHtml(input: string | null | undefined): string {
  24. if (!input) return '';
  25. return sanitizeHtml(input, CARD_HTML_CONFIG);
  26. }
  27. // Welcome text: solo formattazione inline base + a-capo. Niente link, niente liste.
  28. const WELCOME_TEXT_CONFIG: sanitizeHtml.IOptions = {
  29. allowedTags: ['b', 'i', 'strong', 'em', 'br', 'p', 'div', 'span'],
  30. allowedAttributes: {},
  31. disallowedTagsMode: 'discard',
  32. };
  33. export function sanitizeWelcomeText(input: string | null | undefined): string {
  34. if (!input) return '';
  35. return sanitizeHtml(input, WELCOME_TEXT_CONFIG);
  36. }
  37. const HEX_COLOR_RE = /^#[0-9a-fA-F]{6}$/;
  38. export function isValidHexColor(value: unknown): value is string {
  39. return typeof value === 'string' && HEX_COLOR_RE.test(value);
  40. }