0.9.6 • Published 5 months ago

hexo-tag-bilibili-card v0.9.6

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago
IssuesLicenseNPM
Github IssuesLicenseNPM Status

hexo-tag-bilibili-card

一个 Hexo 插件,在你的文章中插入哔哩哔哩卡片,样式模仿和借鉴自哔哩哔哩,基于 hexo-bilibili-card

NPM

安装

npm i hexo-tag-bilibili-card

预览

复制以下代码到 HTML 文件中查看

<script src="https://unpkg.com/hexo-tag-bilibili-card/components/bilibili-card/bilibili-card.js" async></script>
<bilibili-card vid="BV1y54y1a768" type="video" title="【UWP】手把手教你安装 UWP 安装包" author="where-where"
  cover="http://i2.hdslb.com/bfs/archive/41bc750cb5011bb036e008a716a89158c7eb7bb5.jpg" duration="05:21" views="2.2万"
  danmakus="4" comments="75" favorites="253" coins="106" likes="287" info-types="views danmakus"
  theme="system"></bilibili-card>

SVG 预览

使用

最低需求

  • 支持 Web Components
  • 支持 ES6 语法

使用插件

在你的文章中插入以下片段

{% bilibili_card $id $type $info-types $theme %}

其中

属性名描述可选值默认值示例
$id媒体 ID视频:AV, BV;专栏:CV;番剧:MD;音频:AU空,将跳过生成BV1y54y1a768
$type卡片类型video, article, user, live, bangumi, audio, dynamic, favorite, album自动识别 AV, BV, CV, MD, AU,识别失败视为 videovideo
$info-types显示信息views, danmakus, comments, favorites, coins, likes空,由 bilibili-card 分配默认值views danmakus
$theme样式system, light, dark空,由 bilibili-card 分配默认值system

完整示例

{% bilibili_card BV1y54y1a768 video 'views danmakus' system %}

然后你就可以看见文章中的卡片了

使用 Web Components

你也可以直接使用 Web Component 控件

在 Head 中引入

<script src="https://unpkg.com/hexo-tag-bilibili-card/components/bilibili-card/bilibili-card.js" async></script>

在文章中插入

<bilibili-card ...></bilibili-card>

其中

属性名描述默认值
vid媒体 ID
type卡片类型video
title卡片标题哔哩哔哩 (゜-゜)つロ 干杯~
author作者2233
cover封面图片地址
duration媒体时长??:??
views观看量0
danmakus弹幕数0
comments评论数0
favorites收藏数0
coins投币数0
likes点赞数0
info-types显示信息根据卡片类型分配
image-proxy图片代理地址https://images.weserv.nl/?url=
theme样式system

使用 Builder

你还可以使用 bilibiliCardBuilder 来生成 DOM 对象

<link rel="stylesheet" href="https://unpkg.com/hexo-tag-bilibili-card/components/bilibili-card/bilibili-card.css">
<div id="host"></div>
<script type="module">
  import { bilibiliCardBuilder } from "https://unpkg.com/hexo-tag-bilibili-card/components/bilibili-card-builder/bilibili-card-builder.esm.js";
  const card = bilibiliCardBuilder.createCard(
    undefined,
    "views danmakus",
    {
      vid: "BV1y54y1a768",
      title: "【UWP】手把手教你安装 UWP 安装包",
      author: "where-where",
      cover: "http://i2.hdslb.com/bfs/archive/41bc750cb5011bb036e008a716a89158c7eb7bb5.jpg",
      duration: "05:21",
      views: "2.2万",
      danmakus: "4",
      comments: "75",
      favorites: "253",
      coins: "106",
      likes: "287"
    }
  );
  document.getElementById("host").appendChild(card);
</script>

其中

/** 卡片类型 */
type cardType = "video" | "article" | "user" | "live" | "bangumi" | "audio" | "dynamic" | "favorite" | "album";
/** 样式 */
type themeType = "system" | "light" | "dark";
/** 卡片信息 */
type cardInfo =
    {
        /** 媒体 ID */
        vid: string;
        /** 卡片类型 */
        type: cardType;
        /** 卡片标题 */
        title: string;
        /** 作者 */
        author: string;
        /** 封面图片地址 */
        cover: string;
        /** 媒体时长 */
        duration: string;
        /** 观看量 */
        views: string | number;
        /** 弹幕数 */
        danmakus: string | number;
        /** 评论数 */
        comments: string | number;
        /** 收藏数 */
        favorites: string | number;
        /** 投币数 */
        coins: string | number;
        /** 点赞数 */
        likes: string | number;
    };

/** 哔哩哔哩控件生成器 */
declare interface IBilibiliCardBuilder {
    /**
     * 生成哔哩哔哩卡片控件外壳
     * @param imageProxy 图片代理地址
     * @param infoTypes 显示信息
     * @param 卡片信息
     * @param theme 样式
     * @returns 卡片控件
     */
    createHost(imageProxy: string, infoTypes: string, { vid, type, title, author, cover, duration, views, danmakus, comments, favorites, coins, likes }: cardInfo, theme: themeType): HTMLElement;
    /**
     * 使用自定义标签名生成哔哩哔哩卡片控件外壳
     * @param tagName 自定义标签名
     * @param imageProxy 图片代理地址
     * @param infoTypes 显示信息
     * @param 卡片信息
     * @param theme 样式
     * @returns 卡片控件
     */
    createHostWithTagName(tagName: string, imageProxy: string, infoTypes: string, { vid, type, title, author, cover, duration, views, danmakus, comments, favorites, coins, likes }: cardInfo, theme: themeType): HTMLElement;
    /**
     * 为哔哩哔哩卡片控件外壳生成卡片内容
     * @param element 控件外壳
     */
    praseElement(element: HTMLElement): void;
    /**
     * 生成哔哩哔哩卡片控件
     * @param imageProxy 图片代理地址
     * @param infoTypes 显示信息
     * @param 卡片信息
     * @param theme 样式
     * @returns 卡片控件
     */
    createCard(imageProxy: string, infoTypes: string, { vid, type, title, author, cover, duration, views, danmakus, comments, favorites, coins, likes }: cardInfo, theme: themeType): HTMLElement;
    /**
     * 使用自定义标签名生成哔哩哔哩卡片控件
     * @param tagName 自定义标签名
     * @param imageProxy 图片代理地址
     * @param infoTypes 显示信息
     * @param 卡片信息
     * @param theme 样式
     * @returns 卡片控件
     */
    createCardWithTagName(tagName: string, imageProxy: string, infoTypes: string, { vid, type, title, author, cover, duration, views, danmakus, comments, favorites, coins, likes }: cardInfo, theme: themeType): HTMLElement;
    /**
     * 注册哔哩哔哩卡片控件属性变化观察器
     * @param element 卡片控件
     */
    registerObserver(element: HTMLElement): void;
    /**
     * 当前生成器所用 Window 对象
     */
    window: Window;
}

/** 哔哩哔哩控件生成器 */
const bilibiliCardBuilder: IBilibiliCardBuilder;

/** 全局导出 */
globalThis.bilibiliCardBuilder = bilibiliCardBuilder;
/** CommonJS 导出 */
module.exports = bilibiliCardBuilder;
/** ES6 导出 */
export default bilibiliCardBuilder;

兼容性

请根据注释自行取舍

<!-- 补全不支持的依赖 -->
<script src="https://cdn.jsdelivr.net/npm/babel-polyfill/dist/polyfill.min.js"></script>
<!-- 补全 Web Components 支持 -->
<script src="https://cdn.jsdelivr.net/npm/@webcomponents/webcomponentsjs/webcomponents-loader.js"></script>
<!-- 补全不支持的语法 -->
<script src="https://cdn.jsdelivr.net/npm/@babel/standalone@7.11/babel.min.js"></script>
<!-- 设置 type 为 text/babel 以支持新语法 -->
<script src="https://cdn.jsdelivr.net/npm/hexo-tag-bilibili-card/components/bilibili-card/bilibili-card.js" type="text/babel" async></script>
<!-- 部分情况下控件无法正常自动插入 CSS 依赖,需要手动补充 -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-tag-bilibili-card/components/bilibili-card/bilibili-card.css">
<!-- 补全 CSS 变量支持 -->
<script src="https://cdn.jsdelivr.net/npm/css-vars-ponyfill/dist/css-vars-ponyfill.min.js"></script>
<script>
  cssVars({
    variables: {
      "--bilibili-accent-color": "#fb7299",
      "--bilibili-card-border-color": "#e7e7e7",
      "--bilibili-card-background-color": "white",
      "--bilibili-card-cover-color": "#c9c9c9",
      "--bilibili-card-cover-background-color": "#f4f4f4",
      "--bilibili-card-title-color": "#505050",
      "--bilibili-card-subtitle-color": "white",
      "--bilibili-card-subtitle-background-color": "rgba(0, 0, 0, .4)",
      "--bilibili-card-content-color": "#999"
    },
    watch: true
  });
</script>
<bilibili-card ...></bilibili-card>

配置

由于哔哩哔哩图片地址的跨域限制,因此需要配置图片代理,目前未找到大陆地区较为稳定的服务,默认使用的 https://images.weserv.nl/?url=,部分地区受到了 DNS 污染及反应过慢。建议参考 rsstt-img-relay 自建服务

bilibili_card:
  enable: true  # 是否启用,默认关
  image_proxy: https://images.weserv.nl/?url= # 图片代理
  inject_layouts: [default] # 需要注入的布局
  generator_assets: ["default"] # 需要生成的资源
  mode: component # 模式选择,component 为 Web 组件
                  # 其他为直接插入 HTML,样式可选 system, light, dark

引用及参考

参与人员

Contributors

0.9.6

5 months ago

0.9.5

9 months ago

0.8.4

1 year ago

0.8.3

1 year ago

0.7.2

1 year ago

0.7.1

1 year ago

0.7.0

1 year ago