-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Recommended way to use Custom Vue components in .md #157
Comments
Yes, at the moment it's. This is a kind of feature that we would want to tell users to use VuePress instead. A little background on this. It might subject to change in the future, but for now, VitePress is aiming to be smaller VuePress. Like Lumen for Laravel (if you're familiar with PHP 👀). VitePress should have bare minimal features for authoring docs, and for simple markdown based build system for Vue, and all necessary features like Search, Service Workers, built in to the core, where else VuePress has those features enabled by plugins. That's reason why we're merging features like Carbon Ads. And also to note, VuePress might integrate with Vite in the future. So that user can get benefits from Vite, with all the powerful features in VuePress (we should consider the naming though 😅). However, maybe we could provide a script that traverse file system and auto include components inside
Yes, this is the way to go about it. However, I would really like to make this customization process much more smoother. Currently, creating a whole new theme, is easy but extending the default theme is painful. You have to import theme from So yes, we should document this, but before doing that I want to rethink, and refactor how to do the customization. So I guess we can use this issue to discuss about it! Here's what I'm thinking, but not 100% sure yet. Create brand new themeexport default {
Layout: CustomLayout,
NotFound: CustomNotFound,
enhanceApp({ app, router, siteData }) {}
} Very well. Easy. Good enough. Only adding componentsimport Theme from 'vitepress/dist/client/theme' // <- rename to theme instead of default-theme
import CustomComponent from './components/CustomComponent.vue'
export default {
...Theme,
enhanceApp({ app, router, siteData }) {
app.component('CustomComponent', CustomComponent)
}
} I don't like 'vitepress/dist/client/theme' path... but... hmmm.... Extending default theme layoutimport 'vitepress/dist/client/styles' // import global css
import NotFound from 'vitepress/dist/client/theme/NotFound.vue' // Use default NotFound component.
import Layout from './components/Layout.vue' // Use your own layout
export default {
Layout,
NotFound,
enhanceApp({ app, router, siteData }) {
app.component('CustomComponent', CustomComponent)
}
} And in <template>
<Layout>
<!-- Inject something to slot -->
<template #page-top>
<p>Hello page top!</p>
</template>
</Layout>
</template>
<script setup>
import Layout from 'vitepress/dist/client/theme/Layout.vue'
</script> I guess simple enough? Importing global CSS is not that... hmmm... |
So using Vite instead in VuePress, and not extending VitePress? Interesting, there could be a lot of duplication in that case 🤔
I like this, if you place it in the theme, you are not polluting the tool. Maybe it can be a config for the theme? Something like:
(Or +1 for renaming default-theme to theme. For the paths, it would be great if we could tell users to just use something like the code uses internally
|
Two ideas about importing the theme, sorry if this was discussed already.
This may also help to force vitepress to properly expose everything the default theme uses, so other themes can be play at the same level.
function enhanceTheme(enhanceApp,custom) {
import('../client/styles') // (can we do something like this?)
return {
...Theme,
...custom,
enhanceApp
}
} So your examples can be written as Only adding components import CustomComponent from './components/CustomComponent.vue'
export default enhanceTheme(
({ app, router, siteData }) => {
app.component('CustomComponent', CustomComponent)
}
) And Extending default theme layout import Layout from './components/Layout.vue' // Use your own layout
export default enhanceTheme(
({ app, router, siteData }) => {
app.component('CustomComponent', CustomComponent)
},
{ Layout }
) |
FWIW, adding this in <script setup>
import CustomComponent from '../components/CustomComponent.vue'
</script> |
I just tested it and works fine 👍 In my opinion, importing the components you use on a page directly in the .md is quite nice. At least for one-of components (like examples, playgrounds, etc). For components like or a that you end up using everywhere across your docs, there should still be an easy way to register global components. |
While you can write HTML directly into the md file, due to the md parsing it becomes almost impossible to do anything which would need any sort of layout. When trying to get around this issue myself I ended up copying over the entire default theme folder so I could add my custom component to I think it needs to be clear and easy for developers to add a custom component. It seems like it's trending to magically load them in when they're needed(Nuxt.js, Vuetify, etc). I'm not sure about the performance cost of it but having a configurable |
In the next release you can do // .vitepress/theme/index.js
import { defaultTheme } from 'vitepress'
import MyGlobalComp from './Comp.vue'
defaultTheme.enhanceApp = ({ app }) => {
app.component('MyGlobalComp', MyGlobalComp)
} And yes we can consider auto registering components in a directory as global. Also not sure what you mean by
since you can use |
Thanks for the reply @yyx990803 and good to know! That would make life easier. Sorry for not being clear on the markdown parsing. Previously I had an issue using HTML markup within the markdown file, it was rendering out incorrectly (was moving divs around). I've tried to replicate it now but looks like I can't, so was likely an issue on my end. Irrelevant to this issue either way. |
I use the following pattern currently: // @ts-ignore
const modules = import.meta.globEager('../components/**/*.vue')
const components = []
for (const path in modules) {
components.push(modules[path].default)
}
export default {
...DefaultTheme,
enhanceApp({ app }) {
components.forEach(component => {
app.component(component.name, component)
})
}
} |
Closing since this is documented with details 👍 |
I am looking into porting the using vue in Markdown from vuepress:
https://vuepress.vuejs.org/guide/using-vue.html
If I understand correctly, vitepress doesn't want to auto register components by convention as Vuepress does. Is this the case? I actually liked this feature, but I understand that vitepress wants to keep the moving parts as small as possible.
What is the recommended way to register the components? I see that in vue-router-next docs they are registered globally inside
enhanceApp
: https://github.com/vuejs/vue-router-next/search?q=HomeSponsors.Same as with this comment: #92 (comment)
Should we document this way in the docs?
Some thoughts about this. It would be great that users that want to use the default theme as is, do not need to learn straight away about enhanceApp to be able to use a vue component in their markdown.
If auto registering by convention in a folder like
.vitepress/components
is not an option, could we import them directly in the markdown?Script & style hoisting is working in vitepress: https://vuepress.vuejs.org/guide/using-vue.html#script-style-hoisting, but I tried this example to import a Component and it is not at this point.
The text was updated successfully, but these errors were encountered: