From bedaa12c934eb8ae11be9f4bd8fe2e2560751a96 Mon Sep 17 00:00:00 2001 From: nnecec Date: Mon, 24 Jul 2023 17:02:59 +0800 Subject: [PATCH 1/3] feat(prettier-plugin-zh): support spaceAroundNumber --- .changeset/fifty-hairs-worry.md | 5 +++++ packages/core/src/rules/index.ts | 2 +- ...ace-around-alphabet.ts => space-around.ts} | 15 ++++++++++---- packages/core/src/utils/convert-text.ts | 2 +- packages/prettier-plugin-zh/readme.md | 20 ++++++++++++++----- packages/prettier-plugin-zh/src/options.ts | 6 ++++++ .../prettier-plugin-zh/src/parsers/index.ts | 7 ++++--- .../src/transforms/default-transform.ts | 3 +++ .../src/transforms/index.ts | 5 +---- .../src/transforms/transform-markdown.ts | 11 +++++----- .../src/transforms/types.ts | 5 +++++ packages/prettier-plugin-zh/src/types.ts | 3 ++- .../tests/fixtures/space-around-alphabet.md | 9 --------- .../tests/fixtures/space-around.md | 9 +++++++++ ...e-around-alphabet.mdx => space-around.mdx} | 0 15 files changed, 69 insertions(+), 33 deletions(-) create mode 100644 .changeset/fifty-hairs-worry.md rename packages/core/src/rules/{space-around-alphabet.ts => space-around.ts} (60%) create mode 100644 packages/prettier-plugin-zh/src/transforms/default-transform.ts create mode 100644 packages/prettier-plugin-zh/src/transforms/types.ts delete mode 100644 packages/prettier-plugin-zh/tests/fixtures/space-around-alphabet.md create mode 100644 packages/prettier-plugin-zh/tests/fixtures/space-around.md rename packages/prettier-plugin-zh/tests/fixtures/{space-around-alphabet.mdx => space-around.mdx} (100%) diff --git a/.changeset/fifty-hairs-worry.md b/.changeset/fifty-hairs-worry.md new file mode 100644 index 0000000..896a573 --- /dev/null +++ b/.changeset/fifty-hairs-worry.md @@ -0,0 +1,5 @@ +--- +'prettier-plugin-zh': minor +--- + +support space around number diff --git a/packages/core/src/rules/index.ts b/packages/core/src/rules/index.ts index 7f5ee24..d7e5d36 100644 --- a/packages/core/src/rules/index.ts +++ b/packages/core/src/rules/index.ts @@ -1 +1 @@ -export * from './space-around-alphabet' +export * from './space-around' diff --git a/packages/core/src/rules/space-around-alphabet.ts b/packages/core/src/rules/space-around.ts similarity index 60% rename from packages/core/src/rules/space-around-alphabet.ts rename to packages/core/src/rules/space-around.ts index 0d2093e..67df0f5 100644 --- a/packages/core/src/rules/space-around-alphabet.ts +++ b/packages/core/src/rules/space-around.ts @@ -1,11 +1,11 @@ import { convertText } from '../utils' -export function spaceAroundAlphabet(text: string) { +function spaceAround(text: string, reg: RegExp) { const convertedText = convertText(text) - + console.log(text, convertText) const boundaries: number[] = [] - const close = convertedText.matchAll(/AZ|ZA/g) + const close = convertedText.matchAll(reg) for (const item of close) { boundaries.push(item.index as number) @@ -17,8 +17,8 @@ export function spaceAroundAlphabet(text: string) { let newContent = '' for (const boundary of boundaries) { + newContent += `${text.slice(lastMatchPos, boundary + 1)} ` lastMatchPos = boundary + 1 - newContent += text.slice(lastMatchPos, boundary + 1) } newContent += text.slice(lastMatchPos) @@ -26,3 +26,10 @@ export function spaceAroundAlphabet(text: string) { } return text } + +export function spaceAroundAlphabet(text: string) { + return spaceAround(text, /AZ|ZA/g) +} +export function spaceAroundNumber(text: string) { + return spaceAround(text, /NZ|ZN/g) +} diff --git a/packages/core/src/utils/convert-text.ts b/packages/core/src/utils/convert-text.ts index 073b235..fb01e36 100644 --- a/packages/core/src/utils/convert-text.ts +++ b/packages/core/src/utils/convert-text.ts @@ -3,7 +3,7 @@ import { isChineseCharacter, isEnglishCharacter, isNumberCharacter } from './hel /** * https://github.com/lint-md/lint-md/blob/master/src/utils/mark-text.ts * - * 你好世界 hello world!!! -> ZZZZAAAAA-AAAAA--- + * 你好世界 hello world!!!1234 -> ZZZZAAAAA-AAAAA---NNNN */ export const convertText = (text: string) => { const res = [...text].map(value => { diff --git a/packages/prettier-plugin-zh/readme.md b/packages/prettier-plugin-zh/readme.md index aeaa43d..f247554 100644 --- a/packages/prettier-plugin-zh/readme.md +++ b/packages/prettier-plugin-zh/readme.md @@ -4,18 +4,21 @@ ## Features -- add spaces between Chinese and English characters. +- spaceAroundAlphabet: add spaces between Chinese and English characters. +- spaceAroundNumber: add spaces between Chinese and Number characters. - In the future, more formatting options for Chinese text will be added. ## Installation -You can install **prettier-plugin-zh** using npm or yarn: +You can install **prettier-plugin-zh** using npm, yarn or pnpm: ```bash # npm npm install prettier prettier-plugin-zh --save-dev +# yarn +yarn add prettier prettier-plugin-zh --dev # pnpm -pnpm add prettier prettier-plugin-zh --dev +pnpm add prettier prettier-plugin-zh --save-dev ``` ## Usage @@ -34,16 +37,23 @@ pnpm add prettier prettier-plugin-zh --dev ```json { "plugins": ["prettier-plugin-zh"], - "spaceAroundAlphabet": true + "spaceAroundAlphabet": true, + "spaceAroundNumber": true } ``` -### `spaceAroundAlphabet` (default: `true`) +### `spaceAroundAlphabet` - **Type:** `boolean` - **Default:** `true` - **Description:** When set to `true`, this option will enable the insertion of spaces between Chinese and English characters. If set to `false`, this feature will be disabled. +### `spaceAroundNumber` + +- **Type:** `boolean` +- **Default:** `true` +- **Description:** When set to `true`, this option will enable the insertion of spaces between Chinese and Number characters. If set to `false`, this feature will be disabled. + ## Contributing Contributions to **prettier-plugin-zh** are welcome! If you want to add more formatting options or fix issues, feel free to open a pull request on our GitHub repository. We appreciate your help in making this plugin more robust and useful. diff --git a/packages/prettier-plugin-zh/src/options.ts b/packages/prettier-plugin-zh/src/options.ts index 59778c6..f89471c 100644 --- a/packages/prettier-plugin-zh/src/options.ts +++ b/packages/prettier-plugin-zh/src/options.ts @@ -9,4 +9,10 @@ export const options: Record = { description: 'Provide space around alphabet.', type: 'boolean', }, + spaceAroundNumber: { + category: 'Global', + default: true, + description: 'Provide space around number.', + type: 'boolean', + }, } diff --git a/packages/prettier-plugin-zh/src/parsers/index.ts b/packages/prettier-plugin-zh/src/parsers/index.ts index 7994d16..5c1e367 100644 --- a/packages/prettier-plugin-zh/src/parsers/index.ts +++ b/packages/prettier-plugin-zh/src/parsers/index.ts @@ -4,18 +4,19 @@ import { defaultTransform, transformMarkdown } from '../transforms' import { getBasePlugins } from './base-parser' -import type {ParserFormat } from './base-parser'; +import type { Transform } from '../transforms/types' +import type { ParserFormat } from './base-parser' const base = getBasePlugins() -export function createParser(parserFormat: ParserFormat, transform = defaultTransform): Parser { +export function createParser(parserFormat: ParserFormat, transform: Transform = defaultTransform): Parser { return { ...base.parsers[parserFormat], async parse(text, options) { const original = base.parsers[parserFormat] const ast = await original.parse(text, options) - transform(ast) + transform(ast, options) return ast }, diff --git a/packages/prettier-plugin-zh/src/transforms/default-transform.ts b/packages/prettier-plugin-zh/src/transforms/default-transform.ts new file mode 100644 index 0000000..a20bc1c --- /dev/null +++ b/packages/prettier-plugin-zh/src/transforms/default-transform.ts @@ -0,0 +1,3 @@ +import type { Transform } from './types' + +export const defaultTransform: Transform = ast => ast diff --git a/packages/prettier-plugin-zh/src/transforms/index.ts b/packages/prettier-plugin-zh/src/transforms/index.ts index 6256a20..bbe5364 100644 --- a/packages/prettier-plugin-zh/src/transforms/index.ts +++ b/packages/prettier-plugin-zh/src/transforms/index.ts @@ -1,5 +1,2 @@ -import type { AST } from 'prettier' - +export * from './default-transform' export * from './transform-markdown' - -export const defaultTransform = (ast: AST): AST => ast diff --git a/packages/prettier-plugin-zh/src/transforms/transform-markdown.ts b/packages/prettier-plugin-zh/src/transforms/transform-markdown.ts index 93e187d..88b4d8a 100644 --- a/packages/prettier-plugin-zh/src/transforms/transform-markdown.ts +++ b/packages/prettier-plugin-zh/src/transforms/transform-markdown.ts @@ -1,13 +1,14 @@ -import { spaceAroundAlphabet } from 'core' - -import type { AST } from 'prettier' +import { spaceAroundAlphabet, spaceAroundNumber } from 'core' import { traverseChildren } from '../utils' -export function transformMarkdown(ast: AST) { +import type { Transform } from './types' + +export const transformMarkdown: Transform = (ast, options) => { traverseChildren(ast, child => { if (child.type === 'text') { - child.value = spaceAroundAlphabet(child.value) + if (options.spaceAroundAlphabet) child.value = spaceAroundAlphabet(child.value) + if (options.spaceAroundNumber) child.value = spaceAroundNumber(child.value) } }) } diff --git a/packages/prettier-plugin-zh/src/transforms/types.ts b/packages/prettier-plugin-zh/src/transforms/types.ts new file mode 100644 index 0000000..e68ac5c --- /dev/null +++ b/packages/prettier-plugin-zh/src/transforms/types.ts @@ -0,0 +1,5 @@ +import type { AST, ParserOptions } from 'prettier' + +import type { ZhOptions } from '../types' + +export type Transform = (text: AST, options: ParserOptions & ZhOptions) => void diff --git a/packages/prettier-plugin-zh/src/types.ts b/packages/prettier-plugin-zh/src/types.ts index 39b7ec3..db68bbf 100644 --- a/packages/prettier-plugin-zh/src/types.ts +++ b/packages/prettier-plugin-zh/src/types.ts @@ -1,3 +1,4 @@ export interface ZhOptions { - spaceAroundAlphabet: boolean + spaceAroundAlphabet?: boolean + spaceAroundNumber?: boolean } diff --git a/packages/prettier-plugin-zh/tests/fixtures/space-around-alphabet.md b/packages/prettier-plugin-zh/tests/fixtures/space-around-alphabet.md deleted file mode 100644 index 547994c..0000000 --- a/packages/prettier-plugin-zh/tests/fixtures/space-around-alphabet.md +++ /dev/null @@ -1,9 +0,0 @@ -# 标题 Title - -- 名称 Name 中国 China - -ZH - -- 名称 Name 美国 American - -EN diff --git a/packages/prettier-plugin-zh/tests/fixtures/space-around.md b/packages/prettier-plugin-zh/tests/fixtures/space-around.md new file mode 100644 index 0000000..d28295c --- /dev/null +++ b/packages/prettier-plugin-zh/tests/fixtures/space-around.md @@ -0,0 +1,9 @@ +# 标题 Title + +- 名称Name中国China面积960 + +ZH + +- 名称Name美国American面积937 + +EN diff --git a/packages/prettier-plugin-zh/tests/fixtures/space-around-alphabet.mdx b/packages/prettier-plugin-zh/tests/fixtures/space-around.mdx similarity index 100% rename from packages/prettier-plugin-zh/tests/fixtures/space-around-alphabet.mdx rename to packages/prettier-plugin-zh/tests/fixtures/space-around.mdx From b50f33b13b728741670876c0399142c486705c0e Mon Sep 17 00:00:00 2001 From: nnecec Date: Mon, 24 Jul 2023 18:38:43 +0800 Subject: [PATCH 2/3] build: auto changesets --- .github/workflows/ci.yml | 24 ++++++++++++++++++++++++ .github/workflows/release.yml | 34 ++++++++++++++++++++++++++++++++++ .npmrc | 1 + package.json | 14 +++++++++----- pnpm-workspace.yaml | 4 ++-- turbo.json | 2 +- 6 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 .github/workflows/release.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..3e82b45 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,24 @@ +name: Node CI + +on: [pull_request] + +jobs: + build: + timeout-minutes: 20 + + runs-on: ubuntu-latest + + steps: + - name: Checkout Repo + uses: actions/checkout@v2 + + - name: Use Node.js 16 + uses: actions/setup-node@v1 + with: + node-version: 16.x + + - name: Install Dependencies + run: yarn --frozen-lockfile + + - name: Test + run: yarn test diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..afb087c --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,34 @@ +name: Release + +on: + push: + branches: + - main + +concurrency: ${{ github.workflow }}-${{ github.ref }} + +jobs: + release: + name: Release + runs-on: ubuntu-latest + steps: + - name: Checkout Repo + uses: actions/checkout@v3 + + - name: Setup Node.js 16.x + uses: actions/setup-node@v3 + with: + node-version: 16.x + + - name: Install Dependencies + run: pnpm install + + - name: Create Release Pull Request or Publish to npm + id: changesets + uses: changesets/action@v1 + with: + # This expects you to have a script called release which does a build for your packages and calls changeset publish + publish: pnpm run release + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/.npmrc b/.npmrc index ded82e2..797065e 100644 --- a/.npmrc +++ b/.npmrc @@ -1 +1,2 @@ +//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN} auto-install-peers = true diff --git a/package.json b/package.json index ca5ac45..e4da64d 100644 --- a/package.json +++ b/package.json @@ -3,9 +3,16 @@ "private": true, "scripts": { "build": "turbo run build", + "changeset": "changeset", "dev": "turbo run dev", "format": "prettier --write \"**/*.{ts,tsx,md}\"", - "lint": "turbo run lint" + "lint": "turbo run lint", + "prerelease": "pnpm clean && pnpm install && pnpm build", + "release": "pnpm run prerelease && changeset publish", + "test": "turbo run test" + }, + "dependencies": { + "@changesets/cli": "^2.26.2" }, "devDependencies": { "@nnecec/eslint-config": "^0.5.1", @@ -16,8 +23,5 @@ "prettier-plugin-zh": "workspace:*", "turbo": "^1.10.9" }, - "packageManager": "pnpm@8.6.0", - "dependencies": { - "@changesets/cli": "^2.26.2" - } + "packageManager": "pnpm@8.6.0" } diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 3ff5faa..44665b7 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -1,3 +1,3 @@ packages: - - "apps/*" - - "packages/*" + - 'apps/**' + - 'packages/**' diff --git a/turbo.json b/turbo.json index ec4d016..dd482c1 100644 --- a/turbo.json +++ b/turbo.json @@ -4,7 +4,7 @@ "pipeline": { "build": { "dependsOn": ["^build"], - "outputs": [".next/**", "!.next/cache/**"] + "outputs": [".next/**", "!.next/cache/**", "dist/**", "build/**"] }, "lint": {}, "dev": { From 8e045c588ece5a1e229e0782b93280888560994a Mon Sep 17 00:00:00 2001 From: nnecec Date: Tue, 25 Jul 2023 00:56:05 +0800 Subject: [PATCH 3/3] test: init test --- .github/workflows/ci.yml | 13 +++++++++---- .github/workflows/release.yml | 14 +++++++++++--- .npmrc | 3 ++- apps/website/tsconfig.json | 17 +++-------------- package.json | 2 ++ packages/core/package.json | 3 ++- packages/eslint-plugin-zh/package.json | 4 +++- packages/prettier-plugin-zh/package.json | 3 ++- pnpm-workspace.yaml | 4 ++-- turbo.json | 3 +++ 10 files changed, 39 insertions(+), 27 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3e82b45..487e7a3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,9 +1,9 @@ -name: Node CI +name: CI Test on: [pull_request] jobs: - build: + test: timeout-minutes: 20 runs-on: ubuntu-latest @@ -17,8 +17,13 @@ jobs: with: node-version: 16.x + - name: Use pnpm 8 + uses: pnpm/action-setup@v2 + with: + version: 8 + - name: Install Dependencies - run: yarn --frozen-lockfile + run: pnpm install --frozen-lockfile - name: Test - run: yarn test + run: pnpm test diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index afb087c..a2301a4 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -20,15 +20,23 @@ jobs: with: node-version: 16.x + - uses: pnpm/action-setup@v2 + with: + version: 8 + - name: Install Dependencies - run: pnpm install + run: pnpm install --frozen-lockfile + + - name: Build + run: pnpm build - - name: Create Release Pull Request or Publish to npm + - name: Create Release Pull Request or Release to npm id: changesets uses: changesets/action@v1 with: # This expects you to have a script called release which does a build for your packages and calls changeset publish - publish: pnpm run release + bump: pnpm bump + release: pnpm run release env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} NPM_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/.npmrc b/.npmrc index 797065e..0d935a4 100644 --- a/.npmrc +++ b/.npmrc @@ -1,2 +1,3 @@ -//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN} auto-install-peers = true +//registry.npmjs.org/:_authToken=$NPM_TOKEN + diff --git a/apps/website/tsconfig.json b/apps/website/tsconfig.json index 3df13e8..b3af553 100644 --- a/apps/website/tsconfig.json +++ b/apps/website/tsconfig.json @@ -5,11 +5,7 @@ "name": "next" } ], - "lib": [ - "dom", - "dom.iterable", - "esnext" - ], + "lib": ["dom", "dom.iterable", "esnext"], "allowJs": true, "skipLibCheck": true, "strict": false, @@ -23,13 +19,6 @@ "isolatedModules": true, "jsx": "preserve" }, - "include": [ - "next-env.d.ts", - "**/*.ts", - "**/*.tsx", - ".next/types/**/*.ts" - ], - "exclude": [ - "node_modules" - ] + "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], + "exclude": ["node_modules"] } diff --git a/package.json b/package.json index e4da64d..936990d 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,9 @@ "private": true, "scripts": { "build": "turbo run build", + "bump": "changeset version", "changeset": "changeset", + "ci:publish": "pnpm publish -r", "dev": "turbo run dev", "format": "prettier --write \"**/*.{ts,tsx,md}\"", "lint": "turbo run lint", diff --git a/packages/core/package.json b/packages/core/package.json index e5314ee..3a5a042 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -26,7 +26,8 @@ "scripts": { "build": "tsup src/index.ts --format esm,cjs --dts", "dev": "tsup src/index.ts --format esm,cjs --watch --dts", - "clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist" + "clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist", + "test": "echo \"Error: no test specified\" && exit 0" }, "devDependencies": { "@nnecec/tsconfig": "^0.1.6", diff --git a/packages/eslint-plugin-zh/package.json b/packages/eslint-plugin-zh/package.json index e81fcb6..e84a64e 100644 --- a/packages/eslint-plugin-zh/package.json +++ b/packages/eslint-plugin-zh/package.json @@ -23,7 +23,9 @@ "files": [ "./dist" ], - "scripts": {}, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 0" + }, "devDependencies": { "eslint": "^8.45.0" }, diff --git a/packages/prettier-plugin-zh/package.json b/packages/prettier-plugin-zh/package.json index 3102b26..8c0b9d0 100644 --- a/packages/prettier-plugin-zh/package.json +++ b/packages/prettier-plugin-zh/package.json @@ -25,7 +25,8 @@ "scripts": { "build": "tsup src/index.ts --format esm,cjs --dts", "dev": "tsup src/index.ts --format esm,cjs --watch --dts", - "clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist" + "clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist", + "test": "echo \"Error: no test specified\" && exit 0" }, "devDependencies": { "@nnecec/tsconfig": "^0.1.6", diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 44665b7..e9b0dad 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -1,3 +1,3 @@ packages: - - 'apps/**' - - 'packages/**' + - 'apps/*' + - 'packages/*' diff --git a/turbo.json b/turbo.json index dd482c1..14052b6 100644 --- a/turbo.json +++ b/turbo.json @@ -10,6 +10,9 @@ "dev": { "cache": false, "persistent": true + }, + "test": { + "cache": false } } }