我正在尝试用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.
发布于 2014-05-29 08:21:51
从jade 1.0开始,有一种更简单的方法来处理这个问题,不幸的是,我在官方文档中找不到它。
您可以使用以下语法添加内联元素:
#[a.someClass A Link!]
因此,在p中不包含多行的示例如下所示:
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]]
发布于 2012-07-30 05:05:10
另一种方法是:
p
| this is the start of the para
a(href="http://example.com") a link
|
| this is the rest of the paragraph.
发布于 2012-09-29 05:39:08
另一种完全不同的方法是创建一个过滤器,它首先尝试替换链接,然后使用jade渲染。
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>
完整的工作示例: index.js (使用nodejs运行)
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子块(可能用${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());
https://stackoverflow.com/questions/6989653
复制相似问题