renderTextWithStyles.js 794 B

12345678910111213141516171819202122
  1. function renderTextWithStyles(text) {
  2. if (!text) return ''
  3. return String(text)
  4. .replace(/&/g, '&')
  5. .replace(/</g, '&lt;')
  6. .replace(/>/g, '&gt;')
  7. .replace(/@([A-Za-z0-9+/=.\-]+\.ed25519)/g, (_, id) =>
  8. `<a href="/author/${encodeURIComponent('@' + id)}" class="styled-link" target="_blank">@${id}</a>`
  9. )
  10. .replace(/#(\w+)/g, (_, tag) =>
  11. `<a href="/hashtag/${encodeURIComponent(tag)}" class="styled-link" target="_blank">#${tag}</a>`
  12. )
  13. .replace(/(https?:\/\/[^\s]+)/g, url =>
  14. `<a href="${url}" target="_blank" class="styled-link">${url}</a>`
  15. )
  16. .replace(/([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})/g, email =>
  17. `<a href="mailto:${email}" class="styled-link">${email}</a>`
  18. )
  19. }
  20. module.exports = { renderTextWithStyles }