chore: initial import
This commit is contained in:
20
utils/rich-text.js
Normal file
20
utils/rich-text.js
Normal file
@@ -0,0 +1,20 @@
|
||||
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} />`
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user