|
| 1 | +# @attributech/nuxt-drupal-image |
| 2 | + |
| 3 | +A Nuxt module for handling Drupal image styles. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Compatible with Nuxt 2 (with Bridge) and Nuxt 3 |
| 8 | +- TypeScript support |
| 9 | +- Auto-imports for the useImageUrl composable |
| 10 | +- Simplifies working with Drupal image styles |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +```bash |
| 15 | +npm install @attributech/nuxt-drupal-image |
| 16 | +``` |
| 17 | + |
| 18 | +## Setup |
| 19 | + |
| 20 | +Add the module to your `nuxt.config.ts`: |
| 21 | + |
| 22 | +### Nuxt 3 |
| 23 | + |
| 24 | +```ts |
| 25 | +export default defineNuxtConfig({ |
| 26 | + modules: [ |
| 27 | + '@attributech/nuxt-drupal-image' |
| 28 | + ] |
| 29 | +}) |
| 30 | +``` |
| 31 | + |
| 32 | +### Nuxt 2 with Bridge |
| 33 | + |
| 34 | +```ts |
| 35 | +import { defineNuxtConfig } from '@nuxt/bridge' |
| 36 | + |
| 37 | +export default defineNuxtConfig({ |
| 38 | + buildModules: [ |
| 39 | + '@attributech/nuxt-drupal-image' |
| 40 | + ] |
| 41 | +}) |
| 42 | +``` |
| 43 | + |
| 44 | +## Usage |
| 45 | + |
| 46 | +### useImageUrl |
| 47 | + |
| 48 | +A composable for building Drupal image style URLs. |
| 49 | + |
| 50 | +```ts |
| 51 | +const imageUrl = useImageUrl(imageUri, style) |
| 52 | +``` |
| 53 | + |
| 54 | +#### Parameters |
| 55 | + |
| 56 | +- `imageUri`: The Drupal image URI (e.g., 'public://image.jpg') |
| 57 | +- `style`: The Drupal image style name (e.g., 'thumbnail', 'large') |
| 58 | + |
| 59 | +#### Returns |
| 60 | + |
| 61 | +- A string containing the complete URL to the styled image |
| 62 | + |
| 63 | +#### Example |
| 64 | + |
| 65 | +```vue |
| 66 | +<template> |
| 67 | + <img :src="imageUrl" alt="My image" /> |
| 68 | +</template> |
| 69 | +
|
| 70 | +<script setup> |
| 71 | +const imageUri = 'public://my-image.jpg' |
| 72 | +const imageUrl = useImageUrl(imageUri, 'large') |
| 73 | +</script> |
| 74 | +``` |
| 75 | + |
| 76 | +#### Configuration |
| 77 | + |
| 78 | +The composable uses the `serverUrl` from your Nuxt runtime config. Make sure to set this in your `nuxt.config.ts`: |
| 79 | + |
| 80 | +```ts |
| 81 | +// Nuxt 3 |
| 82 | +export default defineNuxtConfig({ |
| 83 | + runtimeConfig: { |
| 84 | + public: { |
| 85 | + serverUrl: process.env.SERVER_URL || 'https://example.com' |
| 86 | + } |
| 87 | + } |
| 88 | +}) |
| 89 | + |
| 90 | +// Nuxt 2 with Bridge |
| 91 | +export default defineNuxtConfig({ |
| 92 | + publicRuntimeConfig: { |
| 93 | + serverUrl: process.env.SERVER_URL || 'https://example.com' |
| 94 | + } |
| 95 | +}) |
| 96 | +``` |
| 97 | + |
| 98 | +## Migration from existing code |
| 99 | + |
| 100 | +### Migrating from util/imageUrl.js |
| 101 | + |
| 102 | +If you're currently using the utility function from `util/imageUrl.js`, you can migrate to the composable as follows: |
| 103 | + |
| 104 | +Before: |
| 105 | +```js |
| 106 | +import imageUrl from '@/util/imageUrl' |
| 107 | + |
| 108 | +const url = imageUrl(imageUri, style, apiUrl) |
| 109 | +``` |
| 110 | + |
| 111 | +After: |
| 112 | +```js |
| 113 | +import { useImageUrl } from '@attributech/nuxt-drupal-image' |
| 114 | + |
| 115 | +// The apiUrl is now taken from the Nuxt runtime config |
| 116 | +const url = useImageUrl(imageUri, style) |
| 117 | +``` |
| 118 | + |
| 119 | +## License |
| 120 | + |
| 121 | +MIT |
0 commit comments