Skip to content

Commit 23de20a

Browse files
authored
fix(module): possibility to remove module's default remark/rehype plugins from bundle (#276)
1 parent 1ed537c commit 23de20a

File tree

5 files changed

+61
-48
lines changed

5 files changed

+61
-48
lines changed

src/module.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,21 @@ export default defineNuxtModule<ModuleOptions>({
3434
},
3535
// Default configuration options of the Nuxt module
3636
defaults: {
37-
remarkPlugins: {},
38-
rehypePlugins: {},
37+
remarkPlugins: {
38+
'remark-mdc': {},
39+
'remark-emoji': {},
40+
'remark-gfm': {}
41+
},
42+
rehypePlugins: {
43+
'rehype-external-links': {},
44+
'rehype-sort-attribute-values': {},
45+
'rehype-sort-attributes': {},
46+
'rehype-raw': {
47+
options: {
48+
passThrough: ['element']
49+
}
50+
}
51+
},
3952
highlight: false,
4053
headings: {
4154
anchorLinks: {

src/runtime/parser/options.ts

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,16 @@
1-
import remarkEmoji from 'remark-emoji'
2-
import remarkGFM from 'remark-gfm'
3-
import remarkMDC from 'remark-mdc'
4-
import rehypeExternalLinks from 'rehype-external-links'
5-
import rehypeSortAttributeValues from 'rehype-sort-attribute-values'
6-
import rehypeSortAttributes from 'rehype-sort-attributes'
7-
import rehypeRaw from 'rehype-raw'
81
import type { MDCParseOptions } from '@nuxtjs/mdc'
92
import handlers from './handlers'
103

114
export const defaults: MDCParseOptions = {
125
remark: {
13-
plugins: {
14-
'remark-mdc': {
15-
instance: remarkMDC
16-
},
17-
'remark-emoji': {
18-
instance: remarkEmoji
19-
},
20-
'remark-gfm': {
21-
instance: remarkGFM
22-
}
23-
}
6+
plugins: {}
247
},
258
rehype: {
269
options: {
2710
handlers,
2811
allowDangerousHtml: true
2912
},
30-
plugins: {
31-
'rehype-external-links': {
32-
instance: rehypeExternalLinks
33-
},
34-
'rehype-sort-attribute-values': {
35-
instance: rehypeSortAttributeValues
36-
},
37-
'rehype-sort-attributes': {
38-
instance: rehypeSortAttributes
39-
},
40-
'rehype-raw': {
41-
instance: rehypeRaw,
42-
options: {
43-
passThrough: ['element']
44-
}
45-
}
46-
}
13+
plugins: {}
4714
},
4815
highlight: false,
4916
toc: {

src/templates/mdc-imports.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,15 @@ function processUnistPlugins(plugins: Record<string, UnistPlugin>) {
3131
const definitions: string[] = []
3232
Object.entries(plugins).forEach(([name, plugin]) => {
3333
const instanceName = `_${pascalCase(name).replace(/\W/g, '')}`
34-
imports.push(`import ${instanceName} from '${plugin.src || name}'`)
35-
if (Object.keys(plugin).length) {
36-
definitions.push(` '${name}': { instance: ${instanceName}, options: ${JSON.stringify(plugin.options || plugin)} },`)
34+
if (plugin) {
35+
imports.push(`import ${instanceName} from '${plugin.src || name}'`)
36+
if (Object.keys(plugin).length) {
37+
definitions.push(` '${name}': { instance: ${instanceName}, options: ${JSON.stringify(plugin.options || plugin)} },`)
38+
} else {
39+
definitions.push(` '${name}': { instance: ${instanceName} },`)
40+
}
3741
} else {
38-
definitions.push(` '${name}': { instance: ${instanceName} },`)
42+
definitions.push(` '${name}': false,`)
3943
}
4044
})
4145

src/types/module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ export interface ModuleOptions {
1010
/**
1111
* A map of remark plugins to be used for processing markdown.
1212
*/
13-
remarkPlugins?: Record<string, UnistPlugin>
13+
remarkPlugins?: Record<string, UnistPlugin | false>
1414
/**
1515
* A map of remark plugins to be used for processing markdown.
1616
*/
17-
rehypePlugins?: Record<string, UnistPlugin>
17+
rehypePlugins?: Record<string, UnistPlugin | false>
1818

1919
highlight?: {
2020
/**

test/utils/parser.ts

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,43 @@
11
import { vi } from 'vitest'
22
import { createWasmOnigEngine } from 'shiki/engine/oniguruma'
3-
import { parseMarkdown as _parseMarkDown } from '../../src/runtime/parser'
4-
import type { MDCParseOptions } from '../../src/types'
5-
import { rehypeHighlight } from '../../src/runtime/highlighter/rehype-nuxt'
3+
import remarkGFM from 'remark-gfm'
4+
import remarkMDC from 'remark-mdc'
5+
import rehypeExternalLinks from 'rehype-external-links'
6+
import rehypeSortAttributeValues from 'rehype-sort-attribute-values'
7+
import rehypeSortAttributes from 'rehype-sort-attributes'
8+
import rehypeRaw from 'rehype-raw'
69
import { createShikiHighlighter } from '../../src/runtime/highlighter/shiki'
10+
import { rehypeHighlight } from '../../src/runtime/highlighter/rehype-nuxt'
11+
import type { MDCParseOptions } from '../../src/types'
12+
import { parseMarkdown as _parseMarkDown } from '../../src/runtime/parser'
713

814
vi.mock('#mdc-imports', () => {
915
return {
10-
remarkPlugins: {},
11-
rehypePlugins: {},
16+
remarkPlugins: {
17+
'remark-mdc': {
18+
instance: remarkMDC
19+
},
20+
'remark-gfm': {
21+
instance: remarkGFM
22+
}
23+
},
24+
rehypePlugins: {
25+
'rehype-external-links': {
26+
instance: rehypeExternalLinks
27+
},
28+
'rehype-sort-attribute-values': {
29+
instance: rehypeSortAttributeValues
30+
},
31+
'rehype-sort-attributes': {
32+
instance: rehypeSortAttributes
33+
},
34+
'rehype-raw': {
35+
instance: rehypeRaw,
36+
options: {
37+
passThrough: ['element']
38+
}
39+
}
40+
},
1241
highlight: {}
1342
}
1443
})

0 commit comments

Comments
 (0)