大家好
我正在使用 discourse API 来检索主题/所有帖子,然后是每个帖子及其内容。
我的论坛托管在 forums.xxx.com 上。
我使用 API 检索帖子并将它们放在我的 www.xxx.com 域上,并在两者之间来回链接。
这一切都有效,除了我拉取的内容使用相对 URL - 所以链接到一个用户(在论坛域上时)将是 forums.xxx.com/u/the-user。
当我将该帖子拉取到我的 www.x 域时,它链接到 www.xxx.com/u/the-user - 这显然不存在。
同样的问题也延伸到帖子中包含的任何表情符号或图像 - 链接已损坏。
有没有办法让传入的链接(我使用 content: data['cooked'])链接回原始域?
我实际上问了 ChatGPT 同样的问题,并立即得到了一个可行的答案
使用一个辅助方法来替换已处理数据中的 URL
def update_urls(content)
forum_domain = 'https://forums.xxx.com'
# 更新 href 属性
content.gsub!(/href=\"(\\/[^\"]*)\"/i) { |match| "href=\\\"#{forum_domain}#{$1}\\\"\" }
# 更新 src 属性
content.gsub!(/src=\"(\\/[^\"]*)\"/i) { |match| "src=\\\"#{forum_domain}#{$1}\\\"\" }
content
end
所以我会把它留在这里给其他人——如果有人有更优雅的现成解决方案,我很感兴趣。
2 个赞
system
(system)
关闭
3
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.