mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-14 15:14:56 -04:00

Signed-off-by: Tilman Vatteroth <tilman.vatteroth@tu-dortmund.de> Co-authored-by: Tilman Vatteroth <tilman.vatteroth@tu-dortmund.de> Co-authored-by: Erik Michelson <github@erik.michelson.eu>
28 lines
1.1 KiB
TypeScript
28 lines
1.1 KiB
TypeScript
import { DomElement } from 'domhandler'
|
|
import MarkdownIt from 'markdown-it'
|
|
import markdownItRegex from 'markdown-it-regex'
|
|
import React from 'react'
|
|
import { ComponentReplacer } from '../ComponentReplacer'
|
|
import { getAttributesFromHedgeDocTag } from '../utils'
|
|
import { replaceLegacyYoutubeShortCode } from './replace-legacy-youtube-short-code'
|
|
import { replaceYouTubeLink } from './replace-youtube-link'
|
|
import { YouTubeFrame } from './youtube-frame'
|
|
|
|
export class YoutubeReplacer extends ComponentReplacer {
|
|
private counterMap: Map<string, number> = new Map<string, number>()
|
|
|
|
public getReplacement (node: DomElement): React.ReactElement | undefined {
|
|
const attributes = getAttributesFromHedgeDocTag(node, 'youtube')
|
|
if (attributes && attributes.id) {
|
|
const videoId = attributes.id
|
|
const count = (this.counterMap.get(videoId) || 0) + 1
|
|
this.counterMap.set(videoId, count)
|
|
return <YouTubeFrame id={videoId}/>
|
|
}
|
|
}
|
|
|
|
public static readonly markdownItPlugin: MarkdownIt.PluginSimple = (markdownIt) => {
|
|
markdownItRegex(markdownIt, replaceYouTubeLink)
|
|
markdownItRegex(markdownIt, replaceLegacyYoutubeShortCode)
|
|
}
|
|
}
|