Денис Крешихин

Денис
Крешихин

software development, computer science

2024 © Денис Крешихин

Sharing a code through social networks

There is a problem with formatting code in social networks. If you pass a piece of code ass a message, as result, you'll see a corrupted code with smiles and wrong indents.

This is an attempt to solve the problem by a web service srclnk.ru which created by me for transforming a piece of code to a link with an image.

code2image

At first, I've written a small library code2image] which allows to create an image based on a piece of code.

var code = "var hello = 'hello';"

var styles = {}; // all are default

code2image.render(code, 'test.png', styles,
    function(){
        console.log('ok!');
});

The result of execution:

result

Main dependencies of the libary is hightlight-js and node-canvas.

Because hightlight-js recognize a code not very well I've written also a library which uses Bayesian classifier tolmach implemented in natural. This approach works a little better.

srclnk.ru

The web service srclnk.ru is lightweight wrapper of a library code2image which allows to place a piece of code and take a image with formatted and highlighted code. This image can be posted in social networks as typical media message.