2021-06-07 13:16:10 +00:00
|
|
|
import { unescape, flattenDeep } from 'lodash'
|
2021-06-12 16:47:23 +00:00
|
|
|
import { getTagName, processTextForEmoji, getAttrs } from 'src/services/html_converter/utility.service.js'
|
|
|
|
import { convertHtmlToTree } from 'src/services/html_converter/html_tree_converter.service.js'
|
2021-06-10 15:52:01 +00:00
|
|
|
import { convertHtmlToLines } from 'src/services/html_converter/html_line_converter.service.js'
|
2022-07-10 07:39:02 +00:00
|
|
|
import { marked } from 'marked'
|
|
|
|
import markedMfm from 'marked-mfm'
|
2021-06-07 00:14:48 +00:00
|
|
|
import StillImage from 'src/components/still-image/still-image.vue'
|
2021-08-14 23:41:53 +00:00
|
|
|
import MentionsLine, { MENTIONS_LIMIT } from 'src/components/mentions_line/mentions_line.vue'
|
2021-08-23 17:57:21 +00:00
|
|
|
import HashtagLink from 'src/components/hashtag_link/hashtag_link.vue'
|
2021-06-07 00:14:48 +00:00
|
|
|
|
|
|
|
import './rich_content.scss'
|
|
|
|
|
2022-08-17 09:34:50 +00:00
|
|
|
const selectContent = (html, sourceContent, mfm) => {
|
|
|
|
if (!mfm) return html
|
|
|
|
return sourceContent === '' ? html : sourceContent
|
|
|
|
}
|
|
|
|
|
2021-06-12 17:42:17 +00:00
|
|
|
/**
|
|
|
|
* RichContent, The Über-powered component for rendering Post HTML.
|
|
|
|
*
|
|
|
|
* This takes post HTML and does multiple things to it:
|
2021-08-14 23:41:53 +00:00
|
|
|
* - Groups all mentions into <MentionsLine>, this affects all mentions regardles
|
|
|
|
* of where they are (beginning/middle/end), even single mentions are converted
|
|
|
|
* to a <MentionsLine> containing single <MentionLink>.
|
2021-06-12 17:42:17 +00:00
|
|
|
* - Replaces emoji shortcodes with <StillImage>'d images.
|
|
|
|
*
|
|
|
|
* There are two problems with this component's architecture:
|
|
|
|
* 1. Parsing HTML and rendering are inseparable. Attempts to separate the two
|
|
|
|
* proven to be a massive overcomplication due to amount of things done here.
|
|
|
|
* 2. We need to output both render and some extra data, which seems to be imp-
|
|
|
|
* possible in vue. Current solution is to emit 'parseReady' event when parsing
|
|
|
|
* is done within render() function.
|
|
|
|
*
|
|
|
|
* Apart from that one small hiccup with emit in render this _should_ be vue3-ready
|
|
|
|
*/
|
2022-03-16 20:13:21 +00:00
|
|
|
export default {
|
2021-06-07 00:14:48 +00:00
|
|
|
name: 'RichContent',
|
2022-04-12 15:10:19 +00:00
|
|
|
components: {
|
|
|
|
MentionsLine,
|
|
|
|
HashtagLink
|
|
|
|
},
|
2021-06-07 00:14:48 +00:00
|
|
|
props: {
|
2021-06-07 15:39:51 +00:00
|
|
|
// Original html content
|
2021-06-07 00:14:48 +00:00
|
|
|
html: {
|
|
|
|
required: true,
|
|
|
|
type: String
|
|
|
|
},
|
2021-06-22 17:16:26 +00:00
|
|
|
attentions: {
|
|
|
|
required: false,
|
|
|
|
default: () => []
|
|
|
|
},
|
2021-06-07 15:39:51 +00:00
|
|
|
// Emoji object, as in status.emojis, note the "s" at the end...
|
2021-06-07 00:14:48 +00:00
|
|
|
emoji: {
|
|
|
|
required: true,
|
|
|
|
type: Array
|
2021-06-07 15:39:51 +00:00
|
|
|
},
|
|
|
|
// Whether to handle links or not (posts: yes, everything else: no)
|
|
|
|
handleLinks: {
|
|
|
|
required: false,
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
2021-06-10 09:08:31 +00:00
|
|
|
},
|
|
|
|
// Meme arrows
|
|
|
|
greentext: {
|
|
|
|
required: false,
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
2022-07-10 07:39:02 +00:00
|
|
|
},
|
|
|
|
// Render Misskey Markdown
|
|
|
|
mfm: {
|
|
|
|
required: false,
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
2022-08-17 09:34:50 +00:00
|
|
|
},
|
|
|
|
sourceContent: {
|
|
|
|
required: false,
|
|
|
|
type: String,
|
|
|
|
default: ''
|
2021-06-07 00:14:48 +00:00
|
|
|
}
|
|
|
|
},
|
2021-06-11 00:11:58 +00:00
|
|
|
// NEVER EVER TOUCH DATA INSIDE RENDER
|
2022-03-16 20:13:21 +00:00
|
|
|
render () {
|
2022-07-13 17:59:41 +00:00
|
|
|
// Don't greentext MFM
|
2022-07-15 09:55:47 +00:00
|
|
|
const greentext = this.mfm ? false : this.greentext
|
2022-07-13 17:59:41 +00:00
|
|
|
|
2021-06-10 09:08:31 +00:00
|
|
|
// Pre-process HTML
|
2022-08-17 09:34:50 +00:00
|
|
|
const useContent = selectContent(this.html, this.sourceContent, this.mfm)
|
|
|
|
const { newHtml: html } = preProcessPerLine(useContent, greentext)
|
2021-08-11 23:49:37 +00:00
|
|
|
let currentMentions = null // Current chain of mentions, we group all mentions together
|
2021-08-18 17:54:04 +00:00
|
|
|
// This is used to recover spacing removed when parsing mentions
|
|
|
|
let lastSpacing = ''
|
2021-08-11 23:49:37 +00:00
|
|
|
|
2021-06-11 00:11:58 +00:00
|
|
|
const lastTags = [] // Tags that appear at the end of post body
|
|
|
|
const writtenMentions = [] // All mentions that appear in post body
|
2021-08-14 23:41:53 +00:00
|
|
|
const invisibleMentions = [] // All mentions that go beyond the limiter (see MentionsLine)
|
|
|
|
// to collapse too many mentions in a row
|
2021-06-11 00:11:58 +00:00
|
|
|
const writtenTags = [] // All tags that appear in post body
|
|
|
|
// unique index for vue "tag" property
|
|
|
|
let mentionIndex = 0
|
|
|
|
let tagsIndex = 0
|
2021-06-10 09:08:31 +00:00
|
|
|
|
2021-06-07 00:14:48 +00:00
|
|
|
const renderImage = (tag) => {
|
2021-06-07 15:39:51 +00:00
|
|
|
return <StillImage
|
2022-03-17 06:47:19 +00:00
|
|
|
{...getAttrs(tag)}
|
2021-06-07 15:39:51 +00:00
|
|
|
class="img"
|
|
|
|
/>
|
2021-06-07 00:14:48 +00:00
|
|
|
}
|
2021-06-10 09:08:31 +00:00
|
|
|
|
2021-06-11 00:11:58 +00:00
|
|
|
const renderHashtag = (attrs, children, encounteredTextReverse) => {
|
2022-03-22 14:40:45 +00:00
|
|
|
const { index, ...linkData } = getLinkData(attrs, children, tagsIndex++)
|
2021-06-11 00:11:58 +00:00
|
|
|
writtenTags.push(linkData)
|
|
|
|
if (!encounteredTextReverse) {
|
|
|
|
lastTags.push(linkData)
|
|
|
|
}
|
2022-04-12 15:10:19 +00:00
|
|
|
const { url, tag, content } = linkData
|
|
|
|
return <HashtagLink url={url} tag={tag} content={content}/>
|
2021-06-11 00:11:58 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 17:16:26 +00:00
|
|
|
const renderMention = (attrs, children) => {
|
2021-06-11 00:11:58 +00:00
|
|
|
const linkData = getLinkData(attrs, children, mentionIndex++)
|
2021-06-22 17:16:26 +00:00
|
|
|
linkData.notifying = this.attentions.some(a => a.statusnet_profile_url === linkData.url)
|
2021-06-11 00:11:58 +00:00
|
|
|
writtenMentions.push(linkData)
|
2021-08-11 23:49:37 +00:00
|
|
|
if (currentMentions === null) {
|
|
|
|
currentMentions = []
|
|
|
|
}
|
|
|
|
currentMentions.push(linkData)
|
2021-08-14 23:41:53 +00:00
|
|
|
if (currentMentions.length > MENTIONS_LIMIT) {
|
|
|
|
invisibleMentions.push(linkData)
|
|
|
|
}
|
2021-08-11 23:49:37 +00:00
|
|
|
if (currentMentions.length === 1) {
|
|
|
|
return <MentionsLine mentions={ currentMentions } />
|
2021-06-11 00:11:58 +00:00
|
|
|
} else {
|
2021-08-11 23:49:37 +00:00
|
|
|
return ''
|
2021-06-11 00:11:58 +00:00
|
|
|
}
|
2021-06-07 13:16:10 +00:00
|
|
|
}
|
2021-06-07 15:39:51 +00:00
|
|
|
|
2022-07-10 07:39:02 +00:00
|
|
|
const renderMisskeyMarkdown = (content) => {
|
2022-08-02 09:37:18 +00:00
|
|
|
// Untangle code blocks from <br> tags and other html encodings
|
2022-08-01 13:13:14 +00:00
|
|
|
const codeblocks = content.match(/(<br\/>)?(~~~|```)\w*<br\/>.+?<br\/>\2\1?/g)
|
|
|
|
if (codeblocks) {
|
|
|
|
codeblocks.forEach((pre) => {
|
2022-08-02 09:37:18 +00:00
|
|
|
content = content.replace(pre,
|
|
|
|
pre.replaceAll('<br/>', '\n')
|
|
|
|
.replaceAll('&', '&')
|
|
|
|
.replaceAll('<', '<')
|
|
|
|
.replaceAll('>', '>')
|
|
|
|
.replaceAll('"', '"')
|
|
|
|
.replaceAll(''', "'")
|
|
|
|
)
|
2022-08-01 13:13:14 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-10 07:39:02 +00:00
|
|
|
marked.use(markedMfm, {
|
|
|
|
mangle: false,
|
|
|
|
gfm: false,
|
|
|
|
breaks: true
|
|
|
|
})
|
2022-07-10 14:38:35 +00:00
|
|
|
const mfmHtml = document.createElement('template')
|
|
|
|
mfmHtml.innerHTML = marked.parse(content)
|
|
|
|
|
|
|
|
// Add options with set values to CSS
|
2022-08-11 17:14:05 +00:00
|
|
|
if (mfmHtml.content.firstChild) {
|
|
|
|
Array.from(mfmHtml.content.firstChild.getElementsByClassName('mfm')).map((el) => {
|
|
|
|
if (el.dataset.speed) {
|
|
|
|
el.style.animationDuration = el.dataset.speed
|
2022-07-10 14:38:35 +00:00
|
|
|
}
|
2022-08-11 17:14:05 +00:00
|
|
|
if (el.dataset.deg) {
|
|
|
|
el.style.transform = `rotate(${el.dataset.deg}deg)`
|
|
|
|
}
|
|
|
|
if (Array.from(el.classList).includes('_mfm_font_')) {
|
|
|
|
const font = Object.keys(el.dataset)[0]
|
|
|
|
if (['serif', 'monospace', 'cursive', 'fantasy', 'emoji', 'math'].includes(font)) {
|
|
|
|
el.style.fontFamily = font
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-07-10 14:38:35 +00:00
|
|
|
|
|
|
|
return mfmHtml.innerHTML
|
2022-07-10 07:39:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-11 00:11:58 +00:00
|
|
|
// Processor to use with html_tree_converter
|
2022-08-17 09:34:50 +00:00
|
|
|
const processItem = (item, index, array) => {
|
2021-06-10 09:08:31 +00:00
|
|
|
// Handle text nodes - just add emoji
|
2021-06-07 00:14:48 +00:00
|
|
|
if (typeof item === 'string') {
|
2021-06-08 10:42:16 +00:00
|
|
|
const emptyText = item.trim() === ''
|
2021-08-11 23:49:37 +00:00
|
|
|
if (item.includes('\n')) {
|
|
|
|
currentMentions = null
|
2021-06-08 08:38:44 +00:00
|
|
|
}
|
2021-08-11 23:49:37 +00:00
|
|
|
if (emptyText) {
|
|
|
|
// don't include spaces when processing mentions - we'll include them
|
|
|
|
// in MentionsLine
|
2021-08-18 17:54:04 +00:00
|
|
|
lastSpacing = item
|
2022-02-03 20:23:28 +00:00
|
|
|
// Don't remove last space in a container (fixes poast mentions)
|
|
|
|
return (index !== array.length - 1) && (currentMentions !== null) ? item.trim() : item
|
2021-06-08 08:38:44 +00:00
|
|
|
}
|
2021-08-15 15:11:38 +00:00
|
|
|
|
2021-08-11 23:49:37 +00:00
|
|
|
currentMentions = null
|
2021-06-07 00:14:48 +00:00
|
|
|
if (item.includes(':')) {
|
2021-06-18 18:42:46 +00:00
|
|
|
item = ['', processTextForEmoji(
|
|
|
|
item,
|
2021-06-07 00:14:48 +00:00
|
|
|
this.emoji,
|
|
|
|
({ shortcode, url }) => {
|
|
|
|
return <StillImage
|
2021-06-07 09:49:54 +00:00
|
|
|
class="emoji img"
|
2021-06-07 00:14:48 +00:00
|
|
|
src={url}
|
|
|
|
title={`:${shortcode}:`}
|
|
|
|
alt={`:${shortcode}:`}
|
|
|
|
/>
|
|
|
|
}
|
2021-06-15 11:43:44 +00:00
|
|
|
)]
|
2021-06-07 00:14:48 +00:00
|
|
|
}
|
2021-06-18 18:42:46 +00:00
|
|
|
return item
|
2021-06-07 00:14:48 +00:00
|
|
|
}
|
2021-06-10 09:08:31 +00:00
|
|
|
|
2021-06-07 15:39:51 +00:00
|
|
|
// Handle tag nodes
|
2021-06-07 00:14:48 +00:00
|
|
|
if (Array.isArray(item)) {
|
2021-06-15 11:43:44 +00:00
|
|
|
const [opener, children, closer] = item
|
2021-06-07 00:14:48 +00:00
|
|
|
const Tag = getTagName(opener)
|
2021-06-11 00:11:58 +00:00
|
|
|
const attrs = getAttrs(opener)
|
2021-08-18 17:54:04 +00:00
|
|
|
const previouslyMentions = currentMentions !== null
|
|
|
|
/* During grouping of mentions we trim all the empty text elements
|
|
|
|
* This padding is added to recover last space removed in case
|
|
|
|
* we have a tag right next to mentions
|
|
|
|
*/
|
|
|
|
const mentionsLinePadding =
|
|
|
|
// Padding is only needed if we just finished parsing mentions
|
|
|
|
previouslyMentions &&
|
|
|
|
// Don't add padding if content is string and has padding already
|
|
|
|
!(children && typeof children[0] === 'string' && children[0].match(/^\s/))
|
|
|
|
? lastSpacing
|
|
|
|
: ''
|
2021-06-07 13:16:10 +00:00
|
|
|
switch (Tag) {
|
2021-08-11 23:49:37 +00:00
|
|
|
case 'br':
|
|
|
|
currentMentions = null
|
|
|
|
break
|
2021-06-07 15:39:51 +00:00
|
|
|
case 'img': // replace images with StillImage
|
2021-08-18 17:54:04 +00:00
|
|
|
return ['', [mentionsLinePadding, renderImage(opener)], '']
|
2021-06-07 15:39:51 +00:00
|
|
|
case 'a': // replace mentions with MentionLink
|
|
|
|
if (!this.handleLinks) break
|
2021-06-07 13:16:10 +00:00
|
|
|
if (attrs['class'] && attrs['class'].includes('mention')) {
|
2021-06-15 22:20:20 +00:00
|
|
|
// Handling mentions here
|
2021-08-11 23:49:37 +00:00
|
|
|
return renderMention(attrs, children)
|
2021-06-15 22:20:20 +00:00
|
|
|
} else {
|
2021-08-11 23:49:37 +00:00
|
|
|
currentMentions = null
|
2021-08-18 17:54:04 +00:00
|
|
|
break
|
2021-06-07 13:16:10 +00:00
|
|
|
}
|
2021-08-11 23:49:37 +00:00
|
|
|
case 'span':
|
2021-08-12 16:37:04 +00:00
|
|
|
if (this.handleLinks && attrs['class'] && attrs['class'].includes('h-card')) {
|
2021-08-11 23:49:37 +00:00
|
|
|
return ['', children.map(processItem), '']
|
|
|
|
}
|
2021-06-07 00:14:48 +00:00
|
|
|
}
|
2021-06-15 22:20:20 +00:00
|
|
|
|
2021-06-07 00:14:48 +00:00
|
|
|
if (children !== undefined) {
|
2021-08-18 17:54:04 +00:00
|
|
|
return [
|
2021-08-23 18:36:18 +00:00
|
|
|
'',
|
|
|
|
[
|
|
|
|
mentionsLinePadding,
|
|
|
|
[opener, children.map(processItem), closer]
|
|
|
|
],
|
|
|
|
''
|
2021-08-18 17:54:04 +00:00
|
|
|
]
|
2021-06-07 00:14:48 +00:00
|
|
|
} else {
|
2021-08-18 17:54:04 +00:00
|
|
|
return ['', [mentionsLinePadding, item], '']
|
2021-06-07 00:14:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-11 00:11:58 +00:00
|
|
|
|
2021-06-10 15:52:01 +00:00
|
|
|
// Processor for back direction (for finding "last" stuff, just easier this way)
|
|
|
|
let encounteredTextReverse = false
|
|
|
|
const processItemReverse = (item, index, array, what) => {
|
|
|
|
// Handle text nodes - just add emoji
|
|
|
|
if (typeof item === 'string') {
|
|
|
|
const emptyText = item.trim() === ''
|
2021-06-12 14:20:21 +00:00
|
|
|
if (emptyText) return item
|
2021-06-10 15:52:01 +00:00
|
|
|
if (!encounteredTextReverse) encounteredTextReverse = true
|
2021-06-18 18:42:46 +00:00
|
|
|
return unescape(item)
|
2021-06-10 15:52:01 +00:00
|
|
|
} else if (Array.isArray(item)) {
|
|
|
|
// Handle tag nodes
|
|
|
|
const [opener, children] = item
|
2021-06-15 11:43:44 +00:00
|
|
|
const Tag = opener === '' ? '' : getTagName(opener)
|
2021-06-10 15:52:01 +00:00
|
|
|
switch (Tag) {
|
|
|
|
case 'a': // replace mentions with MentionLink
|
|
|
|
if (!this.handleLinks) break
|
|
|
|
const attrs = getAttrs(opener)
|
|
|
|
// should only be this
|
2021-08-23 17:57:21 +00:00
|
|
|
if (
|
|
|
|
(attrs['class'] && attrs['class'].includes('hashtag')) || // Pleroma style
|
|
|
|
(attrs['rel'] === 'tag') // Mastodon style
|
|
|
|
) {
|
2021-06-10 15:52:01 +00:00
|
|
|
return renderHashtag(attrs, children, encounteredTextReverse)
|
2021-06-15 22:20:20 +00:00
|
|
|
} else {
|
|
|
|
attrs.target = '_blank'
|
|
|
|
const newChildren = [...children].reverse().map(processItemReverse).reverse()
|
|
|
|
|
2022-03-17 06:47:19 +00:00
|
|
|
return <a {...attrs}>
|
2021-06-15 22:20:20 +00:00
|
|
|
{ newChildren }
|
|
|
|
</a>
|
2021-06-10 15:52:01 +00:00
|
|
|
}
|
2021-06-15 11:43:44 +00:00
|
|
|
case '':
|
|
|
|
return [...children].reverse().map(processItemReverse).reverse()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render tag as is
|
|
|
|
if (children !== undefined) {
|
2021-06-15 22:20:20 +00:00
|
|
|
const newChildren = Array.isArray(children)
|
|
|
|
? [...children].reverse().map(processItemReverse).reverse()
|
|
|
|
: children
|
2022-03-22 14:40:45 +00:00
|
|
|
return <Tag {...getAttrs(opener)}>
|
2021-06-15 22:20:20 +00:00
|
|
|
{ newChildren }
|
2021-06-15 11:43:44 +00:00
|
|
|
</Tag>
|
|
|
|
} else {
|
|
|
|
return <Tag/>
|
2021-06-10 15:52:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return item
|
|
|
|
}
|
2021-06-11 00:11:58 +00:00
|
|
|
|
2022-07-10 07:39:02 +00:00
|
|
|
const pass1 = convertHtmlToTree(this.mfm ? renderMisskeyMarkdown(html) : html).map(processItem)
|
2021-06-15 11:43:44 +00:00
|
|
|
const pass2 = [...pass1].reverse().map(processItemReverse).reverse()
|
2021-06-11 08:05:28 +00:00
|
|
|
// DO NOT USE SLOTS they cause a re-render feedback loop here.
|
|
|
|
// slots updated -> rerender -> emit -> update up the tree -> rerender -> ...
|
|
|
|
// at least until vue3?
|
|
|
|
const result = <span class="RichContent">
|
2021-06-15 11:43:44 +00:00
|
|
|
{ pass2 }
|
2021-06-11 08:05:28 +00:00
|
|
|
</span>
|
|
|
|
|
2021-06-11 00:11:58 +00:00
|
|
|
const event = {
|
|
|
|
lastTags,
|
|
|
|
writtenMentions,
|
2021-08-14 23:41:53 +00:00
|
|
|
writtenTags,
|
|
|
|
invisibleMentions
|
2021-06-11 00:11:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DO NOT MOVE TO UPDATE. BAD IDEA.
|
|
|
|
this.$emit('parseReady', event)
|
|
|
|
|
|
|
|
return result
|
2021-06-07 00:14:48 +00:00
|
|
|
}
|
2022-03-16 20:13:21 +00:00
|
|
|
}
|
2021-06-10 09:08:31 +00:00
|
|
|
|
2021-06-11 00:11:58 +00:00
|
|
|
const getLinkData = (attrs, children, index) => {
|
2021-08-14 23:41:53 +00:00
|
|
|
const stripTags = (item) => {
|
|
|
|
if (typeof item === 'string') {
|
|
|
|
return item
|
|
|
|
} else {
|
|
|
|
return item[1].map(stripTags).join('')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const textContent = children.map(stripTags).join('')
|
2021-06-11 00:11:58 +00:00
|
|
|
return {
|
|
|
|
index,
|
|
|
|
url: attrs.href,
|
2021-08-23 17:57:21 +00:00
|
|
|
tag: attrs['data-tag'],
|
2021-08-14 23:41:53 +00:00
|
|
|
content: flattenDeep(children).join(''),
|
|
|
|
textContent
|
2021-06-11 00:11:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 15:52:01 +00:00
|
|
|
/** Pre-processing HTML
|
|
|
|
*
|
2021-08-14 23:41:53 +00:00
|
|
|
* Currently this does one thing:
|
2021-06-10 15:52:01 +00:00
|
|
|
* - add green/cyantexting
|
|
|
|
*
|
|
|
|
* @param {String} html - raw HTML to process
|
|
|
|
* @param {Boolean} greentext - whether to enable greentexting or not
|
|
|
|
*/
|
2021-08-14 23:41:53 +00:00
|
|
|
export const preProcessPerLine = (html, greentext) => {
|
2021-06-13 10:29:26 +00:00
|
|
|
const greentextHandle = new Set(['p', 'div'])
|
2021-06-10 15:52:01 +00:00
|
|
|
|
2021-06-13 19:22:59 +00:00
|
|
|
const lines = convertHtmlToLines(html)
|
|
|
|
const newHtml = lines.reverse().map((item, index, array) => {
|
2021-06-10 15:52:01 +00:00
|
|
|
if (!item.text) return item
|
|
|
|
const string = item.text
|
|
|
|
|
|
|
|
// Greentext stuff
|
2021-06-13 10:29:26 +00:00
|
|
|
if (
|
|
|
|
// Only if greentext is engaged
|
|
|
|
greentext &&
|
2021-08-14 23:41:53 +00:00
|
|
|
// Only handle p's and divs. Don't want to affect blockquotes, code etc
|
2021-06-13 10:29:26 +00:00
|
|
|
item.level.every(l => greentextHandle.has(l)) &&
|
|
|
|
// Only if line begins with '>' or '<'
|
|
|
|
(string.includes('>') || string.includes('<'))
|
|
|
|
) {
|
2021-06-10 15:52:01 +00:00
|
|
|
const cleanedString = string.replace(/<[^>]+?>/gi, '') // remove all tags
|
|
|
|
.replace(/@\w+/gi, '') // remove mentions (even failed ones)
|
|
|
|
.trim()
|
|
|
|
if (cleanedString.startsWith('>')) {
|
|
|
|
return `<span class='greentext'>${string}</span>`
|
|
|
|
} else if (cleanedString.startsWith('<')) {
|
|
|
|
return `<span class='cyantext'>${string}</span>`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-14 23:41:53 +00:00
|
|
|
return string
|
2021-06-10 15:52:01 +00:00
|
|
|
}).reverse().join('')
|
2021-06-11 00:11:58 +00:00
|
|
|
|
2021-08-14 23:41:53 +00:00
|
|
|
return { newHtml }
|
2021-06-10 09:08:31 +00:00
|
|
|
}
|