You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Jekyll站点中实现Bootstrap两栏有序列表自动拆分?

实现Jekyll自动拆分有序列表为两栏并延续序号

当然可以实现!不用再手动拆分列表和维护序号,我们可以用Jekyll的Liquid模板系统来自动处理这个需求。下面是具体的解决方案:


步骤1:创建自定义Include模板

在你的Jekyll站点的_includes文件夹中,新建一个名为two-column-ol.html的文件,写入以下Liquid代码:

{% assign ol_html = include.content | markdownify %}
{% assign li_items = ol_html | split: '<li>' | shift %}
{% assign total_items = li_items.size %}
{% assign left_count = total_items | divided_by: 2 | plus: total_items | modulo: 2 %}
{% assign right_start = left_count | plus: 1 %}

<div class="row">
  <!-- Left Column -->
  <div class="col-sm">
    <ol>
      {% for item in li_items limit: left_count %}
        <li>{{ item | split: '</li>' | first }}</li>
      {% endfor %}
    </ol>
  </div>

  <!-- Right Column -->
  <div class="col-sm">
    {% if total_items > left_count %}
    <ol start="{{ right_start }}">
      {% for item in li_items offset: left_count %}
        <li>{{ item | split: '</li>' | first }}</li>
      {% endfor %}
    </ol>
    {% endif %}
  </div>
</div>

模板逻辑说明:

  • Markdown转HTML:用markdownify过滤器把你输入的Markdown有序列表转换成标准的HTML有序列表。
  • 拆分列表项:通过split提取出每个<li>元素的内容,去掉多余的外层标签。
  • 自动计算栏位数量:
    • 当列表总数为奇数时,左栏项数会比右栏多1(比如5项时,左栏3项,右栏2项)
    • 当总数为偶数时,两栏项数相等
  • 延续序号:右栏的<ol>标签通过start属性自动设置起始序号,完全不需要手动维护。

步骤2:在Markdown文章中使用

现在你可以在任意Markdown文件中,用以下方式调用这个模板:

---
layout: default
---

{% capture my_list %}
1. Zebras
2. Lions
3. Tigers
4. Gorillas
5. Elephants
{% endcapture %}

{% include two-column-ol.html content=my_list %}

复杂内容支持

即使你的列表项包含链接、加粗、斜体等Markdown格式,这个方案也能完美处理。比如:

{% capture my_list %}
1. [Zebras](https://example.com) *are herbivores*
2. **Lions** are apex predators
3. Tigers love swimming
4. Gorillas live in family groups
{% endcapture %}

{% include two-column-ol.html content=my_list %}

渲染后会保留所有格式,并且序号自动延续。


效果验证

当你运行Jekyll构建或预览时,上面的示例会生成:

  • 左栏:1. Zebras、2. Lions、3. Tigers(如果是5项的话)
  • 右栏:4. Gorillas、5. Elephants
    完全符合你预期的两栏布局,而且后续更新列表内容时,只需要修改capture块里的Markdown列表,拆分和序号会自动调整。

内容的提问来源于stack exchange,提问作者jelias

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:48:22