Skip to content

Commit

Permalink
add escapeHashtags setting (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
kbravh authored Nov 18, 2022
1 parent c296742 commit ee5629c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 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.11.2",
"version": "2.12.0",
"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.11.2",
"version": "2.12.0",
"description": "Save tweets as beautiful markdown files in Obsidian (https://obsidian.md)",
"main": "main.js",
"engines": {
Expand Down
16 changes: 16 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface TTMSettings {
tags: string[]
cssclass: string
freeformFrontmatter: string[]
escapeHashtags: boolean
avatars: boolean
avatarSize: string
defaultToThread: boolean
Expand Down Expand Up @@ -45,6 +46,7 @@ export const DEFAULT_SETTINGS: TTMSettings = {
tags: [],
cssclass: '',
freeformFrontmatter: [],
escapeHashtags: false,
avatars: true,
avatarSize: '',
defaultToThread: false,
Expand Down Expand Up @@ -297,6 +299,20 @@ export class TTMSettingTab extends PluginSettingTab {
})
)

new Setting(containerEl)
.setName('Escape hashtags')
.setDesc(
'Prevent hashtags from being included in Obsidian tags (change # to \\#)'
)
.addToggle(toggle =>
toggle
.setValue(this.plugin.settings.escapeHashtags)
.onChange(async value => {
this.plugin.settings.escapeHashtags = value
await this.plugin.saveSettings()
})
)

new Setting(containerEl)
.setName('Include profile pictures')
.setDesc(
Expand Down
12 changes: 9 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,11 @@ type GenericEntity = Pick<
/**
* replace any mentions, hashtags, cashtags, urls with links
*/
export const replaceEntities = (entities: Entities, text: string): string => {
export const replaceEntities = (
settings: TTMSettings,
entities: Entities,
text: string
): string => {
/**
* Each entity comes with start and end indices. However, if we were to replace
* them in the order they occur, the indices further down the line would be shifted
Expand All @@ -342,7 +346,9 @@ export const replaceEntities = (entities: Entities, text: string): string => {
})),
...(entities?.hashtags ?? []).map(hashtag => ({
...hashtag,
replacement: `[#${hashtag.tag}](https://twitter.com/hashtag/${hashtag.tag})`,
replacement: `[${settings.escapeHashtags ? '\\' : ''}#${
hashtag.tag
}](https://twitter.com/hashtag/${hashtag.tag})`,
})),
...(entities?.cashtags ?? []).map(cashtag => ({
...cashtag,
Expand Down Expand Up @@ -400,7 +406,7 @@ export const buildMarkdown = async (
* replace entities with markdown links
*/
if (tweet.data?.entities && plugin.settings.includeLinks) {
text = replaceEntities(tweet.data.entities, text)
text = replaceEntities(plugin.settings, tweet.data.entities, text)
}

text = decode(text)
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.11.2": "0.12.17"
"2.12.0": "0.12.17"
}

0 comments on commit ee5629c

Please sign in to comment.