Skip to content

Commit

Permalink
chore: fix docs build
Browse files Browse the repository at this point in the history
  • Loading branch information
KaelWD committed Aug 15, 2024
1 parent e4ce28d commit 802742f
Show file tree
Hide file tree
Showing 5 changed files with 338 additions and 91 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"mkdirp": "^3.0.1",
"moment": "^2.30.1",
"rimraf": "^5.0.5",
"sass": "^1.77.8",
"sass-embedded": "^1.77.8",
"semver": "^7.6.0",
"shelljs": "^0.8.5",
"stringify-object": "^5.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,49 @@ const validate = ajv.compile({
},
})

async function setupPages (payload, options) {
if (!options.pages) {
const pagesPlugin = payload.viteConfig.plugins!
.find(p => p && 'name' in p && p.name === 'vite-plugin-pages') as Plugin
options.pages = await pagesPlugin.api.getResolvedRoutes() as []
}

const page = options.pages.find(p => payload.fileName.endsWith(p.component))

if (!page) throw new Error('Unable to find page')

const locale = page.path.split('/').at(1)!

options.files ??= new Map()
options.files.set(page.path, {
...payload,
html: payload.html?.cloneNode(true),
})

let original = payload
if (locale !== 'en') {
const originalPath = page.path.replace(`/${locale}/`, '/en/')
original = options.files.get(originalPath)
if (!original) {
options.awaiting ??= new Map()
const awaiting = options.awaiting.get(originalPath) ?? []
const { promise, resolve } = Promise.withResolvers<Pipeline<'metaExtracted'>>()
awaiting.push(resolve)
options.awaiting.set(originalPath, awaiting)
original = await promise
}
} else {
if (options.awaiting?.has(page.path)) {
options.awaiting.get(page.path)!.forEach(fn => fn({
...payload,
html: payload.html?.cloneNode(true),
}))
}
}

return { page, locale, original }
}

export const frontmatterBuilder = createBuilder('frontmatterBuilder', 'metaExtracted')
.options<{
files?: Map<string, Pipeline<'metaExtracted'>>
Expand All @@ -60,46 +103,17 @@ export const frontmatterBuilder = createBuilder('frontmatterBuilder', 'metaExtra
}>()
.initializer()
.handler(async (payload, options) => {
const { frontmatter, fileName } = payload

if (!options.pages) {
const pagesPlugin = payload.viteConfig.plugins!
.find(p => p && 'name' in p && p.name === 'vite-plugin-pages') as Plugin
options.pages = await pagesPlugin.api.getResolvedRoutes() as []
}

const page = options.pages.find(p => fileName.endsWith(p.component))
const { locale, original } = await setupPages(payload, options)

if (!page) throw new Error('Unable to find page')

const locale = page.path.split('/').at(0)!

options.files ??= new Map()
options.files.set(page.path, payload)

const { meta, ...rest } = frontmatter
const { meta, ...rest } = payload.frontmatter

if (locale !== 'en') {
const originalPath = page.path.replace(`/${locale}/`, '/en/')
let original = options.files.get(originalPath)
if (!original) {
options.awaiting ??= new Map()
const awaiting = options.awaiting.get(originalPath) ?? []
const { promise, resolve } = Promise.withResolvers<Pipeline<'metaExtracted'>>()
awaiting.push(resolve)
options.awaiting.set(originalPath, awaiting)
original = await promise
}
Object.assign(rest, {
assets: original.frontmatter.assets,
related: original.frontmatter.related,
})
}

if (options.awaiting?.has(page.path)) {
options.awaiting.get(page.path)!.forEach(fn => fn(payload))
}

payload.frontmatter = {
meta,
assets: rest.assets,
Expand Down Expand Up @@ -179,3 +193,24 @@ function generateToc (content: string) {

return headings
}

export const scriptFixer = createBuilder('scriptFixer', 'dom')
.options()
.initializer()
.handler(async (payload, options) => {
const { locale, original } = await setupPages(payload, options)

if (locale !== 'en') {
const setup = payload.html.querySelector('script[setup]')
const origSetup = original.html.querySelector('script[setup]')
if (setup) {
if (!origSetup) {
throw new Error(`Extra setup in ${payload.fileName}`)
}
setup.innerHTML = origSetup.innerHTML
}
}

return payload
})
.meta()
4 changes: 2 additions & 2 deletions packages/docs/src/pages/en/api/[name].md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ meta:
keywords: api, vuetify
---

<script setup lang="ts">
<script setup>
const name = shallowRef('')
</script>

Expand All @@ -19,4 +19,4 @@ meta:

<ApiSearch />

<ApiView @update:name="name = $event" />
<ApiView v-on:update:name="name = $event" />
7 changes: 3 additions & 4 deletions packages/docs/vite.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Api from './build/api-plugin'
import { Examples } from './build/examples-plugin'
import { genAppMetaInfo } from './src/utils/metadata'
import { MdiJs } from './build/mdi-js'
import { frontmatterBuilder, getRouteMeta } from './build/frontmatterMeta'
import { frontmatterBuilder, getRouteMeta, scriptFixer } from './build/markdownBuilders'

const resolve = (file: string) => fileURLToPath(new URL(file, import.meta.url))

Expand Down Expand Up @@ -55,10 +55,9 @@ export default defineConfig(({ command, mode, isSsrBuild }) => {
css: {
preprocessorOptions: {
sass: {
api: 'modern'
api: 'modern-compiler'
}
},
preprocessorMaxWorkers: true,
},
build: {
sourcemap: true,
Expand Down Expand Up @@ -151,7 +150,7 @@ export default defineConfig(({ command, mode, isSsrBuild }) => {
exposeFrontmatter: true,
exposeExcerpt: false,
markdownItSetup: configureMarkdown,
builders: [frontmatterBuilder()]
builders: [frontmatterBuilder(), scriptFixer()]
}),

// https://github.com/hannoeru/vite-plugin-pages
Expand Down
Loading

0 comments on commit 802742f

Please sign in to comment.