NodeJS Jade (Pug) inline link in dynamic text

I have this NodeJS application that uses Jade as its templating language. On one particular page, one text block is retrieved from the server, which reads text from the database.

The problem is that the returned text can contain line breaks and links, and the operator can change that text at any time. How do I display these elements correctly?

Most answers suggest using a newline:

p
    | this is the start of the para  
    a(href='http://example.com') a link
    |  and this is the rest of the paragraph

      

But I cannot do this as I cannot know when the element appears a

. I figured out how to get the newline correctly with this trick:

p
    each l in line.description.split(/\n/)
        = l
        br

      

But I can't seem to figure out how to get the render links correctly. Somebody knows?

Edit:
I am open to any link format in the database, whichever solves the problem. For example, for example, the database contains the following text:

Hello!
We would like you to visit [a("http://www.google.com")Google]

      

Then we would like to output text that looks like this:

Hello!
We would like you to visit Google

+3


source to share


1 answer


It looks like you are looking for unescaped string interpolation . The link doesn't work in the exit because Pug automatically escapes from it. Wrap the content you want to insert with !{}

and it should stop breaking links. (Disclaimer: Make sure you don't leave user input unoccupied - this is only a viable option if you know for sure that your DB content does not contain unwanted HTML / JS code.)

See this CodePen for an illustration.



With this approach, you will need to use the standard HTML ( <a>

) tags in your DB text. If you don't want that, you can take a look at Pug filters like markdown-it (you will still need to avoid compiling this filter output anyway).

+2


source







All Articles