Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format/tidy/beautify in JavaScript

How can I format/tidy/beautify HTML in JavaScript? I have tried doing a search/replace for angle brackets (<, >) and indenting accordingly. But of course it does not take into account when the is JS or CSS etc inside the HTML.

The reason I want to do this is I have made a content editor (CMS) which has both WYSIWYG and source code views. The problem the code written by the WYSIWYG editor is normally a single line. So I would like a JavaScript that could format this into a more readable form on demand.

Here what I have so far:

function getIndent(level) {     var result = '',         i = level * 4;     if (level < 0) {         throw "Level is below 0";     }     while (i--) {         result += ' ';     }     return result; }  function style_html(html) {     html = html.trim();     var result = '',         indentLevel = 0,         tokens = html.split(/</);     for (var i = 0, l = tokens.length; i < l; i++) {         var parts = tokens[i].split(/>/);         if (parts.length === 2) {             if (tokens[i][0] === '/') {                 indentLevel--;             }             result += getIndent(indentLevel);             if (tokens[i][0] !== '/') {                 indentLevel++;             }              if (i > 0) {                 result += '<';             }              result += parts[0].trim() + ">\n";             if (parts[1].trim() !== '') {                 result += getIndent(indentLevel) + parts[1].trim().replace(/\s+/g, ' ') + "\n";             }              if (parts[0].match(/^(img|hr|br)/)) {                 indentLevel--;             }         } else {             result += getIndent(indentLevel) + parts[0] + "\n";         }     }     return result; } 
like image 539
Petah Avatar asked Oct 12 '10 09:10

Petah


People also ask

How do I beautify a JavaScript file?

To do this, go to File->Preferences->Settings: Under Text Editor, select Format On Save. Now when saving a file, it will be beautified.

What is beautify JavaScript?

Code formatting and JS Beautifier JS Beautify is a technique used to help developers format code in a consistent way. Beautify helps to ensure all their code is up to the latest formatting standards. It also allows them to improve the code quality and prevent bugs.

How do I get beautify source code?

To enhance and beautify your code, copy and paste it into the text box on our code formatter page. Once your code has been entered, you can select whether it is HTML, XML, CSS, JavaScript, PHP, or JSON or, if you are unsure of the code format you are trying to beautify, you can select the autodetect button.

How do I beautify HTML code?

To improve the formatting of your HTML source code, you can use the Format Document command Ctrl+Shift+I to format the entire file or Format Selection Ctrl+K Ctrl+F to just format the selected text. The HTML formatter is based on js-beautify.


1 Answers

I use this method to format HTML. Simple, but does the job:

function format(html) {     var tab = '\t';     var result = '';     var indent= '';      html.split(/>\s*</).forEach(function(element) {         if (element.match( /^\/\w/ )) {             indent = indent.substring(tab.length);         }          result += indent + '<' + element + '>\r\n';          if (element.match( /^<?\w[^>]*[^\/]$/ ) && !element.startsWith("input")  ) {              indent += tab;                       }     });      return result.substring(1, result.length-3); } 
like image 108
michal.jakubeczy Avatar answered Sep 20 '22 15:09

michal.jakubeczy