-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
112 lines (102 loc) · 2.53 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const { merge } = require("lodash");
const { readFileSync } = require("node:fs");
const path = require("node:path");
const nunjucks = require("nunjucks");
const css = hexo.extend.helper.get("css").bind(hexo);
const js = hexo.extend.helper.get("js").bind(hexo);
/**
* @type {{js?: string; css?: string}}
*/
const swiperUserConfig = hexo.config.swiper;
const swiperGlobalConfig = merge(
{
js: "https://cdn.jsdelivr.net/npm/[email protected]/swiper-bundle.min.js",
css: "https://cdn.jsdelivr.net/npm/[email protected]/swiper-bundle.min.css",
},
swiperUserConfig
);
/**
* create random str that we can use it to distinguish swiper instance.
* @returns {string}
*/
const createRandomId = () => {
return Math.random().toString(36).slice(-8);
};
hexo.extend.tag.register(
"swiper",
(args, content) => {
const random = createRandomId();
const id = `swiperInst-${random}`;
const data = hexo.render.renderSync({
text: content,
});
const str = nunjucks.renderString(
readFileSync(path.resolve(__dirname, "src", "swiper.njk"), "utf8"),
{
swiperId: id,
direction: "horizontal",
swiperItemData: data,
}
);
return str;
},
{
ends: true,
}
);
hexo.extend.tag.register(
"swiperItem",
(args, content) => {
const data = hexo.render.renderSync({
text: content,
engine: "markdown",
});
return `<div class="swiper-slide">${data}</div>`;
},
{
ends: true,
}
);
hexo.extend.tag.register("swiperImageItem", (args, content) => {
const [src, ratio = "1.77778"] = args;
return `<div class="swiper-slide">
<img
class="swiper-slide-img"
src=${src}
style="aspect-ratio: ${ratio}"
/>
</div>`;
});
// insert swiper js
hexo.extend.injector.register("body_end", () => {
const { js: jsUrl } = swiperGlobalConfig;
return js(jsUrl);
});
// insert swiper css
hexo.extend.injector.register("body_end", () => {
const { css: cssUrl } = swiperGlobalConfig;
return css(cssUrl);
});
hexo.extend.injector.register("body_end", () => {
const { theme } = hexo.config;
if (theme === "next") {
return `<style>
:root {
--swiper-theme-color: var(--theme-color);
--swiper-pagination-bottom: 0;
}
.swiper {
padding-bottom: 32px;
margin-bottom: 20px;
}
.swiper .swiper-slide .swiper-slide-img {
display: block;
width: 100%;
object-fit: contain;
background: var(--body-bg-color);
margin: 0;
}
</style>`;
}
return "";
});