helper.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { cloneDeep } from 'lodash-es'
  2. /**
  3. * 截取富文本中的url地址返回url地址
  4. * @param text 富文本内容
  5. * @returns
  6. */
  7. export const getImageComponent = (text: string): string[] => {
  8. const pattern = /<img.*?src="(.*?)".*?>/gi
  9. const matches = text.match(pattern)
  10. if (matches) {
  11. const paths: string[] = []
  12. for (let i = 0; i < matches.length; i++) {
  13. const match = matches[i]
  14. const image_path = match.match(/src="(.*?)"/)![1]
  15. paths.push(image_path)
  16. }
  17. return paths
  18. } else {
  19. return []
  20. }
  21. }
  22. /**
  23. * 截取字符串的内容
  24. * @param rich_text
  25. * @returns
  26. */
  27. export const getPlainText = (rich_text: string): string => {
  28. var pattern = /<[^>]+>/g
  29. var content = rich_text.replace(pattern, '')
  30. return content.trim()
  31. }
  32. /**
  33. * @description 数组转树形
  34. * @param {Array} data 数据
  35. * @param {Object} props `{ parent: 'pid', children: 'children' }`
  36. */
  37. export const arrayToTree = (data: any[], props = { id: 'id', parentId: 'pid', children: 'children' }) => {
  38. data = cloneDeep(data)
  39. const { id, parentId, children } = props
  40. const result: any[] = []
  41. const map = new Map()
  42. data.forEach((item) => {
  43. map.set(item[id], item)
  44. const parent = map.get(item[parentId])
  45. if (parent) {
  46. parent[children] = parent[children] ?? []
  47. parent[children].push(item)
  48. } else {
  49. result.push(item)
  50. }
  51. })
  52. return result
  53. }