Skip to content

Commit d69b8c6

Browse files
committed
Feat: 改用 <caniuse-embed> 元素嵌入组件
reference: https://github.com/Lruihao/caniuse-embed-element
1 parent 6f1edaa commit d69b8c6

8 files changed

Lines changed: 85 additions & 36 deletions

File tree

README.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,26 @@ FixIt 主题 `v0.4.0` 及以上版本。
3232
# ... other partials
3333
```
3434

35+
组件配置:
36+
37+
```toml
38+
[params]
39+
# CanIUse Embed Element Configuration
40+
# See: https://github.com/Lruihao/caniuse-embed-element
41+
[params.caniuse_embed]
42+
# show the past N versions that match the feature, range is 0 - 5, default is 2
43+
past = 2
44+
# show the future N versions that match the feature, range is 0 - 3, default is 1
45+
future = 1
46+
# the origin of the caniuse embed data source, default is "https://caniuse.lruihao.cn"
47+
origin = "https://caniuse.lruihao.cn"
48+
# loading strategy for the iframe (eager or lazy), default is lazy
49+
loading = "lazy"
50+
# only production environment effective
51+
# e.g. https://unpkg.com/@cell-x/caniuse-embed-element/dist/caniuse-embed-element.iife.js
52+
cdn = ""
53+
```
54+
3555
## 使用 Shortcode
3656

3757
`caniuse` shortcode 有以下命名参数:
@@ -40,6 +60,7 @@ FixIt 主题 `v0.4.0` 及以上版本。
4060
- **past** _[可选]_(第二个位置参数)显示过去 N 个版本,范围是 `0 - 5`,默认为 `2`
4161
- **future** _[可选]_(第三个位置参数)显示未来 N 个版本,范围是 `0 - 3`,默认为 `1`
4262
- **origin** _[可选]_(第四个位置参数)caniuse embed 数据源,例如:`https://caniuse-embed-x.vercel.app`
63+
- **loading** _[可选]_(第五个位置参数)iframe 的加载策略(`eager``lazy`),默认为 `lazy`
4364

4465
> 点击 `caniuse.com` 网站上功能左边 `#` 号,URL 中的 `pathname` 即为 `feature` 参数。
4566
@@ -53,12 +74,12 @@ FixIt 主题 `v0.4.0` 及以上版本。
5374

5475
## 参考
5576

77+
- [The CanIUse Embed — Add support tables to your site](https://caniuse.lruihao.cn/)
5678
- [Can I use… Support tables for HTML5, CSS3, etc](https://caniuse.com/)
57-
- [The CanIUse Embed — Add support tables to your site](https://caniuse-embed.vercel.app/)
58-
- [贡献指南 / 开发组件](https://fixit.lruihao.cn/zh-cn/contributing/components/)
5979

6080
## 致谢
6181

82+
- [Lruihao/caniuse-embed-element](https://github.com/Lruihao/caniuse-embed-element)
83+
- [pengzhanbo/caniuse-embed](https://github.com/pengzhanbo/caniuse-embed)
6284
- [mdn-browser-compat-data](https://github.com/mdn/browser-compat-data)
6385
- [Fyrd/caniuse](https://github.com/Fyrd/caniuse)
64-
- [pengzhanbo/caniuse-embed](https://github.com/pengzhanbo/caniuse-embed)

assets/css/shortcode-caniuse/index.scss

Lines changed: 0 additions & 9 deletions
This file was deleted.

assets/js/shortcode-caniuse.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
function setCanIUseEmbedsTheme(allCanIUseEmbeds, isDark) {
22
allCanIUseEmbeds.forEach(function (embed) {
3-
embed.setAttribute('data-theme', isDark ? 'dark' : 'light');
3+
embed.theme = isDark ? 'dark' : 'light';
44
});
55
}
66

77
function CanIUseShortcodeInit() {
88
if (typeof window.fixit?.switchThemeEventSet === 'object') {
9-
const allCanIUseEmbeds = document.querySelectorAll('.ciu-embed');
9+
const allCanIUseEmbeds = document.querySelectorAll('caniuse-embed');
1010
setCanIUseEmbedsTheme(allCanIUseEmbeds, window.fixit.isDark);
1111
window.fixit?.switchThemeEventSet.add(function (isDark) {
1212
setCanIUseEmbedsTheme(allCanIUseEmbeds, isDark);
@@ -20,4 +20,4 @@ if (document.readyState !== 'loading') {
2020
CanIUseShortcodeInit();
2121
} else {
2222
document.addEventListener('DOMContentLoaded', caniuseShortcodeInit, false);
23-
}
23+
}

assets/lib/shortcode-caniuse/caniuse-embed-element.iife.js

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/lib/shortcode-caniuse/embed.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

hugo.toml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11
[module]
22
[module.hugoVersion]
33
extended = true
4-
min = "0.127.0"
4+
min = "0.146.0"
5+
6+
[params]
7+
# CanIUse Embed Element Configuration
8+
# See: https://github.com/Lruihao/caniuse-embed-element
9+
[params.caniuse_embed]
10+
# show the past N versions that match the feature, range is 0 - 5, default is 2
11+
past = 2
12+
# show the future N versions that match the feature, range is 0 - 3, default is 1
13+
future = 1
14+
# the origin of the caniuse embed data source, default is "https://caniuse.lruihao.cn"
15+
origin = "https://caniuse.lruihao.cn"
16+
# loading strategy for the iframe (eager or lazy), default is lazy
17+
loading = "lazy"
18+
# only production environment effective
19+
# e.g. https://unpkg.com/@cell-x/caniuse-embed-element/dist/caniuse-embed-element.iife.js
20+
cdn = ""

layouts/_partials/inject/shortcode-caniuse.html

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
{{- if .HasShortcode "caniuse" -}}
22
{{- $fingerprint := .Site.Store.Get "fingerprint" -}}
3+
{{- $caniuseEmbedConfig := .Param "caniuse_embed" -}}
34

4-
{{- /* shortcode-caniuse.min.css */ -}}
5-
{{- $options := dict "Source" "css/shortcode-caniuse/index.scss" "Fingerprint" $fingerprint -}}
6-
{{- $toCSS := dict "targetPath" "css/shortcode-caniuse.min.css" "enableSourceMap" true -}}
7-
{{- $options = dict "Context" . "ToCSS" $toCSS | merge $options -}}
8-
{{- partial "plugin/style.html" $options -}}
9-
10-
{{- /* embed.js */ -}}
11-
{{- $source := resources.Get "lib/shortcode-caniuse/embed.js" -}}
12-
{{- if hugo.IsProduction | and .Site.Params.cdn -}}
13-
{{- $source = "https://caniuse.lruihao.cn/embed.js" -}}
5+
{{- /* caniuse-embed-element.iife.js */ -}}
6+
{{- $source := resources.Get "lib/shortcode-caniuse/caniuse-embed-element.iife.js" -}}
7+
{{- if hugo.IsProduction | and $caniuseEmbedConfig.cdn -}}
8+
{{- $source = $caniuseEmbedConfig.cdn -}}
149
{{- end -}}
15-
{{- /* debug */ -}}
16-
{{/* {{- $source = "http://localhost:4321/embed.js" -}} */}}
17-
{{- dict "Source" $source "Fingerprint" $fingerprint "Defer" true | dict "Page" . "Data" | partial "store/script.html" -}}
10+
{{- dict "Source" $source "Fingerprint" $fingerprint "Async" true "Defer" true | dict "Page" . "Data" | partial "store/script.html" -}}
1811

1912
{{- /* shortcode-caniuse.min.js */ -}}
2013
{{- $options := dict "targetPath" "js/shortcode-caniuse.min.js" "minify" hugo.IsProduction -}}

layouts/_shortcodes/caniuse.html

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
{{- /*
2-
reference https://github.com/pengzhanbo/caniuse-embed
2+
reference https://github.com/Lruihao/caniuse-embed-element
33
<feature>: Feature name
44
<past>: Show the past N versions that match the feature, range is 0 - 5, default is 2
55
<future>: Show the future N versions that match the feature, range is 0 - 3, default is 1
66
<origin>: The origin of the caniuse embed data source, default is "https://caniuse.lruihao.cn"
7+
<loading>: Loading strategy for the iframe (eager or lazy), default is lazy
78
*/ -}}
9+
{{- $caniuseEmbed := .Page.Param "caniuse_embed" -}}
810
{{- $feature := cond .IsNamedParams (.Get "feature") (.Get 0) -}}
9-
{{- $past := cond .IsNamedParams (.Get "past") (.Get 1) | default 2 -}}
10-
{{- $future := cond .IsNamedParams (.Get "future") (.Get 2) | default 1 -}}
11-
{{- $origin := cond .IsNamedParams (.Get "origin") (.Get 3) | default "https://caniuse.lruihao.cn" -}}
12-
{{- $meta := delimit (collections.Shuffle (slice "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "a" "b" "c" "d" "e" "f") | first 4) "" -}}
11+
{{- $past := cond .IsNamedParams (.Get "past") (.Get 1) | default $caniuseEmbed.past | default 2 -}}
12+
{{- $future := cond .IsNamedParams (.Get "future") (.Get 2) | default $caniuseEmbed.future | default 1 -}}
13+
{{- $origin := cond .IsNamedParams (.Get "origin") (.Get 3) | default $caniuseEmbed.origin | default "https://caniuse.lruihao.cn" -}}
14+
{{- $loading := cond .IsNamedParams (.Get "loading") (.Get 4) | default $caniuseEmbed.loading | default "lazy" -}}
1315

14-
<p class="ciu-embed" data-feature="{{ $feature }}" data-past="{{ $past }}" data-future="{{ $future }}" data-origin="{{ $origin }}" data-observer="true" data-theme="" data-meta="{{ $meta }}"></p>
16+
<caniuse-embed feature="{{ $feature }}" past="{{ $past }}" future="{{ $future }}" origin="{{ $origin }}"></caniuse-embed>
1517
{{- /* EOF */ -}}

0 commit comments

Comments
 (0)