From 2ee6052fcb9275476e2fe54d7930ec3155012e50 Mon Sep 17 00:00:00 2001 From: Eiinu Date: Fri, 27 Oct 2023 10:47:48 +0800 Subject: [PATCH] feat: add prettier-plugin (#2618) --- .prettierrc | 3 +- package.json | 1 + packages/nutui-prettier-plugin/index.js | 72 +++++++++++++++++++++ packages/nutui-prettier-plugin/package.json | 18 ++++++ pnpm-lock.yaml | 16 +++++ 5 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 packages/nutui-prettier-plugin/index.js create mode 100644 packages/nutui-prettier-plugin/package.json diff --git a/.prettierrc b/.prettierrc index a78b08e8db..2538d0717e 100644 --- a/.prettierrc +++ b/.prettierrc @@ -7,5 +7,6 @@ "printWidth": 120, "proseWrap": "preserve", "htmlWhitespaceSensitivity": "strict", - "trailingComma": "none" + "trailingComma": "none", + "plugins": ["@nutui/prettier-plugin"] } diff --git a/package.json b/package.json index 28c3180c9e..91ccb3d3c3 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/nutui-prettier-plugin/index.js b/packages/nutui-prettier-plugin/index.js new file mode 100644 index 0000000000..6eab482c0f --- /dev/null +++ b/packages/nutui-prettier-plugin/index.js @@ -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`; + } +} diff --git a/packages/nutui-prettier-plugin/package.json b/packages/nutui-prettier-plugin/package.json new file mode 100644 index 0000000000..1f131b6b7a --- /dev/null +++ b/packages/nutui-prettier-plugin/package.json @@ -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" +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a57b4e4346..c9c2a5732e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -33,6 +33,9 @@ importers: '@nutui/eslint-config': specifier: workspace:* version: link:packages/nutui-eslint-config + '@nutui/prettier-plugin': + specifier: workspace:^ + version: link:packages/nutui-prettier-plugin '@tarojs/taro': specifier: 3.6.14 version: 3.6.14(postcss@8.4.31)(vue@3.3.4) @@ -180,6 +183,15 @@ importers: specifier: ^3.3.4 version: 3.3.4 + packages/nutui-prettier-plugin: + devDependencies: + linguist-languages: + specifier: ^7.27.0 + version: 7.27.0 + prettier: + specifier: ^3.0.3 + version: 3.0.3 + packages/nutui-taro-demo: dependencies: '@babel/runtime': @@ -10330,6 +10342,10 @@ packages: resolution: {integrity: sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg==} dev: true + /linguist-languages@7.27.0: + resolution: {integrity: sha512-Wzx/22c5Jsv2ag+uKy+ITanGA5hzvBZngrNGDXLTC7ZjGM6FLCYGgomauTkxNJeP9of353OM0pWqngYA180xgw==} + dev: true + /linkify-it@3.0.3: resolution: {integrity: sha512-ynTsyrFSdE5oZ/O9GEf00kPngmOfVwazR5GKDq6EYfhlpFug3J2zybX56a2PRRpc9P+FuSoGNAwjlbDs9jJBPQ==} dependencies: