DEV Community

Cover image for How I built a clean Social Media Caption Formatter in Vanilla JS
NovusTools
NovusTools

Posted on • Originally published at novustools.com

How I built a clean Social Media Caption Formatter in Vanilla JS

As developers and creators, we know the pain of crafting the perfect social media caption, only to have Instagram or LinkedIn ruin the line breaks and formatting when published. I wanted a fast, offline tool to clean and format text, so I built a Caption Formatter.

The Approach

I built this using 100% Vanilla JS. There are no server calls, so your drafts and unreleased content stay completely private in your browser.

Here is a quick look at how the core logic handles zero-width spaces to force clean line breaks on stubborn platforms:

function formatCaption(inputText) {
    // Replace standard double line breaks with a zero-width space
    // to force platforms like Instagram to respect the spacing.
    const zeroWidthSpace = '\u2063';

    let formattedText = inputText.replace(/\n\n/g, `\n${zeroWidthSpace}\n`);

    // Additional cleaning for extra whitespace
    formattedText = formattedText.trim();

    return formattedText;
}
Enter fullscreen mode Exit fullscreen mode

Try it out

You can use the live tool for free here: Caption Formatter

Let me know what you think or if you'd add any specific hashtag generators in the comments!

Top comments (0)