使用Markdown Jekyll中的图像标题

ors*_*iro 118 markdown github jekyll

我在Github上主持一个Jekyll博客并用Markdown写我的帖子.当我添加图像时,我按以下方式执行:

![name of the image](http://link.com/image.jpg)

然后,这将显示文本中的图像.

但是,如何告诉Markdown添加图片下方或上方显示的标题?

And*_*Wei 249

我知道这是一个老问题,但我认为我仍然会分享我添加图片标题的方法.您将无法使用captionfigcaption标记,但这将是一个简单的替代方案,而不使用任何插件.

在降价时,您可以使用强调标记包装标题并将其直接放在图像下方,而不插入新行,如下所示:

![](path_to_image)
*image_caption*
Run Code Online (Sandbox Code Playgroud)

这将生成以下HTML:

<p>
    <img src="path_to_image" alt>
    <em>image_caption</em>
</p>
Run Code Online (Sandbox Code Playgroud)

然后在CSS中,您可以使用以下选择器设置样式,而不会干扰em页面上的其他标记:

img + em { }
Run Code Online (Sandbox Code Playgroud)

请注意,图像和标题之间不能有空行,因为这样会生成:

<p>
    <img src="path_to_image" alt>
</p>
<p>
    <em>image_caption</em>
</p>
Run Code Online (Sandbox Code Playgroud)

你也可以使用你想要的任何标签em.只要确保有标签,否则你将无法设置标签.

  • 惊人的!无需记住一些愚蠢的 jekyll 语法 :) (4认同)
  • 我是这个的忠实粉丝 (2认同)
  • @ChriiSchee 您可以将其放置在主 CSS 文件中,也可以创建自己的 CSS 文件并将其链接到默认布局。例如,我的默认布局链接到 main.css 文件 `&lt;link href="{{ site.baseurl }}/assets/css/main.css" rel="stylesheet"&gt;` 因此我只需在以下位置添加自定义 CSS 定义该文件的底部: `// 我的自定义 css img + em { display: block; 文本对齐:居中;} //图像标题` (2认同)

IQA*_*eas 93

如果你不希望使用任何插件(这意味着你可以把它推到GitHub上的情况下直接产生第一现场),你可以创建一个名为的新文件image.html_includes:

<figure class="image">
  <img src="{{ include.url }}" alt="{{ include.description }}">
  <figcaption>{{ include.description }}</figcaption>
</figure>
Run Code Online (Sandbox Code Playgroud)

然后使用以下标记显示降价图像:

{% include image.html url="/images/my-cat.jpg" description="My cat, Robert Downey Jr." %}
Run Code Online (Sandbox Code Playgroud)

  • 更好的标记是:`<figure class ="image"> <img rel="nofollow noreferrer" src ="{{include.url}}"alt ="{{include.description}}"> <figcaption> {{include.description}} </ figcaption> </图>` (20认同)
  • Jekyll现在包含一个`site.url`变量. (3认同)
  • 啊,对,这是[Octopress](http://octopress.org/)中添加的变量.我编辑了它,所以示例代码只使用图像的相对URL. (2认同)
  • 有没有办法将标题格式设置为 Markdown 而不仅仅是文本?我想使用 Markdown 语法进行超链接,但它不起作用。也尝试过克拉姆当 (2认同)

Bil*_*kin 50

您可以使用表格.它工作正常.

| ![space-1.jpg](http://www.storywarren.com/wp-content/uploads/2016/09/space-1.jpg) | 
|:--:| 
| *Space* |
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述

  • 这个答案是最好的..使用纯降价并得到你需要的东西。谢谢! (21认同)
  • 有点离题,但这也适用于 Jupyter Notebooks。 (2认同)
  • 如果您想让表格居中,请使用 &lt;center&gt;,后跟新行 table,后跟新行 &lt;/center&gt;。 (2认同)

bry*_*aun 41

正确的HTML与字幕的图像使用,是<figure><figcaption>.

没有Markdown相当于此,所以如果你只是添加偶尔的标题,我建议你只需将这个html添加到你的Markdown文档中:

Lorem ipsum dolor sit amet, consectetur adipiscing elit...

<figure>
  <img src="{{site.url}}/assets/image.jpg" alt="my alt text"/>
  <figcaption>This is my caption text.</figcaption>
</figure>

Vestibulum eu vulputate magna...
Run Code Online (Sandbox Code Playgroud)

Markdown规范鼓励您在这种情况下嵌入HTML,因此它会显示得很好.它比搞乱插件要简单得多.

如果您正在尝试使用其他Markdown-y功能(如表格,星号等)来生成字幕,那么您只是在讨论如何使用Markdown.

  • 这个答案没有得到任何关注是太糟糕了 - 我真的认为这是最简单和最语义正确的.我特别为使用表格格式化的所有答案感到苦恼,这些表格只是20世纪90年代的混乱.;-) (3认同)

Cor*_*ory 10

我发现对最高投票答案的一个轻微重复,我发现它更明确一点是使用 jekyll 语法将类添加到某物,然后以这种方式对其进行样式化。

所以在帖子中你会有:

![My image](/images/my-image.png)

{:.image-caption}
*The caption for my image*
Run Code Online (Sandbox Code Playgroud)

然后在您的 CSS 文件中,您可以执行以下操作:

.image-caption {
  text-align: center;
  font-size: .8rem;
  color: light-grey;
Run Code Online (Sandbox Code Playgroud)

出来好看!


Rob*_*ral 7

这个问题有两个语义正确的解决方案:

  1. 使用插件
  2. 创建自定义包含

1. 使用插件

我已经尝试了几个插件来做到这一点,我最喜欢的是jekyll-figure.

1.1. 安装jekyll-figure

安装的一种方法jekyll-figure是添加gem "jekyll-figure"到您的插件组中的 Gemfile。

然后bundle install从终端窗口运行。

1.2. 用jekyll-figure

只需将您的降价包装在{% figure %}{% endfigure %}标签中。

您的标题位于开始{% figure %}标签中,您甚至可以使用 Markdown 对其进行样式设置!

例子:

{% figure caption:"Le logo de **Jekyll** et son clin d'oeil à Robert Louis Stevenson" %}
    ![Le logo de Jekyll](/assets/images/2018-08-07-jekyll-logo.png)
{% endfigure %}
Run Code Online (Sandbox Code Playgroud)

1.3. 风格化

现在您的图像和标题在语义上是正确的,您可以根据需要应用 CSS:

  • figure (对于图像和标题)
  • figure img (仅用于图像)
  • figcaption (仅用于标题)

2. 创建自定义包含

您需要文件夹中创建一个image.html文件_includes,并在 Markdown 中使用 Liquid 将其包含在内

2.1. 创建 _includes/image.html

image.html在 _includes 文件夹中创建文档:

<!-- _includes/image.html -->
<figure>
    {% if include.url %}
    <a href="{{ include.url }}">
    {% endif %}
    <img
        {% if include.srcabs %}
            src="{{ include.srcabs }}"
        {% else %}
            src="{{ site.baseurl }}/assets/images/{{ include.src }}"
        {% endif %}
    alt="{{ include.alt }}">
    {% if include.url %}
    </a>
    {% endif %}
    {% if include.caption %}
        <figcaption>{{ include.caption }}</figcaption>
    {% endif %}
</figure>
Run Code Online (Sandbox Code Playgroud)

2.2. 在 Markdown 中,使用 Liquid 包含图像

/assets/images带有标题的图像:

This is [Jekyll](https://jekyllrb.com)'s logo :

{% include image.html
    src="jekyll-logo.png" <!-- image filename (placed in /assets/images) -->
    alt="Jekyll's logo" <!-- alt text -->
    caption="This is Jekyll's logo, featuring Dr. Jekyll's serum!" <!-- Caption -->
%}
Run Code Online (Sandbox Code Playgroud)

使用绝对 URL 的(外部)图像:(更改src=""srcabs=""

This is [Jekyll](https://jekyllrb.com)'s logo :

{% include image.html
    srcabs="https://jekyllrb.com/img/logo-2x.png" <!-- absolute URL to image file -->
    alt="Jekyll's logo" <!-- alt text -->
    caption="This is Jekyll's logo, featuring Dr. Jekyll's serum!" <!-- Caption -->
%}
Run Code Online (Sandbox Code Playgroud)

可点击的图像:(添加url=""参数)

This is [Jekyll](https://jekyllrb.com)'s logo :

{% include image.html
    src="https://jekyllrb.com/img/logo-2x.png" <!-- absolute URL to image file -->
    url="https://jekyllrb.com" <!-- destination url -->
    alt="Jekyll's logo" <!-- alt text -->
    caption="This is Jekyll's logo, featuring Dr. Jekyll's serum!" <!-- Caption -->
%}
Run Code Online (Sandbox Code Playgroud)

没有标题的图像:

This is [Jekyll](https://jekyllrb.com)'s logo :

{% include image.html
    src="https://jekyllrb.com/img/logo-2x.png" <!-- absolute URL to image file -->
    alt="Jekyll's logo" <!-- alt text -->
%}
Run Code Online (Sandbox Code Playgroud)

2.3. 风格化

现在您的图像和标题在语义上是正确的,您可以根据需要应用 CSS:

  • figure (对于图像和标题)
  • figure img (仅用于图像)
  • figcaption (仅用于标题)

  • 为了完整起见,如果您想使用 jekyll-figure,您必须将 jekyll-figure 添加到 _config.yml 中的插件中 (2认同)

Has*_*can 5

<p align="center">
  <img alt="img-name" src="/path/image-name.png" width="300">
  <br>
    <em>caption</em>
</p>
Run Code Online (Sandbox Code Playgroud)

这是基本的字幕使用。无需使用额外的插件。