|
| 1 | +# Getting started |
| 2 | + |
| 3 | +## Installation |
| 4 | + |
| 5 | +Install `@nolebase/vitepress-plugin-thumbnail-hash` to your project dependencies by running the following command: |
| 6 | + |
| 7 | +::: code-group |
| 8 | + |
| 9 | +```shell [@antfu/ni] |
| 10 | +ni @nolebase/vitepress-plugin-thumbnail-hash -D |
| 11 | +``` |
| 12 | + |
| 13 | +```shell [pnpm] |
| 14 | +pnpm add @nolebase/vitepress-plugin-thumbnail-hash -D |
| 15 | +``` |
| 16 | + |
| 17 | +```shell [npm] |
| 18 | +npm install @nolebase/vitepress-plugin-thumbnail-hash -D |
| 19 | +``` |
| 20 | + |
| 21 | +```shell [yarn] |
| 22 | +yarn add @nolebase/vitepress-plugin-thumbnail-hash -D |
| 23 | +``` |
| 24 | + |
| 25 | +::: |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +It consists two major steps to integrate the Inline Links Previewing plugin into your VitePress project: |
| 30 | + |
| 31 | +- [Configure Vite plugin](#configure-vite-plugin) (data fetching, logs aggregation) |
| 32 | +- [Integrate with VitePress](#integrate-with-vitepress-theme) (UI and components) |
| 33 | + |
| 34 | +### Configure Vite plugin |
| 35 | + |
| 36 | +There are two ways to integrate the Thumbnail hashing for images Vite plugin into your VitePress project: |
| 37 | + |
| 38 | +1. [**Recommended**: Use the `vite` option in VitePress's primary configuration file (usually located at `docs/.vitepress/config.ts`, file paths and extensions may be vary)](#configure-vite-plugin-in-vitepresss-config-file) |
| 39 | +2. [Create a separated Vite configuration file (e.g. `vite.config.ts`) in the root directory of your VitePress project](#configure-vite-plugin-in-a-separated-vite-configuration-file) |
| 40 | + |
| 41 | +#### Configure Vite plugin in VitePress's config file |
| 42 | + |
| 43 | +In VitePress's [**primary configuration file**](https://vitepress.dev/reference/site-config#config-resolution) (not this is not a **theme configuration file**, it's usually located at `docs/.vitepress/config.ts`, file paths and extensions may be vary), we need to import `ThumbnailHashImages` and configure it properly: |
| 44 | + |
| 45 | +<!--@include: @/pages/en/snippets/details-colored-diff.md--> |
| 46 | + |
| 47 | +<!--@include: @/pages/en/snippets/configure-tsconfig.md--> |
| 48 | + |
| 49 | +```typescript twoslash |
| 50 | +import { defineConfig } from 'vitepress' |
| 51 | +import { // [!code ++] |
| 52 | + ThumbnailHashImages, // [!code ++] |
| 53 | +} from '@nolebase/vitepress-plugin-thumbnail-hash/vite' // [!code ++] |
| 54 | +
|
| 55 | +// https://vitepress.dev/reference/site-config |
| 56 | +export default defineConfig({ |
| 57 | + vite: { // [!code ++] |
| 58 | + plugins: [ // [!code ++] |
| 59 | + ThumbnailHashImages(), // [!code ++] |
| 60 | + ], |
| 61 | + }, // [!code ++] |
| 62 | + lang: 'en', |
| 63 | + title: 'Site Name', |
| 64 | + themeConfig: { |
| 65 | + // rest of the options... |
| 66 | + } |
| 67 | + // rest of the options... |
| 68 | +}) |
| 69 | +``` |
| 70 | + |
| 71 | +#### Configure Vite plugin in a separated Vite configuration file |
| 72 | + |
| 73 | +##### Ensure `vite.config.ts` is created |
| 74 | + |
| 75 | +If you understand what `vite.config.ts` is already and have created it, you can skip this preparation step and jump to the next step [Configure plugin in `vite.config.ts`](#configure-plugin-in-viteconfigts). |
| 76 | + |
| 77 | +::: tip New to `vite.config.ts` is? |
| 78 | + |
| 79 | +First of all, `vite.config.ts` is a configuration file for [Vite](https://vitejs.org), the build tool that VitePress is built on. It allows developers to build and transform the assets, content and data in the project. |
| 80 | + |
| 81 | +VitePress itself contains entire set of Vite options in its [**primary configuration file**](https://vitepress.dev/reference/site-config#config-resolution) (not this is not a **theme configuration file**, it's usually located at `docs/.vitepress/config.ts`, file paths and extensions may be vary), these options, and yet, the `vite.config.ts` are identical in terms of configurations. |
| 82 | + |
| 83 | +::: |
| 84 | + |
| 85 | +Therefore, please create a separated `vite.config.ts` file in the root directory of your VitePress project: |
| 86 | + |
| 87 | +::: tip Where is the root directory of VitePress project? |
| 88 | + |
| 89 | +VitePress project's root directory is where the parent directory of `.vitepress` directory is. |
| 90 | + |
| 91 | +For example: |
| 92 | + |
| 93 | +```shell |
| 94 | +. |
| 95 | +├── docs |
| 96 | +│ ├── .vitepress |
| 97 | +│ │ ├── config.ts |
| 98 | +│ │ └── theme |
| 99 | +│ │ └── index.ts |
| 100 | +│ └── README.md |
| 101 | +``` |
| 102 | + |
| 103 | +In this case, the root directory is `docs`. |
| 104 | + |
| 105 | +```shell |
| 106 | +. |
| 107 | +├── .vitepress |
| 108 | +│ ├── config.ts |
| 109 | +│ └── theme |
| 110 | +│ └── index.ts |
| 111 | +└── README.md |
| 112 | +``` |
| 113 | + |
| 114 | +In this case, the root directory is `./`. |
| 115 | + |
| 116 | +::: |
| 117 | + |
| 118 | +```shell |
| 119 | +touch vite.config.ts |
| 120 | +``` |
| 121 | + |
| 122 | +##### Configure plugin in `vite.config.ts` |
| 123 | + |
| 124 | +In the standalone [Vite configuration file](https://vitejs.dev/config/) (e.g. `vite.config.ts`) file we have under our root directory, we need to import `ThumbnailHashImages` and configure it properly: |
| 125 | + |
| 126 | +<!--@include: @/pages/en/snippets/details-colored-diff.md--> |
| 127 | + |
| 128 | +<!--@include: @/pages/en/snippets/configure-tsconfig.md--> |
| 129 | + |
| 130 | +```typescript twoslash |
| 131 | +import { join } from 'node:path' |
| 132 | +import { defineConfig } from 'vite' |
| 133 | +import { // [!code ++] |
| 134 | + ThumbnailHashImages, // [!code ++] |
| 135 | +} from '@nolebase/vitepress-plugin-thumbnail-hash/vite' // [!code ++] |
| 136 | +
|
| 137 | +export default defineConfig(() => { |
| 138 | + return { |
| 139 | + plugins: [ // [!code ++] |
| 140 | + ThumbnailHashImages(), // [!code ++] |
| 141 | + ] |
| 142 | + // other vite configurations... |
| 143 | + } |
| 144 | +}) |
| 145 | +``` |
| 146 | + |
| 147 | +### Integrate with VitePress theme |
| 148 | + |
| 149 | +Now, let's integrate the Thumbnail hashing for images UI widgets into your VitePress project. |
| 150 | + |
| 151 | +In VitePress's [**theme configuration file**](https://vitepress.dev/reference/default-theme-config#default-theme-config) (note that it's not a **configuration file**, it's usually located at `docs/.vitepress/theme/index.ts`, file paths and extensions may be vary), install the Vue plugin and use the components: |
| 152 | + |
| 153 | +<!--@include: @/pages/en/snippets/details-colored-diff.md--> |
| 154 | + |
| 155 | +::: code-group |
| 156 | + |
| 157 | +```typescript twoslash [docs/.vitepress/theme/index.ts] |
| 158 | +import { h } from 'vue' |
| 159 | +import DefaultTheme from 'vitepress/theme' |
| 160 | +import type { Theme as ThemeConfig } from 'vitepress' |
| 161 | +import { // [!code ++] |
| 162 | + NolebaseUnlazyImg, // [!code ++] |
| 163 | +} from '@nolebase/vitepress-plugin-thumbnail-hash/client' // [!code ++] |
| 164 | +
|
| 165 | +import '@nolebase/vitepress-plugin-thumbnail-hash/client/style.css' // [!code ++] |
| 166 | +
|
| 167 | +export const Theme: ThemeConfig = { |
| 168 | + extends: DefaultTheme, |
| 169 | + Layout: () => { |
| 170 | + // other configurations... |
| 171 | + }, |
| 172 | + enhanceApp({ app }) { |
| 173 | + app.component('NolebaseUnlazyImg', NolebaseUnlazyImg) // [!code ++] |
| 174 | + }, |
| 175 | +} |
| 176 | + |
| 177 | +export default Theme |
| 178 | +``` |
| 179 | + |
| 180 | +::: |
| 181 | + |
| 182 | +## What's next? |
| 183 | + |
| 184 | +In order to show and use the needed thumbnail hashes for VitePress pages. Take a look at another plugin called [`markdown-it-unlazy-img`](../markdown-it-unlazy-img/) too. |
| 185 | + |
| 186 | +## Troubleshooting |
| 187 | + |
| 188 | +### Encountered `Cannot find module ... or its corresponding type declarations` error? |
| 189 | + |
| 190 | +<!--@include: @/pages/en/snippets/troubleshooting-cannot-find-module.md--> |
0 commit comments