Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(prettier-plugin-zh): support spaceAroundNumber #1

Merged
merged 3 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fifty-hairs-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'prettier-plugin-zh': minor
---

support space around number
29 changes: 29 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: CI Test

on: [pull_request]

jobs:
test:
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: Use pnpm 8
uses: pnpm/action-setup@v2
with:
version: 8

- name: Install Dependencies
run: pnpm install --frozen-lockfile

- name: Test
run: pnpm test
42 changes: 42 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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

- uses: pnpm/action-setup@v2
with:
version: 8

- name: Install Dependencies
run: pnpm install --frozen-lockfile

- name: Build
run: pnpm build

- 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
bump: pnpm bump
release: pnpm run release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
auto-install-peers = true
//registry.npmjs.org/:_authToken=$NPM_TOKEN

17 changes: 3 additions & 14 deletions apps/website/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
"name": "next"
}
],
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
Expand All @@ -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"]
}
16 changes: 11 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@
"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"
"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",
Expand All @@ -16,8 +25,5 @@
"prettier-plugin-zh": "workspace:*",
"turbo": "^1.10.9"
},
"packageManager": "[email protected]",
"dependencies": {
"@changesets/cli": "^2.26.2"
}
"packageManager": "[email protected]"
}
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/rules/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './space-around-alphabet'
export * from './space-around'
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -17,12 +17,19 @@ 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)
return newContent
}
return text
}

export function spaceAroundAlphabet(text: string) {
return spaceAround(text, /AZ|ZA/g)
}
export function spaceAroundNumber(text: string) {
return spaceAround(text, /NZ|ZN/g)
}
2 changes: 1 addition & 1 deletion packages/core/src/utils/convert-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
4 changes: 3 additions & 1 deletion packages/eslint-plugin-zh/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
"files": [
"./dist"
],
"scripts": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 0"
},
"devDependencies": {
"eslint": "^8.45.0"
},
Expand Down
3 changes: 2 additions & 1 deletion packages/prettier-plugin-zh/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 15 additions & 5 deletions packages/prettier-plugin-zh/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions packages/prettier-plugin-zh/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ export const options: Record<keyof ZhOptions, SupportOption> = {
description: 'Provide space around alphabet.',
type: 'boolean',
},
spaceAroundNumber: {
category: 'Global',
default: true,
description: 'Provide space around number.',
type: 'boolean',
},
}
7 changes: 4 additions & 3 deletions packages/prettier-plugin-zh/src/parsers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { Transform } from './types'

export const defaultTransform: Transform = ast => ast
5 changes: 1 addition & 4 deletions packages/prettier-plugin-zh/src/transforms/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
import type { AST } from 'prettier'

export * from './default-transform'
export * from './transform-markdown'

export const defaultTransform = (ast: AST): AST => ast
11 changes: 6 additions & 5 deletions packages/prettier-plugin-zh/src/transforms/transform-markdown.ts
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
5 changes: 5 additions & 0 deletions packages/prettier-plugin-zh/src/transforms/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { AST, ParserOptions } from 'prettier'

import type { ZhOptions } from '../types'

export type Transform = (text: AST, options: ParserOptions & ZhOptions) => void
3 changes: 2 additions & 1 deletion packages/prettier-plugin-zh/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export interface ZhOptions {
spaceAroundAlphabet: boolean
spaceAroundAlphabet?: boolean
spaceAroundNumber?: boolean
}

This file was deleted.

9 changes: 9 additions & 0 deletions packages/prettier-plugin-zh/tests/fixtures/space-around.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# 标题 Title

- 名称Name中国China面积960

ZH

- 名称Name美国American面积937

EN
4 changes: 2 additions & 2 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
packages:
- "apps/*"
- "packages/*"
- 'apps/*'
- 'packages/*'
5 changes: 4 additions & 1 deletion turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
"pipeline": {
"build": {
"dependsOn": ["^build"],
"outputs": [".next/**", "!.next/cache/**"]
"outputs": [".next/**", "!.next/cache/**", "dist/**", "build/**"]
},
"lint": {},
"dev": {
"cache": false,
"persistent": true
},
"test": {
"cache": false
}
}
}
Loading