Skip to content

Commit

Permalink
Make file extension recognition case insensitive
Browse files Browse the repository at this point in the history
This fixes issue #57

It seems necessary to introduce a version to the application settings.
The file extensions for Markdown files were case sensitive before.
  • Loading branch information
c3er committed Dec 17, 2024
1 parent 1441395 commit f9bc0af
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ exports.isText = filePath => {

exports.extractFileEnding = filePath => {
const parts = filePath.split(".")
return parts.length > 1 ? parts.at(-1) : ""
return parts.length > 1 ? parts.at(-1).toLowerCase() : ""
}

exports.transformRelativePath = (documentDirectory, filePath) =>
Expand Down
34 changes: 31 additions & 3 deletions app/lib/storageMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ let electron

const JSON_INDENTATION = 4

const APPLICATION_SETTINGS_VERSION = 1
const DOCUMENT_SETTINGS_VERSION = 0
const FILE_HISTORY_VERSION = 0

const APPLICATION_SETTINGS_FILE = "app-settings.json"
const DOCUMENT_SETTINGS_FILE = "doc-settings.json"
const FILE_HISTORY_FILE = "file-history.json"
Expand All @@ -21,13 +25,23 @@ let _fileHistory
let _documentSettings = {}

class StorageBase {
#VERSION_KEY = "version"

_storagePath
_data

constructor(storageDir, storageFile) {
curentVersion
actualVersion

constructor(currentVersion, storageDir, storageFile) {
StorageBase._initStorageDir(storageDir)

this._storagePath = path.join(storageDir, storageFile)
this._data = StorageBase._initData(this._storagePath)

this.curentVersion = currentVersion
this.actualVersion = this._data[this.#VERSION_KEY] ?? 0
this._data[this.#VERSION_KEY] = currentVersion
}

_save() {
Expand Down Expand Up @@ -94,6 +108,11 @@ class ApplicationSettings extends StorageBase {
DRAG_DROP_BEHAVIOR_DEFAULT = dragDrop.behavior.ask
FILE_HISTORY_SIZE_DEFAULT = 5

constructor(storageDir, storageFile) {
super(APPLICATION_SETTINGS_VERSION, storageDir, storageFile)
this._updateVersion()
}

get theme() {
return this._loadValue(this.#THEME_KEY, electron.nativeTheme.themeSource)
}
Expand Down Expand Up @@ -196,6 +215,15 @@ class ApplicationSettings extends StorageBase {
this._data[key] = value
this._save()
}

_updateVersion() {
if (this.actualVersion === this.VERSION) {
return
}

// Currently, the only previous version is version 0
this.mdFileTypes = [...new Set(this.mdFileTypes.map(fileType => fileType.toLowerCase()))]
}
}

class DocumentSettings extends StorageBase {
Expand All @@ -218,7 +246,7 @@ class DocumentSettings extends StorageBase {
_documentData

constructor(storageDir, storageFile, documentPath) {
super(storageDir, storageFile)
super(DOCUMENT_SETTINGS_VERSION, storageDir, storageFile)
if (!this._data[documentPath]) {
this._data[documentPath] = {}
}
Expand Down Expand Up @@ -294,7 +322,7 @@ class DocumentSettings extends StorageBase {

class FileHistory extends StorageBase {
constructor(storageDir, storageFile) {
super(storageDir, storageFile)
super(FILE_HISTORY_VERSION, storageDir, storageFile)
if (!this._data.files) {
this._data.files = []
}
Expand Down
26 changes: 22 additions & 4 deletions test/libStorage.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,30 @@ const assert = require("assert")
const lib = require("./testLib")
const mocking = require("./mocking")

const common = require("../app/lib/common")

const storage = require("../app/lib/storageMain")

async function initStorage() {
await lib.removeDataDir()
storage.reset()
storage.init(mocking.dataDir, mocking.electron)
}

describe("Storage", () => {
describe("Application settings", () => {
let applicationSettings

beforeEach(async () => {
await initStorage()
applicationSettings = storage.loadApplicationSettings()
})

it("has default Markdown endings", () => {
assert.deepStrictEqual(applicationSettings.mdFileTypes, common.FILE_EXTENSIONS)
})
})

describe("File history", () => {
let applicationSettings
let fileHistory
Expand All @@ -18,10 +39,7 @@ describe("Storage", () => {
}

beforeEach(async () => {
await lib.removeDataDir()
storage.reset()

storage.init(mocking.dataDir, mocking.electron)
await initStorage()

applicationSettings = storage.loadApplicationSettings()
fileHistory = storage.loadFileHistory()
Expand Down

0 comments on commit f9bc0af

Please sign in to comment.