Rake 任务 posts:remap - 0 篇帖子已重新映射!

我正在尝试查找并替换论坛迁移到 Discourse 后遗留的一些损坏图片,并且取得了很大成功(非常喜欢 remap 命令!),但这些示例却找不到任何可替换的内容,我看不出有什么错误,您能帮忙看看吗?

示例内容:

<img src="https://londonbikers.com/images/transparent.png" alt="PICT0738.JPG" data-orig-src="upload://1yz82u6bbkaAQ7xP06qNTmX9qy3.JPG" width="666" height="500" class="d-lazyload">

以及

<img src="https://londonbikers.com/images/transparent.png" alt="Rhonda.JPG" data-orig-src="upload://vXQ67D17QssTe5cu6lamJ5XB5Ep.JPG" width="590" height="500" class="d-lazyload">

我需要分两步完成此操作,因为我需要保留 alt 标签的内容,并围绕其重建 img 元素。第一步 remap 失败了:

即 查找:
<img src="https://londonbikers.com/images/transparent.png" alt="

并 替换为:
<img src="https://londonbikersarchive.blob.core.windows.net/forum-attachments/

我假设双引号需要用反斜杠转义?所以我使用了以下命令:

rake posts:remap["<img src=\"https://londonbikers.com/images/transparent.png\" alt=\"","<img src=\"https://londonbikersarchive.blob.core.windows.net/forum-attachments/"]

但系统提示 0 篇帖子已重新映射!,而实际上有数千篇包含这些元素的帖子。有什么建议吗?

我的第二步将是把标签的末尾从:
width="666" height="500" class="d-lazyload">

重新映射为:
/>

哦,等等,有些图片能正常显示了。这是 Discourse 的附件功能吗?是否存在某种自定义渲染逻辑,而不是将 HTML 字符串直接存储在数据库中?

例如,这张图片显示了照片:

<img src="https://londonbikers.com/uploads/default/original/1X/25b381a9dc03f023a1c5d0af4f2f3132dfbf45c7.jpg" alt="The_rides_0010.jpg" width="640" height="480" class="d-lazyload">

但另一张却没有显示。我可以将文件放在云 Blob 存储中,所以要么将其导入 Discourse,要么直接从 Blob 存储引用:

<img src="https://londonbikers.com/images/transparent.png" alt="__2.jpg" data-orig-src="upload://411oUp2Yn6wumJCRAiFptkwTF58.jpg" width="662" height="500" class="d-lazyload">

那些图片是 raw 格式的吗?

我认为与其用 remap 处理,不如在 Rails 控制台中使用 Ruby 更合适。

抱歉,Jay,我完全没听懂你刚才说的 :slight_smile:

是原始的文件格式吗?不,只是普通的网页格式。在 Rails 控制台中做什么呢?

raw 字段包含的是你在编辑器中看到的文本。你正在替换的内容,应该是在用户界面中编辑帖子时所看到的内容,对吗?(cooked 是经过处理后的内容,也就是实际显示的内容。)

执行一些操作来重写帖子。这可能足以让你“危险”起来:

bps = Post.where("raw like '%//SOME-STRING%'")
bps.each do |post|
  post.raw.gsub!(/OLD/,'NEW')
  post.save
end

与 rake 任务相比,它的优势在于你无需应对多层转义问题。