Skip to content

Commit

Permalink
feat(plugin-caniuse): 新增 浏览器版本支持列表选项
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhanbo committed Dec 30, 2023
1 parent d32cd37 commit d537d5a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
3 changes: 2 additions & 1 deletion plugins/plugin-caniuse/src/client/resolveCanIUse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ export function resolveCanIUse(): void {

if (typeof data === 'string' && data.includes('ciu_embed')) {
const [, feature, height] = data.split(':')
const el = document.querySelector(`.ciu_embed[data-feature="${feature}"]`)
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')
}
}
})
Expand Down
6 changes: 4 additions & 2 deletions plugins/plugin-caniuse/src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ export function caniusePlugin({
const render = (tokens: Token[], index: number): string => {
const token = tokens[index]
if (token.nesting === 1) {
const feature = token.info.trim().slice(type.length).trim() || ''
return feature ? resolveCanIUse(feature, mode) : ''
const info = token.info.trim().slice(type.length).trim() || ''
const feature = info.split(/\s+/)[0]
const versions = info.match(/\{(.*)\}/)?.[1] || ''
return feature ? resolveCanIUse(feature, mode, versions) : ''
}
else {
return ''
Expand Down
28 changes: 26 additions & 2 deletions plugins/plugin-caniuse/src/node/resolveCanIUse.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { CanIUseMode } from '../shared/index.js'

export function resolveCanIUse(feature: string, mode: CanIUseMode): string {
export function resolveCanIUse(feature: string, mode: CanIUseMode, versions: string): string {
if (!feature)
return ''

Expand All @@ -12,11 +12,35 @@ export function resolveCanIUse(feature: string, mode: CanIUseMode): string {
</picture>`
}

const periods = 'future_2,future_1,current,past_1,past_2'
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}`

return `<div class="ciu_embed" style="margin:16px 0" data-feature="${feature}"><iframe src="${src}" frameborder="0" width="100%" height="400px"></iframe></div>`
}

function resolveVersions(versions: string): string {
if (!versions)
return 'future_1,current,past_1,past_2'

const list = versions
.split(',')
.map(v => Number(v.trim()))
.filter(v => !Number.isNaN(v) && v >= -5 && v <= 3)

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(',')
}

0 comments on commit d537d5a

Please sign in to comment.