首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Jade:段落中的链接

Jade:段落中的链接
EN

Stack Overflow用户
提问于 2011-08-09 07:05:19
回答 11查看 74.6K关注 0票数 124

我正在尝试用Jade创作一些段落,但当段落中有链接时,我发现这很困难。

我能想到的最好的,我想知道是否有一种方法可以用更少的标记来做到这一点:

代码语言:javascript
复制
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.
EN

回答 11

Stack Overflow用户

回答已采纳

发布于 2014-05-29 08:21:51

从jade 1.0开始,有一种更简单的方法来处理这个问题,不幸的是,我在官方文档中找不到它。

您可以使用以下语法添加内联元素:

代码语言:javascript
复制
#[a.someClass A Link!]

因此,在p中不包含多行的示例如下所示:

代码语言:javascript
复制
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]

您还可以执行嵌套的内联元素:

代码语言:javascript
复制
p: This is a #[a(href="#") link with a nested #[span element]]
票数 122
EN

Stack Overflow用户

发布于 2012-07-30 05:05:10

另一种方法是:

代码语言:javascript
复制
p
  | this is the start of the para
  a(href="http://example.com") a link
  | 
  | this is the rest of the paragraph.
票数 47
EN

Stack Overflow用户

发布于 2012-09-29 05:39:08

另一种完全不同的方法是创建一个过滤器,它首先尝试替换链接,然后使用jade渲染。

代码语言:javascript
复制
h1 happy days
:inline
  p this can have [a link](http://going-nowhere.com/) in it

渲染:

代码语言:javascript
复制
<h1>happy days</h1><p>this can have <a href='http://going-nowhere.com/'>a link</a> in it</p>

完整的工作示例: index.js (使用nodejs运行)

代码语言:javascript
复制
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}之类的东西来标识),所以……

代码语言:javascript
复制
p some paragraph text where ${a(href="wherever.htm") the link} is embedded

这可以用与上面完全相同的方式来实现。

一般解决方案的工作示例:

代码语言:javascript
复制
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());
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6989653

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档