logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

normalizer.js (2063B)


  1. import escapeTextContentForBrowser from 'escape-html';
  2. import emojify from '../../features/emoji/emoji';
  3. const domParser = new DOMParser();
  4. export function normalizeAccount(account) {
  5. account = { ...account };
  6. const displayName = account.display_name.length === 0 ? account.username : account.display_name;
  7. account.display_name_html = emojify(escapeTextContentForBrowser(displayName));
  8. account.note_emojified = emojify(account.note);
  9. if (account.fields) {
  10. account.fields = account.fields.map(pair => ({
  11. ...pair,
  12. name_emojified: emojify(escapeTextContentForBrowser(pair.name)),
  13. value_emojified: emojify(pair.value),
  14. }));
  15. }
  16. if (account.moved) {
  17. account.moved = account.moved.id;
  18. }
  19. return account;
  20. }
  21. export function normalizeStatus(status, normalOldStatus) {
  22. const normalStatus = { ...status };
  23. normalStatus.account = status.account.id;
  24. if (status.reblog && status.reblog.id) {
  25. normalStatus.reblog = status.reblog.id;
  26. }
  27. // Only calculate these values when status first encountered
  28. // Otherwise keep the ones already in the reducer
  29. if (normalOldStatus) {
  30. normalStatus.search_index = normalOldStatus.get('search_index');
  31. normalStatus.contentHtml = normalOldStatus.get('contentHtml');
  32. normalStatus.spoilerHtml = normalOldStatus.get('spoilerHtml');
  33. normalStatus.hidden = normalOldStatus.get('hidden');
  34. } else {
  35. const searchContent = [status.spoiler_text, status.content].join('\n\n').replace(/<br\s*\/?>/g, '\n').replace(/<\/p><p>/g, '\n\n');
  36. const emojiMap = normalStatus.emojis.reduce((obj, emoji) => {
  37. obj[`:${emoji.shortcode}:`] = emoji;
  38. return obj;
  39. }, {});
  40. normalStatus.search_index = domParser.parseFromString(searchContent, 'text/html').documentElement.textContent;
  41. normalStatus.contentHtml = emojify(normalStatus.content, emojiMap);
  42. normalStatus.spoilerHtml = emojify(escapeTextContentForBrowser(normalStatus.spoiler_text || ''), emojiMap);
  43. normalStatus.hidden = normalStatus.sensitive;
  44. }
  45. return normalStatus;
  46. }