这是我在
这里的答案转载,其中我使用部分内容来涵盖博客的所有图像需求。
正如其他人所说,图像是行内元素,Markdown解析器将强制将它们包装在块级元素中。目前被接受的答案是hacky:它在你的markdown中看起来很乱,不够健壮/可扩展,并且不容易修改。我在我的博客上使用Middleman并创建了一个部分内容用于我的图像,它可以实现我想要的所有图像功能。我假设/希望Jekyll也有部分内容,并且尽管与Jekyll的语法不同,但此答案仍然与您相关。
请注意,如果我的图像链接不以“http://”,“https:/”或“/”开头,则会添加前缀。这是因为我有一个固定的博客图片组织结构,在使用此部分时更简单,我只需要图片名称而不需要其路径的其他部分。
这是部分内容:
partials/_image.erb
<%
#initialize local variables in case they were not included
caption ||= ""
alt ||= ""
classes ||= ""
#strip '.html' extension off article link to get name of folder
url = current_article.url
url_without_ext = url[0..url.length-6]
#determine if image has an exact link or belongs to "/images/blog/CURRENT_ARTICLE_NAME/"
prefix = src[0..6]
if prefix != "http://" and prefix != "https:/" and src[0] !="/" then
#image belongs to "/images/blog/CURRENT_ARTICLE_NAME/" - add prefix to src
src = "/images#{url_without_ext}/#{src}"
end
%>
{::nomarkdown}
<figure class="<%= classes %>">
<a target="_blank" href="<%= src %>" >
<img src="<%= src %>" alt="<%= alt %>">
</a>
<figcaption><%= caption %></figcaption>
</figure>
{:/}
然后这样调用部分视图:
不会添加前缀:
<%= partial "partials/image.erb", locals: {
src: "/images/icons/tech/atom.svg",
alt: "Atom",
classes: "icon" } %>
将添加前缀:
<%= partial "partials/image.erb", locals: {
src: "my-lovely-dog.svg",
alt: "Dog",
caption: "Woof woof!",
classes: "icon" } %>
我会将此部分内容保持最新
在这里(以防我添加/编辑任何内容)。