Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: filter frontmatter (#1303) #1312

Merged
merged 1 commit into from Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 53 additions & 0 deletions packages/client/constants.ts
@@ -1,6 +1,7 @@
import type { ComputedRef, InjectionKey, Ref, UnwrapNestedRefs } from 'vue'
import type { RouteRecordRaw } from 'vue-router'
import type { ClicksContext, RenderContext } from '@slidev/types'
import { objectOmit } from '@vueuse/core'
import type { SlidevContext } from './modules/context'

export const injectionClicksContext: InjectionKey<Ref<ClicksContext>> = Symbol('slidev-clicks-context')
Expand All @@ -24,3 +25,55 @@ export const TRUST_ORIGINS = [
'localhost',
'127.0.0.1',
]

const FRONTMATTER_FIELDS = [
'clicks',
'disabled',
'hide',
'hideInToc',
'layout',
'level',
'preload',
'routeAlias',
'src',
'title',
'transition',
]

const HEADMATTER_FIELDS = [
...FRONTMATTER_FIELDS,
'theme',
'titleTemplate',
'info',
'author',
'keywords',
'presenter',
'download',
'exportFilename',
'export',
'highlighter',
'lineNumbers',
'monaco',
'remoteAssets',
'selectable',
'record',
'colorSchema',
'routerMode',
'aspectRatio',
'canvasWidth',
'themeConfig',
'favicon',
'plantUmlServer',
'fonts',
'defaults',
'drawings',
'htmlAttrs',
'mdc',
]

export function filterFrontmatter(frontmatter: Record<string, any>, pageNo: number) {
return {
...objectOmit(frontmatter, pageNo === 0 ? HEADMATTER_FIELDS : FRONTMATTER_FIELDS),
frontmatter,
}
}
3 changes: 2 additions & 1 deletion packages/slidev/node/plugins/loaders.ts
Expand Up @@ -27,6 +27,7 @@ const vueContextImports = [
injectionCurrentPage as _injectionCurrentPage,
injectionRenderContext as _injectionRenderContext,
injectionFrontmatter as _injectionFrontmatter,
filterFrontmatter as _filterFrontmatter,
} from "@slidev/client/constants.ts"`.replace(/\n\s+/g, '\n'),
'const $slidev = _vueInject(_injectionSlidevContext)',
'const $nav = _vueToRef($slidev, "nav")',
Expand Down Expand Up @@ -487,7 +488,7 @@ export function createSlidesLoader(
let body = code.slice(injectA, injectB).trim()
if (body.startsWith('<div>') && body.endsWith('</div>'))
body = body.slice(5, -6)
code = `${code.slice(0, injectA)}\n<InjectedLayout v-bind="frontmatter">\n${body}\n</InjectedLayout>\n${code.slice(injectB)}`
code = `${code.slice(0, injectA)}\n<InjectedLayout v-bind="_filterFrontmatter(frontmatter,${pageNo})">\n${body}\n</InjectedLayout>\n${code.slice(injectB)}`

return code
}
Expand Down