Skip to content

Commit

Permalink
feat: add prettier-plugin (jd-opensource#2618)
Browse files Browse the repository at this point in the history
  • Loading branch information
eiinu authored Oct 27, 2023
1 parent 177907e commit 2ee6052
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"printWidth": 120,
"proseWrap": "preserve",
"htmlWhitespaceSensitivity": "strict",
"trailingComma": "none"
"trailingComma": "none",
"plugins": ["@nutui/prettier-plugin"]
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"@commitlint/cli": "^18.0.0",
"@commitlint/config-conventional": "^18.0.0",
"@nutui/eslint-config": "workspace:*",
"@nutui/prettier-plugin": "workspace:^",
"@tarojs/taro": "3.6.14",
"@types/node": "^18.18.5",
"@vitejs/plugin-vue": "^4.4.0",
Expand Down
72 changes: 72 additions & 0 deletions packages/nutui-prettier-plugin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import linguistLanguages from 'linguist-languages';
import { printers as MarkdownPrinter, parsers as MarkdownParsers } from 'prettier/plugins/markdown';

export const languages = [
{
...linguistLanguages.Markdown,
parsers: ['markdown']
}
];

export const parsers = {
markdown: {
...MarkdownParsers.markdown
}
};

export const printers = {
mdast: {
...MarkdownPrinter.mdast,
print: pluginPrint
}
};

function pluginPrint(path, options, print) {
const node = path.getValue();

if (node.type == 'table') {
return printTable(path, options, print);
}
return MarkdownPrinter.mdast.print(path, options, print);
}

function printTable(path, options, print) {
const contents = path.map(
() =>
path.map(() => {
const text = print().flat(Infinity).join('');
return { text };
}, 'children'),
'children'
);

const alignedTable = printTableContents();
return [alignedTable];

function printTableContents() {
/** @type{Doc[]} */
const parts = [printRow(contents[0]), printAlign()];
if (contents.length > 1) {
for (let i = 1; i < contents.length - 1; i++) {
parts.push(printRow(contents[i]));
}
parts.push(printRow(contents[contents.length - 1], true));
}
return parts;
}

function printAlign() {
const align = contents[0].map(() => {
return `---`;
});

return `| ${align.join(' | ')} |\n`;
}

function printRow(rowContents, end = false) {
const columns = rowContents.map(({ text }) => {
return ` ${text} `;
});
return end ? `|${columns.join('|')}|` : `|${columns.join('|')}|\n`;
}
}
18 changes: 18 additions & 0 deletions packages/nutui-prettier-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@nutui/prettier-plugin",
"version": "0.0.1",
"private": "true",
"description": "",
"type": "module",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"devDependencies": {
"prettier": "^3.0.3",
"linguist-languages": "^7.27.0"
},
"keywords": [],
"author": "jdf2e",
"license": "MIT"
}
16 changes: 16 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2ee6052

Please sign in to comment.