【问题标题】:How to create jQuery Tabs out of Markdown如何使用 Markdown 创建 jQuery 选项卡
【发布时间】:2009-03-06 12:35:12
【问题描述】:

我目前正在运行一个基于 PHP 的内容管理系统,它在 Markdown Extra 的帮助下生成其 HTML 内容。大多数内容由标题和子标题构成,导致页面很长。在每一页的开头,我借助一个列表和Markdown Extra's Header Id Attribute 创建了一个目录。

为了缩短页面视图的长度,我想在第一级标题上使用 jQuery 的 Tabs Plugin

问题在于 Markdown Extra 的输出与 jQuery Tabs Plugin 所期望的不兼容,因为 Markdown Extra 没有将部分的内容包装到单独的 div 标记中。

有没有办法让这两个库一起工作?

编辑

这是 HTML 输出的样子:

 <p>Some intro text...</p>
 <h3>Table of content</h3>
 <ul>
  <li><a href="#sub1">Topic 1</a></li>
  <li><a href="#sub2">Topic 2</a></li>
  <li><a href="#sub3">Topic 3</a></li>
 </ul>
 <h3 id="sub1">Topic 1</h3>
 <p>The content of this topic.</p>
 <h3 id="sub2">Topic 2</h3>
 <p>The content of this topic.</p>
 <h3 id="sub3">Topic 3</h3>
 <p>The content of this topic.</p>

.. 这是对应的 Markdown 代码:

Some intro text...

###Table of content###

* [Topic 1](#sub1)
* [Topic 2](#sub2)
* [Topic 3](#sub3)

###Topic 1### {#sub1}

The content of this topic.

###Topic 2### {#sub2}

The content of this topic.

###Topic 3### {#sub3}

The content of this topic.

解决方案

在 cobbal 的帮助下,我做了这个 jQuery 语句,它将 Markdown 标记转换为 Tabs 插件可以使用的东西:

  $("#tabs :header").each(function() 
  {
    var id = $(this).attr("id");
    if(id=="") return;    
    var div = $("<div>");
    var tagName = this.tagName;    
    $(this).nextAll().each(function()
    {
      if(this.tagName==tagName) return false;
      div.append(this);
    });
    $(this).removeAttr("id");
    div.attr("id", id);
    $(this).before(div);
    div.prepend(this);
  });

但事实上,我需要转换标记以适应 Tabs 插件,而不仅仅是告诉 Tabs 插件它应该以不同的方式选择其内容,这一事实可能使该解决方案不是理想的解决方案。

【问题讨论】:

  • 你能发布一个markdown Extra输出的代码示例吗?

标签: javascript jquery tabs markdown


【解决方案1】:

在您的文档上。准备就绪

$("#sub1,#sub2,#sub3").each(function() {
    var containerDiv = $("<div>").attr("id", $(this).attr("id") + "div");
    $(this).before(containerDiv);
    containerDiv.append(this);
});

并将您的降价更改为

...
* [Topic 1](#sub1div)
* [Topic 2](#sub2div)
* [Topic 3](#sub3div)
...

【讨论】:

  • 这只会将标题包装成一个 div .. 但无论如何感谢您的提示,我能够制作自己的 jQuery 语句
【解决方案2】:

在我看来,您可以自己将目录包装在 div 中。

您链接到的 Markdown Extra 页面的一半显示了如何在 div 中插入 Markdown,如下所示:

PHP Markdown Extra 提供了一种将 Markdown 格式的文本放入任何 块级标签。您可以通过向标签添加降价属性来做到这一点 值 1 — 给出 markdown="1" — 像这样:

<div markdown="1">
This is *true* markdown text.
</div>

markdown="1" 属性将被剥离,并且 's 的内容将从 Markdown 转换为 HTML。最终结果将如下所示:

<div>

<p>This is <em>true</em> markdown text.</p>

</div>

【讨论】:

  • 但这会破坏我开始使用 Markdown 时所寻求的可维护性...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-23
  • 2012-04-17
  • 1970-01-01
  • 1970-01-01
  • 2015-11-23
  • 1970-01-01
相关资源
最近更新 更多