Skip to content

Commit

Permalink
allow for text variable to be used when naming files
Browse files Browse the repository at this point in the history
  • Loading branch information
kbravh committed Nov 10, 2021
1 parent 15399ef commit 20f9b8d
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 8 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@ To download a tweet thread, paste the link to the **LAST** tweet in the thread,

![The modal to download a new tweet.](https://raw.githubusercontent.com/kbravh/obsidian-tweet-to-markdown/main/images/tweet_url_modal.png)

Once the tweet is downloaded, you'll be presented a window to set the name of the file that will be created. You can use the variables `[[handle]]`, `[[name]]`, and `[[id]]` when naming your file, which will be automatically replaced with the author's handle, name, and the tweet ID.
Once the tweet is downloaded, you'll be presented a window to set the name of the file that will be created. You can use the variables `[[handle]]`, `[[name]]`, `[[text]]`, and `[[id]]` when naming your file, which will be automatically replaced according to the following chart. The file extension `.md` will be added automatically.

| Variable | Replacement |
|:---:|---|
|[[handle]]|The user's handle (the part that follows the @ symbol)|
|[[name]]|The user's name|
|[[id]]|The unique ID assigned to the tweet|
|[[text]]|The entire text of the tweet|

![The modal to name a downloaded tweet.](https://raw.githubusercontent.com/kbravh/obsidian-tweet-to-markdown/main/images/tweet_complete_modal.png)

Expand All @@ -61,7 +68,14 @@ On the Tweet to Markdown settings page in Obsidian, you can customize the way th

### Custom File Name

Tweets are, by default, saved with the filename `[[handle]] - [[id]].md`. You can instead enter your own format in the **Filename** field using the variables `[[name]]`, `[[handle]]`, and `[[id]]` in your filename, which will automatically be replaced. The file extension `.md` will also be added.
Tweets are, by default, saved with the filename `[[handle]] - [[id]].md`. You can instead enter your own format in the **Filename** field using the variables `[[name]]`, `[[handle]]`, `[[text]]`, and `[[id]]` in your filename, which will be automatically replaced according to the following chart. The file extension `.md` will be added automatically.

| Variable | Replacement |
|:---:|---|
|[[handle]]|The user's handle (the part that follows the @ symbol)|
|[[name]]|The user's name|
|[[id]]|The unique ID assigned to the tweet|
|[[text]]|The entire text of the tweet|

### Custom File Path

Expand Down
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": "1.0.7",
"version": "1.1.0",
"minAppVersion": "0.12.16",
"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": "1.0.7",
"version": "1.1.0",
"description": "Save tweets as beautiful markdown files in Obsidian (https://obsidian.md)",
"main": "main.js",
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion src/TweetCompleteModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class TweetCompleteModal extends Modal {
new Setting(contentEl)
.setName('Filename')
.setDesc(
'Set the name of the file. You can use the placeholders [[handle]], [[name]], and [[id]].'
'Set the name of the file. You can use the placeholders [[handle]], [[name]], [[text]], and [[id]].'
)
.addText(input => {
input.setValue(filename)
Expand Down
2 changes: 2 additions & 0 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ export interface Tweet {
includes: Includes
data: Data
errors?: Error[]
// other error fields
reason?: string
}

export interface TweetURL {
Expand Down
2 changes: 1 addition & 1 deletion src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class TTMSettingTab extends PluginSettingTab {
new Setting(containerEl)
.setName('Filename')
.setDesc(
'The name to give the saved tweet file. You can use the placeholders [[handle]], [[name]], and [[id]]. Defaults to "[[handle]] - [[id]]"'
'The name to give the saved tweet file. You can use the placeholders [[handle]], [[name]], [[text]] and [[id]]. Defaults to "[[handle]] - [[id]]"'
)
.addText(text =>
text
Expand Down
10 changes: 9 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ export const getTweet = async (id: string, bearer: string): Promise<Tweet> => {
if (tweet.errors) {
throw new Error(tweet.errors[0].detail)
}
if (tweet?.reason) {
switch (tweet.reason) {
case 'client-not-enrolled':
default:
throw new Error('There seems to be a problem with your bearer token.')
}
}
return tweet
}

Expand Down Expand Up @@ -87,6 +94,7 @@ export const createFilename = (tweet: Tweet, filename = ''): string => {
filename = filename.replace('[[name]]', tweet.includes.users[0].name)
filename = filename.replace('[[handle]]', tweet.includes.users[0].username)
filename = filename.replace('[[id]]', tweet.data.id)
filename = filename.replace('[[text]]', normalizePath(tweet.data.text))
filename += '.md'
return filename
}
Expand Down Expand Up @@ -135,7 +143,7 @@ export const buildMarkdown = async (
tweet: Tweet,
type: 'normal' | 'thread' | 'quoted' = 'normal'
): Promise<string> => {
let metrics = []
let metrics: string[] = []
metrics = [
`likes: ${tweet.data.public_metrics.like_count}`,
`retweets: ${tweet.data.public_metrics.retweet_count}`,
Expand Down
2 changes: 1 addition & 1 deletion versions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"1.0.7": "0.12.16"
"1.1.0": "0.12.16"
}

0 comments on commit 20f9b8d

Please sign in to comment.