简体   繁体   English

如何使用不同的 colors 制作 TiddlyWiki 目录 (toc)

[英]How to make a TiddlyWiki table of contents (toc) with differnt colors

I use我用

<div class="tc-table-of-contents">
<<toc "Inhalt">>
</div>

where every tiddler tagged with Inhalt is listed in toc.其中每个标有Inhalt的 tiddler 都列在目录中。 This works fine.这很好用。

But I have an additional tag named Fahrt .但是我有一个名为Fahrt的附加标签。 Is it possible to change the color in the toc of this entries?是否可以更改此条目目录中的颜色? The result should look like this:结果应如下所示:

Only tag Inhalt        --> normal blue color 
tag Inhalt + tag Fahrt --> perhaps a lighter blue oder different color

This isn't possible with the default toc macros, but we can write a new macro based on the built-in toc macro to do this without too much trouble.这对于默认的toc宏是不可能的,但是我们可以在内置的toc宏的基础上编写一个新的宏来做到这一点,而不会有太多麻烦。 We'll make a version of the macro, template-toc , that uses a template to display each element in the table of contents – this way, we'll be able to reuse our work to format TOC elements in a totally arbitrary way.我们将制作宏的一个版本, template-toc ,它使用一个模板来显示目录中的每个元素——这样,我们将能够重用我们的工作以完全任意的方式格式化 TOC 元素。 ( Fuller explanation of templates. NB: I wrote this.) 对模板的更全面的解释。注意:我写了这个。)

We first start by copying the macros toc and toc-body from $:/core/macros/toc and pasting them into a new tiddler $:/template-toc-macros (you can call this tiddler anything you want) with the tag $:/tags/Macro (this will cause the macros in it to be available in any tiddler in the wiki).我们首先从$:/core/macros/toc复制宏toctoc-body并将它们粘贴到一个新的 tiddler $:/template-toc-macros (你可以随意调用这个 tiddler),标签$:/tags/Macro (这将导致其中的宏在 wiki 中的任何 tiddler 中可用)。

Then we rename toc-body and all references to template-toc-body , likewise toc to template-toc .然后我们将toc-body和所有对template-toc-body引用重命名,同样将toc重命名为template-toc We add a parameter template as the second parameter of both of these macros, and adjust the bodies of both so that they transclude the <<__template__>> parameter as a tiddler rather than looking into the caption and title fields for a title and creating a link with this as the text.我们添加一个参数template作为这两个宏的第二个参数,并调整两者的主体,以便它们将<<__template__>>参数作为一个 tiddler 而不是查看标题的captiontitle字段并创建一个与此链接作为文本。 That makes the body of this tiddler look like this:这使得这个 tiddler 的主体看起来像这样:

\define template-toc-body(tag,template,sort:"",itemClassFilter,exclude,path)
\whitespace trim
<ol class="tc-toc">
  <$list filter="""[all[shadows+tiddlers]tag<__tag__>!has[draft.of]$sort$] -[<__tag__>] -[enlist<__exclude__>]""">
    <$let item=<<currentTiddler>> path={{{ [<__path__>addsuffix[/]addsuffix<__tag__>] }}}>
      <$set name="excluded" filter="""[enlist<__exclude__>] [<__tag__>]""">
        <$set name="toc-item-class" filter=<<__itemClassFilter__>> emptyValue="toc-item-selected" value="toc-item">
          <li class=<<toc-item-class>>>
            <$transclude tiddler=<<__template__>>/>
            <$macrocall $name="template-toc-body" tag=<<item>> template=<<__template__>> sort=<<__sort__>> itemClassFilter=<<__itemClassFilter__>> exclude=<<excluded>> path=<<path>>/>
          </li>
        </$set>
      </$set>
    </$let>
  </$list>
</ol>
\end

\define template-toc(tag,template,sort:"",itemClassFilter:"")
<$macrocall $name="template-toc-body"  tag=<<__tag__>> template=<<__template__>> sort=<<__sort__>> itemClassFilter=<<__itemClassFilter__>> />
\end

Now here's how we use this: we create a template tiddler which, given that the variable <<currentTiddler>> is set to a particular tiddler we want to include in the TOC, renders the HTML/wikitext we want to include in the table of contents.下面是我们如何使用它:我们创建一个模板 tiddler,假设变量<<currentTiddler>>被设置为我们想要包含在 TOC 中的特定 tiddler,呈现我们想要包含在表中的 HTML/wikitext内容。 In this example, we'll call this tiddler MyTemplate , but you'll probably want to use something more descriptive.在此示例中,我们将此 tiddler 称为MyTemplate ,但您可能希望使用更具描述性的名称。 In your case, the text will look something like:在您的情况下,文本将类似于:

<$link to=<<currentTiddler>>>
<$list filter="[all[current]tag[Fahrt]]" emptyMessage="""<$view field='caption'><$view field='title' /></$view>""">
  <span style="color: red;"><$view field='caption'><$view field='title' /></$view></span>
</$list>
</$link>

That is, if the filter [all[current]tag[Fahrt]] has any output, ie, the currentTiddler is tagged Fahrt , then fill in the body of the $list widget (creating a span with a color: red; CSS property), containing the caption field if it exists on the tiddler, or the title field otherwise.也就是说,如果过滤器[all[current]tag[Fahrt]]有任何 output,即currentTiddler被标记为Fahrt ,然后填写$list小部件的主体(创建一个 span with a color: red; CSS 属性),如果 tiddler 上存在标题字段,则包含caption字段,否则包含title字段。 If it's not tagged Fahrt , then fill in the contents of emptyMessage , which does the same thing but without the color.如果没有标记Fahrt ,则填写emptyMessage的内容,它做同样的事情但没有颜色。 In either case, create a link going to currentTiddler containing that content.在任何一种情况下,创建一个指向包含该内容的currentTiddler的链接。

Lastly, wherever you want to show the table of contents, call the template-toc macro instead of the toc macro, and pass it the template you just created as a second argument:最后,无论你想在哪里显示目录,调用template-toc宏而不是toc宏,并将你刚刚创建的模板作为第二个参数传递给它:

<div class="tc-table-of-contents">
  <<template-toc "Inhalt" MyTemplate>>
</div>

Result:结果: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM