在我的Jekyll包含文件中如何获得帖子的降价?

时间:2014-09-10 01:07:07

标签: jekyll

有没有办法在包含文件中获得帖子降价?

我的包含文件包含以下代码:{{workingPost.content}}

当我在markdown文件中包含它时,我得到了HTML。我将它传递给另一个期望markdown的包含,而不是传递HTML。

有没有办法访问markdown而不是帖子的HTML?

这里要求的是代码文件。他们所做的是获得网站右侧的精选帖子,如this。在该链接中,代码是静态HTML。我想通过以下代码更新它以使其动态化。我已经有第一个图像抓取代码在收到降价时工作。我也希望使用该代码从这里的帖子中获取第一张图片,但是当代码获得它时,markdown已经变成了HTML。

包含文件fulfill.html

{% assign workingPost = nil %}
{% for page in site.posts %}
  {% if page.title == 'We Convert All Dollars To Bitcoin' %}
    {% assign workingPost = page %}
  {% endif %}
{% endfor %}
    <div class="panel panel-default">
      <div class="panel-heading text-center">
        <h3 class="panel-title">Popular</h3>
      </div>

      <div class="panel-body">
          <a href="{{workingPost.url}}">
            {% assign workingPostContent = workingPost.content %}
            {% include first-post-image-src2.html param=workingPostContent %}
                <img src="{% include first-post-image-src2.html param=workingPostContent %}" alt="{{workingPost.title}}">
          <p>{{workingPost.excerpt}}</p>
              <p class="btn btn-md btn-success" role="button">READ POST</p>
              <br>
              <br>
          </a>
    </div>
  </div>

包括文件first-post-image-src2.html

{% capture result %}
{% assign htmlAgain= 'empty' %}
{% assign foundImageAgain = 0 %}
      {% assign imagesAgain = include.param | split:"![" %}
      {% for imageAgain in imagesAgain %}
        {% if imageAgain contains '](' %}
          {% if foundImageAgain == 0 %}
            {% assign htmlAgain = imageAgain | split:"](" %}
            {% assign htmlAgain = htmlAgain[1] %}
            {% assign htmlAgain = htmlAgain | split:")" | first %}
            {% assign foundImageAgain = 1 %}
          {% endif %}
        {% endif %}
      {% endfor %}
{%endcapture%}{{site.url}}{{htmlAgain|strip}}

2 个答案:

答案 0 :(得分:1)

是的,从markdown转换为html是Jekyll构建时的第一个想法之一。所以,没有办法在包含中获得降价。绕过此限制的唯一方法是使用插件执行此操作。但它不是主题。

现在回到你的代码。这是复杂而脆弱的。

杰基尔拥有做你想做的所有必要的功能。不要尝试用液体进行数据处理。使用标签和过滤器,您不必担心Gem升级会破坏您的网站并使您进行非常困难的调试。

例如:代码中的某个位置,您正在处理带有| split:"/>"过滤器的字符串,该过滤器依赖于kramdown呈现ìmg标记的方式。如果有一天他们决定删除此useless closing slash,您的代码就会中断。

您可以采用的方式:以最简单的形式将所有数据放入帖子中,然后将其与简单的Jekyll标记和过滤器一起使用。

我们的想法是使用yaml Front Matter custom variables和Jekyll post or page excerpt functionalities

在_config.yml中,定义一个新的摘录分隔符:

excerpt_separator: "<!-- excerpt end -->" # default is "\n\n" = two new lines

所有帖子中

---
excerpt_image_src: "/images/dollarsToBitcoins.jpg"
excerpt_image_alt: "Bitcoin Bulls converts dollars to bitcoins."
popular : true # I'll explain that latter
---
Bitcoin Bulls customers pay in USD but those dollars are all converted to bitcoin.
<!-- excerpt end -->

Bulls, I'm excited to announce...

default.html

{% if page.is_post %}
    <link rel="alternate" type="application/atom+xml" title="{{ site.name }} — Atom" href="{{ site.url }}/blog/feed.atom" />
    <meta property="og:image" content="{{ site.url }}{{ page.excerpt_image_src | strip_newlines }}" />
    <meta property="og:description" content="{{page.excerpt}}" />
    {% else %}

_includes / fulfill.html

<div class="panel panel-default">
  <div class="panel-heading text-center">
    <h3 class="panel-title">Popular</h3>
  </div>
  {% for p in site.posts %}{% if p.popular == true %}
  <div class="panel-body">
    <a href="{{p.url}}">
            <img src="{{p.excerpt_image_src}}" alt="{{excerpt_image_alt}}">
      <p>{{p.excerpt}}</p>
          <p class="btn btn-md btn-success" role="button">READ POST</p><br><br>
        </a>
  </div>
  {% endif %}{% endfor %}
</div>

请注意使用前端变量{% if p.popular == true %}过滤帖子的popular: true

_includes / blog-post.html

<li>
  <a href="{{ post.url }}">
    <p>{{post.date | date: "%B %d, %Y" }}</p>
    <img src="{{post.excerpt_image_src}}" alt="{{post.excerpt_image_alt}}">

    <!-- No need to wrap excerpt in <p> tag, Jekyll does it.
         If you want to put your own tag :
         <div>{{ post.excerpt | strip_html }}</div> -->
    {{ post.excerpt }}

    <p class="btn btn-md btn-success" role="button">READ POST</p><br><br>
  </a>
</li>

_layouts / post.html

<h1>{{page.title}}</h1>
<div style="color:#666;">by David Smith on {{page.date | date: "%B %d, %Y"  }}</div>
{% if page.excerpt_image_src %}
    <p><img src="{{page.excerpt_image_src}}" alt="{{page.excerpt_image_alt}}"></p>
{% endif %}
{{ page.content | remove: page.excerpt | markdownify }}
<br>

由于您未在帖子页面中实际显示帖子摘录,因此{{ page.content | remove: page.excerpt | markdownify }}{{ page.content | markdownify }}。如果您想要显示摘录:| markdownify

我不知道为什么但是page.content返回mardown而不是html,所以过滤器{{1}}将markdown转换为html。

瞧瞧! Bitcoinbulls万岁!

答案 1 :(得分:0)

降价促销不可用。它提前呈现,不可用。

this answer开始,听起来就像降价一样。

在我的情况下,我制作了我的第一个post-image-src2.html include处理它得到markdown或HTML的情况:

{% capture result %}
{% assign htmlAgain= 'empty' %}
{% assign foundImageAgain = 0 %}
  {% if include.param contains '![' %}
      {% assign imagesAgain = include.param | split:"![" %}
      {% for imageAgain in imagesAgain %}
        {% if imageAgain contains '](' %}
          {% if foundImageAgain == 0 %}
            {% assign htmlAgain = imageAgain | split:"](" %}
            {% assign htmlAgain = htmlAgain[1] %}
            {% assign htmlAgain = htmlAgain | split:")" | first %}
            {% assign foundImageAgain = 1 %}
          {% endif %}
        {% endif %}
      {% endfor %}
  {% endif %}
    {% if foundImageAgain ==0 %}
    {% assign imagesAgain = include.param | split:"<img" %}
      {% for imageAgain in imagesAgain %}
        {% if imageAgain contains 'src="' %}
          {% if foundImageAgain == 0 %}
            {% assign htmlAgain = imageAgain | split:'src="' %}
            {% assign htmlAgain = htmlAgain[1] %}
            {% assign htmlAgain = htmlAgain | split:'"' | first %}
            {% assign foundImageAgain = 1 %}
          {% endif %}
        {% endif %}
      {% endfor %}
    {% endif %}
{%endcapture%}{{site.url}}{{htmlAgain|strip}}