Skip to content

Commit

Permalink
chore: refactor code and remove leftovers
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandros94 committed Apr 14, 2024
1 parent 1dacf09 commit 14ae0c9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/runtime/components/nuxt-markdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ interface Props extends Partial<Omit<Config, 'components'>> {
const props = defineProps<Props>()
const {
forceHtml,
source
}= toRefs(props)
const { rendered: NuxtMarkdown, content, $md, md, newRequired } = useNuxtMarkdown({
const { config, content, $md, md, newRequired, rendered: NuxtMarkdown } = useNuxtMarkdown({
source,
as: props.as,
components: props.components,
forceHtml,
forceHtml: props.forceHtml,
new: props.new,
options: props.options,
plugins: props.plugins,
})
defineExpose({
config,
content,
md: newRequired ? md : $md,
NuxtMarkdown,
Expand Down
20 changes: 11 additions & 9 deletions src/runtime/composables/use-nuxt-markdown.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import MarkdownIt from 'markdown-it'
import { compile, computed, h, ref, toValue } from 'vue'
import { computed, h, ref, toValue } from 'vue'
import type { Ref, MaybeRefOrGetter } from 'vue'
import { defu } from 'defu'

import MarkdownIt from 'markdown-it'

import { useRuntimeConfig, useNuxtApp } from '#imports'
import type { Config, DeepMROG } from '../types'
import { NuxtLink } from '#components'
import type { Config } from '../types'

/**
* The main composable to access or customize md, renderer and content.
Expand All @@ -27,7 +29,7 @@ import { NuxtLink } from '#components'
* - `md` - a blank markdown-it instance
* - `rendered` - the rendered Vnode
*/
export const useNuxtMarkdown = (overrides?: { source?: MaybeRefOrGetter<string | undefined> } & DeepMROG<Config>) => {
export const useNuxtMarkdown = (overrides?: { source?: MaybeRefOrGetter<string | undefined> } & Partial<Config>) => {

const { source, ...overridesRest } = overrides ?? {}
const { $md } = useNuxtApp()
Expand Down Expand Up @@ -79,13 +81,13 @@ export const useNuxtMarkdown = (overrides?: { source?: MaybeRefOrGetter<string |
}

if (configDef.plugins) {
for (const plugin of toValue(configDef.plugins)) {
for (const plugin of configDef.plugins) {
md.value.use(plugin)
}
}

// Check if a new istance is required
const newRequired = !!(toValue(configDef.new) || configDef.disable || configDef.enable || configDef.plugins || overridesRest.options)
const newRequired = !!(configDef.new || configDef.disable || configDef.enable || configDef.plugins || overridesRest.options)

const content = computed(() => {
if (newRequired) {
Expand All @@ -97,13 +99,13 @@ export const useNuxtMarkdown = (overrides?: { source?: MaybeRefOrGetter<string |
})

const rendered = () => {
if (toValue(configDef.forceHtml) || !vueRuntimeCompiler) {
if (configDef.forceHtml || !vueRuntimeCompiler) {
return h(configDef.as, { innerHTML: content.value, })
} else {
return h(configDef.as, [
h({
components: toValue(configDef.components),
render: compile(content.value),
components: configDef.components,
template: content.value,
})
])
}
Expand Down
6 changes: 4 additions & 2 deletions src/runtime/plugins/nuxt-markdown.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { defineNuxtPlugin } from '#app'
import { type Ref, ref } from 'vue'

import MarkdownIt from 'markdown-it'
import MarkdownItGitHubAlerts from 'markdown-it-github-alerts'
import mdcPlugin from 'markdown-it-mdc'
import MarkdownItGitHubAlerts from 'markdown-it-github-alerts'
import shikiPlugin from '@shikijs/markdown-it'
import anchorPlugin from 'markdown-it-anchor'

import { defineNuxtPlugin } from '#app'
import type { ModuleOptions } from '../../module'

export default defineNuxtPlugin(async nuxtApp => {
Expand Down
6 changes: 1 addition & 5 deletions src/runtime/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Component, MaybeRefOrGetter } from 'vue'
import type { Component } from 'vue'
import type {
Options as MarkdownItOptions,
PluginSimple,
Expand All @@ -18,7 +18,3 @@ export interface Config {

export type MarkdownItOptions = MarkdownItOptions
export type PluginSimple = PluginSimple

export type DeepMROG<T> = T extends object ? {
[P in keyof T]?: MaybeRefOrGetter<T[P]>
} : MaybeRefOrGetter<T>

0 comments on commit 14ae0c9

Please sign in to comment.