import { IMG_REGEX, YOUTUBE_REGEX, WHITE_LIST, DOMParser, POST_REGEX, } from '../consts'
import { extractYtStartTime } from '../helper'
import { proxifyImageSrc } from '../proxify-image-src'
import { linkify } from './linkify.method'
export function text(node: HTMLElement, forApp: boolean, webp: boolean): void {
if (['a', 'code'].includes(node.parentNode.nodeName)) {
return
}
const linkified = linkify(node.nodeValue, forApp, webp)
if (linkified !== node.nodeValue) {
const replaceNode = DOMParser.parseFromString(
`${linkified}`
)
node.parentNode.insertBefore(replaceNode, node)
node.parentNode.removeChild(node)
return
}
if (node.nodeValue.match(IMG_REGEX)) {
const attrs = forApp ? `data-href="${node.nodeValue}" class="markdown-img-link" src="${proxifyImageSrc(node.nodeValue, 0, 0, webp ? 'webp' : 'match')}"` : `class="markdown-img-link" src="${proxifyImageSrc(node.nodeValue, 0, 0, webp ? 'webp' : 'match')}"`
const replaceNode = DOMParser.parseFromString(
``
)
node.parentNode.replaceChild(replaceNode, node)
}
// If a youtube video
if (node.nodeValue.match(YOUTUBE_REGEX)) {
const e = YOUTUBE_REGEX.exec(node.nodeValue)
if (e[1]) {
const vid = e[1]
const thumbnail = proxifyImageSrc(`https://img.youtube.com/vi/${vid.split('?')[0]}/hqdefault.jpg`, 0, 0, webp ? 'webp' : 'match')
const embedSrc = `https://www.youtube.com/embed/${vid}?autoplay=1`
let attrs = `class="markdown-video-link markdown-video-link-youtube" data-embed-src="${embedSrc}" data-youtube="${vid}"`
//extract start time if available
const startTime = extractYtStartTime(node.nodeValue);
if(startTime){
attrs = attrs.concat(` data-start-time="${startTime}"`);
}
const thumbImg = node.ownerDocument.createElement('img')
thumbImg.setAttribute('class', 'no-replace video-thumbnail')
thumbImg.setAttribute('src', thumbnail)
const play = node.ownerDocument.createElement('span')
play.setAttribute('class', 'markdown-video-play')
const replaceNode = DOMParser.parseFromString(`