{“content”:“\u003cdiv data-theme-toc="true"\u003e \u003c/div\u003e\n\nDiscourse 具有使用 Javascript API 创建 IFRAME 的功能,以便将主题的评论嵌入到远程站点中。有关此功能实际应用的示例,请查看 Coding Horror 的博客。该博客通过 Ghost 运行,但评论是从他的 Discourse 论坛 嵌入的。\n\n使用此设置时,需要注意的一点是用户必须导航到您的论坛才能发表回复。这是故意的,因为我们认为 Discourse 论坛上的发帖界面目前比我们能通过 Javascript 嵌入的界面要丰富得多。\n\n本指南将向您展示如何在自己的博客或网站上设置评论嵌入。\n\n### 工作原理\n\n在 Discourse 中,一个主题由许多帖子组成。当您在另一个站点上嵌入 Discourse 时,您是将一个文档(博客文章、HTML 页面等)与单个主题关联起来。当人们在该主题中发帖时,他们的评论将自动显示在您嵌入它的页面上。\n\nYou have the choice to have Discourse create the topics automatically when a new embedding is found, or you can create the topics yourself in advance. \n\n### 配置 Discourse 以进行嵌入(简单设置)\n\n以下设置将在 Discourse 论坛(运行于 =DISCOURSE=)上嵌入评论源,在虚构博客 URL http://example.com/blog/entry-123.html 上的页面中显示。\n\n论坛的域名\n博客的域名\n\n1. 访问 Discourse 安装中的 管理 > 自定义 > 嵌入。 https://=DISCOURSE=/admin/customize/embedding\n\n2. 创建至少一个可嵌入主机。这应该是您希望嵌入评论的主机名(域名)。在本例中,主机是 =BLOG= —— 请注意没有 http:// 和路径。\n\n - 路径白名单 允许您指定远程主机上接受嵌入的路径。\n\n - 发布到分类 - 如果您在输入主机时提供了分类,则从该主机导入的帖子将自动进入该分类。不同的主机可以发布到不同的分类。\n\n - 标签 - 您可以为从给定主机创建的主题自动应用标签。\n\n - 帖子作者 - 您可以选择按主机覆盖主题创建用户。如果未设置,则使用“帖子和主题”选项卡中的默认值。\n \n3. 导航到帖子和主题选项卡,填写主题创建的用户名字段。这是在新嵌入被发现时创建主题的用户。让我们假设我们的 discourse 有一个名为 eviltrout 的用户,所以其值为 eviltrout。\n\n4. 将以下 HTML 插入到 http://=BLOG=/blog/entry-123.html 处的网页上\n\nhtml\n\u003cdiv id='discourse-comments'\u003e\u003c/div\u003e\n\n\u003cscript type=\"text/javascript\"\u003e\n DiscourseEmbed = {\n discourseUrl: 'https://discourse.example.com/',\n discourseEmbedUrl: 'http://example.com/blog/entry-123.html',\n // className: 'CLASS_NAME',\n };\n\n (function() {\n var d = document.createElement('script'); d.type = 'text/javascript'; d.async = true;\n d.src = DiscourseEmbed.discourseUrl + 'javascripts/embed.js';\n (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(d);\n })();\n\u003c/script\u003e\n\n\n代码片段中可配置的部分位于 DiscourseEmbed 对象中。discourseUrl 是到您的 Discourse 基础的完整路径,包含尾部斜杠。discourseEmbedUrl 是当前嵌入评论源的文档。\n\n如果正确设置了此项,当您第一次访问 http://=BLOG=/blog/entry-123.html 时,它将尝试加载博客文章的评论。由于没有评论,它会指示 Discourse 论坛在后台创建一个新主题。eviltrout 将在后台创建一个新主题,并且将抓取您的博客内容作为第一篇帖子,并自动提取文本。\n\n新主题创建后,用户可以对其进行评论,下次访问 http://=BLOG=/blog/entry-123.html 时,他们的评论将自动显示。\n\nclassName 是可选的,它将向嵌入项添加一个您选择的类,以便您可以使用 CSS 进行自定义。\n\n\u003e
提示: 如果您的博客有多个作者,您可以在每页添加一个 \u003cmeta name=\"discourse-username\" content=\"author_username\"\u003e 标签。当 Discourse 抓取页面以创建主题时,它将使用此 meta 标签来确定帖子作者,覆盖管理设置中设置的默认用户名。\n\n### 在多个页面上嵌入\n\n在上面的示例中,我们在 Javascript 代码片段中硬编码了我们的 http://=BLOG=/blog/entry-123.html URL。这通常是不够的,因为许多网站都有许多自动生成的页面。例如,在博客中,每篇文章通常都有自己的页面。为了支持这一点,请在希望显示评论的每个页面上放置相同的代码片段,但用当前页面的 URL 替换传递给 discourseEmbedUrl 的值。在我的博客上,我为 discourseEmbedUrl 使用的值是:'http://eviltrout.com\u003c%= current_page.url %\u003e' —— 随着新博客页面的创建,Discourse 上也会自动为它们创建新主题。\n\n### 样式化您的嵌入内容\n\nYou have the ability to add a stylesheet for your embedded comments. Use the Embedded CSS section of the Theme editor at Admin \u003e Customize \u003e Themes \u003e [your theme] \u003e Edit CSS/HTML and you can add a custom stylesheet that will be served up with your embedded comments. By default we think the layout looks nice on a white background, but if your site has a unique layout you’ll want to style it yourself.\n\n###(可选)添加轮询源\n\n如上所述,Discourse 将自动抓取嵌入它的任何站点。但是,HTML 有时可能难以解析,并且可能无法正确提取您的帖子内容。许多博客和网站支持 RSS/Atom 源进行联合,Discourse 可以使用它来更准确地提取您的博客文章内容。\n\nDiscourse 附带 RSS 轮询插件(默认包含)。如果您在嵌入 Discourse 的站点上设置了 RSS 或 Atom 源,您可以启用 rss_polling_enabled 站点设置,并通过管理 > 插件 > RSS 轮询添加您的源 URL。添加源 URL 后,Discourse 将解析该源,并根据您添加到“嵌入设置”中的“允许的主机”将帖子发布到相应的分类中。\n\n###(替代配置)链接到现有主题\n\n有些人更喜欢不让 Discourse 在他们的论坛上自动创建主题。他们希望自己创建主题,然后只需告诉他们的嵌入代码他们想要关联哪个主题。您可以通过稍微更改嵌入代码来实现这一点:\n\nhtml\n\u003cdiv id='discourse-comments'\u003e\u003c/div\u003e\n\n\u003cscript type=\"text/javascript\"\u003e\n window.DiscourseEmbed = {\n discourseUrl: 'https://=DISCOURSE=/',\n topicId: 12345\n };\n\n (function() {\n var d = document.createElement('script'); d.type = 'text/javascript'; d.async = true;\n d.src = window.DiscourseEmbed.discourseUrl + 'javascripts/embed.js';\n (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(d);\n })();\n\u003c/script\u003e\n\n\n唯一的区别是我们用 Discourse 中某个主题的 ID 替换了 discourseEmbedUrl。如果您这样做,将不会创建任何主题,并且该主题的评论将自动显示。\n\n### 设置引用策略\n\n由于最近(2020 年 9 月)浏览器默认引用策略的更改,Discourse 现在明确默认将 iframe 的引用策略设置为 \"no-referrer-when-downgrade\"。如果出于安全原因您的网站需要更严格的引用策略,可以通过在嵌入脚本的 DiscourseEmbed 对象中添加 discourseReferrerPolicy 值来设置。例如:\n\njs\nDiscourseEmbed = { discourseUrl: 'https://forum.example.com/',\n discourseEmbedUrl: '\u003cyour_posts_canonical_URL\u003e',\n discourseReferrerPolicy: 'strict-origin-when-cross-origin'};\n\n\n### 以编程方式查询嵌入详情\n\n我们有一个 API 端点,可以使用 embed_url 作为参数查询嵌入详情:\n\n\ncurl 'https://meta.discourse.org/embed/info?embed_url=https://blog.discourse.org/2021/04/discourse-team-grows-to-50' -H 'API-KEY: logapikeygoeshere' -H 'API-USERNAME: apiusernamehere' \n\n\n响应如下:\n\njson\n{\n \"topic_id\": 187794,\n \"post_id\": 925017,\n \"topic_slug\": \"discourse-team-grows-to-50-blog\",\n \"comment_count\": 2\n}\n\n\n### 嵌入私人站点评论\n\n对于私有 Discourse 实例,如果 Discourse 位于博客域名的子域名下,那么已登录 Discourse 的用户将看到评论。未登录 Discourse 的用户将看到“拒绝连接”消息。如果 Discourse 和博客位于完全不同的域下,则私人论坛不会显示任何评论。\n\n### 故障排除\n\n用户在嵌入 Discourse 时遇到的最常见问题是为添加的可嵌入主机设置了不正确的值。请务必仔细检查它是否仅是您站点的域名,并且不包含任何多余的斜杠或无效字符。”}
116 个赞