Formating javascript source code [closed]

自作多情 提交于 2019-11-29 13:04:20

The developer tools, integrated in most modern browsers, are capable of cleaning up the formatting. As an example, below is an animated GIF showing how you can achieve this in Microsoft Edge:

Similar functionality exists for both Chrome, and Firefox as well.

There are also online solutions if you want to copy/paste a large block of minified code.

Brian Agnew's link should work fine. There's also the standalone Polystyle which I can recommend (costs $15 though).

On a side note, it may be better for you to get the non-minified version of TinyMCE and work with that in the first place. Some minifiers not only remove line breaks, but change variables names and other code elements as well.

Most JavaScript engines beautify functions. Knowing this, here is a function that can help you with your problem:

function beautify (code) {
  return new Function(code).toString(0)
    .replace(/^function\s*\w*\s*\(\s*\)\s*{?|;?}?$/g, "")
    .replace(/\n\s{4}/g, "\n").replace(/^\n/, "")
}

SpiderMonkey and Rhino can also un-beautify (minify) them if you use function.toString(-1) in case you ever need to do the opposite. I also have a function for that too:

function minify (code) {
  new Function(code).toString(-1)
    .replace(/^function\s*\w*\s*\(\s*\)\s*{?|;?}?$/g, "");
}

Edit: It seems you only need to do this for tinymce.js. You can download the TinyMCE source code as it's open source.

Also take a look at this: http://closure-compiler.appspot.com/home; A Google tool.

Choose [Pretty print] under Formatting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!