From 37e1482c466fb06705c4a47943bc1061f3abb7d0 Mon Sep 17 00:00:00 2001 From: Tilman Vatteroth Date: Sat, 8 Apr 2023 18:15:48 +0200 Subject: [PATCH] feat(renderer): Add intermediate class for markdown renderer extensions with event emitter Signed-off-by: Tilman Vatteroth --- .../base/event-markdown-renderer-extension.ts | 16 ++++++++++++++++ .../base/markdown-renderer-extension.ts | 3 --- .../table-of-contents-markdown-extension.ts | 4 ++-- .../hooks/use-markdown-extensions.ts | 3 +++ frontend/src/extensions/base/app-extension.ts | 2 +- .../task-list/task-list-markdown-extension.ts | 4 ++-- 6 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 frontend/src/components/markdown-renderer/extensions/base/event-markdown-renderer-extension.ts diff --git a/frontend/src/components/markdown-renderer/extensions/base/event-markdown-renderer-extension.ts b/frontend/src/components/markdown-renderer/extensions/base/event-markdown-renderer-extension.ts new file mode 100644 index 000000000..65950563b --- /dev/null +++ b/frontend/src/components/markdown-renderer/extensions/base/event-markdown-renderer-extension.ts @@ -0,0 +1,16 @@ +/* + * SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file) + * + * SPDX-License-Identifier: AGPL-3.0-only + */ +import { MarkdownRendererExtension } from './markdown-renderer-extension' +import type { EventEmitter2 } from 'eventemitter2' + +/** + * Base class for Markdown renderer extensions that need an event emitter. + */ +export abstract class EventMarkdownRendererExtension extends MarkdownRendererExtension { + constructor(protected readonly eventEmitter: EventEmitter2) { + super() + } +} diff --git a/frontend/src/components/markdown-renderer/extensions/base/markdown-renderer-extension.ts b/frontend/src/components/markdown-renderer/extensions/base/markdown-renderer-extension.ts index d4338300a..d60f8117a 100644 --- a/frontend/src/components/markdown-renderer/extensions/base/markdown-renderer-extension.ts +++ b/frontend/src/components/markdown-renderer/extensions/base/markdown-renderer-extension.ts @@ -5,15 +5,12 @@ */ import type { NodeProcessor } from '../../node-preprocessors/node-processor' import type { ComponentReplacer } from '../../replace-components/component-replacer' -import type { EventEmitter2 } from 'eventemitter2' import type MarkdownIt from 'markdown-it' /** * Base class for Markdown extensions. */ export abstract class MarkdownRendererExtension { - constructor(protected readonly eventEmitter?: EventEmitter2) {} - // eslint-disable-next-line @typescript-eslint/no-unused-vars public configureMarkdownIt(markdownIt: MarkdownIt): void { return diff --git a/frontend/src/components/markdown-renderer/extensions/table-of-contents/table-of-contents-markdown-extension.ts b/frontend/src/components/markdown-renderer/extensions/table-of-contents/table-of-contents-markdown-extension.ts index 930bed309..00d956c8b 100644 --- a/frontend/src/components/markdown-renderer/extensions/table-of-contents/table-of-contents-markdown-extension.ts +++ b/frontend/src/components/markdown-renderer/extensions/table-of-contents/table-of-contents-markdown-extension.ts @@ -4,7 +4,7 @@ * SPDX-License-Identifier: AGPL-3.0-only */ import { tocSlugify } from '../../../editor-page/table-of-contents/toc-slugify' -import { MarkdownRendererExtension } from '../base/markdown-renderer-extension' +import { EventMarkdownRendererExtension } from '../base/event-markdown-renderer-extension' import type { TocAst } from '@hedgedoc/markdown-it-plugins' import { toc } from '@hedgedoc/markdown-it-plugins' import equal from 'fast-deep-equal' @@ -13,7 +13,7 @@ import type MarkdownIt from 'markdown-it' /** * Adds table of content to the markdown rendering. */ -export class TableOfContentsMarkdownExtension extends MarkdownRendererExtension { +export class TableOfContentsMarkdownExtension extends EventMarkdownRendererExtension { public static readonly EVENT_NAME = 'TocChange' private lastAst: TocAst | undefined = undefined diff --git a/frontend/src/components/markdown-renderer/hooks/use-markdown-extensions.ts b/frontend/src/components/markdown-renderer/hooks/use-markdown-extensions.ts index fdb98dd42..459de2256 100644 --- a/frontend/src/components/markdown-renderer/hooks/use-markdown-extensions.ts +++ b/frontend/src/components/markdown-renderer/hooks/use-markdown-extensions.ts @@ -28,6 +28,9 @@ export const useMarkdownExtensions = ( const extensionEventEmitter = useExtensionEventEmitter() const frontendConfig = useFrontendConfig() return useMemo(() => { + if (!extensionEventEmitter) { + throw new Error("can't build markdown render extensions without event emitter.") + } return [ ...optionalAppExtensions.flatMap((extension) => extension.buildMarkdownRendererExtensions({ diff --git a/frontend/src/extensions/base/app-extension.ts b/frontend/src/extensions/base/app-extension.ts index 0d1952279..f8ab1419d 100644 --- a/frontend/src/extensions/base/app-extension.ts +++ b/frontend/src/extensions/base/app-extension.ts @@ -14,7 +14,7 @@ import { Fragment } from 'react' export interface MarkdownRendererExtensionOptions { frontendConfig: FrontendConfig - eventEmitter?: EventEmitter2 + eventEmitter: EventEmitter2 } export abstract class AppExtension { diff --git a/frontend/src/extensions/extra-integrations/task-list/task-list-markdown-extension.ts b/frontend/src/extensions/extra-integrations/task-list/task-list-markdown-extension.ts index a3d5e08eb..e65a8fa86 100644 --- a/frontend/src/extensions/extra-integrations/task-list/task-list-markdown-extension.ts +++ b/frontend/src/extensions/extra-integrations/task-list/task-list-markdown-extension.ts @@ -3,7 +3,7 @@ * * SPDX-License-Identifier: AGPL-3.0-only */ -import { MarkdownRendererExtension } from '../../../components/markdown-renderer/extensions/base/markdown-renderer-extension' +import { EventMarkdownRendererExtension } from '../../../components/markdown-renderer/extensions/base/event-markdown-renderer-extension' import type { ComponentReplacer } from '../../../components/markdown-renderer/replace-components/component-replacer' import { TaskListReplacer } from './task-list-replacer' import { taskLists } from '@hedgedoc/markdown-it-plugins' @@ -12,7 +12,7 @@ import type MarkdownIt from 'markdown-it' /** * Adds support for interactive checkbox lists to the markdown rendering using the github checklist syntax. */ -export class TaskListMarkdownExtension extends MarkdownRendererExtension { +export class TaskListMarkdownExtension extends EventMarkdownRendererExtension { public configureMarkdownIt(markdownIt: MarkdownIt): void { taskLists(markdownIt, { enabled: true,