From e89fa7e97e4b6fb29d3208a0d647e43231ba7478 Mon Sep 17 00:00:00 2001 From: d3m1d0v Date: Fri, 22 Sep 2023 22:39:55 +0300 Subject: [PATCH] fix: replace require() with esm import --- demo/md-plugins.ts | 8 ++-- src/core/ExtensionsManager.ts | 8 ++-- .../markdown/Deflist/DeflistSpecs/index.ts | 6 +-- .../markdown/Mark/MarkSpecs/index.ts | 5 +-- .../Subscript/SubscriptSpecs/index.ts | 6 +-- .../Underline/UnderlineSpecs/index.ts | 6 +-- src/global.d.ts | 42 +++++++++++++++++++ 7 files changed, 59 insertions(+), 22 deletions(-) create mode 100644 src/global.d.ts diff --git a/demo/md-plugins.ts b/demo/md-plugins.ts index 9dcffb89..46c72c68 100644 --- a/demo/md-plugins.ts +++ b/demo/md-plugins.ts @@ -3,10 +3,10 @@ import type {PluginWithParams} from 'markdown-it/lib'; import file from '@doc-tools/transform/lib/plugins/file'; import imsize from '@doc-tools/transform/lib/plugins/imsize'; -const sub = require('markdown-it-sub'); -const ins = require('markdown-it-ins'); -const mark = require('markdown-it-mark'); -const color = require('markdown-it-color').colorPlugin; +import sub from 'markdown-it-sub'; +import ins from 'markdown-it-ins'; +import mark from 'markdown-it-mark'; +import color from 'markdown-it-color'; import math from 'markdown-it-katex'; import meta from '@doc-tools/transform/lib/plugins/meta'; diff --git a/src/core/ExtensionsManager.ts b/src/core/ExtensionsManager.ts index 09f69d1f..a172e77b 100644 --- a/src/core/ExtensionsManager.ts +++ b/src/core/ExtensionsManager.ts @@ -1,4 +1,5 @@ import MarkdownIt from 'markdown-it'; +import attrsPlugin, {AttrsOptions} from 'markdown-it-attrs'; import type {Plugin} from 'prosemirror-state'; import {ActionsManager} from './ActionsManager'; import {ExtensionBuilder} from './ExtensionBuilder'; @@ -15,8 +16,6 @@ import type { } from './types/extension'; import type {MarkViewConstructor, NodeViewConstructor} from './types/node-views'; -const attrs = require('markdown-it-attrs'); - type ExtensionsManagerParams = { extensions: Extension; options?: ExtensionsManagerOptions; @@ -59,7 +58,10 @@ export class ExtensionsManager { constructor({extensions, options = {}}: ExtensionsManagerParams) { this.#extensions = extensions; - this.#md = new MarkdownIt(options.mdOpts ?? {}).use(attrs, options.attrsOpts ?? {}); + this.#md = new MarkdownIt(options.mdOpts ?? {}).use( + attrsPlugin, + options.attrsOpts ?? {}, + ); this.#mdWithoutAttrs = new MarkdownIt(options.mdOpts ?? {}); if (options.linkifyTlds) { diff --git a/src/extensions/markdown/Deflist/DeflistSpecs/index.ts b/src/extensions/markdown/Deflist/DeflistSpecs/index.ts index 2ccc8955..a64c5ab6 100644 --- a/src/extensions/markdown/Deflist/DeflistSpecs/index.ts +++ b/src/extensions/markdown/Deflist/DeflistSpecs/index.ts @@ -1,4 +1,4 @@ -import type {PluginSimple} from 'markdown-it'; +import deflistPlugin from 'markdown-it-deflist'; import type {NodeSpec} from 'prosemirror-model'; import type {ExtensionAuto} from '../../../../core'; import {nodeTypeFactory} from '../../../../utils/schema'; @@ -7,8 +7,6 @@ import {fromYfm} from './fromYfm'; import {getSpec} from './spec'; import {toYfm} from './toYfm'; -const mdPlugin: PluginSimple = require('markdown-it-deflist'); - export {DeflistNode} from './const'; export const defListType = nodeTypeFactory(DeflistNode.List); export const defTermType = nodeTypeFactory(DeflistNode.Term); @@ -22,7 +20,7 @@ export type DeflistSpecsOptions = { export const DeflistSpecs: ExtensionAuto = (builder, opts) => { const spec = getSpec(opts); - builder.configureMd((md) => md.use(mdPlugin)); + builder.configureMd((md) => md.use(deflistPlugin)); builder .addNode(DeflistNode.List, () => ({ spec: spec[DeflistNode.List], diff --git a/src/extensions/markdown/Mark/MarkSpecs/index.ts b/src/extensions/markdown/Mark/MarkSpecs/index.ts index 3da48dbd..84a63b4e 100644 --- a/src/extensions/markdown/Mark/MarkSpecs/index.ts +++ b/src/extensions/markdown/Mark/MarkSpecs/index.ts @@ -1,14 +1,13 @@ -import type {PluginSimple} from 'markdown-it'; +import markPlugin from 'markdown-it-mark'; import type {ExtensionAuto} from '../../../../core'; import {markTypeFactory} from '../../../../utils/schema'; -const mdPlugin: PluginSimple = require('markdown-it-mark'); export const markMarkName = 'mark'; export const markMarkType = markTypeFactory(markMarkName); export const MarkSpecs: ExtensionAuto = (builder) => { builder - .configureMd((md) => md.use(mdPlugin)) + .configureMd((md) => md.use(markPlugin)) .addMark(markMarkName, () => ({ spec: { parseDOM: [{tag: 'mark'}], diff --git a/src/extensions/markdown/Subscript/SubscriptSpecs/index.ts b/src/extensions/markdown/Subscript/SubscriptSpecs/index.ts index 42a5a6e0..a05ffc22 100644 --- a/src/extensions/markdown/Subscript/SubscriptSpecs/index.ts +++ b/src/extensions/markdown/Subscript/SubscriptSpecs/index.ts @@ -1,15 +1,13 @@ -import type {PluginSimple} from 'markdown-it'; +import subPlugin from 'markdown-it-sub'; import type {ExtensionAuto} from '../../../../core'; import {markTypeFactory} from '../../../../utils/schema'; -const sub: PluginSimple = require('markdown-it-sub'); - export const subscriptMarkName = 'sub'; export const subscriptType = markTypeFactory(subscriptMarkName); export const SubscriptSpecs: ExtensionAuto = (builder) => { builder - .configureMd((md) => md.use(sub)) + .configureMd((md) => md.use(subPlugin)) .addMark(subscriptMarkName, () => ({ spec: { excludes: '_', diff --git a/src/extensions/markdown/Underline/UnderlineSpecs/index.ts b/src/extensions/markdown/Underline/UnderlineSpecs/index.ts index 7779a229..2e58df09 100644 --- a/src/extensions/markdown/Underline/UnderlineSpecs/index.ts +++ b/src/extensions/markdown/Underline/UnderlineSpecs/index.ts @@ -1,15 +1,13 @@ -import type {PluginSimple} from 'markdown-it'; +import insPlugin from 'markdown-it-ins'; import type {ExtensionAuto} from '../../../../core'; import {markTypeFactory} from '../../../../utils/schema'; -const ins: PluginSimple = require('markdown-it-ins'); - export const underlineMarkName = 'ins'; export const underlineType = markTypeFactory(underlineMarkName); export const UnderlineSpecs: ExtensionAuto = (builder) => { builder - .configureMd((md) => md.use(ins)) + .configureMd((md) => md.use(insPlugin)) .addMark(underlineMarkName, () => ({ spec: { parseDOM: [{tag: 'ins'}, {tag: 'u'}], diff --git a/src/global.d.ts b/src/global.d.ts new file mode 100644 index 00000000..fcb963a0 --- /dev/null +++ b/src/global.d.ts @@ -0,0 +1,42 @@ +/* eslint-disable @typescript-eslint/no-duplicate-imports */ + +declare module 'markdown-it-attrs' { + import type {PluginWithOptions} from 'markdown-it'; + export type AttrsOptions = { + /** @default '{' */ + leftDelimiter?: string; + /** @default '}' */ + rightDelimiter?: string; + /** + * empty array = all attributes are allowed + * @default [] + */ + allowedAttributes?: (string | RegExp)[]; + }; + declare const plugin: PluginWithOptions; + export = plugin; +} + +declare module 'markdown-it-deflist' { + import type {PluginSimple} from 'markdown-it'; + declare const plugin: PluginSimple; + export = plugin; +} + +declare module 'markdown-it-mark' { + import type {PluginSimple} from 'markdown-it'; + declare const plugin: PluginSimple; + export = plugin; +} + +declare module 'markdown-it-sub' { + import type {PluginSimple} from 'markdown-it'; + declare const plugin: PluginSimple; + export = plugin; +} + +declare module 'markdown-it-ins' { + import type {PluginSimple} from 'markdown-it'; + declare const plugin: PluginSimple; + export = plugin; +}