Skip to content

Commit

Permalink
Add open daily note base feature (#79)
Browse files Browse the repository at this point in the history
* 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
francishamel authored Jul 15, 2020
1 parent cfc94ab commit 6b99a8b
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 7 deletions.
48 changes: 47 additions & 1 deletion packages/foam-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,58 @@
],
"activationEvents": [
"workspaceContains:.vscode/foam.json",
"onCommand:foam-vscode.update-wikilinks"
"onCommand:foam-vscode.update-wikilinks",
"onCommand:foam-vscode.open-daily-note"
],
"main": "./out/extension.js",
"contributes": {
"commands": [
{
"command": "foam-vscode.update-wikilinks",
"title": "Foam: Update Markdown Reference List"
},
{
"command": "foam-vscode.open-daily-note",
"title": "Foam: Open Daily Note"
}
],
"configuration": {
"title": "Foam",
"properties": {
"foam.openDailyNote.fileExtension": {
"type": "string",
"scope": "resource",
"default": "md"
},
"foam.openDailyNote.filenameFormat": {
"type": "string",
"default": "isoDate",
"markdownDescription": "Specifies how the daily note filename is formatted. See the [dateformat docs](https://www.npmjs.com/package/dateformat) for valid formats",
"scope": "resource"
},
"foam.openDailyNote.titleFormat": {
"type": [
"string",
"null"
],
"default": null,
"markdownDescription": "Specifies how the daily note title is formatted. Will default to the filename format if set to null. See the [dateformat docs](https://www.npmjs.com/package/dateformat) for valid formats",
"scope": "resource"
},
"foam.openDailyNote.directory": {
"type": [
"string",
"null"
],
"default": null,
"description": "The directory into which daily notes should be created. Defaults to the workspace root."
}
}
},
"keybindings": [
{
"command": "foam-vscode.open-daily-note",
"key": "alt+d"
}
]
},
Expand All @@ -37,6 +81,7 @@
"publish-extension": "npx vsce publish && yarn npm-cleanup"
},
"devDependencies": {
"@types/dateformat": "^3.0.1",
"@types/glob": "^7.1.1",
"@types/node": "^13.11.0",
"@types/vscode": "^1.45.1",
Expand All @@ -49,6 +94,7 @@
"vscode-test": "^1.3.0"
},
"dependencies": {
"dateformat": "^3.0.3",
"foam-core": "^0.2.0"
}
}
10 changes: 4 additions & 6 deletions packages/foam-vscode/src/features/index.ts
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];
96 changes: 96 additions & 0 deletions packages/foam-vscode/src/features/open-daily-note.ts
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;

0 comments on commit 6b99a8b

Please sign in to comment.