unplugin / unplugin-vue-markdown

Compile Markdown to Vue component
MIT License
508 stars 27 forks source link

feat: improve markdown component name #16

Open privatenumber opened 1 year ago

privatenumber commented 1 year ago

Problem

When using this plugin in conjunction with vite-plugin-pages, creating a markdown page with a specific name can cause component resolution issues with unplugin-vue-components.

For example, if I create a projects.md, behind the scenes, this plugin transforms it a component named "Projects". So when trying to auto-import the ~/components/Projects.vue component via <Projects /> using unplugin-vue-components, it doesn't work because Vue.js thinks I'm self-referencing the current page-component.

Feature request

To avoid this problem, I suggest implementing one of the following solutions:

antfu commented 1 year ago

Yeah, I think it makes sense to have a better default and a custom function for users to customize the naming convention. PR welcome :)

privatenumber commented 1 year ago

Based on https://github.com/vuejs/core/blob/623ba514ec0f5adc897db90c0f986b1b6905e014/packages/compiler-sfc/src/compileScript.ts#L959, the only way to change the name seems to be adding:

<script>
export default { name: 'replace name' }
</script>

https://play.vuejs.org/#eNp9kTFPwzAQhf/K6ZYuVRDqFgoSoA4wAAJGL5ZzDSmObdnnEinKf8d21AAS6uZ73zvfO3vEW+eqYySscRuU7xxDII7uRhhlTWCQcA2XV8JsL2acQCqWMw3OeoaG9jJqhhGM7KmGlSenpaJSrmD6049r5JBu33dtdQjWpNmjMAACle1dp8k/O+7SdIE1FJKZ1Np+PRaNfaT1SVcfpD7/0Q9hyJrAF0+B/JEELoylb4lnvHt7oiGdF9jbJurkPgNfKVgdc8bZdhdNk2L/8pW0D31+nM6072E3MJlwWioHzc6p+AWmD7g/s/pP3E21KX3CTDh9AyUnkz8=

brc-dd commented 1 year ago

I had done something like this in VitePress: https://github.com/vuejs/vitepress/blob/1f4ae02138820f2ee78ce6ce39847cdfd0ba19ca/src/node/markdownToVue.ts#L314-L339

Similar stuff can be added here: https://github.com/mdit-vue/unplugin-vue-markdown/blob/f1ccc5f8751ce4020059b654219f9e55d17eec55/src/core/markdown.ts#L211-L232