Add markdown renderer for motd (#1840)

* Add markdown renderer for motd

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2022-02-10 09:27:09 +01:00 committed by GitHub
parent 21c12fafba
commit 57cb6f5b15
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 102 additions and 55 deletions

View file

@ -21,12 +21,14 @@ import { SanitizerMarkdownExtension } from '../markdown-extension/sanitizer/sani
* @param markdownContentLines The markdown code lines that should be rendered
* @param additionalMarkdownExtensions A list of {@link MarkdownExtension markdown extensions} that should be used
* @param newlinesAreBreaks Defines if the alternative break mode of markdown it should be used
* @param allowHtml Defines if html is allowed in markdown
* @return The React DOM that represents the rendered markdown code
*/
export const useConvertMarkdownToReactDom = (
markdownContentLines: string[],
additionalMarkdownExtensions: MarkdownExtension[],
newlinesAreBreaks?: boolean
newlinesAreBreaks = true,
allowHtml = true
): ValidReactDomElement[] => {
const lineNumberMapper = useMemo(() => new LineIdMapper(), [])
const htmlToReactTransformer = useMemo(() => new NodeToReactTransformer(), [])
@ -40,8 +42,8 @@ export const useConvertMarkdownToReactDom = (
const markdownIt = useMemo(() => {
const newMarkdownIt = new MarkdownIt('default', {
html: true,
breaks: newlinesAreBreaks ?? true,
html: allowHtml,
breaks: newlinesAreBreaks,
langPrefix: '',
typographer: true
})
@ -52,7 +54,7 @@ export const useConvertMarkdownToReactDom = (
newMarkdownIt.use((markdownIt) => extension.configureMarkdownItPost(markdownIt))
)
return newMarkdownIt
}, [markdownExtensions, newlinesAreBreaks])
}, [allowHtml, markdownExtensions, newlinesAreBreaks])
useMemo(() => {
const replacers = markdownExtensions.reduce(

View file

@ -6,7 +6,6 @@
import type { Element, Node } from 'domhandler'
import { isText } from 'domhandler'
import type MarkdownIt from 'markdown-it'
import type { ReactElement } from 'react'
export type ValidReactDomElement = ReactElement | string | null
@ -15,8 +14,6 @@ export type SubNodeTransform = (node: Node, subKey: number | string) => NodeRepl
export type NativeRenderer = () => ValidReactDomElement
export type MarkdownItPlugin = MarkdownIt.PluginSimple | MarkdownIt.PluginWithOptions | MarkdownIt.PluginWithParams
export const REPLACE_WITH_NOTHING = null
export const DO_NOT_REPLACE = undefined
export type NodeReplacement = ValidReactDomElement | typeof REPLACE_WITH_NOTHING | typeof DO_NOT_REPLACE