From 536ed6110a5ee2211e46102632f6389a499d59a3 Mon Sep 17 00:00:00 2001
From: pengzhanbo
Date: Mon, 29 Apr 2024 00:35:26 +0800
Subject: [PATCH] feat(plugin-md-power): upgrade can-i-use
---
.../src/client/components/CanIUse.vue | 93 +++++++++++++++++++
.../src/client/composables/setupCanIUse.ts | 20 ----
.../src/node/features/caniuse.ts | 32 +++----
.../src/node/prepareConfigFile.ts | 11 ++-
4 files changed, 115 insertions(+), 41 deletions(-)
create mode 100644 plugins/plugin-md-power/src/client/components/CanIUse.vue
delete mode 100644 plugins/plugin-md-power/src/client/composables/setupCanIUse.ts
diff --git a/plugins/plugin-md-power/src/client/components/CanIUse.vue b/plugins/plugin-md-power/src/client/components/CanIUse.vue
new file mode 100644
index 00000000..c23310fc
--- /dev/null
+++ b/plugins/plugin-md-power/src/client/components/CanIUse.vue
@@ -0,0 +1,93 @@
+
+
+
+
+
+
+
+
+
diff --git a/plugins/plugin-md-power/src/client/composables/setupCanIUse.ts b/plugins/plugin-md-power/src/client/composables/setupCanIUse.ts
deleted file mode 100644
index 66bd33ba..00000000
--- a/plugins/plugin-md-power/src/client/composables/setupCanIUse.ts
+++ /dev/null
@@ -1,20 +0,0 @@
-let isBind = false
-export function setupCanIUse(): void {
- if (isBind)
- return
- isBind = true
-
- window.addEventListener('message', (message) => {
- const data = message.data
-
- if (typeof data === 'string' && data.includes('ciu_embed')) {
- const [, feature, height] = data.split(':')
- const el = document.querySelector(`.ciu_embed[data-feature="${feature}"]:not([data-skip])`)
- if (el) {
- const h = Number.parseInt(height) + 30
- ;(el.childNodes[0] as any).height = `${h}px`
- el.setAttribute('data-skip', 'true')
- }
- }
- })
-}
diff --git a/plugins/plugin-md-power/src/node/features/caniuse.ts b/plugins/plugin-md-power/src/node/features/caniuse.ts
index e420e69e..da0665f6 100644
--- a/plugins/plugin-md-power/src/node/features/caniuse.ts
+++ b/plugins/plugin-md-power/src/node/features/caniuse.ts
@@ -7,8 +7,11 @@ import type Token from 'markdown-it/lib/token.mjs'
import type { RuleBlock } from 'markdown-it/lib/parser_block.mjs'
import type { Markdown } from 'vuepress/markdown'
import container from 'markdown-it-container'
+import { customAlphabet } from 'nanoid'
import type { CanIUseMode, CanIUseOptions, CanIUseTokenMeta } from '../../shared/index.js'
+const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz', 5)
+
// @[caniuse]()
const minLength = 12
@@ -17,6 +20,7 @@ const START_CODES = [64, 91, 99, 97, 110, 105, 117, 115, 101]
// regexp to match the import syntax
const SYNTAX_RE = /^@\[caniuse(?:\s*?(embed|image)?(?:{([0-9,\-]*?)})?)\]\(([^)]*)\)/
+const UNDERLINE_RE = /_+/g
function createCanIUseRuleBlock(defaultMode: CanIUseMode): RuleBlock {
return (state, startLine, endLine, silent) => {
@@ -76,18 +80,16 @@ function resolveCanIUse({ feature, mode, versions }: CanIUseTokenMeta): string {
`
}
- const periods = resolveVersions(versions)
- const accessible = 'false'
- const image = 'none'
- const url = 'https://caniuse.bitsofco.de/embed/index.html'
- const src = `${url}?feat=${feature}&periods=${periods}&accessible-colours=${accessible}&image-base=${image}`
+ feature = feature.replace(UNDERLINE_RE, '_')
+ const { past, future } = resolveVersions(versions)
+ const meta = nanoid()
- return ``
+ return ``
}
-function resolveVersions(versions: string): string {
+function resolveVersions(versions: string): { past: number, future: number } {
if (!versions)
- return 'future_1,current,past_1,past_2'
+ return { past: 2, future: 1 }
const list = versions
.split(',')
@@ -97,16 +99,10 @@ function resolveVersions(versions: string): string {
list.push(0)
const uniq = [...new Set(list)].sort((a, b) => b - a)
- const result: string[] = []
- uniq.forEach((v) => {
- if (v < 0)
- result.push(`past_${Math.abs(v)}`)
- if (v === 0)
- result.push('current')
- if (v > 0)
- result.push(`future_${v}`)
- })
- return result.join(',')
+ return {
+ future: uniq[0],
+ past: Math.abs(uniq[uniq.length - 1]),
+ }
}
/**
diff --git a/plugins/plugin-md-power/src/node/prepareConfigFile.ts b/plugins/plugin-md-power/src/node/prepareConfigFile.ts
index 24a075cc..63c0d5a6 100644
--- a/plugins/plugin-md-power/src/node/prepareConfigFile.ts
+++ b/plugins/plugin-md-power/src/node/prepareConfigFile.ts
@@ -51,13 +51,18 @@ export async function prepareConfigFile(app: App, options: MarkdownPowerPluginOp
enhances.add(`app.component('LanguageRepl', LanguageRepl)`)
}
- enhances.add(`if (__VUEPRESS_SSR__) return`)
+ // enhances.add(`if (__VUEPRESS_SSR__) return`)
if (options.caniuse) {
- imports.add(`import { setupCanIUse } from '${CLIENT_FOLDER}composables/setupCanIUse.js'`)
- enhances.add(`router.afterEach(() => setupCanIUse())`)
+ imports.add(`import CanIUse from '${CLIENT_FOLDER}components/CanIUse.vue'`)
+ enhances.add(`app.component('CanIUseViewer', CanIUse)`)
}
+ // if (options.caniuse) {
+ // imports.add(`import { setupCanIUse } from '${CLIENT_FOLDER}composables/setupCanIUse.js'`)
+ // enhances.add(`router.afterEach(() => setupCanIUse())`)
+ // }
+
return app.writeTemp(
'md-power/config.js',
`\