feat(renderer): Add intermediate class for markdown renderer extensions with event emitter

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2023-04-08 18:15:48 +02:00
parent b615ef49a3
commit 37e1482c46
6 changed files with 24 additions and 8 deletions

View file

@ -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()
}
}

View file

@ -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

View file

@ -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

View file

@ -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({

View file

@ -14,7 +14,7 @@ import { Fragment } from 'react'
export interface MarkdownRendererExtensionOptions {
frontendConfig: FrontendConfig
eventEmitter?: EventEmitter2
eventEmitter: EventEmitter2
}
export abstract class AppExtension {

View file

@ -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,