Skip to content

Commit

Permalink
Improve the bilibili shortcode with more options (#1250)
Browse files Browse the repository at this point in the history
* add variables

* finished the initial dev of bilibili improvement

next is documentation

* rearrange bilibili options

* added documentation for improved bilibili shortcode

* fluent chinese
  • Loading branch information
CXwudi authored Apr 12, 2024
1 parent 2509ad2 commit 0b8fa13
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 6 deletions.
12 changes: 12 additions & 0 deletions exampleSite/content/posts/tests/bilibili-tests/index.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ hiddenFromSearch: true

<!--more-->

Simple case `{{</* bilibili BV1Sx411T7QQ */>}}`

{{< bilibili BV1Sx411T7QQ >}}

Play the 3rd page `{{</* bilibili id=BV1TJ411C7An p=3 */>}}`

{{< bilibili id=BV1TJ411C7An p=3 >}}

Enable autoplay with annoying unmute `{{</* bilibili id=BV1TJ411C7An p=3 autoplay=true muted=false */>}}`

{{< bilibili id=BV1TJ411C7An p=3 autoplay=true muted=false >}}

A player with everything diabled `{{</* bilibili id=BV1TJ411C7An p=3 autoplay=0 muted=1 danmaku=0 t=100 hasMuteButton=0 hideCoverInfo=1 hideDanmakuButton=1 noFullScreenButton=1 fjw=1 */>}}` (Looks like only `autoplay`, `muted`, `danmaku` and `t` are working)

{{< bilibili id=BV1TJ411C7An p=3 autoplay=0 muted=1 danmaku=0 t=100 hasMuteButton=0 hideCoverInfo=1 hideDanmakuButton=1 noFullScreenButton=1 fjw=1 >}}
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,43 @@ The rendered output looks like this:

{{< bilibili id=BV1TJ411C7An p=3 >}}

### Advanced Usage

The `bilibili` shortcode supports all named parameters shown in [this blog post](https://zyc420.com/6143.html)

Here is a list of all named parameters:

| Parameter Name | Position | Purpose | How to Use |
|---|---|---|---|
| id | 0 | Video BVID, required | BV1TJ411C7An |
| p | 1 | Video part (default 1) | Enter a number |
| autoplay | 2 | Auto-play (default no) | `1` or `true`: Enable, `0` or `false`: Disable |
| danmaku | 3 | Default danmaku switch (default on) | `1` or `true`: Enable, `0` or `false`: Disable |
| muted | 4 | Default muted (default no) | `1` or `true`: Enable, `0` or `false`: Disable |
| t | 5 | Default start time (default 0) | Enter the value directly, in seconds |

The following options seems not to work, but these are still added to the shortcode in case if they work in the future:

| Parameter Name | Position | Purpose | How to Use |
|---|---|---|---|
| hasMuteButton | 6 | Whether the mute button is displayed (default not displayed) | `1` or `true`: Enable, `0` or `false`: Disable |
| hideCoverInfo | 7 | Whether the information under the video cover like play count and danmaku count is displayed (default displayed) | `1` or `true`: Enable, `0` or `false`: Disable |
| hideDanmakuButton | 8 | Whether to hide the danmaku button (default not hidden) | `1` or `true`: Enable, `0` or `false`: Disable |
| noFullScreenButton | 9 | Whether to hide the full screen button (default displayed) | `1` or `true`: Enable, `0` or `false`: Disable |
| fjw | 10 | Whether to start memory play (default on) | `1` or `true`: Enable, `0` or `false`: Disable |

Example `bilibili` input with all named parameters:

```markdown
{{</* bilibili BV1TJ411C7An 3 0 0 1 30 0 1 1 1 1 */>}}
or
{{</* bilibili id=BV1TJ411C7An p=3 autoplay=0 danmaku=0 muted=1 t=30 hasMuteButton=0 hideCoverInfo=1 hideDanmakuButton=1 noFullScreenButton=1 fjw=1 */>}}
```

The rendered output looks like this:

{{< bilibili id=BV1TJ411C7An p=3 autoplay=0 danmaku=0 muted=1 t=30 hasMuteButton=0 hideCoverInfo=1 hideDanmakuButton=1 noFullScreenButton=1 fjw=1 >}}

## typeit

The `typeit` shortcode provides typing animation based on [TypeIt](https://typeitjs.com/).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,43 @@ https://www.bilibili.com/video/BV1TJ411C7An?p=3

{{< bilibili id=BV1TJ411C7An p=3 >}}

### 高级用法

`bilibili` shortcode 支持[此博客文章](https://zyc420.com/6143.html)中展示的所有命名参数。

以下是所有命名参数的列表:

| 参数名 | 参数位置 | 参数用途 | 使用方法 |
|---|---|---|---|
| id | 0 | 视频BVID,必须项 | BV1TJ411C7An |
| p | 1 | 视频分P(默认为1) | 输入数字 |
| autoplay | 2 | 是否自动播放(默认为否) | `1``true`:启用,`0``false`:关闭 |
| danmaku | 3 | 默认弹幕开关(默认为开启) | `1``true`:启用,`0``false`:关闭 |
| muted | 4 | 是否默认静音(默认为否) | `1``true`:启用,`0``false`:关闭 |
| t | 5 | 默认开始时间(默认为0) | 直接输入数值,单位为秒 |

以下选项目前似乎不起作用,但仍然被加进shortcode中,以希望未来这些选项能够被正常使用:

| 参数名 | 参数位置 | 参数用途 | 使用方法 |
|---|---|---|---|
| hasMuteButton | 6 | 一键静音按钮是否显示(默认不显示) | `1``true`:启用,`0``false`:关闭 |
| hideCoverInfo | 7 | 视频封面下方是否显示播放量弹幕量等信息(默认显示) | `1``true`:启用,`0``false`:关闭 |
| hideDanmakuButton | 8 | 是否隐藏弹幕按钮(默认不隐藏) | `1``true`:启用,`0``false`:关闭 |
| noFullScreenButton | 9 | 是否隐藏全屏按钮(默认显示) | `1``true`:启用,`0``false`:关闭 |
| fjw | 10 | 是否启用记忆播放(默认开启) | `1``true`:启用,`0``false`:关闭 |

一个带有所有命名参数的`bilibili`示例:

```markdown
{{</* bilibili BV1TJ411C7An 3 0 0 1 30 0 1 1 1 1 */>}}
或者
{{</* bilibili id=BV1TJ411C7An p=3 autoplay=0 danmaku=0 muted=1 t=30 hasMuteButton=0 hideCoverInfo=1 hideDanmakuButton=1 noFullScreenButton=1 fjw=1 */>}}
```

呈现的输出效果如下:

{{< bilibili id=BV1TJ411C7An p=3 autoplay=0 danmaku=0 muted=1 t=30 hasMuteButton=0 hideCoverInfo=1 hideDanmakuButton=1 noFullScreenButton=1 fjw=1 >}}

## typeit

`typeit` shortcode 基于 [TypeIt](https://typeitjs.com/) 提供了打字动画.
Expand Down
11 changes: 11 additions & 0 deletions layouts/partials/function/convertBoolToNum.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{- $value := .value -}}
{{- if or (eq .value 1) (eq .value 0) -}}
{{/* Do nothing */}}
{{- else if eq (lower .value) "true" -}}
{{- $value = 1 -}}
{{- else if eq (lower .value) "false" -}}
{{- $value = 0 -}}
{{- else -}}
{{- errorf "Invalid value: %s. Expected true, false, 1, or 0." .value -}}
{{- end -}}
{{- return $value -}}
84 changes: 78 additions & 6 deletions layouts/shortcodes/bilibili.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,79 @@
{{- $bvid := cond .IsNamedParams (.Get "id") (.Get 0) -}}
{{- $page := cond .IsNamedParams (.Get "p") (.Get 1) | default 1 -}}

{{- $autoplayRaw := cond .IsNamedParams (.Get "autoplay") (.Get 2) -}}
{{- $autoplayPara := "" -}}
{{- if not (eq $autoplayRaw "") -}}
{{- $autoplay := partial "function/convertBoolToNum.html" (dict "value" $autoplayRaw) -}}
{{- $autoplayPara = printf "&autoplay=%d" $autoplay -}}
{{/* {{- warnf "The 'autoplay' parameter is set." -}} */}}
{{- else -}}
{{- $autoplayPara = "&autoplay=0" -}}
{{/* {{- warnf "The 'autoplay' parameter is not set. Default to 0." -}} */}}
{{- end -}}

{{/* Optional parameters */}}
{{- $danmakuRaw := cond .IsNamedParams (.Get "danmaku") (.Get 3) -}}
{{- $danmakuPara := "" -}}
{{- if not (eq $danmakuRaw "") -}}
{{- $danmaku := partial "function/convertBoolToNum.html" (dict "value" $danmakuRaw) -}}
{{- $danmakuPara = printf "&danmaku=%d" $danmaku -}}
{{- end -}}

{{- $mutedRaw := cond .IsNamedParams (.Get "muted") (.Get 4) -}}
{{- $mutedPara := "" -}}
{{- if not (eq $mutedRaw "") -}}
{{/* {{- warnf "The 'muted' parameter is set." -}} */}}
{{- $muted := partial "function/convertBoolToNum.html" (dict "value" $mutedRaw) -}}
{{- $mutedPara = printf "&muted=%d" $muted -}}
{{- end -}}

{{- $t := cond .IsNamedParams (.Get "t") (.Get 5) -}}
{{- $tPara := "" -}}
{{- if not (eq $t "") -}}
{{- $tPara = printf "&t=%d" $t -}}
{{- end -}}

{{/* These options seem not working anymore, but we added in case if these options works again */}}
{{- $hasMuteButtonRaw := cond .IsNamedParams (.Get "hasMuteButton") (.Get 6) -}}
{{- $hasMuteButtonPara := "" -}}
{{- if not (eq $hasMuteButtonRaw "") -}}
{{- $hasMuteButton := partial "function/convertBoolToNum.html" (dict "value" $hasMuteButtonRaw) -}}
{{- $hasMuteButtonPara = printf "&hasMuteButton=%d" $hasMuteButton -}}
{{- end -}}

{{- $hideCoverInfoRaw := cond .IsNamedParams (.Get "hideCoverInfo") (.Get 7) -}}
{{- $hideCoverInfoPara := "" -}}
{{- if not (eq $hideCoverInfoRaw "") -}}
{{- $hideCoverInfo := partial "function/convertBoolToNum.html" (dict "value" $hideCoverInfoRaw) -}}
{{- $hideCoverInfoPara = printf "&hideCoverInfo=%d" $hideCoverInfo -}}
{{- end -}}

{{- $hideDanmakuButtonRaw := cond .IsNamedParams (.Get "hideDanmakuButton") (.Get 8) -}}
{{- $hideDanmakuButtonPara := "" -}}
{{- if not (eq $hideDanmakuButtonRaw "") -}}
{{- $hideDanmakuButton := partial "function/convertBoolToNum.html" (dict "value" $hideDanmakuButtonRaw) -}}
{{- $hideDanmakuButtonPara = printf "&hideDanmakuButton=%d" $hideDanmakuButton -}}
{{- end -}}

{{- $noFullScreenButtonRaw := cond .IsNamedParams (.Get "noFullScreenButton") (.Get 9) -}}
{{- $noFullScreenButtonPara := "" -}}
{{- if not (eq $noFullScreenButtonRaw "") -}}
{{- $noFullScreenButton := partial "function/convertBoolToNum.html" (dict "value" $noFullScreenButtonRaw) -}}
{{- $noFullScreenButtonPara = printf "&noFullScreenButton=%d" $noFullScreenButton -}}
{{- end -}}

{{- $fjwRaw := cond .IsNamedParams (.Get "fjw") (.Get 10) -}}
{{- $fjwPara := "" -}}
{{- if not (eq $fjwRaw "") -}}
{{- $fjw := partial "function/convertBoolToNum.html" (dict "value" $fjwRaw) -}}
{{- $fjwPara = printf "&fjw=%d" $fjw -}}
{{- end -}}

{{/* TODO: make the 'player.bilibili.com/player.html' part configurable. This URL can be replaced by '//www.bilibili.com/blackboard/html5mobileplayer.html' to get a mobile version of the iframe player, which is even cleaner. See https://blog.zezeshe.com/archives/use-bilibili-iframe-player.html */}}

{{- $url := printf "//player.bilibili.com/player.html?bvid=%s&page=%v%s%s%s%s%s%s%s%s%s" $bvid $page $autoplayPara $danmakuPara $mutedPara $tPara $hasMuteButtonPara $hideCoverInfoPara $hideDanmakuButtonPara $noFullScreenButtonPara $fjwPara -}}

<div class="bilibili">
{{- if .IsNamedParams -}}
<iframe src="//player.bilibili.com/player.html?bvid={{ .Get `id` }}&page={{ .Get `p` | default 1 }}&autoplay=0" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true"></iframe>
{{- else -}}
<iframe src="//player.bilibili.com/player.html?bvid={{ .Get 0 }}&page={{ .Get 1 | default 1 }}&autoplay=0" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true"></iframe>
{{- end -}}
</div>
<iframe src="{{ $url }}" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true"></iframe>
</div>

0 comments on commit 0b8fa13

Please sign in to comment.