How can I decode quoted-printable content to normal strings in node.js?
Asked Answered
M

4

9

For example, I have a string "this=20is=20a=20string" that I want to convert to "this is a string".

Mushroom answered 1/11, 2012 at 3:57 Comment(3)
try mimelib github.com/andris9/mimelibBarfuss
mimelib is the way to go. =20 was an example. It could be anything, eg =27, =21 - quoted=printable in other words.Mushroom
mimelib is not maintained any more, does anyone have any alternative suggestions?Sennar
M
9

Use mimelib:

var mimelib = require("mimelib");
mimelib.decodeQuotedPrintable("this=20is=20a=20string") === "this is a string"
mimelib.decodeMimeWord("=?iso-8859-1?Q?=27text=27?=") === "'text'"
Mushroom answered 1/11, 2012 at 4:34 Comment(2)
edited 2nd line to make it actually work correctly on a quoted printableGreff
mimelib is now unmaintaiedSherry
C
2

Here a variant where you can specify the charset:

function decodeQuotedPrintable(raw, charset='utf-8') {
    const dc = new TextDecoder(charset);
    return raw.replace(/[\t\x20]$/gm, "").replace(/=(?:\r\n?|\n)/g, "").replace(/((?:=[a-fA-F0-9]{2})+)/g, (m) => {
        const cd = m.substring(1).split('='), uArr=new Uint8Array(cd.length);
        for (let i = 0; i < cd.length; i++) {
            uArr[i] = parseInt(cd[i], 16);
        }
        return dc.decode(uArr);
    });
}

console.log(decodeQuotedPrintable('Freundliche Gr=C3=BCsse'));              // "Freundliche Grüsse"
console.log(decodeQuotedPrintable('I love =F0=9F=8D=95'));                  // "I love 🍕"
console.log(decodeQuotedPrintable('Freundliche Gr=FCsse', 'ISO-8859-1'));   // "Freundliche Grüsse"
console.log(decodeQuotedPrintable('Freundliche Gr=9Fsse', 'macintosh'));    // "Freundliche Grüsse"
Coxcombry answered 16/2, 2023 at 16:37 Comment(0)
G
1

function decodeQuotedPrintable(data)
{
    // normalise end-of-line signals 
    data = data.replace(/(\r\n|\n|\r)/g, "\n");
        
    // replace equals sign at end-of-line with nothing
    data = data.replace(/=\n/g, "");

    // encoded text might contain percent signs
    // decode each section separately
    let bits = data.split("%");
    for (let i = 0; i < bits.length; i ++)
    {
        // replace equals sign with percent sign
        bits[i] = bits[i].replace(/=/g, "%");
        
        // decode the section
        bits[i] = decodeURIComponent(bits[i]);
    }
        
    // join the sections back together
    return(bits.join("%"));
}

Grader answered 15/1, 2022 at 18:43 Comment(1)
This didn't work for me when converting a url. The reason was, this converts the string to percent encoding, but certain characters in a url have a special use and shouldn't be percent encoded (e.g. ? or = for query params). Changing bits[i].replace(...) to bits[i].replace(/=(..)/g, (match, p1) => String.fromCharCode(parseInt(`0x${p1}`))) might work.Satiate
S
1

The quoted-printable package can be used for quoted printable encoding and decoding.

> var utf8 = require('utf8')
undefined
> var quotedPrintable = require('quoted-printable');
undefined
> var s = 'this=20is=20a=20string'
undefined
> utf8.decode(quotedPrintable.decode(s))
'this is a string'
> quotedPrintable.encode(utf8.encode('this is a string'))
'this is a string'
Stoneham answered 27/11, 2022 at 19:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.