21 lines
837 B
JavaScript
21 lines
837 B
JavaScript
import { addFilePrefix } from './common'
|
|
|
|
export function normalizeRichTextImages(content = '', imageStyle = '') {
|
|
const normalizedStyle = String(imageStyle || '').trim()
|
|
|
|
return String(content || '').replace(/<img\b([^>]*)>/gi, (match, rawAttrs = '') => {
|
|
const attrs = String(rawAttrs || '')
|
|
.replace(/\swidth\s*=\s*(['"]).*?\1/gi, '')
|
|
.replace(/\sheight\s*=\s*(['"]).*?\1/gi, '')
|
|
.replace(/\sstyle\s*=\s*(['"]).*?\1/gi, '')
|
|
const normalizedAttrs = attrs.replace(/\ssrc\s*=\s*(['"])(.*?)\1/gi, (srcMatch, quote, srcValue = '') => {
|
|
const normalizedSrc = addFilePrefix(srcValue)
|
|
return normalizedSrc ? ` src=${quote}${normalizedSrc}${quote}` : srcMatch
|
|
})
|
|
|
|
return normalizedStyle
|
|
? `<img${normalizedAttrs} style="${normalizedStyle}" />`
|
|
: `<img${normalizedAttrs} />`
|
|
})
|
|
}
|