mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-12 22:26:08 -04:00
28 lines
775 B
TypeScript
28 lines
775 B
TypeScript
/*
|
|
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
|
*
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
import { NodeProcessor } from '../../node-preprocessors/node-processor'
|
|
import render from 'dom-serializer'
|
|
import type { Document } from 'domhandler'
|
|
import DOMPurify from 'dompurify'
|
|
import { parseDocument } from 'htmlparser2'
|
|
|
|
/**
|
|
* Sanitizes the given {@link Document document}.
|
|
*
|
|
* @see https://cure53.de/purify
|
|
*/
|
|
export class SanitizerNodePreprocessor extends NodeProcessor {
|
|
constructor(private tagNameWhiteList: string[]) {
|
|
super()
|
|
}
|
|
|
|
process(nodes: Document): Document {
|
|
const sanitizedHtml = DOMPurify.sanitize(render(nodes), {
|
|
ADD_TAGS: this.tagNameWhiteList
|
|
})
|
|
return parseDocument(sanitizedHtml)
|
|
}
|
|
}
|