mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-21 18:55:19 -04:00
Add markdown-extension-collection.ts
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
parent
ed6ab1b1fe
commit
f2958a77fe
2 changed files with 67 additions and 31 deletions
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import type { MarkdownExtension } from './markdown-extension'
|
||||
import type { ComponentReplacer } from '../replace-components/component-replacer'
|
||||
import type { Document } from 'domhandler'
|
||||
import type MarkdownIt from 'markdown-it'
|
||||
import { SanitizerMarkdownExtension } from './sanitizer/sanitizer-markdown-extension'
|
||||
|
||||
/**
|
||||
* Contains multiple {@link MarkdownExtension} and uses them to configure parts of the renderer.
|
||||
*/
|
||||
export class MarkdownExtensionCollection {
|
||||
private extensions: MarkdownExtension[]
|
||||
|
||||
public constructor(additionalExtensions: MarkdownExtension[]) {
|
||||
const tagWhiteLists = additionalExtensions.flatMap((extension) => extension.buildTagNameWhitelist())
|
||||
|
||||
this.extensions = [...additionalExtensions, new SanitizerMarkdownExtension(tagWhiteLists)]
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the given {@link MarkdownIt markdown-it instance} using every saved {@link MarkdownExtension extension}.
|
||||
*
|
||||
* @param markdownIt The markdown-it instance to configure.
|
||||
*/
|
||||
public configureMarkdownIt(markdownIt: MarkdownIt): void {
|
||||
this.extensions.forEach((extension) => markdownIt.use((markdownIt) => extension.configureMarkdownIt(markdownIt)))
|
||||
this.extensions.forEach((extension) =>
|
||||
markdownIt.use((markdownIt) => extension.configureMarkdownItPost(markdownIt))
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a node processor that applies the node processor of every saved {@link MarkdownExtension extension}.
|
||||
*
|
||||
* @return the created node processor function
|
||||
*/
|
||||
public buildFlatNodeProcessor(): (document: Document) => Document {
|
||||
return this.extensions
|
||||
.flatMap((extension) => extension.buildNodeProcessors())
|
||||
.reduce(
|
||||
(state, processor) => (document: Document) => state(processor.process(document)),
|
||||
(document: Document) => document
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects all {@link ComponentReplacer component replacers} from all saved {@link MarkdownExtension extension}.
|
||||
*/
|
||||
public buildReplacers(): ComponentReplacer[] {
|
||||
return this.extensions.flatMap((extension) => extension.buildReplacers())
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue