From f9c2209d4b8a4b89cb87198ae945b1d0a2b5f82e Mon Sep 17 00:00:00 2001 From: Karey Higuera Date: Fri, 12 Aug 2022 10:38:25 -0400 Subject: [PATCH] fix bug with multiple occurrences of same mention --- manifest.json | 2 +- package.json | 2 +- src/util.ts | 46 ++++++++++++++++++++++++++++++++++++++-------- versions.json | 2 +- 4 files changed, 41 insertions(+), 11 deletions(-) diff --git a/manifest.json b/manifest.json index 547a9ab..43a86a5 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "obsidian-tweet-to-markdown", "name": "Tweet to Markdown", - "version": "2.10.1", + "version": "2.10.2", "minAppVersion": "0.12.17", "description": "Save tweets as Markdown files, along with their images, polls, etc.", "author": "kbravh", diff --git a/package.json b/package.json index 536e217..a3f0fe1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "obsidian-tweet-to-markdown", - "version": "2.10.1", + "version": "2.10.2", "description": "Save tweets as beautiful markdown files in Obsidian (https://obsidian.md)", "main": "main.js", "engines": { diff --git a/src/util.ts b/src/util.ts index 5a7160d..5d3867a 100644 --- a/src/util.ts +++ b/src/util.ts @@ -346,26 +346,56 @@ export const buildMarkdown = async ( /** * replace any mentions, hashtags, cashtags, urls with links */ - tweet.data.entities?.mentions?.forEach(({username}) => { + /** + * replace any mentions, hashtags, cashtags, urls with links + */ + const mentions = [ + ...new Set( + (tweet.data.entities?.mentions ?? []).map(mention => mention.username) + ), + ] + const tags = [ + ...new Set( + (tweet.data.entities?.hashtags ?? []).map(hashtag => hashtag.tag) + ), + ] + const cashtags = [ + ...new Set( + (tweet.data.entities?.cashtags ?? []).map(cashtag => cashtag.tag) + ), + ] + const urlSet = new Set() + const urls = (tweet.data.entities?.urls ?? []).filter(url => { + if (urlSet.has(url.expanded_url)) { + return false + } else { + urlSet.add(url.expanded_url) + return true + } + }) + mentions.forEach(username => { text = text.replace( - `@${username}`, + new RegExp(`@${username}`, 'g'), `[@${username}](https://twitter.com/${username})` ) }) - tweet.data.entities?.hashtags?.forEach(({tag}) => { + tags.forEach(tag => { text = text.replace( - `#${tag}`, + new RegExp(`#${tag}`, 'g'), `[#${tag}](https://twitter.com/hashtag/${tag}) ` ) }) - tweet.data.entities?.cashtags?.forEach(({tag}) => { + cashtags.forEach(tag => { text = text.replace( - `$${tag}`, + new RegExp(`$${tag}`, 'g'), `[$${tag}](https://twitter.com/search?q=%24${tag})` ) }) - tweet.data.entities?.urls?.forEach(url => { - text = text.replace(url.url, `[${url.display_url}](${url.expanded_url})`) + urls.forEach(url => { + text = text.replace( + new RegExp(url.url, 'g'), + `[${url.display_url}](${url.expanded_url})` + ) }) } diff --git a/versions.json b/versions.json index b804921..cd5cb68 100644 --- a/versions.json +++ b/versions.json @@ -1,3 +1,3 @@ { - "2.10.1": "0.12.17" + "2.10.2": "0.12.17" }