generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from decaf-dev/dev
1.2.0
- Loading branch information
Showing
20 changed files
with
502 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const normalizePath = jest.fn((path) => path); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
module.exports = { | ||
preset: "ts-jest", | ||
testEnvironment: "node", | ||
moduleNameMapper: { | ||
"^src/(.*)$": "<rootDir>/src/$1", | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const removeFrontmatterBlock = (content: string) => { | ||
const FRONTMATTER_REGEX = /^---[\s\S]*?---/; | ||
return content.replace(FRONTMATTER_REGEX, "").trim(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Sanitizes a file name for use in a file system | ||
*/ | ||
export const sanitizeFileName = (name: string) => { | ||
// Replace colon with hyphen | ||
name = name.replace(/:/g, "-"); | ||
// Replace back slash with space | ||
name = name.replace(/\\/g, " "); | ||
// Replace forward slash with space | ||
name = name.replace(/\//g, " "); | ||
// Replace carrot with nothing | ||
name = name.replace(/\^/g, ""); | ||
// Replace left bracket with nothing | ||
name = name.replace(/\[/g, ""); | ||
// Replace right bracket with nothing | ||
name = name.replace(/\]/g, ""); | ||
// Replace hash tag with nothing | ||
name = name.replace(/#/g, ""); | ||
// Replace pipe with nothing | ||
name = name.replace(/\|/g, ""); | ||
return name.trim(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { normalizePath, TFile } from "obsidian"; | ||
import { removeFrontmatterBlock } from "./remove-frontmatter-block"; | ||
import { sanitizeFileName } from "./sanitize-file-name"; | ||
import { truncateFileName } from "./truncate-file-name"; | ||
import { NodeFileSystem, NoteSplitterSettings, Notifier } from "src/types"; | ||
|
||
export const splitByDelimiter = async ( | ||
fileSystem: NodeFileSystem, | ||
notify: Notifier, | ||
file: TFile, | ||
{ | ||
delimiter, | ||
saveFolderPath, | ||
useContentAsTitle, | ||
appendToSplitContent, | ||
deleteOriginalNote, | ||
}: Pick< | ||
NoteSplitterSettings, | ||
| "saveFolderPath" | ||
| "delimiter" | ||
| "useContentAsTitle" | ||
| "appendToSplitContent" | ||
| "deleteOriginalNote" | ||
>, | ||
) => { | ||
const escapedDelimiter = delimiter.replace(/\\n/g, "\n"); | ||
|
||
if (escapedDelimiter === "") { | ||
notify("No delimiter set. Please set a delimiter in the settings."); | ||
return; | ||
} | ||
|
||
const data = await fileSystem.read(file); | ||
const dataWithoutFrontmatter = removeFrontmatterBlock(data); | ||
if (dataWithoutFrontmatter === "") { | ||
notify("No content to split."); | ||
return; | ||
} | ||
|
||
const splitContent = dataWithoutFrontmatter | ||
.split(escapedDelimiter) | ||
.map((content) => content.trim()) | ||
.filter((content) => content !== ""); | ||
|
||
if (splitContent.length === 1) { | ||
notify("Only one section of content found. Nothing to split."); | ||
return; | ||
} | ||
|
||
const folderPath = saveFolderPath || ""; | ||
|
||
try { | ||
await fileSystem.createFolder(folderPath); | ||
} catch (err) { | ||
// Folder already exists | ||
} | ||
|
||
let filesCreated = 0; | ||
for (const [i, originalContent] of splitContent.entries()) { | ||
let updatedContent = originalContent; | ||
if (appendToSplitContent.length > 0) { | ||
updatedContent += appendToSplitContent; | ||
} | ||
|
||
let fileName = ""; | ||
if (useContentAsTitle) { | ||
fileName = originalContent.split("\n")[0] + ".md"; | ||
} else { | ||
fileName = `split-note-${Date.now() + i}.md`; | ||
} | ||
|
||
fileName = sanitizeFileName(fileName); | ||
fileName = truncateFileName(fileName); | ||
|
||
const filePath = normalizePath(`${folderPath}/${fileName}`); | ||
|
||
try { | ||
await fileSystem.create(filePath, updatedContent); | ||
filesCreated++; | ||
} catch (err) { | ||
if (err.message.includes("already exists")) { | ||
const newFilePath = `${folderPath}/Split conflict ${crypto.randomUUID()}.md`; | ||
try { | ||
await fileSystem.create(newFilePath, updatedContent); | ||
filesCreated++; | ||
} catch (err) { | ||
console.error(err); | ||
notify(`Error creating file: ${err.message}`); | ||
} | ||
continue; | ||
} | ||
notify(`Error creating file: ${err.message}`); | ||
console.log(err); | ||
} | ||
} | ||
|
||
if (deleteOriginalNote && filesCreated === splitContent.length) { | ||
await fileSystem.delete(file); | ||
} | ||
|
||
notify("Split into " + filesCreated + " note" + (filesCreated > 1 ? "s" : "") + "."); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* Truncates the string to the maximum length allowed for a file name | ||
*/ | ||
export const truncateFileName = (name: string) => { | ||
const MAX_LENGTH = 255; | ||
|
||
const splitArr = name.split("."); | ||
if (splitArr.length < 2) { | ||
throw new Error("Invalid file name"); | ||
} | ||
|
||
const baseName = splitArr[0]; | ||
const extension = splitArr[1]; | ||
return baseName.substring(0, MAX_LENGTH - extension.length - 1) + "." + splitArr[1]; | ||
}; |
Oops, something went wrong.