Skip to content

Commit

Permalink
feat: support providing runtime HTML Serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
lihbr committed Apr 13, 2022
1 parent 9e0549a commit e6e6d13
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
29 changes: 28 additions & 1 deletion docs/content/en/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ export default (doc) => {
}
```

### Providing an HTML Serializer

You can provide the module an [HTML Serializer](https://prismic.io/docs/core-concepts/html-serializer) by exporting one from the file configured in the `htmlSerializer` option (default: `~/app/prismic/htmlSerializer`):

```javascript[~/app/prismic/htmlSerializer.[jt]s]
export default {
paragraph: ({ children }) => `<p class="fooBar">${children}</p>`,
}
```

### Custom preview page

You can override the default preview page by creating a page at the route configured in the `preview` option (default: `/preview`):
Expand Down Expand Up @@ -243,6 +253,19 @@ prismic: {
}
```

#### `htmlSerializer`

- Type: `string`
- Default: `~/app/prismic/htmlSerializer`

Path to an optional file exporting an [HTML Serializer](https://prismic.io/docs/core-concepts/html-serializer) for the module to use.

```javascript[nuxt.config.[jt]s]
prismic: {
htmlSerializer: '~/prismicHTMLSerializer.js' // attempt to import client from `~/prismicHTMLSerializer.js`
}
```

#### `preview`

- Type: `string | false`
Expand All @@ -260,7 +283,10 @@ prismic: {

```typescript
type PrismicModuleOptions = PrismicPluginOptions & {
preview?: string | false
client?: string;
linkResolver?: string;
htmlSerializer?: string;
preview?: string | false;
}
```
Expand All @@ -270,6 +296,7 @@ type PrismicModuleOptions = PrismicPluginOptions & {
{
client: '~/app/prismic/client',
linkResolver: '~/app/prismic/linkResolver',
htmlSerializer: '~/app/prismic/htmlSerializer',
injectComponents: true,
preview: '/preview'
}
Expand Down
7 changes: 4 additions & 3 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default defineNuxtModule<PrismicModuleOptions>({
clientConfig: {},
client: cleanDoubleSlashes(`~/${nuxt.options.dir.app}/prismic/client`),
linkResolver: cleanDoubleSlashes(`~/${nuxt.options.dir.app}/prismic/linkResolver`),
htmlSerializer: undefined,
htmlSerializer: cleanDoubleSlashes(`~/${nuxt.options.dir.app}/prismic/htmlSerializer`),
injectComponents: true,
components: {},
preview: '/preview'
Expand Down Expand Up @@ -76,10 +76,11 @@ export default defineNuxtModule<PrismicModuleOptions>({
}
addUserFileWithUndefinedFallback('client', mergedOptions.client)
addUserFileWithUndefinedFallback('linkResolver', mergedOptions.linkResolver)
addUserFileWithUndefinedFallback('htmlSerializer', mergedOptions.htmlSerializer)

// Expose options through public runtime config
nuxt.options.publicRuntimeConfig ||= {} as typeof nuxt.options.publicRuntimeConfig
nuxt.options.publicRuntimeConfig[pkgName] = mergedOptions
nuxt.options.runtimeConfig.public ||= {} as typeof nuxt.options.runtimeConfig.public
nuxt.options.runtimeConfig.public[pkgName] = mergedOptions

// Add plugin
addPlugin(resolver.resolve('runtime/plugin'))
Expand Down
9 changes: 7 additions & 2 deletions src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ import { createPrismic } from '@prismicio/vue'
import { name as pkgName } from '../../package.json'
import { PrismicModuleOptions } from '../types'

import linkResolver from '#build/prismic/linkResolver'
import client from '#build/prismic/client'
import linkResolver from '#build/prismic/linkResolver'
import htmlSerializer from '#build/prismic/htmlSerializer'

export default defineNuxtPlugin((nuxtApp) => {
const mergedOptions: PrismicModuleOptions = nuxtApp.payload.config[pkgName]
const mergedOptions: PrismicModuleOptions =
nuxtApp.payload.config[pkgName] ??
nuxtApp.payload.config.public[pkgName] ??
{}

nuxtApp.vueApp.use(createPrismic({
...mergedOptions,
client,
linkResolver,
htmlSerializer,
injectComponents: false, // Handled at module level
components: {
linkInternalComponent: NuxtLink,
Expand Down
11 changes: 6 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { PrismicPluginOptions } from '@prismicio/vue'

export type PrismicModuleOptions = Omit<PrismicPluginOptions, 'endpoint' | 'client' | 'linkResolver'> & {
endpoint: string,
client?: string,
linkResolver?: string,
preview?: string | false,
export type PrismicModuleOptions = Omit<PrismicPluginOptions, 'endpoint' | 'client' | 'linkResolver' | 'htmlSerializer'> & {
endpoint: string;
client?: string;
linkResolver?: string;
htmlSerializer?: string;
preview?: string | false;
};

0 comments on commit e6e6d13

Please sign in to comment.