utils.ts 688 B

12345678910111213141516171819
  1. // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. export const populateEditorNewLines = (htmlContent: string): string => {
  3. const body = document.createElement('div')
  4. body.innerHTML = htmlContent
  5. // prosemirror always adds a visible linebreak inside an empty paragraph,
  6. // but it doesn't return it inside a schema, so we need to add it manually
  7. body.querySelectorAll('p').forEach((p) => {
  8. p.removeAttribute('data-marker')
  9. if (
  10. p.childNodes.length === 0 ||
  11. p.lastChild?.nodeType !== Node.TEXT_NODE ||
  12. p.textContent?.endsWith('\n')
  13. ) {
  14. p.appendChild(document.createElement('br'))
  15. }
  16. })
  17. return body.innerHTML
  18. }