-
Notifications
You must be signed in to change notification settings - Fork 680
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add open daily note base feature (#79)
* Add dateformat package dependency * Implement Open Daily Note feature - Activate command - Add default keybindings for the command - Use settings to configure the filename format, the file extension, the title format and the directory where the note should be created
- Loading branch information
1 parent
cfc94ab
commit 6b99a8b
Showing
3 changed files
with
147 additions
and
7 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 |
---|---|---|
@@ -1,7 +1,5 @@ | ||
import createReferences from "./wikilink-reference-generation"; | ||
import openDailyNote from "./open-daily-note"; | ||
import { FoamFeature } from "../types"; | ||
|
||
import createReferences from './wikilink-reference-generation' | ||
import { FoamFeature } from '../types' | ||
|
||
export const features: FoamFeature[] = [ | ||
createReferences | ||
] | ||
export const features: FoamFeature[] = [createReferences, openDailyNote]; |
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,96 @@ | ||
import { | ||
window, | ||
workspace, | ||
Uri, | ||
WorkspaceConfiguration, | ||
ExtensionContext, | ||
commands, | ||
} from "vscode"; | ||
import { dirname, join } from "path"; | ||
import dateFormat = require("dateformat"); | ||
import fs = require("fs"); | ||
import { FoamFeature } from "../types"; | ||
|
||
const feature: FoamFeature = { | ||
activate: async (context: ExtensionContext) => { | ||
context.subscriptions.push( | ||
commands.registerCommand("foam-vscode.open-daily-note", openDailyNote) | ||
); | ||
}, | ||
}; | ||
|
||
async function openDailyNote() { | ||
const foamConfiguration = workspace.getConfiguration("foam"); | ||
const currentDate = new Date(); | ||
|
||
const dailyNotePath = getDailyNotePath(foamConfiguration, currentDate); | ||
|
||
createDailyNoteIfNotExists(foamConfiguration, dailyNotePath, currentDate); | ||
await focusDailyNote(dailyNotePath); | ||
} | ||
|
||
function getDailyNotePath(configuration: WorkspaceConfiguration, date: Date) { | ||
const rootDirectory = workspace.workspaceFolders[0].uri.fsPath; | ||
const dailyNoteDirectory: string = | ||
configuration.get("openDailyNote.directory") ?? "."; | ||
const dailyNoteFilename = getDailyNoteFileName(configuration, date); | ||
|
||
return join(rootDirectory, dailyNoteDirectory, dailyNoteFilename); | ||
} | ||
|
||
function getDailyNoteFileName( | ||
configuration: WorkspaceConfiguration, | ||
date: Date | ||
): string { | ||
const filenameFormat: string = configuration.get( | ||
"openDailyNote.filenameFormat" | ||
); | ||
const fileExtension: string = configuration.get( | ||
"openDailyNote.fileExtension" | ||
); | ||
|
||
return `${dateFormat(date, filenameFormat, false)}.${fileExtension}`; | ||
} | ||
|
||
async function createDailyNoteIfNotExists( | ||
configuration: WorkspaceConfiguration, | ||
dailyNotePath: string, | ||
currentDate: Date | ||
) { | ||
if (await pathExists(dailyNotePath)) { | ||
return; | ||
} | ||
|
||
createDailyNoteDirectoryIfNotExists(dailyNotePath); | ||
|
||
const titleFormat: string = | ||
configuration.get("openDailyNote.titleFormat") ?? | ||
configuration.get("openDailyNote.filenameFormat"); | ||
|
||
await fs.promises.writeFile( | ||
dailyNotePath, | ||
`# ${dateFormat(currentDate, titleFormat, false)}\r\n` | ||
); | ||
} | ||
|
||
async function createDailyNoteDirectoryIfNotExists(dailyNotePath: string) { | ||
const dailyNoteDirectory = dirname(dailyNotePath); | ||
|
||
if (!(await pathExists(dailyNoteDirectory))) { | ||
await fs.promises.mkdir(dailyNoteDirectory, { recursive: true }); | ||
} | ||
} | ||
|
||
async function focusDailyNote(dailyNotePath: string) { | ||
const document = await workspace.openTextDocument(Uri.parse(dailyNotePath)); | ||
window.showTextDocument(document); | ||
} | ||
|
||
async function pathExists(path: string) { | ||
return fs.promises | ||
.access(path, fs.constants.F_OK) | ||
.then(() => true) | ||
.catch(() => false); | ||
} | ||
|
||
export default feature; |