Skip to content

Commit

Permalink
feat: vue2 mode
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Aug 16, 2021
1 parent 05191dc commit 7bd1629
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ function extractCustomBlock(html: string, options: ResolvedOptions) {
}

export function createMarkdown(options: ResolvedOptions) {
const isVue2 = options.vueVersion.startsWith('2.')

const markdown = new MarkdownIt({
html: true,
linkify: true,
Expand Down Expand Up @@ -85,10 +87,10 @@ export function createMarkdown(options: ResolvedOptions) {
const { head, frontmatter } = frontmatterPreprocess(data || {}, options)
scriptLines.push(`const frontmatter = ${JSON.stringify(frontmatter)}`)

if (options.exposeFrontmatter && !defineExposeRE.test(hoistScripts.scripts.join('')))
if (!isVue2 && options.exposeFrontmatter && !defineExposeRE.test(hoistScripts.scripts.join('')))
scriptLines.push('defineExpose({ frontmatter })')

if (headEnabled && head) {
if (!isVue2 && headEnabled && head) {
scriptLines.push(`const head = ${JSON.stringify(head)}`)
scriptLines.unshift('import { useHead } from "@vueuse/head"')
scriptLines.push('useHead(head)')
Expand All @@ -97,7 +99,11 @@ export function createMarkdown(options: ResolvedOptions) {

scriptLines.push(...hoistScripts.scripts)

const sfc = `<template>${html}</template>\n<script setup>\n${scriptLines.join('\n')}\n</script>\n${customBlocks.blocks.join('\n')}\n`
const scripts = isVue2
? `<script>\n${scriptLines.join('\n')}\nexport default { data() { return { frontmatter } } }\n</script>`
: `<script setup>\n${scriptLines.join('\n')}\n</script>`

const sfc = `<template>${html}</template>\n${scripts}\n${customBlocks.blocks.join('\n')}\n`

return sfc
}
Expand Down
3 changes: 2 additions & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { preprocessHead } from './head'
import { Options, ResolvedOptions } from './types'
import { toArray } from './utils'
import { getVueVersion, toArray } from './utils'

export function resolveOptions(userOptions: Options): ResolvedOptions {
const options = Object.assign({
Expand All @@ -23,6 +23,7 @@ export function resolveOptions(userOptions: Options): ResolvedOptions {
}, userOptions) as ResolvedOptions

options.wrapperClasses = toArray(options.wrapperClasses).filter(i => i).join(' ')
options.vueVersion ??= getVueVersion()

return options
}
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import type MarkdownIt from 'markdown-it'
import type { FilterPattern } from '@rollup/pluginutils'

export interface Options {
/**
* Explicitly set the Vue version.
*
* @default auto detected
*/
vueVersion?: string
/**
* Enable head support, need to install @vueuse/head and register to App in main.js
*
Expand Down
13 changes: 13 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,16 @@ export function toArray<T>(n: T | T[]): T[] {
return [n]
return n
}

export function getVueVersion(defaultVersion = '3.2.0') {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
let v = require('vue')
if (v.default)
v = v.default
return v.version || defaultVersion
}
catch (e) {
return defaultVersion
}
}

0 comments on commit 7bd1629

Please sign in to comment.