2018-12-13 07:45:52 +00:00
|
|
|
import { mark, stop } from './marks'
|
|
|
|
|
|
|
|
let domParser = process.browser && new DOMParser()
|
|
|
|
|
2018-12-19 08:57:56 +00:00
|
|
|
// mentions like "@foo" have to be expanded to "@foo@example.com"
|
|
|
|
function massageMentions (doc, mentions) {
|
2018-12-13 07:45:52 +00:00
|
|
|
let anchors = doc.querySelectorAll('a.mention')
|
|
|
|
for (let i = 0; i < anchors.length; i++) {
|
|
|
|
let anchor = anchors[i]
|
|
|
|
let href = anchor.getAttribute('href')
|
|
|
|
let mention = mentions.find(mention => mention.url === href)
|
|
|
|
if (mention) {
|
|
|
|
anchor.innerText = `@${mention.acct}`
|
|
|
|
}
|
|
|
|
}
|
2018-12-19 08:57:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// paragraphs should be separated by double newlines
|
|
|
|
// single <br/>s should become single newlines
|
|
|
|
function innerTextRetainingNewlines (doc) {
|
|
|
|
let paragraphs = doc.querySelectorAll('p')
|
|
|
|
return Array.from(paragraphs).map(paragraph => {
|
|
|
|
let brs = paragraph.querySelectorAll('br')
|
|
|
|
Array.from(brs).forEach(br => {
|
|
|
|
br.parentNode.replaceChild(doc.createTextNode('\n'), br)
|
|
|
|
})
|
|
|
|
return paragraph.textContent
|
|
|
|
}).join('\n\n')
|
|
|
|
}
|
|
|
|
|
|
|
|
export function statusHtmlToPlainText (html, mentions) {
|
|
|
|
if (!html) {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
mark('statusHtmlToPlainText')
|
|
|
|
let doc = domParser.parseFromString(html, 'text/html')
|
|
|
|
massageMentions(doc, mentions)
|
|
|
|
let res = innerTextRetainingNewlines(doc)
|
2018-12-13 07:45:52 +00:00
|
|
|
stop('statusHtmlToPlainText')
|
|
|
|
return res
|
|
|
|
}
|