vitepress-plugin-watermark 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +147 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +51 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 pengzhanbo
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# vitepress-plugin-watermark
|
|
2
|
+
|
|
3
|
+
Add watermark to your VitePress site.
|
|
4
|
+
|
|
5
|
+
在 vitepress 站点中添加水印。
|
|
6
|
+
|
|
7
|
+
## Usage
|
|
8
|
+
|
|
9
|
+
This plugin only provides a client-side `setupWatermark` function. No node-side configuration is needed.
|
|
10
|
+
|
|
11
|
+
**Installation:**
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
# npm
|
|
15
|
+
npm install -D vitepress-plugin-watermark
|
|
16
|
+
# pnpm
|
|
17
|
+
pnpm add -D vitepress-plugin-watermark
|
|
18
|
+
# yarn
|
|
19
|
+
yarn add -D vitepress-plugin-watermark
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
**Configuration:**
|
|
23
|
+
|
|
24
|
+
Create a custom Layout wrapper component and call `setupWatermark` inside its `<script setup>` block:
|
|
25
|
+
|
|
26
|
+
```vue
|
|
27
|
+
<!-- .vitepress/theme/Layout.vue -->
|
|
28
|
+
<script setup lang="ts">
|
|
29
|
+
import { setupWatermark } from 'vitepress-plugin-watermark'
|
|
30
|
+
import Theme from 'vitepress/theme'
|
|
31
|
+
import { h, useAttrs, useSlots } from 'vue'
|
|
32
|
+
|
|
33
|
+
const slots = useSlots()
|
|
34
|
+
const attrs = useAttrs()
|
|
35
|
+
|
|
36
|
+
const Layout = () => h(Theme.Layout, attrs, slots)
|
|
37
|
+
|
|
38
|
+
setupWatermark()
|
|
39
|
+
</script>
|
|
40
|
+
|
|
41
|
+
<template>
|
|
42
|
+
<Layout />
|
|
43
|
+
</template>
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Then register the custom Layout in your theme:
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
// .vitepress/theme/index.ts
|
|
50
|
+
import type { Theme } from 'vitepress'
|
|
51
|
+
import DefaultTheme from 'vitepress/theme'
|
|
52
|
+
import Layout from './Layout.vue'
|
|
53
|
+
|
|
54
|
+
export default {
|
|
55
|
+
extends: DefaultTheme,
|
|
56
|
+
Layout,
|
|
57
|
+
} satisfies Theme
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Options
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
setupWatermark({
|
|
64
|
+
enabled: true,
|
|
65
|
+
content: 'My Watermark',
|
|
66
|
+
fontColor: '#76747f',
|
|
67
|
+
globalAlpha: 0.165,
|
|
68
|
+
width: 200,
|
|
69
|
+
height: 200,
|
|
70
|
+
rotate: -22,
|
|
71
|
+
fontSize: '16px',
|
|
72
|
+
fontFamily: 'sans-serif',
|
|
73
|
+
// ...
|
|
74
|
+
})
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
| Option | Type | Default | Description |
|
|
78
|
+
| ------------------- | ---------------------------------------------- | --------------------------------------- | --------------------------------------------------------- |
|
|
79
|
+
| `enabled` | `boolean \| ((pageData: PageData) => boolean)` | `true` | Enable watermark; supports a function for dynamic control |
|
|
80
|
+
| `content` | `string` | Site title | Watermark text content |
|
|
81
|
+
| `fontColor` | `string` | `'#76747f'` | Font color |
|
|
82
|
+
| `globalAlpha` | `number` | `0.165` (normal) / `0.005` (blind mode) | Opacity |
|
|
83
|
+
| `width` | `number` | - | Width of each watermark cell |
|
|
84
|
+
| `height` | `number` | - | Height of each watermark cell |
|
|
85
|
+
| `rotate` | `number` | - | Rotation angle in degrees |
|
|
86
|
+
| `fontSize` | `string` | - | Font size |
|
|
87
|
+
| `fontFamily` | `string` | - | Font family |
|
|
88
|
+
| `image` | `string` | - | Image watermark URL |
|
|
89
|
+
| `mode` | `'default' \| 'blind'` | - | Watermark mode: `'blind'` for blind watermark |
|
|
90
|
+
| `layout` | `'default' \| 'grid'` | - | Layout mode |
|
|
91
|
+
| `zIndex` | `number` | - | CSS z-index |
|
|
92
|
+
| `mutationObserve` | `boolean` | - | Enable DOM mutation observer protection |
|
|
93
|
+
| `monitorProtection` | `boolean` | - | Enable monitoring protection |
|
|
94
|
+
| `movable` | `boolean` | - | Allow dragging the watermark |
|
|
95
|
+
| `parent` | `Element \| string` | - | Mount target element |
|
|
96
|
+
|
|
97
|
+
For all available options, see [watermark-js-plus](https://github.com/zhensherlock/watermark-js-plus).
|
|
98
|
+
`setupWatermark` accepts all `WatermarkOptions` parameters.
|
|
99
|
+
|
|
100
|
+
## Per-Page Watermark
|
|
101
|
+
|
|
102
|
+
Control watermark on individual pages via frontmatter:
|
|
103
|
+
|
|
104
|
+
```md
|
|
105
|
+
---
|
|
106
|
+
watermark: true
|
|
107
|
+
---
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Custom Text
|
|
111
|
+
|
|
112
|
+
```md
|
|
113
|
+
---
|
|
114
|
+
watermark: CONFIDENTIAL
|
|
115
|
+
---
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Full Customization
|
|
119
|
+
|
|
120
|
+
```md
|
|
121
|
+
---
|
|
122
|
+
watermark:
|
|
123
|
+
content: DRAFT
|
|
124
|
+
fontColor: '#ff0000'
|
|
125
|
+
globalAlpha: 0.3
|
|
126
|
+
rotate: 30
|
|
127
|
+
---
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Disable on Specific Pages
|
|
131
|
+
|
|
132
|
+
```md
|
|
133
|
+
---
|
|
134
|
+
watermark: false
|
|
135
|
+
---
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Dynamic Enable via Function
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
setupWatermark({
|
|
142
|
+
enabled: (pageData) => {
|
|
143
|
+
// Only enable on pages under guide/
|
|
144
|
+
return pageData.relativePath.startsWith('guide/')
|
|
145
|
+
},
|
|
146
|
+
})
|
|
147
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { PageData } from "vitepress/client";
|
|
2
|
+
import { MaybeRef } from "vue";
|
|
3
|
+
import { WatermarkOptions, WatermarkOptions as WatermarkOptions$1 } from "watermark-js-plus";
|
|
4
|
+
|
|
5
|
+
//#region src/watermark.d.ts
|
|
6
|
+
type SetupWatermarkOptions = Partial<WatermarkOptions$1> & {
|
|
7
|
+
enabled?: boolean | ((pageData: PageData) => boolean);
|
|
8
|
+
};
|
|
9
|
+
declare function setupWatermark(options?: MaybeRef<SetupWatermarkOptions>): void;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { type WatermarkOptions, setupWatermark as default, setupWatermark };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { inBrowser, useData, withBase } from "vitepress/client";
|
|
2
|
+
import { computed, toValue, watch } from "vue";
|
|
3
|
+
import { Watermark } from "watermark-js-plus/es";
|
|
4
|
+
//#region src/watermark.ts
|
|
5
|
+
function setupWatermark(options) {
|
|
6
|
+
if (!inBrowser) return;
|
|
7
|
+
const { frontmatter, site, page, localeIndex } = useData();
|
|
8
|
+
const pageOptions = computed(() => frontmatter.value.watermark);
|
|
9
|
+
const defaultEnabled = computed(() => {
|
|
10
|
+
const enabled = toValue(options)?.enabled;
|
|
11
|
+
return typeof enabled === "function" ? enabled(page.value) : enabled !== false;
|
|
12
|
+
});
|
|
13
|
+
const watermark = new Watermark();
|
|
14
|
+
const getOptions = () => {
|
|
15
|
+
const { enabled, ...clientOptions } = toValue(options) || {};
|
|
16
|
+
const mergedOptions = {
|
|
17
|
+
content: site.value.locales[localeIndex.value]?.title || site.value.title,
|
|
18
|
+
fontColor: "#76747f",
|
|
19
|
+
globalAlpha: clientOptions?.mode === "blind" ? .005 : .165,
|
|
20
|
+
...clientOptions
|
|
21
|
+
};
|
|
22
|
+
const opts = pageOptions.value ?? defaultEnabled.value;
|
|
23
|
+
if (typeof opts === "boolean") return opts ? mergedOptions : false;
|
|
24
|
+
if (typeof opts === "string") {
|
|
25
|
+
mergedOptions.content = opts;
|
|
26
|
+
return mergedOptions;
|
|
27
|
+
}
|
|
28
|
+
return {
|
|
29
|
+
...mergedOptions,
|
|
30
|
+
...opts
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
watch([
|
|
34
|
+
() => toValue(options),
|
|
35
|
+
localeIndex,
|
|
36
|
+
pageOptions,
|
|
37
|
+
defaultEnabled
|
|
38
|
+
], () => {
|
|
39
|
+
const opts = getOptions();
|
|
40
|
+
if (opts === false) watermark.destroy();
|
|
41
|
+
else {
|
|
42
|
+
if (opts.image?.startsWith("/")) opts.image = withBase(opts.image);
|
|
43
|
+
watermark.changeOptions(opts);
|
|
44
|
+
}
|
|
45
|
+
}, { immediate: true });
|
|
46
|
+
}
|
|
47
|
+
//#endregion
|
|
48
|
+
//#region src/index.ts
|
|
49
|
+
var src_default = setupWatermark;
|
|
50
|
+
//#endregion
|
|
51
|
+
export { src_default as default, setupWatermark };
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vitepress-plugin-watermark",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.4.0",
|
|
5
|
+
"description": "Add watermark to your VitePress site.",
|
|
6
|
+
"author": "pengzhanbo <q942450674@outlook.com> (https://github.com/pengzhanbo/)",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"homepage": "https://tuck.pengzhanbo.cn/plugins/watermark",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/pengzhanbo/vitepress-tuck.git",
|
|
12
|
+
"directory": "packages/plugin-watermark"
|
|
13
|
+
},
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/pengzhanbo/vitepress-tuck/issues"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"vitepress",
|
|
19
|
+
"vitepress-plugin",
|
|
20
|
+
"watermark"
|
|
21
|
+
],
|
|
22
|
+
"exports": {
|
|
23
|
+
".": "./dist/index.js"
|
|
24
|
+
},
|
|
25
|
+
"module": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"files": [
|
|
28
|
+
"dist"
|
|
29
|
+
],
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"vitepress": "^1.6.4 || ^2.0.0-alpha.17",
|
|
32
|
+
"vue": "^3.5.0"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"watermark-js-plus": "^1.6.3"
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"clean": "rimraf --glob ./dist",
|
|
42
|
+
"dev": "pnpm '/(tsdown|copy):watch/'",
|
|
43
|
+
"build": "pnpm tsdown && pnpm copy",
|
|
44
|
+
"copy": "cpx \"src/**/*.css\" dist",
|
|
45
|
+
"copy:watch": "pnpm copy -w",
|
|
46
|
+
"tsdown": "tsdown --config-loader unrun",
|
|
47
|
+
"tsdown:watch": "pnpm tsdown -w"
|
|
48
|
+
}
|
|
49
|
+
}
|