如何显示另一个HTML文档来代替标准博客文章?

问题描述 投票:2回答:2

我有一个博客,我使用R Blogdown更新。它有一个具有YAML配置和前端问题的Hugo主题。我在Netlify上主持。我想创建一个帖子,在点击帖子的链接后,用户会看到一个完全独立的html文件而不是标题帖子。例如,我认为以下前面的内容可以在我将所需文档放在'static / files'中的地方...

---
title: 'Example blog post'
author: Logit
date: '2018-02-21'
URL: ["/files/page_to_display_instead.html"]
---

但是我想要的页面没有加载。相反,我的地址栏试图加载'/ posts / 2018-02-21-example-blog-post'

我注意到在我的帖子正文中包含以下内容正是我想要的,并验证我的相对路径,文件名和所需页面是否正确...

Click [here](/files/page_to_display_instead.html) to see the right page.

但这需要用户额外点击才能获得内容并且不是很优雅。

同样,将以下内容放在上述帖子的正文中也接近工作......

![](/files/page_to_display_instead.html)

但是这个解决方案保留了博客标题和主题,只是在框架内显示我想要的页面。它看起来有点难看。

必须有一个简单的解决方案来加载或重定向到所需的页面而不是标题的帖子本身。我在前面的事情中误解了Hugo的“URL”变量的用法吗?我应该使用另一个前端变量或语法吗?在此先感谢您的任何建议。

编辑:除了Sebastien Rochette下面的优秀答案之外,我发现由于我在R Markdown工作,以下解决了这个问题:

```{r, include=FALSE}
shiny::includeHTML("/files/page_to_display_instead.html") 
```
r hugo blogdown
2个回答
2
投票

我认为你可以直接显示你的html文件,如果你把它直接放在博客文件夹中。 html文件的名称将用作slug。但是,如果您的html页面不包含您的模板,您可能不希望这样。

使用iframe

因此,您可以将您的html文件添加为iframe:

---
title: 'Example blog post'
author: Logit
date: '2018-02-21'
---

<iframe width="100%" height="150" name="iframe" src="/files/page_to_display_instead.html"></iframe>

自动调整iframe大小以适合内容

如果您不希望访问者将其视为iframe,则可以使用一些javascript来自动调整iframe的大小。 您需要在主题的“头部”中添加:

<script>
  function resizeIframe(obj) {
    obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
  }
</script>

然后你的帖子将是:

---
title: 'Example blog post'
author: Logit
date: '2018-02-21'
---

<iframe width="100%" height="150" name="iframe" src="/files/page_to_display_instead.html" frameborder="0" scrolling="no" onload="resizeIframe(this)"></iframe>

0
投票

有关iframe的有趣建议......在将youtube内容嵌入iframe时,它对我无效:

https://blogs.nopcode.org/brainstorm/2016-06-10-the-right-to-repair-my-drone/

它会在iframe代码中停止渲染,在这里您可以看到在迁移到blogdown之前用于处理我以前的Jekyll安装的源Markdown:

https://github.com/brainstorm/brainblog/blob/master/content/brainstorm/2016-06-10-the-right-to-repair-my-drone.md

关于如何解决它的任何提示(希望不会改变我的所有博客文章)都是非常受欢迎的!

编辑:嵌入youtube视频的标准博客方式在浏览器控制台中产生令人担忧的消息:

Youtube BlogDown errors

© www.soinside.com 2019 - 2024. All rights reserved.