0

I try to extract text between paragraph tag in a Jade view but it doesn't work.

My subject:

<p> My content. </p> <img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcTJ9ylGJ4SDyl49VGh9Q9an2vruuMip-VIIEG38DgGM3GvxEi_H"> <p> Second sentence. </p>

Jade view:

- var content = post.content.slice('/<p>(.*?)<\/p>/g');
p #{content}

Result:

<
tonymx227
  • 5,293
  • 16
  • 48
  • 91

2 Answers2

1

why do u need to extract text between paragraph tag on server side??

that will be something we do on client side js


in your case, context should be a parameter you pass to jade view, by res.render('view', {context : "My content."}) or res.locals

so you can deal with your #{context} in Jade view

if you want to declare variable context in Jade view

it should be like

-var context= "My content."

p #{content}


reply to jsdom code

in your code

posts[i] is undefined.

I suppose u want to iterate posts, so you should use a iterator here

you can use async module here

in this case, map is very suitable

see here for document -> async map

Creation.findAll({where: "state = 1",order: 'id DESC', limit: 2}).success(function(creations) {
    Post.findAll({where: "state = 1",order: 'id DESC', limit: 2}).success(function(posts){

async.map(posts, function(postEntity, callback){
jsdom.env(
            postEntity.content,
            ["http://code.jquery.com/jquery.js"],
            function(errors, window) {
                //deal with errors
                if(errors) return callback(errors);

                postEntity.content = window.$("p").text();
                callback(null, postEntity);
            }
        );
}, function(err, transformedPosts){
    if(err) return callback(err);
    res.render('index', {
        creations: creations,
        posts: transformedPosts,
        title: "Anthony Cluse | Portfolio"
    });
});

    
});
});

FYI, you should use control flow library to manage your callback code

otherwise, it would be really hard to maintain

I recommend async

Community
  • 1
  • 1
williamC
  • 176
  • 2
  • 12
  • I need to get the text between paragraph tag because I have two views for my posts, the "posts" view with all the posts and the "post" view with a specific post. In the "posts" view, I would like to get a short description from the content, and in the "post" view, I get the content with tags.... – tonymx227 Feb 20 '13 at 23:05
  • you don't have to use extract. in the "posts" view with all the posts, after u query for the posts from DB, you can use substr like `string.substr(0, 10)` to get short description and send as a parameter to jade view. in the "post" view, you just query for a specific post from DB, and it's done. why use exact? – williamC Feb 21 '13 at 05:40
  • Yea, I use substr() function too, but if I don't remove tags, it's look like this "

    Beginner of my senten..."!

    – tonymx227 Feb 21 '13 at 05:44
  • you query your posts from DB? why do you have tag in that? – williamC Feb 21 '13 at 06:19
  • Yes from DB. Because I use tags in my textarea, I format posts myself. – tonymx227 Feb 21 '13 at 06:26
  • 1
    ok I see... you should use a dom parser module instead of extract by yourself. that will be easier and more flexible! try this -> [jsdom](https://github.com/tmpvar/jsdom) – williamC Feb 21 '13 at 06:34
  • Ok, I tried with jsdom, you can see above. But it doesn't work. Maybe, I didn't use it properly. – tonymx227 Feb 21 '13 at 08:01
  • TypeError: Cannot set property 'content' of undefined. – tonymx227 Feb 21 '13 at 08:03
0
var i = 0;
Creation.findAll({where: "state = 1",order: 'id DESC', limit: 2}).success( function(creations) {
    Post.findAll({where: "state = 1",order: 'id DESC', limit: 2}).success(function(posts){
        console.log(posts[i].content);
        jsdom.env(
            posts[i].content,
            ["http://code.jquery.com/jquery.js"],
            function(errors, window) {
                posts[i].content = window.$("p").text();
            }
        );
        i++;
        res.render('index', {
            creations: creations,
            posts: posts,
            title: "Anthony Cluse | Portfolio"
        });
    });
});
tonymx227
  • 5,293
  • 16
  • 48
  • 91