Jade: Links inside a paragraph

后端 未结 13 1944
野趣味
野趣味 2020-12-07 17:34

I\'m trying to author a few paragraphs with Jade, but finding it difficult when there are links inside a paragraph.

The best I can come up with, and I\'m wondering

13条回答
  •  失恋的感觉
    2020-12-07 17:57

    Another completely different approach, would be to create a filter, which has first stab at replacing links, and then renders with jade second

    h1 happy days
    :inline
      p this can have [a link](http://going-nowhere.com/) in it
    

    Renders:

    happy days

    this can have a link in it

    Full working example: index.js (run with 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(/\[(.+?)\]\((.+?)\)/, "$1");
      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());
    

    A more general solution would render mini sub-blocks of jade in a unique block (maybe identified by something like ${jade goes here}), so...

    p some paragraph text where ${a(href="wherever.htm") the link} is embedded
    

    This could be implemented in exactly the same way as above.

    Working example of general solution:

    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());
    

提交回复
热议问题