Skip to content

Commit

Permalink
truncate filenames to fit OS length limit
Browse files Browse the repository at this point in the history
Closes #6
  • Loading branch information
kbravh committed Nov 14, 2021
1 parent 65fad74 commit 8f7f1fd
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 24 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.3",
"version": "1.1.4",
"minAppVersion": "0.12.16",
"description": "Save tweets as Markdown files, along with their images, polls, etc.",
"author": "kbravh",
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "obsidian-tweet-to-markdown",
"version": "1.1.3",
"version": "1.1.4",
"description": "Save tweets as beautiful markdown files in Obsidian (https://obsidian.md)",
"main": "main.js",
"engines": {
"node": ">=10.0.0"
"node": ">=12.0.0"
},
"scripts": {
"dev": "rollup --config rollup.config.js -w",
Expand All @@ -16,7 +16,7 @@
"license": "MIT",
"devDependencies": {
"@rollup/plugin-commonjs": "^18.0.0",
"@rollup/plugin-node-resolve": "^11.2.1",
"@rollup/plugin-node-resolve": "^13.0.6",
"@rollup/plugin-typescript": "^8.2.1",
"@types/node": "^14.14.37",
"gts": "^3.1.0",
Expand Down
23 changes: 9 additions & 14 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import typescript from '@rollup/plugin-typescript';
import {nodeResolve} from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import typescript from '@rollup/plugin-typescript'
import {nodeResolve} from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'

const isProd = (process.env.BUILD === 'production');
const isProd = process.env.BUILD === 'production'

const banner =
`/*
const banner = `/*
THIS IS A GENERATED/BUNDLED FILE BY ROLLUP
if you want to view the source visit the plugins github repository
if you want to view the source visit the plugin's github repository
*/
`;
`

export default {
input: 'main.ts',
Expand All @@ -22,9 +21,5 @@ export default {
banner,
},
external: ['obsidian'],
plugins: [
typescript(),
nodeResolve({browser: true}),
commonjs(),
]
};
plugins: [typescript(), nodeResolve({browser: true}), commonjs()],
}
58 changes: 58 additions & 0 deletions src/unicodeSubstring.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Credit: lautis/unicode-substring
* Rewritten for Obsidian mobile functionality.
*/

const charAt = (string: string, index: number): string => {
const first = string.charCodeAt(index)
let second
if (first >= 0xd800 && first <= 0xdbff && string.length > index + 1) {
second = string.charCodeAt(index + 1)
if (second >= 0xdc00 && second <= 0xdfff) {
return string.substring(index, index + 2)
}
}
return string[index]
}

const slice = (string: string, start: number, end: number): string => {
let accumulator = ''
let character
let stringIndex = 0
let unicodeIndex = 0
const length = string.length

while (stringIndex < length) {
character = charAt(string, stringIndex)
if (unicodeIndex >= start && unicodeIndex < end) {
accumulator += character
}
stringIndex += character.length
unicodeIndex += 1
}
return accumulator
}

const toNumber = (value: string | number, fallback: number): number => {
if (value === undefined) {
return fallback
} else {
return Number(value)
}
}

export const unicodeSubstring = (
string: string,
start: number,
end: number
): string => {
const realStart = toNumber(start, 0)
const realEnd = toNumber(end, string.length)
if (realEnd === realStart) {
return ''
} else if (realEnd > realStart) {
return slice(string, realStart, realEnd)
} else {
return slice(string, realEnd, realStart)
}
}
23 changes: 22 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {Media, Poll, Tweet} from './models'
import {DownloadManager} from './downloadManager'
import TTM from 'main'
import {TTMSettings} from './settings'
import {unicodeSubstring} from './unicodeSubstring'

/**
* Parses out the tweet ID from the URL the user provided
Expand Down Expand Up @@ -93,14 +94,34 @@ const reservedRe = /^\.+$/
const windowsReservedRe = /^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\..*)?$/i
const windowsTrailingRe = /[. ]+$/

/**
* Sanitize a filename to remove any illegal characters.
* Also keeps the filename to 255 bytes or below.
* @param filename string
* @returns string
*/
export const sanitizeFilename = (filename: string): string => {
filename = filename
.replace(illegalRe, '')
.replace(controlRe, '')
.replace(reservedRe, '')
.replace(windowsReservedRe, '')
.replace(windowsTrailingRe, '')
return filename
return truncateBytewise(filename, 252)
}

/**
* Truncate a string to a specified number of bytes
* @param string the string to truncate
* @param length the maximum length in bytes of the trimmed string
* @returns string
*/
export const truncateBytewise = (string: string, length: number): string => {
const originalLength = length
while (new TextEncoder().encode(string).length > originalLength) {
string = unicodeSubstring(string, 0, length--)
}
return string
}

/**
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.3": "0.12.16"
"1.1.4": "0.12.16"
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@
magic-string "^0.25.7"
resolve "^1.17.0"

"@rollup/plugin-node-resolve@^11.2.1":
version "11.2.1"
resolved "https://registry.yarnpkg.com/@rollup/plugin-node-resolve/-/plugin-node-resolve-11.2.1.tgz#82aa59397a29cd4e13248b106e6a4a1880362a60"
integrity sha512-yc2n43jcqVyGE2sqV5/YCmocy9ArjVAP/BeXyTtADTBBX6V0e5UMqwO8CdQ0kzjb6zu5P1qMzsScCMRvE9OlVg==
"@rollup/plugin-node-resolve@^13.0.6":
version "13.0.6"
resolved "https://registry.yarnpkg.com/@rollup/plugin-node-resolve/-/plugin-node-resolve-13.0.6.tgz#29629070bb767567be8157f575cfa8f2b8e9ef77"
integrity sha512-sFsPDMPd4gMqnh2gS0uIxELnoRUp5kBl5knxD2EO0778G1oOJv4G1vyT2cpWz75OU2jDVcXhjVUuTAczGyFNKA==
dependencies:
"@rollup/pluginutils" "^3.1.0"
"@types/resolve" "1.17.1"
Expand Down

0 comments on commit 8f7f1fd

Please sign in to comment.