spliter
spliter

Reputation: 12569

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.

Upvotes: 6

Views: 642

Answers (2)

Alex J
Alex J

Reputation: 537

The Google Closure library has an HTML Pretty Printer module. You should be able to fork off it:

Closure Library

Also, if you are using jQuery, try http://www.davidpirek.com/blog/html-beautifier-jquery-plugin.

Upvotes: 0

JKirchartz
JKirchartz

Reputation: 18022

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);
     
});

Upvotes: 2

Related Questions