{“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 时遇到的最常见问题是为添加的可嵌入主机设置了不正确的值。请务必仔细检查它是否仅是您站点的域名,并且不包含任何多余的斜杠或无效字符。”}
Is it possible to use this method if the Discourse category is hidden from public view?
Probably not, but it might work if all of your viewers are logged in.
I’ve created a tutorial that walks you through the process of setting up a local discourse instance on vagrant for your local tests to embed discourse in your front-end development project. You can find it here: Local discourse: vagrant, ansible, lxd, docker, discourse-embedding
This is different from other descriptions that I found before in that it allows you to run locally a complete discourse set-up like you would do in production to get a feel for the “real” set-up process. In addition this set-up is able to serve as a local development playground for embedding discourse into your project.
Hi, is there a way to disable embedding for posts in draft status? I can’t see any handlebar within Ghost for post status, can I change the embed settings to exclude /p/ which is used to preview posts? I have a problem where posts that are previewed are being added to Discourse. Cheers.
Can’t seem to find the setting to style the embedding iframe any more…? Is the original post out of date somehow?
EDIT: Nevermind, it had just changed places. I can add it to a theme component
thanks!
This sounds like a ghost question, not a Discourse question? However your Ghost integration is working needs to take that into account, not Discourse.
Hello everyone!
I need some help to embed Discourse into READTHEDOCS (using Sphinx)
Does anyone have this kind of experience?
Looks like there is GitHub - pdavide/sphinxcontrib-discourse which always embed a single topic.
Thank you for reply, let check it out
Hey Guys
I’ve been having some trouble embedding to a specific topic:
I’ve been trying different variations of settings and I still just get ‘Error Embedding’ On My Website.
Is it noticeable at all what I may be doing wrong? I’ve spent forever trying to get it to work, any help is appreciated.
Current Code Embedded On Website
Are there any errors in your Javascript console?
In your first screenshot, you have all the paths set to publish to the uncategorized category. If you want to publish all posts to the same category you only need to create one Embeddable host record.
The Path Whitelist values that you have added to your Host entries might be causing problems. You can leave that value blank to publish all posts from your blog site that you’ve added the Discourse embed script to. If you want to set a specific path to publish to a Discourse category, the path should end with /.*. For example /sites/.*
The double slash at the start of your /t/newsletter-discussions/105 path may be a problem. Also, that looks like the path to a Discourse topic. If set, the path should be the path to a grouping of posts on your blog. The purpose of the Path Whitelist setting is to allow you to publish posts from a specific path on your blog to a specific Discourse category.
Ok thank you Simon, I appreciate it. I will give this a shot.
Like @codinghorror, I also use this DIscourse integration with Ghost using the stock configuration which uses the absolute URL as the discourseEmbedURL.
A week or so ago, I decided to change my Ghost blog’s routing so that posts with URLs like https://engineerworkshop.com/2020/02/20/how-to-set-up-wireguard-on-a-raspberry-pi/ are redirected to https://engineerworkshop.com/blog/how-to-set-up-wireguard-on-a-raspberry-pi/, with https://engineerworkshop.com/blog/how-to-set-up-wireguard-on-a-raspberry-pi/ being the new permalink.
The Problem
You can probably anticipate the problem this creates: the discourseEmbedURL is set to the old URL and so when the embed script is called, which now has a different URL, a new comment post is created in Discourse.
Solution Attempt (Which Failed)
Now, prior to this, I had anticipated this problem, and so I thought I was being clever using a regex pattern with remap to fix the URLs: rake posts:remap["[0-9]{4}\/[0-9]{2}\/[0-9]{2}","blog",regex]
While that did “sort of” work, wherever the URL was used in the original Discourse comment post, sadly, when I loaded the blog posts, new comment topics were created in Discourse.
Ultimately, I just reloaded every post on my site and transferred comments from the old topics to my new topics, deleting the originals (I know, I know). Obviously killing pages is bad for SEO, not to mention end-user experience.
My Question
I assume the reason my regex patch attempt didn’t work is because the Discourse topic is somehow keyed to the URL on the backend. (I readily admit to being ignorant of Discourse’s data structure). For future knowledge, and anyone else that may attempt to do the same in the future, is there a way to remap a DIscourse topic to a new blog post URL?
Thanks in advance!
7 posts were merged into an existing topic: WP Discourse Embed Plugin
I love this feature as I have many magazine-style content sites and I can auto generate the topics into our forums but is there a way to get the URL preview working like it does normally in Discourse?
So instead of this:
More like this:
Thanks,
Jim
If you have the link on a separate line by itself it should Onebox.
Then have your entire description line below, including the link, as you show in your first image. That will appear below the Oneboxed link.
Hmm… well that’s a bit of a problem as the page itself is what is being previewed. So putting a link at the very top of the page HTML or article text is kinda problematic. Here is the page in question:
https://modelshipwrights.com/news/us-navy-light-cruiser-cl-89-miami
Hi folks. I’ve almost got this working, but realised that the use case I had in mind looks like it may not be as straightforward as I had hoped, and I am not sure how much additional work is involved.
I’m hoping to use this feature to power discussions on a site that is based around user generated content. I have no issues with the embedding of comments, or getting SSO set up and ensuring my users automatically become members of my community with the same emails, usernames, etc.
What I was hoping is that, when a new piece of user content is created on my site, that the user who published it becomes the author of the related Discourse post, not a single user as defined in the “Username for topic creation” setting of the embed feature. I won’t be allowing signups outside of my current signup process so I can be 100% sure that the Discourse user will already exist or, at least, can be created at that time we create the new thread.
Hopefully it’s obvious why I’d want to do this. ie. the content creator would then be the discussion’s OP on my Discourse site and could be automatically subscribed to the thread and notified of replies, their posts highlighted so as to stand out from other comments, etc, etc.
Any suggestions on where to start?
Thanks!



