【问题标题】:JS-Beautify programmatic options APIJS-Beautify 程序化选项 API
【发布时间】:2018-01-16 04:12:20
【问题描述】:

我目前正在使用这个包 https://www.npmjs.com/package/js-beautify

像这样:

import {js_beautify} from 'js-beautify';

const beautifiedCode = js_beautify(`
    const foo = 'bar';



    console.log(foo);
`)

但是,想象一下,如果我想格式化上面的内容,使空行的最大数量为 1,那么我想要这样:

  const foo = 'bar';

  console.log(foo);

不幸的是,我找不到关于如何将选项传递给程序化 API 的文档,我猜是这样的:

   const beautifiedCode = js_beautify(`
        const foo = 'bar';



        console.log(foo);
    `, 
     {maxBlankLines: 1}
    )

但这只是猜测。如何将选项传递给程序化 API?

【问题讨论】:

    标签: javascript js-beautify


    【解决方案1】:

    美化器可以使用preserve_newlines: false 设置删除标记之间的空白行,或使用max_preserve_newlines 和max_preserve_newlines: 3 等数字值限制换行符的数量。

    Javascript section of the project README.md 中显示了 API 级别使用的示例。

    配置选项名称与 CLI 名称相同,但使用下划线而不是破折号。

    所以要删除所有空行:

    import {js_beautify} from 'js-beautify';
    
    const originalCode = `
        const foo = 'bar';
    
    
    
        console.log(foo);
    `)
    
    
    const beautifiedCode = js_beautify(originalCode, { preserve_newlines: false});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-03
      • 1970-01-01
      • 1970-01-01
      • 2017-11-07
      相关资源
      最近更新 更多