javascripthtmltidyhtmlcleaner

Tidy HTML output with JavaScript


I have a large chunk of HTML. In order for it to fit a certain container, I crop the HTML (not just the text) at, let’s say, 200 characters. Obviously, some of the tags will remain unclosed in this case. Is there a way, except writing the cleaner myself, to clean such cropped snippet without the server being involved?

Online services with public APIs that I can use from JavaScript are acceptable.


Solution

  • You can try the cutter.js library. It's pretty new, so I haven't heard much about it, but it seems like what you're looking for as far as cropping goes.

    Check out my fiddle, testing it out: http://jsfiddle.net/JKirchartz/jwL8v/

    var oElement = document.getElementById("test");
    Cutter.run(oElement, oElement, 100);
    
    $("#gc").click(function(){
        /* This will count words by spaces in the text */
        var tt = $("#test").text().split(" ");
        if (typeof(console) == 'object'){    
            console.log(tt);
        }
        alert("wordcount: "+tt.length);
         
    });