2021-06-07 00:14:48 +00:00
|
|
|
import Vue from 'vue'
|
2021-06-07 13:16:10 +00:00
|
|
|
import { unescape, flattenDeep } from 'lodash'
|
2021-06-10 15:52:01 +00:00
|
|
|
import { convertHtmlToTree, getTagName, processTextForEmoji, getAttrs } from 'src/services/html_converter/html_tree_converter.service.js'
|
|
|
|
import { convertHtmlToLines } from 'src/services/html_converter/html_line_converter.service.js'
|
2021-06-07 00:14:48 +00:00
|
|
|
import StillImage from 'src/components/still-image/still-image.vue'
|
2021-06-07 13:16:10 +00:00
|
|
|
import MentionLink from 'src/components/mention_link/mention_link.vue'
|
2021-06-07 00:14:48 +00:00
|
|
|
|
|
|
|
import './rich_content.scss'
|
|
|
|
|
|
|
|
export default Vue.component('RichContent', {
|
|
|
|
name: 'RichContent',
|
|
|
|
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-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
|
2021-06-10 15:52:01 +00:00
|
|
|
},
|
|
|
|
// Whether to hide last mentions (hellthreads)
|
2021-06-11 00:11:58 +00:00
|
|
|
hideMentions: {
|
2021-06-10 15:52:01 +00:00
|
|
|
required: false,
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
2021-06-07 00:14:48 +00:00
|
|
|
}
|
|
|
|
},
|
2021-06-11 00:11:58 +00:00
|
|
|
// NEVER EVER TOUCH DATA INSIDE RENDER
|
2021-06-07 00:14:48 +00:00
|
|
|
render (h) {
|
2021-06-10 09:08:31 +00:00
|
|
|
// Pre-process HTML
|
2021-06-11 00:11:58 +00:00
|
|
|
const { newHtml: html, lastMentions } = preProcessPerLine(this.html, this.greentext, this.hideLastMentions)
|
|
|
|
const firstMentions = [] // Mentions that appear in the beginning of post body
|
|
|
|
const lastTags = [] // Tags that appear at the end of post body
|
|
|
|
const writtenMentions = [] // All mentions that appear in post body
|
|
|
|
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
|
|
|
|
{...{ attrs: getAttrs(tag) }}
|
|
|
|
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) => {
|
|
|
|
const linkData = getLinkData(attrs, children, tagsIndex++)
|
|
|
|
writtenTags.push(linkData)
|
|
|
|
attrs.target = '_blank'
|
|
|
|
if (!encounteredTextReverse) {
|
|
|
|
lastTags.push(linkData)
|
|
|
|
attrs['data-parser-last'] = true
|
|
|
|
}
|
|
|
|
return <a {...{ attrs }}>
|
|
|
|
{ children.map(processItem) }
|
|
|
|
</a>
|
|
|
|
}
|
|
|
|
|
2021-06-08 08:38:44 +00:00
|
|
|
const renderMention = (attrs, children, encounteredText) => {
|
2021-06-11 00:11:58 +00:00
|
|
|
const linkData = getLinkData(attrs, children, mentionIndex++)
|
|
|
|
writtenMentions.push(linkData)
|
|
|
|
if (!encounteredText) {
|
|
|
|
firstMentions.push(linkData)
|
|
|
|
return ''
|
|
|
|
} else {
|
|
|
|
return <MentionLink
|
2021-06-10 15:52:01 +00:00
|
|
|
url={attrs.href}
|
|
|
|
content={flattenDeep(children).join('')}
|
|
|
|
/>
|
2021-06-11 00:11:58 +00:00
|
|
|
}
|
2021-06-07 13:16:10 +00:00
|
|
|
}
|
2021-06-07 15:39:51 +00:00
|
|
|
|
2021-06-10 09:08:31 +00:00
|
|
|
// We stop treating mentions as "first" ones when we encounter
|
|
|
|
// non-whitespace text
|
2021-06-08 08:38:44 +00:00
|
|
|
let encounteredText = false
|
2021-06-11 00:11:58 +00:00
|
|
|
// Processor to use with html_tree_converter
|
2021-06-10 15:52:01 +00:00
|
|
|
const processItem = (item, index, array, what) => {
|
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() === ''
|
|
|
|
if (emptyText) {
|
2021-06-08 08:38:44 +00:00
|
|
|
return encounteredText ? item : item.trim()
|
|
|
|
}
|
|
|
|
let unescapedItem = unescape(item)
|
|
|
|
if (!encounteredText) {
|
|
|
|
unescapedItem = unescapedItem.trimStart()
|
|
|
|
encounteredText = true
|
|
|
|
}
|
2021-06-07 00:14:48 +00:00
|
|
|
if (item.includes(':')) {
|
2021-06-10 15:52:01 +00:00
|
|
|
unescapedItem = processTextForEmoji(
|
2021-06-08 08:38:44 +00:00
|
|
|
unescapedItem,
|
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-10 15:52:01 +00:00
|
|
|
return unescapedItem
|
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)) {
|
|
|
|
const [opener, children] = item
|
|
|
|
const Tag = getTagName(opener)
|
2021-06-11 00:11:58 +00:00
|
|
|
const attrs = getAttrs(opener)
|
2021-06-07 13:16:10 +00:00
|
|
|
switch (Tag) {
|
2021-06-11 00:11:58 +00:00
|
|
|
case 'span': // replace images with StillImage
|
|
|
|
if (attrs['class'] && attrs['class'].includes('lastMentions')) {
|
|
|
|
if (firstMentions.length > 0) {
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
2021-06-07 15:39:51 +00:00
|
|
|
case 'img': // replace images with StillImage
|
2021-06-07 13:16:10 +00:00
|
|
|
return 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-08 08:38:44 +00:00
|
|
|
return renderMention(attrs, children, encounteredText)
|
2021-06-10 15:52:01 +00:00
|
|
|
} else if (attrs['class'] && attrs['class'].includes('hashtag')) {
|
|
|
|
return item // We'll handle it later
|
2021-06-10 09:08:31 +00:00
|
|
|
} else {
|
|
|
|
attrs.target = '_blank'
|
|
|
|
return <a {...{ attrs }}>
|
|
|
|
{ children.map(processItem) }
|
|
|
|
</a>
|
2021-06-07 13:16:10 +00:00
|
|
|
}
|
2021-06-07 00:14:48 +00:00
|
|
|
}
|
2021-06-10 09:08:31 +00:00
|
|
|
|
2021-06-07 15:39:51 +00:00
|
|
|
// Render tag as is
|
2021-06-07 00:14:48 +00:00
|
|
|
if (children !== undefined) {
|
|
|
|
return <Tag {...{ attrs: getAttrs(opener) }}>
|
|
|
|
{ children.map(processItem) }
|
|
|
|
</Tag>
|
|
|
|
} else {
|
|
|
|
return <Tag/>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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() === ''
|
|
|
|
if (emptyText) return encounteredTextReverse ? item : item.trim()
|
|
|
|
if (!encounteredTextReverse) encounteredTextReverse = true
|
|
|
|
return item
|
|
|
|
} else if (Array.isArray(item)) {
|
|
|
|
// Handle tag nodes
|
|
|
|
const [opener, children] = item
|
|
|
|
const Tag = getTagName(opener)
|
|
|
|
switch (Tag) {
|
|
|
|
case 'a': // replace mentions with MentionLink
|
|
|
|
if (!this.handleLinks) break
|
|
|
|
const attrs = getAttrs(opener)
|
|
|
|
// should only be this
|
|
|
|
if (attrs['class'] && attrs['class'].includes('hashtag')) {
|
|
|
|
return renderHashtag(attrs, children, encounteredTextReverse)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return item
|
|
|
|
}
|
2021-06-11 00:11:58 +00:00
|
|
|
|
|
|
|
const event = {
|
|
|
|
firstMentions,
|
|
|
|
lastMentions,
|
|
|
|
lastTags,
|
|
|
|
writtenMentions,
|
|
|
|
writtenTags
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = <span class="RichContent">
|
2021-06-10 09:08:31 +00:00
|
|
|
{ this.$slots.prefix }
|
2021-06-10 15:52:01 +00:00
|
|
|
{ convertHtmlToTree(html).map(processItem).reverse().map(processItemReverse).reverse() }
|
2021-06-10 09:08:31 +00:00
|
|
|
{ this.$slots.suffix }
|
2021-06-07 09:49:54 +00:00
|
|
|
</span>
|
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
|
|
|
}
|
|
|
|
})
|
2021-06-10 09:08:31 +00:00
|
|
|
|
2021-06-11 00:11:58 +00:00
|
|
|
const getLinkData = (attrs, children, index) => {
|
|
|
|
return {
|
|
|
|
index,
|
|
|
|
url: attrs.href,
|
|
|
|
hashtag: attrs['data-tag'],
|
|
|
|
content: flattenDeep(children).join('')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 15:52:01 +00:00
|
|
|
/** Pre-processing HTML
|
|
|
|
*
|
|
|
|
* Currently this does two things:
|
|
|
|
* - add green/cyantexting
|
|
|
|
* - wrap and mark last line containing only mentions as ".lastMentionsLine" for
|
|
|
|
* more compact hellthreads.
|
|
|
|
*
|
|
|
|
* @param {String} html - raw HTML to process
|
|
|
|
* @param {Boolean} greentext - whether to enable greentexting or not
|
|
|
|
*/
|
2021-06-11 00:11:58 +00:00
|
|
|
export const preProcessPerLine = (html, greentext) => {
|
|
|
|
const lastMentions = []
|
2021-06-10 15:52:01 +00:00
|
|
|
|
2021-06-11 00:11:58 +00:00
|
|
|
const newHtml = convertHtmlToLines(html).reverse().map((item, index, array) => {
|
|
|
|
// Going over each line in reverse to detect last mentions,
|
|
|
|
// keeping non-text stuff as-is
|
2021-06-10 15:52:01 +00:00
|
|
|
if (!item.text) return item
|
|
|
|
const string = item.text
|
|
|
|
|
|
|
|
// Greentext stuff
|
|
|
|
if (greentext && (string.includes('>') || string.includes('<'))) {
|
|
|
|
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-06-11 00:11:58 +00:00
|
|
|
// Converting that line part into tree
|
2021-06-10 15:52:01 +00:00
|
|
|
const tree = convertHtmlToTree(string)
|
|
|
|
|
|
|
|
// If line has loose text, i.e. text outside a mention or a tag
|
|
|
|
// we won't touch mentions.
|
|
|
|
let hasLooseText = false
|
|
|
|
let hasMentions = false
|
|
|
|
const process = (item) => {
|
|
|
|
if (Array.isArray(item)) {
|
|
|
|
const [opener, children, closer] = item
|
|
|
|
const tag = getTagName(opener)
|
2021-06-11 00:11:58 +00:00
|
|
|
// If we have a link we probably have mentions
|
2021-06-10 15:52:01 +00:00
|
|
|
if (tag === 'a') {
|
|
|
|
const attrs = getAttrs(opener)
|
|
|
|
if (attrs['class'] && attrs['class'].includes('mention')) {
|
2021-06-11 00:11:58 +00:00
|
|
|
// Got mentions
|
2021-06-10 15:52:01 +00:00
|
|
|
hasMentions = true
|
|
|
|
return [opener, children, closer]
|
|
|
|
} else {
|
2021-06-11 00:11:58 +00:00
|
|
|
// Not a mention? Means we have loose text or whatever
|
2021-06-10 15:52:01 +00:00
|
|
|
hasLooseText = true
|
|
|
|
return [opener, children, closer]
|
|
|
|
}
|
|
|
|
} else if (tag === 'span' || tag === 'p') {
|
2021-06-11 00:11:58 +00:00
|
|
|
// For span and p we need to go deeper
|
|
|
|
return [opener, [...children].map(process), closer]
|
2021-06-10 09:08:31 +00:00
|
|
|
} else {
|
2021-06-11 00:11:58 +00:00
|
|
|
// Everything else equals to a loose text
|
2021-06-10 15:52:01 +00:00
|
|
|
hasLooseText = true
|
|
|
|
return [opener, children, closer]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof item === 'string') {
|
|
|
|
if (item.trim() !== '') {
|
2021-06-11 00:11:58 +00:00
|
|
|
// only meaningful strings are loose text
|
2021-06-10 15:52:01 +00:00
|
|
|
hasLooseText = true
|
2021-06-10 09:08:31 +00:00
|
|
|
}
|
2021-06-10 15:52:01 +00:00
|
|
|
return item
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-11 00:11:58 +00:00
|
|
|
// We now processed our tree, now we need to mark line as lastMentions
|
|
|
|
const result = [...tree].map(process)
|
2021-06-10 15:52:01 +00:00
|
|
|
|
2021-06-11 00:11:58 +00:00
|
|
|
// Only check last (first since list is reversed) line
|
|
|
|
if (hasMentions && !hasLooseText && index === 0) {
|
|
|
|
let mentionIndex = 0
|
|
|
|
const process = (item) => {
|
|
|
|
if (Array.isArray(item)) {
|
|
|
|
const [opener, children] = item
|
|
|
|
const tag = getTagName(opener)
|
|
|
|
if (tag === 'a') {
|
|
|
|
const attrs = getAttrs(opener)
|
|
|
|
lastMentions.push(getLinkData(attrs, children, mentionIndex++))
|
|
|
|
} else if (children) {
|
|
|
|
children.forEach(process)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.forEach(process)
|
|
|
|
// we DO need mentions here so that we conditionally remove them if don't
|
|
|
|
// have first mentions
|
|
|
|
return ['<span class="lastMentions">', flattenDeep(result).join(''), '</span>'].join('')
|
2021-06-10 09:08:31 +00:00
|
|
|
} else {
|
2021-06-10 15:52:01 +00:00
|
|
|
return flattenDeep(result).join('')
|
2021-06-10 09:08:31 +00:00
|
|
|
}
|
2021-06-10 15:52:01 +00:00
|
|
|
}).reverse().join('')
|
2021-06-11 00:11:58 +00:00
|
|
|
|
|
|
|
return { newHtml, lastMentions }
|
2021-06-10 09:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getHeadTailLinks = (html) => {
|
|
|
|
// Exported object properties
|
|
|
|
}
|