长文图片缩放:编辑器拖拽把手的比例在文章页生效(alt 数字还原为宽度并居中)+ 把手默认半透明提高可发现性
This commit is contained in:
1 parent
a74299d716
commit
b93fe9847c
4 files changed
+25
-5
No files matched your search
@@ -28,6 +28,22 @@ export function thumbURL(u, w = 480) {
|
||||
return '/uploads/thumb/' + key + '?w=' + w
|
||||
}
|
||||
|
||||
// 编辑器拖拽缩放的比例展示:Milkdown 图片块把高度比例写进 markdown 的 alt
|
||||
// (),渲染出的 <img alt="0.6"> 在这里还原成对应的宽度并居中。
|
||||
// 纯数字 alt 才生效(>=1 的当 100% 处理),正常图片的描述性 alt 不受影响。
|
||||
export function applyImageRatio(html) {
|
||||
if (!html) return html
|
||||
return html.replace(/<img\b[^>]*>/gi, (tag) => {
|
||||
const m = tag.match(/\balt="(\d*\.?\d+)"/i)
|
||||
if (!m) return tag
|
||||
const ratio = Number.parseFloat(m[1])
|
||||
if (!Number.isFinite(ratio) || ratio <= 0 || ratio >= 1) return tag
|
||||
const style = `display:block;width:${(ratio * 100).toFixed(1)}%;max-width:100%;height:auto;margin-inline:auto;`
|
||||
if (/\bstyle="/i.test(tag)) return tag.replace(/\bstyle="/i, 'style="' + style)
|
||||
return tag.replace(/^<img/i, '<img style="' + style + '"')
|
||||
})
|
||||
}
|
||||
|
||||
// 时间线正文 HTML(短文全文 / 长文 600 字符截断)里的 <img> 换缩略图 + 补懒加载。
|
||||
// 只在展示层重写,库里存的正文不动。截断的 HTML 可能带半截标签,
|
||||
// 正则只匹配完整的 <img ...src="..."> ,坏尾巴交给 sanitize/浏览器容错。
|
||||
|
||||
Reference in new issue
Block a user