diff options
Diffstat (limited to 'vnext/src/utils/embed.js')
-rw-r--r-- | vnext/src/utils/embed.js | 44 |
1 files changed, 35 insertions, 9 deletions
diff --git a/vnext/src/utils/embed.js b/vnext/src/utils/embed.js index fcef651d..b6bbde14 100644 --- a/vnext/src/utils/embed.js +++ b/vnext/src/utils/embed.js @@ -122,27 +122,35 @@ function messageReplyReplace(messageId) { * @param {string} txt text message * @param {string} messageId current message id * @param {boolean} isCode set when message contains *code tag + * @param {boolean} isDurov skip rules non-compatible with Telegram * @returns {string} formatted message */ -function juickFormat(txt, messageId, isCode) { +function juickFormat(txt, messageId, isCode, isDurov) { const urlRe = /(?:\[([^\][]+)\](?:\[([^\]]+)\]|\(((?:[a-z]+:\/\/|www\.|ftp\.)(?:\([-\S+*&@#/%=~|$?!:;,.]*\)|[-\S+*&@#/%=~|$?!:;,.])*(?:\([-\S+*&@#/%=~|$?!:;,.]*\)|[\S+*&@#/%=~|$]))\))|\b(?:[a-z]+:\/\/|www\.|ftp\.)(?:\([-\S+*&@#/%=~|$?!:;,.]*\)|[-\S+*&@#/%=~|$?!:;,.])*(?:\([-\S+*&@#/%=~|$?!:;,.]*\)|[\S+*&@#/%=~|$]))/gi const bqReplace = m => m.replace(/^(?:>|>)\s?/gmi, '') - return (isCode) - ? formatText(txt, [ + if (isCode) { + return formatText(txt, [ { pr: 1, re: urlRe, with: urlReplaceInCode }, { pr: 1, re: /\B(?:#(\d+))?(?:\/(\d+))?\b/g, with: messageReplyReplace(messageId) }, { pr: 1, re: /\B@([\w-]+)\b/gi, with: '<a href="/$1">@$1</a>' }, ]) - : formatText(txt, [ - { pr: 0, re: /((?:^(?:>|>)\s?[\s\S]+?$\n?)+)/gmi, brackets: true, with: ['<q>', '</q>', bqReplace] }, + } else { + const rules = [ + { pr: 0, re: /((?:^(?:>|>)\s?[\s\S]+?$\n?)+)/gmi, brackets: true, with: ['<blockquote>', '</blockquote>', bqReplace] }, { pr: 1, re: urlRe, with: urlReplace }, { pr: 1, re: /\B(?:#(\d+))?(?:\/(\d+))?\b/g, with: messageReplyReplace(messageId) }, { pr: 1, re: /\B@([\w-]+)\b/gi, with: '<a href="/$1">@$1</a>' }, { pr: 2, re: /\B\*([^\n]+?)\*((?=\s)|(?=$)|(?=[!"#$%&'*+,\-./:;<=>?@[\]^_`{|}~()]+))/g, brackets: true, with: ['<b>', '</b>'] }, { pr: 2, re: /\B\/([^\n]+?)\/((?=\s)|(?=$)|(?=[!"#$%&'*+,\-./:;<=>?@[\]^_`{|}~()]+))/g, brackets: true, with: ['<i>', '</i>'] }, { pr: 2, re: /\b_([^\n]+?)_((?=\s)|(?=$)|(?=[!"#$%&'*+,\-./:;<=>?@[\]^_`{|}~()]+))/g, brackets: true, with: ['<u>', '</u>'] }, - { pr: 3, re: /\n/g, with: '<br/>' }, - ]) + ] + if (!isDurov) { + rules.push( + { pr: 3, re: /\n/g, with: '<br/>' } + ) + } + return formatText(txt, rules) + } } /** * @external RegExpExecArray @@ -200,7 +208,7 @@ function getEmbeddableLinkTypes() { className: 'video compact', re: /\.(webm|mp4|m4v|ogv)(?:\?[\w&;?=]*)?$/i, makeNode: function(aNode, reResult, div) { - div.innerHTML = `<video src="${aNode.href}" title="${aNode.href}" controls></video>` + div.innerHTML = `<video src="${aNode.href}#t=0.001" title="${aNode.href}" controls></video>` return div } }, @@ -275,7 +283,7 @@ function getEmbeddableLinkTypes() { const twitter_url = reResult[0].startsWith(wrong_prefix) ? reResult[0].replace(wrong_prefix, correct_prefix) : reResult[0] - fetch('https://x.juick.com/oembed?url=' + twitter_url) + fetch('/api/v2/oembed?url=' + twitter_url) .then(response => response.json()) .then(json => { div.innerHTML = json.html @@ -314,6 +322,24 @@ function getEmbeddableLinkTypes() { return div } }, + { + name: 'Tiktok', + id: 'embed_tiktok', + className: 'tiktok compact', + re: /https?:\/\/www\.?tiktok\.com\/(\S+)/i, + makeNode: function(aNode, reResult, div) { + const tiktok_url = reResult[0] + fetch('https://www.tiktok.com/oembed?url=' + tiktok_url) + .then(response => response.json()) + .then(json => { + div.innerHTML = json.html + let script = document.createElement('script') + script.src = 'https://www.tiktok.com/embed.js' + div.appendChild(script) + }).catch(console.log) + return div + } + }, ] } |