Skip to content

Commit

Permalink
fix(module): possibility to remove module's default remark/rehype plu…
Browse files Browse the repository at this point in the history
…gins from bundle (#276)
  • Loading branch information
farnabaz authored Nov 6, 2024
1 parent 1ed537c commit 23de20a
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 48 deletions.
17 changes: 15 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,21 @@ export default defineNuxtModule<ModuleOptions>({
},
// Default configuration options of the Nuxt module
defaults: {
remarkPlugins: {},
rehypePlugins: {},
remarkPlugins: {
'remark-mdc': {},
'remark-emoji': {},
'remark-gfm': {}
},
rehypePlugins: {
'rehype-external-links': {},
'rehype-sort-attribute-values': {},
'rehype-sort-attributes': {},
'rehype-raw': {
options: {
passThrough: ['element']
}
}
},
highlight: false,
headings: {
anchorLinks: {
Expand Down
37 changes: 2 additions & 35 deletions src/runtime/parser/options.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,16 @@
import remarkEmoji from 'remark-emoji'
import remarkGFM from 'remark-gfm'
import remarkMDC from 'remark-mdc'
import rehypeExternalLinks from 'rehype-external-links'
import rehypeSortAttributeValues from 'rehype-sort-attribute-values'
import rehypeSortAttributes from 'rehype-sort-attributes'
import rehypeRaw from 'rehype-raw'
import type { MDCParseOptions } from '@nuxtjs/mdc'
import handlers from './handlers'

export const defaults: MDCParseOptions = {
remark: {
plugins: {
'remark-mdc': {
instance: remarkMDC
},
'remark-emoji': {
instance: remarkEmoji
},
'remark-gfm': {
instance: remarkGFM
}
}
plugins: {}
},
rehype: {
options: {
handlers,
allowDangerousHtml: true
},
plugins: {
'rehype-external-links': {
instance: rehypeExternalLinks
},
'rehype-sort-attribute-values': {
instance: rehypeSortAttributeValues
},
'rehype-sort-attributes': {
instance: rehypeSortAttributes
},
'rehype-raw': {
instance: rehypeRaw,
options: {
passThrough: ['element']
}
}
}
plugins: {}
},
highlight: false,
toc: {
Expand Down
12 changes: 8 additions & 4 deletions src/templates/mdc-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ function processUnistPlugins(plugins: Record<string, UnistPlugin>) {
const definitions: string[] = []
Object.entries(plugins).forEach(([name, plugin]) => {
const instanceName = `_${pascalCase(name).replace(/\W/g, '')}`
imports.push(`import ${instanceName} from '${plugin.src || name}'`)
if (Object.keys(plugin).length) {
definitions.push(` '${name}': { instance: ${instanceName}, options: ${JSON.stringify(plugin.options || plugin)} },`)
if (plugin) {
imports.push(`import ${instanceName} from '${plugin.src || name}'`)
if (Object.keys(plugin).length) {
definitions.push(` '${name}': { instance: ${instanceName}, options: ${JSON.stringify(plugin.options || plugin)} },`)
} else {
definitions.push(` '${name}': { instance: ${instanceName} },`)
}
} else {
definitions.push(` '${name}': { instance: ${instanceName} },`)
definitions.push(` '${name}': false,`)
}
})

Expand Down
4 changes: 2 additions & 2 deletions src/types/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export interface ModuleOptions {
/**
* A map of remark plugins to be used for processing markdown.
*/
remarkPlugins?: Record<string, UnistPlugin>
remarkPlugins?: Record<string, UnistPlugin | false>
/**
* A map of remark plugins to be used for processing markdown.
*/
rehypePlugins?: Record<string, UnistPlugin>
rehypePlugins?: Record<string, UnistPlugin | false>

highlight?: {
/**
Expand Down
39 changes: 34 additions & 5 deletions test/utils/parser.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
import { vi } from 'vitest'
import { createWasmOnigEngine } from 'shiki/engine/oniguruma'
import { parseMarkdown as _parseMarkDown } from '../../src/runtime/parser'
import type { MDCParseOptions } from '../../src/types'
import { rehypeHighlight } from '../../src/runtime/highlighter/rehype-nuxt'
import remarkGFM from 'remark-gfm'
import remarkMDC from 'remark-mdc'
import rehypeExternalLinks from 'rehype-external-links'
import rehypeSortAttributeValues from 'rehype-sort-attribute-values'
import rehypeSortAttributes from 'rehype-sort-attributes'
import rehypeRaw from 'rehype-raw'
import { createShikiHighlighter } from '../../src/runtime/highlighter/shiki'
import { rehypeHighlight } from '../../src/runtime/highlighter/rehype-nuxt'
import type { MDCParseOptions } from '../../src/types'
import { parseMarkdown as _parseMarkDown } from '../../src/runtime/parser'

vi.mock('#mdc-imports', () => {
return {
remarkPlugins: {},
rehypePlugins: {},
remarkPlugins: {
'remark-mdc': {
instance: remarkMDC
},
'remark-gfm': {
instance: remarkGFM
}
},
rehypePlugins: {
'rehype-external-links': {
instance: rehypeExternalLinks
},
'rehype-sort-attribute-values': {
instance: rehypeSortAttributeValues
},
'rehype-sort-attributes': {
instance: rehypeSortAttributes
},
'rehype-raw': {
instance: rehypeRaw,
options: {
passThrough: ['element']
}
}
},
highlight: {}
}
})
Expand Down

0 comments on commit 23de20a

Please sign in to comment.