我正在尝试使用Jade编写几个段落,但在段落内包含链接时会感到困难。
最好的方法可能是我能想到的,但我想知道是否有一种使用更少标记的方法:
p
span.
this is the start
of the para.
a(href="http://example.com") a link
span.
and this is the rest of
the paragraph.
从jade 1.0开始,有一种更简单的方法来处理这个问题,不幸的是我在官方文档中找不到它的任何信息。
您可以使用以下语法添加内联元素:
#[a.someClass A Link!]
那么,不用在段落中跨越多行的示例将会是这样:
p: #[span this is the start of the para] #[a(href="http://example.com") a link] #[span and this is the rest of the paragraph]
您还可以嵌套内联元素:
p: This is a #[a(href="#") link with a nested #[span element]]
你可以使用Markdown过滤器,使用Markdown(和允许的HTML)编写你的段落。
:markdown
this is the start of the para.
[a link](http://example.com)
and this is the rest of the paragraph.
另外,似乎你可以直接输出HTML而不会有任何问题:
p
| this is the start of the para.
| <a href="http://example.com">a link</a>
| and this is he rest of the paragraph
我自己并不知道这一点,只是使用了Jade命令行工具进行了测试。它似乎能够正常工作。
编辑: 实际上可以完全在Jade中完成,如下所示:
p
| this is the start of the para
a(href='http://example.com;) a link
| and this is the rest of the paragraph
在段落末尾不要忘记多加一个空格(虽然你看不到它),以及在|
和之间需要加一个空格。否则看起来会像这样:
para.a linkand
而不是para a link and
p 这是一个段落#[a(href="#") 带有链接]在里面
。详见 https://github.com/visionmedia/jade/issues/936 - Will
,但我正在考虑将来的方法。 - Dave Newton另一种方法:
p
| this is the start of the para
a(href="http://example.com") a link
|
| this is the rest of the paragraph.
ul
each val in results
p
| blah blah
a(href="#{val.url}") #{val.name}
| more blah
查看插值
h1 happy days
:inline
p this can have [a link](http://going-nowhere.com/) in it
<h1>happy days</h1><p>this can have <a href='http://going-nowhere.com/'>a link</a> in it</p>
var f, jade;
jade = require('jade');
jade.filters.inline = function(txt) {
// simple regex to match links, might be better as parser, but seems overkill
txt = txt.replace(/\[(.+?)\]\((.+?)\)/, "<a href='$2'>$1</a>");
return jade.compile(txt)();
};
jadestring = ""+ // p.s. I hate javascript's non-handling of multiline strings
"h1 happy days\n"+
":inline\n"+
" p this can have [a link](http://going-nowhere.com/) in it"
f = jade.compile(jadestring);
console.log(f());
${jade goes here}
的标识符标识),因此...p some paragraph text where ${a(href="wherever.htm") the link} is embedded
这可以与上面完全相同地实现。
var f, jade;
jade = require('jade');
jade.filters.inline = function(txt) {
txt = txt.replace(/\${(.+?)}/, function(a,b){
return jade.compile(b)();
});
return jade.compile(txt)();
};
jadestring = ""+ // p.s. I hate javascript's non-handling of multiline strings
"h1 happy days\n"+
":inline\n"+
" p this can have ${a(href='http://going-nowhere.com/') a link} in it"
f = jade.compile(jadestring);
console.log(f());
编辑:此功能已实现并关闭问题,请参见上面的答案。
我已发布了一个问题,以便将此功能添加到Jade中
https://github.com/visionmedia/jade/issues/936
尚未有时间实施它,更多+1可能会有所帮助!
这是我能想到的最好的方案。
-var a = function(href,text){ return "<a href='"+href+"'>"+text+"</a>" }
p this is an !{a("http://example.com/","embedded link")} in the paragraph
渲染...
<p>this is an <a href='http://example.com/'>embedded link</a> in the paragraph</p>
功能正常,但感觉有点像是一种折中的方法 - 实际上应该有一种语法来处理这个问题!
p Convert a .fit file using <a href="http://connect.garmin.com/"> Garmin Connect's</a> export functionality.
p
| At Times Like These We Suggest Just Going:
a(ui-sref="app") HOME
|
我必须在链接后面直接添加一个句号,就像这样:
这是你的测试[链接]。
我是这样解决的:
label(for="eula").lbl.lbl-checkbox.lbl-eula #{i18n.signup.text.accept_eula}
| <a href="#about/termsandconditions" class=".lnk.lnk-eula">#{i18n.signup.links.eula}</a>.