You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.4 KiB

10 months ago
import { cloneDeep } from 'lodash-es'
/**
* url地址返回url地址
* @param text
* @returns
*/
export const getImageComponent = (text: string): string[] => {
const pattern = /<img.*?src="(.*?)".*?>/gi
const matches = text.match(pattern)
if (matches) {
const paths: string[] = []
for (let i = 0; i < matches.length; i++) {
const match = matches[i]
const image_path = match.match(/src="(.*?)"/)![1]
paths.push(image_path)
}
return paths
} else {
return []
}
}
/**
*
* @param rich_text
* @returns
*/
export const getPlainText = (rich_text: string): string => {
var pattern = /<[^>]+>/g
var content = rich_text.replace(pattern, '')
return content.trim()
}
/**
* @description
* @param {Array} data
* @param {Object} props `{ parent: 'pid', children: 'children' }`
*/
export const arrayToTree = (data: any[], props = { id: 'id', parentId: 'pid', children: 'children' }) => {
data = cloneDeep(data)
const { id, parentId, children } = props
const result: any[] = []
const map = new Map()
data.forEach((item) => {
map.set(item[id], item)
const parent = map.get(item[parentId])
if (parent) {
parent[children] = parent[children] ?? []
parent[children].push(item)
} else {
result.push(item)
}
})
return result
}