# 代码高亮及其自动格式化测试
## js
```js
const markdownItInstance = markdownit({
highlight(str, lang) {
lang = lang.toLowerCase();
if (lang2parser[lang]) {
console.log({ str, lang });
try {
str = prettier.format(str, {
parser: lang2parser[lang],
plugins,
});
} catch {}
}
if (lang && hljs.getLanguage(lang)) {
try {
return hljs.highlight(str, { language: lang }).value;
} catch (__) {}
}
return '';
},
});
```
## ts
```ts
const plugins: Plugin[] = [
parserBabel,
parserYaml,
parserHtml,
parserPostcss,
parserJava,
];
const lang2parser: Record<string, BuiltInParserName | string> = {
js: 'babel',
jsx: 'babel',
ts: 'babel-ts',
tsx: 'babel-ts',
json: 'json',
json5: 'json5',
yaml: 'yaml',
html: 'html',
vue: 'vue',
scss: 'scss',
css: 'css',
less: 'less',
java: 'java',
};
```
## Java
```java
public class HelloWorld {
public static void main(String[] args) {System.out.println("Hello World!");;;;;}
@
Overridepublic String toString() {
return "Hello World";
}
public int sum(int argument1,int argument2,int argument3,int argument4,int argument5
) {
return argument1+argument2+ argument3 +argument4 + argument5;
}
}
```