Skip to content

Commit

Permalink
feat(prettier-plugin-zh): support no space between number and unit
Browse files Browse the repository at this point in the history
  • Loading branch information
nnecec committed Jul 27, 2023
1 parent 0705cb2 commit 8dd4743
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/core/src/rules/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './no-space-between-number-unit'
export * from './space-around'
Empty file.
7 changes: 7 additions & 0 deletions packages/core/src/rules/no-space-between-number-unit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function noSpaceBetweenNumberUnit(text: string, units?: string[]) {
if (!units || units.length === 0) return text

return text.replaceAll(new RegExp(`\\d+\\s+(${units.join('|')})`, 'g'), match => {
return match.replaceAll(' ', '')
})
}
2 changes: 1 addition & 1 deletion packages/core/src/rules/space-around.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { convertText } from '../utils'

function spaceAround(text: string, reg: RegExp) {
const convertedText = convertText(text)
console.log(text, convertText)

const boundaries: number[] = []

const close = convertedText.matchAll(reg)
Expand Down
Empty file.
1 change: 1 addition & 0 deletions packages/prettier-plugin-zh/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const defaultUnits = ['%', '°']
1 change: 1 addition & 0 deletions packages/prettier-plugin-zh/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './constants'
export { options } from './options'
export { parsers } from './parsers'
7 changes: 7 additions & 0 deletions packages/prettier-plugin-zh/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import type { SupportOption } from 'prettier'
import type { ZhOptions } from './types'

export const options: Record<keyof ZhOptions, SupportOption> = {
noSpaceBetweenNumberUnit: {
array: true,
category: 'Global',
default: [{ value: [] }],
description: 'Clear space between number and unit.',
type: 'string',
},
spaceAroundAlphabet: {
category: 'Global',
default: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { spaceAroundAlphabet, spaceAroundNumber } from 'core'
import { noSpaceBetweenNumberUnit, spaceAroundAlphabet, spaceAroundNumber } from 'core'

import { traverseChildren } from '../utils'

Expand All @@ -9,6 +9,9 @@ export const transformMarkdown: Transform = (ast, options) => {
if (child.type === 'text') {
if (options.spaceAroundAlphabet) child.value = spaceAroundAlphabet(child.value)
if (options.spaceAroundNumber) child.value = spaceAroundNumber(child.value)
if (options.noSpaceBetweenNumberUnit && options.noSpaceBetweenNumberUnit.length > 0) {
child.value = noSpaceBetweenNumberUnit(child.value, options.noSpaceBetweenNumberUnit)
}
}
})
}
1 change: 1 addition & 0 deletions packages/prettier-plugin-zh/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface ZhOptions {
noSpaceBetweenNumberUnit?: string[]
spaceAroundAlphabet?: boolean
spaceAroundNumber?: boolean
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
角度為 90 ° 的角,就是直角。新 MacBook Pro 有 15 % 的 CPU 性能提升。这数字是 3 . 1415
2 changes: 2 additions & 0 deletions prettier.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const { defaultUnits } = require('prettier-plugin-zh')
/**
* @type {import('prettier').Config}
*/
module.exports = {
...require('@nnecec/prettier-config'),
noSpaceBetweenNumberUnit: defaultUnits,
plugins: ['prettier-plugin-zh'],
}

0 comments on commit 8dd4743

Please sign in to comment.