From 4fcbd5511e09478a10fbad6d0cf84a0e88ed10e6 Mon Sep 17 00:00:00 2001 From: pengzhanbo Date: Fri, 6 Sep 2024 00:17:53 +0800 Subject: [PATCH] style: lint fix --- docs/.vuepress/themes/composables/caniuse.ts | 4 +- .../themes/composables/theme-colors.ts | 20 ++++---- .../src/client/composables/size.ts | 24 +++++----- .../src/node/highlight/highlight.ts | 43 +++++++++-------- .../src/node/markdown/lineNumberPlugin.ts | 2 +- .../node/twoslash/renderer-floating-vue.ts | 46 +++++++++--------- theme/src/client/composables/home.ts | 48 +++++++++---------- theme/src/client/composables/outline.ts | 2 +- theme/src/node/prepare/prepareEncrypt.ts | 3 +- 9 files changed, 97 insertions(+), 95 deletions(-) diff --git a/docs/.vuepress/themes/composables/caniuse.ts b/docs/.vuepress/themes/composables/caniuse.ts index c6f33368..bead1ff9 100644 --- a/docs/.vuepress/themes/composables/caniuse.ts +++ b/docs/.vuepress/themes/composables/caniuse.ts @@ -15,7 +15,7 @@ const api = 'https://caniuse.pengzhanbo.cn/features.json' const pastVersions: SelectItem[] = [ { label: '不显示旧版本', value: '0' }, - ...Array(5).fill(0).map((_, i) => ({ + ...Array.from({ length: 5 }).fill(0).map((_, i) => ({ label: `旧版本(当前版本 - ${i + 1})`, value: `${i + 1}`, })), @@ -23,7 +23,7 @@ const pastVersions: SelectItem[] = [ const futureVersions: SelectItem[] = [ { label: '不显示未来版本', value: '0' }, - ...Array(3).fill(0).map((_, i) => ({ + ...Array.from({ length: 3 }).fill(0).map((_, i) => ({ label: `未来版本(当前版本 + ${i + 1})`, value: `${i + 1}`, })), diff --git a/docs/.vuepress/themes/composables/theme-colors.ts b/docs/.vuepress/themes/composables/theme-colors.ts index 3185d0ad..5572c854 100644 --- a/docs/.vuepress/themes/composables/theme-colors.ts +++ b/docs/.vuepress/themes/composables/theme-colors.ts @@ -114,16 +114,6 @@ export function setupThemeColors() { return content } - function resolveDefaultColors(type: 'light' | 'dark') { - return preset.map(group => ({ - name: group.name, - group: group.group.map(item => ({ - ...item, - value: DEFAULT_PRESET[type][item.key], - })), - })) - } - function reset() { lightColors.value = resolveDefaultColors('light') darkColors.value = resolveDefaultColors('dark') @@ -137,6 +127,16 @@ export function setupThemeColors() { }) } +function resolveDefaultColors(type: 'light' | 'dark') { + return preset.map(group => ({ + name: group.name, + group: group.group.map(item => ({ + ...item, + value: DEFAULT_PRESET[type][item.key], + })), + })) +} + export function useThemeColors() { const result = inject(themeColorSymbol) diff --git a/plugins/plugin-md-power/src/client/composables/size.ts b/plugins/plugin-md-power/src/client/composables/size.ts index de87b569..895261ea 100644 --- a/plugins/plugin-md-power/src/client/composables/size.ts +++ b/plugins/plugin-md-power/src/client/composables/size.ts @@ -31,18 +31,6 @@ export function useSize( const width = computed(() => toValue(options.width) || '100%') const height = ref('auto') - const getRadio = (ratio: number | string | undefined): number => { - if (typeof ratio === 'string') { - const [width, height] = ratio.split(':') - const parsedRadio = Number(width) / Number(height) - - if (!Number.isNaN(parsedRadio)) - return parsedRadio - } - - return typeof ratio === 'number' ? ratio : 16 / 9 - } - const getHeight = (width: number): string => { const height = toValue(options.height) const ratio = getRadio(toValue(options.ratio)) @@ -66,3 +54,15 @@ export function useSize( return { el, width, height, resize } } + +function getRadio(ratio: number | string | undefined): number { + if (typeof ratio === 'string') { + const [width, height] = ratio.split(':') + const parsedRadio = Number(width) / Number(height) + + if (!Number.isNaN(parsedRadio)) + return parsedRadio + } + + return typeof ratio === 'number' ? ratio : 16 / 9 +} diff --git a/plugins/plugin-shikiji/src/node/highlight/highlight.ts b/plugins/plugin-shikiji/src/node/highlight/highlight.ts index 3796b79e..0c76b8cc 100644 --- a/plugins/plugin-shikiji/src/node/highlight/highlight.ts +++ b/plugins/plugin-shikiji/src/node/highlight/highlight.ts @@ -31,27 +31,6 @@ export async function highlight( await options.shikiSetup?.(highlighter) const loadedLanguages = highlighter.getLoadedLanguages() - const removeMustache = (s: string, mustaches: Map) => { - return s.replace(mustacheRE, (match) => { - let marker = mustaches.get(match) - if (!marker) { - marker = nanoid() - mustaches.set(match, marker) - } - return marker - }) - } - - const restoreMustache = (s: string, mustaches: Map, twoslash: boolean) => { - mustaches.forEach((marker, match) => { - s = s.replaceAll(marker, match) - }) - - if (twoslash) - s = s.replace(/\{/g, '{') - - return `${s}\n` - } return (str: string, language: string, attrs: string = '') => { const lang = getLanguage(loadedLanguages, language, defaultLang) @@ -85,3 +64,25 @@ export async function highlight( } } } + +function removeMustache(s: string, mustaches: Map) { + return s.replace(mustacheRE, (match) => { + let marker = mustaches.get(match) + if (!marker) { + marker = nanoid() + mustaches.set(match, marker) + } + return marker + }) +} + +function restoreMustache(s: string, mustaches: Map, twoslash: boolean) { + mustaches.forEach((marker, match) => { + s = s.replaceAll(marker, match) + }) + + if (twoslash) + s = s.replace(/\{/g, '{') + + return `${s}\n` +} diff --git a/plugins/plugin-shikiji/src/node/markdown/lineNumberPlugin.ts b/plugins/plugin-shikiji/src/node/markdown/lineNumberPlugin.ts index 451e57dc..71192642 100644 --- a/plugins/plugin-shikiji/src/node/markdown/lineNumberPlugin.ts +++ b/plugins/plugin-shikiji/src/node/markdown/lineNumberPlugin.ts @@ -48,7 +48,7 @@ export function lineNumberPlugin(md: Markdown, { lineNumbers = true }: LineNumbe = Number(info.match(LINE_NUMBERS_START_REGEXP)?.[1] ?? 1) - 1 const lineNumbersStyle = `style="counter-reset:line-number ${startNumbers}"` - const lineNumbersCode = [...Array(lines.length)] + const lineNumbersCode = [...Array.from({ length: lines.length })] .map(() => `
`) .join('') diff --git a/plugins/plugin-shikiji/src/node/twoslash/renderer-floating-vue.ts b/plugins/plugin-shikiji/src/node/twoslash/renderer-floating-vue.ts index dd0a0988..4c43eb74 100644 --- a/plugins/plugin-shikiji/src/node/twoslash/renderer-floating-vue.ts +++ b/plugins/plugin-shikiji/src/node/twoslash/renderer-floating-vue.ts @@ -47,29 +47,6 @@ export function rendererFloatingVue(options: TwoslashFloatingVueRendererOptions 'theme': floatingVueTheme, } - function compose(parts: { token: Element | Text, popup: Element }): Element[] { - return [ - { - type: 'element', - tagName: 'span', - properties: {}, - children: [parts.token], - }, - { - type: 'element', - tagName: 'template', - properties: { - 'v-slot:popper': '{}', - }, - content: { - type: 'root', - children: [vPre(parts.popup)], - }, - children: [], - }, - ] - } - const rich = rendererRich({ classExtra: classCopyIgnore, ...options, @@ -142,6 +119,29 @@ export function rendererFloatingVue(options: TwoslashFloatingVueRendererOptions return rich } +function compose(parts: { token: Element | Text, popup: Element }): Element[] { + return [ + { + type: 'element', + tagName: 'span', + properties: {}, + children: [parts.token], + }, + { + type: 'element', + tagName: 'template', + properties: { + 'v-slot:popper': '{}', + }, + content: { + type: 'root', + children: [vPre(parts.popup)], + }, + children: [], + }, + ] +} + function vPre(el: T): T { if (el.type === 'element') { el.properties = el.properties || {} diff --git a/theme/src/client/composables/home.ts b/theme/src/client/composables/home.ts index 9e7053b2..df49031b 100644 --- a/theme/src/client/composables/home.ts +++ b/theme/src/client/composables/home.ts @@ -64,30 +64,6 @@ export function useHomeHeroTintPlate( return defaultTint }) - function toPlate(plate: number | string) { - return typeof plate === 'number' || Number(plate) === Number.parseInt(plate) - ? [plate, plate, plate].map(n => Number(n)) - : plate.includes(',') ? plate.replace(/\s/g, '').split(',').map(n => Number(n)) : [] - } - - function toTint([r, g, b]: number[]) { - return { r: toColor(r), g: toColor(g), b: toColor(b) } - } - - function toColor(num: number) { - const offset = 256 - num - return { value: num, offset: offset > 64 ? 64 : offset } - } - - function toNumber(tint: TintPlate): TintPlate { - Object.keys(tint).forEach((key) => { - const p = tint[key] - p.value = Number(p.value) - p.offset = Number(p.offset) - }) - return tint - } - onMounted(() => { if (canvas.value && enable.value) { ctx = canvas.value.getContext('2d')! @@ -135,3 +111,27 @@ export function useHomeHeroTintPlate( return (Math.floor(b.value + b.offset * Math.sin(5 * Math.sin(t / 9) + ((x - 100) * (x - 100) + (y - 100) * (y - 100)) / 1100))) } } + +function toPlate(plate: number | string) { + return typeof plate === 'number' || Number(plate) === Number.parseInt(plate) + ? [plate, plate, plate].map(n => Number(n)) + : plate.includes(',') ? plate.replace(/\s/g, '').split(',').map(n => Number(n)) : [] +} + +function toTint([r, g, b]: number[]) { + return { r: toColor(r), g: toColor(g), b: toColor(b) } +} + +function toColor(num: number) { + const offset = 256 - num + return { value: num, offset: offset > 64 ? 64 : offset } +} + +function toNumber(tint: TintPlate): TintPlate { + Object.keys(tint).forEach((key) => { + const p = tint[key] + p.value = Number(p.value) + p.offset = Number(p.offset) + }) + return tint +} diff --git a/theme/src/client/composables/outline.ts b/theme/src/client/composables/outline.ts index cf772c43..20adaccf 100644 --- a/theme/src/client/composables/outline.ts +++ b/theme/src/client/composables/outline.ts @@ -137,7 +137,7 @@ export function resolveHeaders(headers: MenuItem[], range?: ThemeOutline): MenuI resolvedHeaders.push({ element, link }) const ret: MenuItem[] = [] - // eslint-disable-next-line no-labels, no-restricted-syntax + // eslint-disable-next-line no-labels outer: for (let i = 0; i < headers.length; i++) { const cur = headers[i] if (i === 0) { diff --git a/theme/src/node/prepare/prepareEncrypt.ts b/theme/src/node/prepare/prepareEncrypt.ts index 05f04df4..fe14cad7 100644 --- a/theme/src/node/prepare/prepareEncrypt.ts +++ b/theme/src/node/prepare/prepareEncrypt.ts @@ -30,8 +30,9 @@ export async function prepareEncrypt(app: App, encrypt?: PlumeThemeEncrypt) { } } +const salt = () => genSaltSync(random(8, 16)) + function resolveEncrypt(encrypt?: PlumeThemeEncrypt): EncryptConfig { - const salt = () => genSaltSync(random(8, 16)) const admin = encrypt?.admin ? toArray(encrypt.admin) .filter(isStringLike)