使⽤prettier格式化(美化)代码字符串,上传提交操作Prettier格式化代码
1.简述
想必prettier⼤家都熟悉,前端的⼩伙伴们如果⽤vs code那就可能⽤过这个美化代码插件
prettier不仅是美化代码插件,它还能在代码中将字符串格式化
2.格式化代码
安装
npm i prettier
格式化html或vue
// 引⼊prettier
import prettier from"prettier/standalone";
// 引⼊格式化插件 Html
import parrHtml from"prettier/parr-html";
// 格式化代码
export function prettierCode(code){
try{
// 参数1:代码字符串,参数2:格式化配置
return prettier.format(code,{
// 使⽤html格式
parr:"html",
// 使⽤引⼊的插件parrHtml格式化
plugins:[parrHtml],
// 允许vue脚本
vueIndentScriptAndStyle:true,
});
}catch(error){
// 如果格式化失败,返回源码
return code;
裹尸还}
}
效果
未格式化
<template><div id="app"><router-view/></div></template>
<script>export default {name: 'MagicTemplateWebApp',data() {return {};},mounted() {},methods: {},};
</script>
<style lang="scss"></style>
格式化后的⽂本
<template>
<div id="app">
<router-view/>
</div>
</template>周公解梦原版大全
<script>
export default {
name: 'MagicTemplateWebApp',
data() {
data() {
return {
};
},
mounted() {
},
methods: {
},
};
</script>
<style lang="scss">
</style>
来来来,再喝⼀杯
默认引⼊prettier时引⼊的插件
这是源码⾥参数2的⼀些配置
长征中的故事
export interface RequiredOptions extends doc.printer.Options {
/**
* Print micolons at the ends of statements.
* @default true
*/
mi: boolean;
/**
* U single quotes instead of double quotes.
* @default fal
回顾的近义词
*/
singleQuote: boolean;
/**
* U single quotes in JSX.
* @default fal
*/
jsxSingleQuote: boolean;
/
**
* Print trailing commas wherever possible.
* @default 'es5'
*/
trailingComma: 'none' | 'es5' | 'all';
/**
* Print spaces between brackets in object literals.
* @default true
*/
bracketSpacing: boolean;
/**
* Put the `>` of a multi-line HTML (HTML, JSX, Vue, Angular) element at the end of the last line instead of being * alone on the next line (does not apply to lf closing elements).
* @default fal
*/
bracketSameLine: boolean;
/**
* Put the `>` of a multi-line JSX element at the end of the last line instead of being alone on the next line.
* @default fal
* @deprecated u bracketSameLine instead
拿铁是什么*/
jsxBracketSameLine: boolean;
/**
* Format only a gment of a file.
* @default 0
*/
rangeStart: number;
/**
* Format only a gment of a file.
* @default Infinity
*/
rangeEnd: number;
/**
* Specify which parr to u.
*/
parr: LiteralUnion<BuiltInParrName> | CustomParr;
/**
* Specify the input filepath. This will be ud to do parr inference.
*/
filepath: string;
/**
* Prettier can restrict itlf to only format files that contain a special comment, called a pragma, at the top of the file.
* This is very uful when gradually transitioning large, unformatted codebas to prettier.
* @default fal
鱼图片简笔画
*/
requirePragma: boolean;
/**
* Prettier can inrt a special @format marker at the top of files specifying that
* the file has been formatted with prettier. This works well when ud in tandem with衣服发黄怎么办
* the --require-pragma option. If there is already a docblock at the top of
* the file then this option will add a newline to it with the @format marker.
* @default fal
*/
inrtPragma: boolean;
/**
* By default, Prettier will wrap markdown text as-is since some rvices u a linebreak-nsitive renderer.
* In some cas you may want to rely on editor/viewer soft wrapping instead, so this option allows you to opt out.
* @default 'prerve'
*/
proWrap: 'always' | 'never' | 'prerve';
/**
* Include parenthes around a sole arrow function parameter.
* @default 'always'
*/
arrowParens: 'avoid' | 'always';
/**
* Provide ability to support new languages to prettier.
*/
plugins: Array<string | Plugin>;
/**
* Specify plugin directory paths to arch for plugins if not installed in the same `node_modules` where prettier is located. */
pluginSearchDirs: string[];
/**
* How to handle whitespaces in HTML.
* @default 'css'
*/
htmlWhitespaceSensitivity: 'css' | 'strict' | 'ignore';
/**
* Which end of line characters to apply.
* @default 'lf'
*/
endOfLine: 'auto' | 'lf' | 'crlf' | 'cr';
/**
* Change when properties in objects are quoted.
* @default 'as-needed'
*/
quoteProps: 'as-needed' | 'consistent' | 'prerve';
/
**
* Whether or not to indent the code inside <script> and <style> tags in Vue files. * @default fal
*/
怎么设置浏览器
vueIndentScriptAndStyle: boolean;
/**
* Control whether Prettier formats quoted code embedded in the file.
* @default 'auto'
*/
embeddedLanguageFormatting: 'auto' | 'off';
}
parr参数
export type BuiltInParrName =
| 'angular'
| 'babel-flow'
| 'babel-ts'
| 'babel'
| 'css'
| 'espree'
| 'flow'
| 'glimmer'
| 'graphql'
| 'html'
| 'json-stringify'
| 'json'
| 'json5'
| 'less'
| 'lwc'
| 'markdown'
| 'mdx'
| 'meriyah'
| 'scss'
| 'typescript'
| 'vue'
| 'yaml';