简体   繁体   English

通过 HTML 插入非 Hugo 渲染的博客文章中的图像

[英]Images inserted via HTML into blog posts not rendered by Hugo

I'm using HTML snippets such as我正在使用 HTML 片段,例如

<img src="site.jpg" style="width:100px; float:left; margin: 0px 5px 5px 0px" border="0" />

to insert scaled and offset images into blog posts in Hugo.在 Hugo 的博客文章中插入缩放和偏移图像。 Unfortunately I'm finding with the latest version of Hugo that images inserted this way are not rendering.不幸的是,我发现最新版本的 Hugo 无法渲染以这种方式插入的图像。 If I do an image insert via Markdown, the images are inserted fine, but it appears impossible apply a style tag?如果我通过 Markdown 插入图像,图像插入得很好,但似乎不可能应用样式标签?

Any suggestions on how I can diagnose the issue here?关于如何在此处诊断问题的任何建议? I'm currently at a loss.我目前不知所措。

It turns out that inline HTML was getting filtered out by Hugo.事实证明,内联 HTML 被 Hugo 过滤掉了。 The following needed to be added to config.toml to allow the HTML to be inserted into the resulting pages.需要将以下内容添加到config.toml以允许将 HTML 插入到结果页面中。

[markup]
  [markup.goldmark]
    [markup.goldmark.renderer]
      unsafe = true

This came to pass in Hugo 0.60.0 with CommonMark compliance .这在符合 CommonMark 的Hugo 0.60.0 中得以实现

不确定这是否有帮助,但我稍微整理了您的语法。

<img src="site.jpg" style="width:100px; float:left; margin: 0px 5px 5px 0px; border:0px;" />

I would prefer to keep the HTML out of the MD as much as possible;我希望尽可能将 HTML 保留在 MD 之外; setting anything to "unsafe" always makes me a little nervous, and I presume that was the intention when the the parameter was named "unsafe"!将任何东西设置为“不安全”总是让我有点紧张,我认为这就是参数命名为“不安全”时的意图!

I think my first port of call would be to use the figure shortcode , that supports a class name, as well as nice-to-haves like captions and titles (which may have accessiblity and SEO benefits).我认为我的第一个调用端口是使用图形短代码,它支持类名,以及像标题和标题这样的好东西(可能具有可访问性和 SEO 优势)。

{{< figure src="site.jpg" title="Cool Image" class="pull-left" >}}

If that doesn't suit, the options would be a custom shortcode, or target through pure css, you could use nth-child to alternate images to the left and right if desired.如果这不适合,选项将是自定义短代码,或通过纯 css 定位,如果需要,您可以使用nth-child 左右交替图像。

Edit - Additional info编辑 - 附加信息

Create a CSS file and link it in the head, for example I have <project-root>static/styles/main.css and this in the head of my layout创建一个 CSS 文件并将其链接到头部,例如我有<project-root>static/styles/main.css并且这个在我的布局的头部

<link href="/styles/main.css" rel="stylesheet" type="text/css">

Very important to note that static is not part of the url/href.非常重要的是要注意static不是 url/href 的一部分。

Above I assigned the figure the class of pull-left so in main.css your style would look like上面我给这个图指定了pull-left的类,所以在 main.css 中你的样式看起来像

.pull-left{
    width:100px; 
    float:left; 
    margin: 0px 5px 5px 0px;
}

Added details from Link in Comments评论中的链接添加了详细信息

Best to make a copy of theme files rather than overwriting so create最好复制主题文件而不是覆盖所以创建

layouts -> partials -> head.html

normally copied from通常复制自

themes -> actual-theme -> layouts -> partials -> head.html

and add in the normal HTML link tag.并添加普通的 HTML 链接标签。 (Fun bonus fact, themes are optional if you want to create from scratch) (有趣的奖励事实,如果您想从头开始创建主题是可选的)

You can also specify files in the config file您也可以在配置文件中指定文件

custom_css = ["css/custom.css"]
custom_js = ["js/custom.js"]

And use this code to add to the head.html并使用此代码添加到 head.html

// css 
{{ range .Site.Params.custom_css -}}
    <link rel="stylesheet" href="{{ . | absURL }}">
{{- end }}

// javascript
{{ range .Site.Params.custom_js -}}
    <link rel="stylesheet" href="{{ . | absURL }}">
{{- end }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM