Skip to content

Commit 389602c

Browse files
authored
chore: migrate to @unhead/vue from @vueuse/head (#18)
1 parent 8092b32 commit 389602c

File tree

11 files changed

+59
-57
lines changed

11 files changed

+59
-57
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Or you can use [`vite-plugin-components`](#work-with-vite-plugin-components) for
8989

9090
## Frontmatter
9191

92-
Frontmatter will be parsed and inject into Vue's instance data `frontmatter` field.
92+
Frontmatter will be parsed and inject into Vue's instance data `frontmatter` field.
9393

9494
For example:
9595

@@ -114,10 +114,10 @@ It will also be passed to the wrapper component's props if you have set `wrapper
114114

115115
## Document head and meta
116116

117-
To manage document head and meta, you would need to install [`@vueuse/head`](https://github.com/vueuse/head) and do some setup.
117+
To manage document head and meta, you would need to install [`@unhead/vue`](https://unhead.harlanzw.com/integrations/vue/setup) and do some setup.
118118

119119
```bash
120-
npm i @vueuse/head
120+
npm i @unhead/vue
121121
```
122122

123123
```js
@@ -140,7 +140,7 @@ export default {
140140
```js
141141
// src/main.js
142142
import { createApp } from 'vue'
143-
import { createHead } from '@vueuse/head' // <--
143+
import { createHead } from '@unhead/vue' // <--
144144

145145
const app = createApp(App)
146146

@@ -159,7 +159,7 @@ meta:
159159
---
160160
```
161161

162-
For more options available, please refer to [`@vueuse/head`'s docs](https://github.com/vueuse/head).
162+
For more options available, please refer to [`@unhead/vue`'s docs](https://unhead.harlanzw.com/integrations/vue/setup).
163163

164164
## Options
165165

example/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createApp } from 'vue'
2-
import { createHead } from '@vueuse/head'
2+
import { createHead } from '@unhead/vue'
33
import {
44
createRouter,
55
createWebHistory,

example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"build": "cross-env DEBUG=vite-plugin-vue-markdown:* vite build"
66
},
77
"dependencies": {
8-
"@vueuse/head": "^1.0.24",
8+
"@unhead/vue": "^1.1.26",
99
"vue": "^3.2.47",
1010
"vue-router": "^4.1.6"
1111
},

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
5656
"@antfu/eslint-config": "^0.38.4",
5757
"@antfu/ni": "^0.21.3",
5858
"@types/node": "^18.15.11",
59+
"@unhead/vue": "^1.1.26",
5960
"@vue/test-utils": "^2.3.2",
60-
"@vueuse/head": "^1.1.23",
6161
"bumpp": "^9.1.0",
6262
"eslint": "^8.38.0",
6363
"rollup": "^3.20.2",

pnpm-lock.yaml

Lines changed: 25 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/markdown.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ export function createMarkdown(options: ResolvedOptions) {
132132

133133
if (!isVue2 && headEnabled && head) {
134134
scriptLines.push(`const head = ${JSON.stringify(head)}`)
135-
scriptLines.unshift('import { useHead } from "@vueuse/head"')
135+
const importFrom = headEnabled === 'unhead' ? '@unhead/vue' : '@vueuse/head'
136+
scriptLines.unshift(`import { useHead } from "${importFrom}"`)
136137
scriptLines.push('useHead(head)')
137138
}
138139
}

src/options.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { toArray } from '@antfu/utils'
22
import type { Options, ResolvedOptions } from './types'
3-
import { getVueVersion } from './utils'
3+
import { getVueVersion, isUnheadVueInstalled } from './utils'
44

55
export function resolveOptions(userOptions: Options): ResolvedOptions {
66
const defaultOptions: ResolvedOptions = {
@@ -36,6 +36,9 @@ export function resolveOptions(userOptions: Options): ResolvedOptions {
3636
...userOptions,
3737
}
3838

39+
if (options.headEnabled === true)
40+
options.headEnabled = isUnheadVueInstalled() ? 'unhead' : 'vueuse'
41+
3942
options.wrapperClasses = toArray(options.wrapperClasses)
4043
.filter((i?: string) => i)
4144
.join(' ')

src/types.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,17 @@ export interface Options {
4949
vueVersion?: string
5050

5151
/**
52-
* Enable head support, need to install @vueuse/head and register to App in main.js
52+
* Enable head support, need to install @unhead/vue and register to App in main.js
53+
*
54+
* When `true` is passed, @unhead/vue will be used if installed.
55+
* If not, @vueuse/head will be used.
5356
*
5457
* @default false
5558
*/
56-
headEnabled?: boolean
59+
headEnabled?: boolean | 'unhead' | 'vueuse'
5760

5861
/**
59-
* The head field in frontmatter used to be used for @vueuse/head
62+
* The head field in frontmatter used to be used for @unhead/vue
6063
*
6164
* When an empty string is passed, it will use the root properties of the frontmatter
6265
*
@@ -184,6 +187,7 @@ export interface Options {
184187
}
185188

186189
export interface ResolvedOptions extends Required<Options> {
190+
headEnabled: 'unhead' | 'vueuse' | false
187191
wrapperClasses: string
188192
}
189193

src/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,14 @@ export function getVueVersion(defaultVersion = '3.2.0') {
1010
return defaultVersion
1111
}
1212
}
13+
14+
export function isUnheadVueInstalled() {
15+
try {
16+
const _require = require
17+
_require('@unhead/vue')
18+
return true
19+
}
20+
catch {
21+
return false
22+
}
23+
}

test/__snapshots__/excerpt.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Vitest Snapshot v1
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

33
exports[`excerpt > raw excerpt 1`] = `
44
"<template><div class=\\"markdown-body\\"><p>This is an excerpt which is kept as <strong>raw Markdown</strong>.</p>

0 commit comments

Comments
 (0)