Skip to content

Commit

Permalink
fix bug with multiple occurrences of same mention
Browse files Browse the repository at this point in the history
  • Loading branch information
kbravh committed Aug 12, 2022
1 parent 3dd675e commit f9c2209
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
46 changes: 38 additions & 8 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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})`
)
})
}

Expand Down
2 changes: 1 addition & 1 deletion versions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"2.10.1": "0.12.17"
"2.10.2": "0.12.17"
}

0 comments on commit f9c2209

Please sign in to comment.