Skip to content

Commit

Permalink
remove sanitize filename dependency to restore mobile functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
kbravh committed Nov 14, 2021
1 parent e7f44cb commit 65fad74
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 27 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": "1.1.2",
"version": "1.1.3",
"minAppVersion": "0.12.16",
"description": "Save tweets as Markdown files, along with their images, polls, etc.",
"author": "kbravh",
Expand Down
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-tweet-to-markdown",
"version": "1.1.2",
"version": "1.1.3",
"description": "Save tweets as beautiful markdown files in Obsidian (https://obsidian.md)",
"main": "main.js",
"engines": {
Expand All @@ -25,7 +25,5 @@
"tslib": "^2.2.0",
"typescript": "^4.2.4"
},
"dependencies": {
"sanitize-filename": "^1.6.3"
}
"dependencies": {}
}
24 changes: 22 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {App, normalizePath, request, TAbstractFile} from 'obsidian'
import {Media, Poll, Tweet} from './models'
import {DownloadManager} from './downloadManager'
import sanitize from 'sanitize-filename'
import TTM from 'main'
import {TTMSettings} from './settings'

Expand Down Expand Up @@ -83,6 +82,27 @@ export const createPollTable = (polls: Poll[]): string[] => {
})
}

/**
* Filename sanitization. Credit: parshap/node-sanitize-filename
* Rewrite to allow functionality on Obsidian mobile.
*/
const illegalRe = /[/?<>\\:*|"]/g
// eslint-disable-next-line no-control-regex
const controlRe = /[\x00-\x1f\x80-\x9f]/g
const reservedRe = /^\.+$/
const windowsReservedRe = /^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\..*)?$/i
const windowsTrailingRe = /[. ]+$/

export const sanitizeFilename = (filename: string): string => {
filename = filename
.replace(illegalRe, '')
.replace(controlRe, '')
.replace(reservedRe, '')
.replace(windowsReservedRe, '')
.replace(windowsTrailingRe, '')
return filename
}

/**
* Creates a filename based on the tweet and the user defined options.
* @param {Tweet} tweet - The entire tweet object from the Twitter v2 API
Expand All @@ -96,7 +116,7 @@ export const createFilename = (tweet: Tweet, filename = ''): string => {
filename = filename.replace('[[handle]]', tweet.includes.users[0].username)
filename = filename.replace('[[id]]', tweet.data.id)
filename = filename.replace('[[text]]', tweet.data.text)
return sanitize(filename) + '.md'
return sanitizeFilename(filename) + '.md'
}

/**
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.1.2": "0.12.16"
"1.1.3": "0.12.16"
}
19 changes: 0 additions & 19 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1880,13 +1880,6 @@ rxjs@^6.6.0:
resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a"
integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==

sanitize-filename@^1.6.3:
version "1.6.3"
resolved "https://registry.yarnpkg.com/sanitize-filename/-/sanitize-filename-1.6.3.tgz#755ebd752045931977e30b2025d340d7c9090378"
integrity sha512-y/52Mcy7aw3gRm7IrcGDFx/bCk4AhRh2eI9luHOQM86nZsqwiRkkq2GekHXBBD+SmPidc8i2PqtYZl+pWJ8Oeg==
dependencies:
truncate-utf8-bytes "^1.0.0"

semver-diff@^3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/semver-diff/-/semver-diff-3.1.1.tgz#05f77ce59f325e00e2706afd67bb506ddb1ca32b"
Expand Down Expand Up @@ -2076,13 +2069,6 @@ trim-newlines@^3.0.0:
resolved "https://registry.yarnpkg.com/trim-newlines/-/trim-newlines-3.0.1.tgz#260a5d962d8b752425b32f3a7db0dcacd176c144"
integrity sha512-c1PTsA3tYrIsLGkJkzHF+w9F2EyxfXGo4UyJc4pFL++FMjnq0HJS69T3M7d//gKrFKwy429bouPescbjecU+Zw==

truncate-utf8-bytes@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/truncate-utf8-bytes/-/truncate-utf8-bytes-1.0.2.tgz#405923909592d56f78a5818434b0b78489ca5f2b"
integrity sha1-QFkjkJWS1W94pYGENLC3hInKXys=
dependencies:
utf8-byte-length "^1.0.1"

tslib@^1.8.1, tslib@^1.9.0:
version "1.14.1"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
Expand Down Expand Up @@ -2185,11 +2171,6 @@ url-parse-lax@^3.0.0:
dependencies:
prepend-http "^2.0.0"

utf8-byte-length@^1.0.1:
version "1.0.4"
resolved "https://registry.yarnpkg.com/utf8-byte-length/-/utf8-byte-length-1.0.4.tgz#f45f150c4c66eee968186505ab93fcbb8ad6bf61"
integrity sha1-9F8VDExm7uloGGUFq5P8u4rWv2E=

v8-compile-cache@^2.0.3:
version "2.3.0"
resolved "https://registry.yarnpkg.com/v8-compile-cache/-/v8-compile-cache-2.3.0.tgz#2de19618c66dc247dcfb6f99338035d8245a2cee"
Expand Down

0 comments on commit 65fad74

Please sign in to comment.