micnews / article-json-to-fbia

Render article JSON in the Facebook Instant Articles format

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problem with images

cmarrero01 opened this issue · comments

Hi,

I trasform html to JSON article without problem, but when I try to convert the json with article-json-to-fbia, all images is gone.

For some reason when transform to instant articles disapear all imagenes.

var body = articleJson(doc[0].body);
var body2 = convertToFbia(body);
res.send(body2).

The image is in JSON with the correct URL and size.. but for some unespected reazon, the image disapear.

Hi @cmarrero01,

can you add an example of the structure how the article json you're passing into convertToFbia() looks?

HI @ellell ,

I Solve the problem... it was the hierarchy of article json object.. All images or custom embeds that is in children property, inside of a pharaprases for example, the npm package fbia erase that.

My solution was, parse de json article to put all elements outside of children property, and without any hierarchy.

For example, this is a recursivity to get out all elementos from children properties of article json.

function getTypeImageRecursivity(elements,body){
            for(var prop in body){
                if(!body[prop] && !body[prop].children){
                    continue;
                }

                if(!body[prop].children){
                    var obj = body[prop];
                    if(body[prop].type === 'text'){
                        obj = {
                            type: 'paragraph',
                            children: [body[prop]]
                        };
                    }
                    elements.push(obj);
                    continue;
                }

                getTypeImageRecursivity(elements,body[prop].children);
            }
        }

And then... I do this.


doc.forEach(function(article){
                        var body = articleJson(article.body);
                        var elements = [];
                        getTypeImageRecursivity(elements,body);
                        article.body = convertToFbia(elements);
                        article.body = article.body.replace(new RegExp('<article>', 'g'), '');
                        article.body = article.body.replace(new RegExp('</article>', 'g'), '');
                        article.body = article.body.replace(new RegExp('</img>', 'g'), '');
                        article.body = article.body.replace(new RegExp('"//', 'g'), '"https://');
                        article.body = article.body.replace(new RegExp('<h3>', 'g'), '<h2>');
                        article.body = article.body.replace(new RegExp('</h3>', 'g'), '</h2>');
                        article.body = article.body.replace(new RegExp('<h4>', 'g'), '<h2>');
                        article.body = article.body.replace(new RegExp('</h4>', 'g'), '</h2>');
                        article.body = article.body.replace(new RegExp('<figure><iframe', 'g'), '<figure class="op-interactive"><iframe');
                    });

Best
C

@cmarrero01 Ah I see. Yes, some of these article-json modules are very opinionated about how things can be nested, and unfortunately it's not well documented currently. I'm glad you were able to solve it!