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(module): possibility to remove module's default remark/rehype plugins from bundle #276

Merged
merged 2 commits into from
Nov 6, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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