Strange encoding issue

Plugin Development
  • Plugin & Theme Dev
    #1

    Trying to write a nodebb plugin but I'm experiencing some strange result when trying to parse a post (0.7.0 latest).

    I'm picking up the data from data.postData.content which contains a a-tag with url like this:

    <a href="http://www.banggood.com/New-Version-Upgraded-Hubsan-X4-H107-2_4G-4CH-Remote-Control-RC-Quadcopter-p-71838.html?ss=hej&p=4711">

    But the parameter-separator & of the url turn into &amp; which makes it really hard to pick up the parameter-values.

    ...nggood.com/New-Version-Upgraded-Hubsan-X4-H107-2_4G-4CH-Remote-Control-RC-Quadcopter-p-71838.html?ss=hej&amp;p=4711

    Are there any preprocessing going on before handing over the parse post data? It happens to all urls in the post with parameters.

    (I've moved up my plugin to run first of all in the prio queue)

  • Plugin & Theme Dev
    #2

    Hasn't anyone noticed this problem?

    All link processing in a plugins will become much more hackey where I have to

    Embed.parse = function(data, callback) {
    
        var match = /<a\s+[^>]*href="([^"]*)"[^>]*>/i.exec(data.postData.content);
        var url = match[1];    // <-- In this url all parameter is separated by &amp; instead of &
        // Ugly fix
        realUrl = replace(/\&amp;/g, '&');
         
        // Should I change it back to &amp; before doing callback?
        .....
    }
    

    Somewhere in the pipe after plugin.parse url:s get translated back by NodeBB so this isn't visible in the forum. But I want to manipulate the url parameter on-it's-way to the end-user.

  • GNU/Linux
    #3

    At default priority (5) and run before markdown, you should be seeing the link before it turns into an anchor tag. Make sure your plugin is executed before markdown (though it does seem like you did that already...)

    Maybe if you set priority of 4 for the parse.post hook, that might do it.

  • Plugin & Theme Dev
    #4

    Yes, that's it! Setting prio 4 gives access to the markup.

    I'd also like to add a custom "card" at the bottom of the post (like in github-issue plugin). But what happens if I start inserting a html template in this phase? Guess it will mess up markup parsing in a later phase? The plugin.json looks like this:

    {
        "id": "nodebb-plugin-affiliate",
        "name": "Affiliate",
        "description": "Earn money on your user generated content. Converts links in posts to affiliate variants (ebay, aliexpress, amazon, banggood, ...). With an optional box showing product image, price and stock availablility.",
        "url": "https://github.com/henrikekblad/nodebb-plugin-affiliate",
        "library": "./index.js",
        "templates": "templates",
        "less": [
            "less/default.less"
        ],
        "hooks": [
            { "hook": "filter:parse.post", "method": "parse", "priority": 4 },
            { "hook": "filter:parse.raw", "method": "parse", "priority": 4  },
            { "hook": "static:app.load", "method": "init" },
            { "hook": "filter:admin.header.build", "method": "buildMenu" }
        ],
        "staticDirs": {
            "static": "./static"
        }
    }
    
  • GNU/Linux
    #5

    If you add html at priority level 4, it would get stripped out by the markdown plugin... so we're back to the original problem with the escaped &...

  • Plugin & Theme Dev
    #6

    Yeah, just realised that... I'd better go back to my old strategy.

  • GNU/Linux
    #7

    If the markdown plugin turns the & into &amp;, does it end up showing up incorrectly in the final parsed HTML?

  • Plugin & Theme Dev
    #8

    Yepp.. I convert it back after parsing manipulating the url.


Suggested Topics


| | | |