From e4f769e495ebc2a664455e1e8b118bc9ae06e367 Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Wed, 6 Nov 2024 00:33:38 +0100 Subject: [PATCH 1/2] fix(module): possibility to remove module's default remark/rehype plugin from bundle --- src/module.ts | 17 ++++++++++++++-- src/runtime/parser/options.ts | 37 ++--------------------------------- src/templates/mdc-imports.ts | 12 ++++++++---- src/types/module.ts | 4 ++-- test/utils/parser.ts | 7 ++++++- 5 files changed, 33 insertions(+), 44 deletions(-) diff --git a/src/module.ts b/src/module.ts index 4a3d5ac1..f6c1ca51 100644 --- a/src/module.ts +++ b/src/module.ts @@ -34,8 +34,21 @@ export default defineNuxtModule({ }, // 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: { diff --git a/src/runtime/parser/options.ts b/src/runtime/parser/options.ts index 05bcb3ec..d8daa54a 100644 --- a/src/runtime/parser/options.ts +++ b/src/runtime/parser/options.ts @@ -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: { diff --git a/src/templates/mdc-imports.ts b/src/templates/mdc-imports.ts index d00cca6c..ade84bed 100644 --- a/src/templates/mdc-imports.ts +++ b/src/templates/mdc-imports.ts @@ -31,11 +31,15 @@ function processUnistPlugins(plugins: Record) { 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,`) } }) diff --git a/src/types/module.ts b/src/types/module.ts index f756a567..32c6f4e8 100644 --- a/src/types/module.ts +++ b/src/types/module.ts @@ -10,11 +10,11 @@ export interface ModuleOptions { /** * A map of remark plugins to be used for processing markdown. */ - remarkPlugins?: Record + remarkPlugins?: Record /** * A map of remark plugins to be used for processing markdown. */ - rehypePlugins?: Record + rehypePlugins?: Record highlight?: { /** diff --git a/test/utils/parser.ts b/test/utils/parser.ts index 3a9ebc46..62f12359 100644 --- a/test/utils/parser.ts +++ b/test/utils/parser.ts @@ -1,5 +1,6 @@ import { vi } from 'vitest' import { createWasmOnigEngine } from 'shiki/engine/oniguruma' +import remarkMdc from 'remark-mdc' import { parseMarkdown as _parseMarkDown } from '../../src/runtime/parser' import type { MDCParseOptions } from '../../src/types' import { rehypeHighlight } from '../../src/runtime/highlighter/rehype-nuxt' @@ -7,7 +8,11 @@ import { createShikiHighlighter } from '../../src/runtime/highlighter/shiki' vi.mock('#mdc-imports', () => { return { - remarkPlugins: {}, + remarkPlugins: { + 'remark-mdc': { + instance: remarkMdc + } + }, rehypePlugins: {}, highlight: {} } From 52fc47b3b57e40967bd143e7f61fc54ac50dcd6d Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Wed, 6 Nov 2024 02:43:45 +0100 Subject: [PATCH 2/2] test: update mock --- test/utils/parser.ts | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/test/utils/parser.ts b/test/utils/parser.ts index 62f12359..35cbd16d 100644 --- a/test/utils/parser.ts +++ b/test/utils/parser.ts @@ -1,19 +1,43 @@ import { vi } from 'vitest' import { createWasmOnigEngine } from 'shiki/engine/oniguruma' -import remarkMdc from 'remark-mdc' -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: { 'remark-mdc': { - instance: remarkMdc + 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'] + } } }, - rehypePlugins: {}, highlight: {} } })