Skip to content

Commit

Permalink
fix: note title not respect file name
Browse files Browse the repository at this point in the history
  • Loading branch information
levirs565 committed May 26, 2023
1 parent 77d52fc commit 7c9746a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
11 changes: 7 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from "obsidian";
import { DendronView, VIEW_TYPE_DENDRON } from "./view";
import { activeFile, rootNote } from "./store";
import { NoteTree, generateNoteTitle, getNoteTemplate } from "./note";
import { NoteTree, generateNoteTitle, getNoteTemplate, isUseTitleCase } from "./note";
import { LookupModal } from "./modal/lookup";
import { dendronActivityBarIcon, dendronActivityBarName } from "./icons";
import { InvalidRootModal } from "./modal/invalid-root";
Expand Down Expand Up @@ -64,9 +64,12 @@ export default class DendronTreePlugin extends Plugin {

onunload() {}

async createNote(dendronPath: string) {
const path = `${this.settings.vaultPath}/${dendronPath}.md`;
const title = generateNoteTitle(dendronPath);
async createNote(baseName: string) {
const path = `${this.settings.vaultPath}/${baseName}.md`;
const title = generateNoteTitle(
NoteTree.getPathFromFileName(baseName).at(-1)!,
isUseTitleCase(baseName)
);
const template = getNoteTemplate(title);
return await this.app.vault.create(path, template);
}
Expand Down
38 changes: 28 additions & 10 deletions src/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ export class Note {
parent?: Note;
title = "";

constructor(name: string) {
this.name = name.toLowerCase();
this.title = generateNoteTitle(this.name);
constructor(private originalName: string, private titlecase: boolean) {
this.name = originalName.toLowerCase();
this.title = generateNoteTitle(this.originalName, this.titlecase);
}

appendChild(note: Note) {
Expand Down Expand Up @@ -52,13 +52,30 @@ export class Note {
syncMetadata(metadataCache: MetadataCache) {
if (!this.file) return;
const cache = metadataCache.getFileCache(this.file);
this.title = cache?.frontmatter?.["title"] ?? generateNoteTitle(this.name);
this.title =
cache?.frontmatter?.["title"] ?? generateNoteTitle(this.originalName, this.titlecase);
}
}

export function generateNoteTitle(path: string) {
return path
.substring(path.lastIndexOf(".") + 1)
/**
* Check whetever generated note title must be title case or not
* @param baseName file base name
*/

export function isUseTitleCase(baseName: string) {
return baseName.toLowerCase() === baseName;
}

/**
* Generate title for note
* @param originalName name of note before lowercased (not filename)
* @param titlecase use title case or use original name
* @returns title for note
*/

export function generateNoteTitle(originalName: string, titlecase: boolean) {
if (!titlecase) return originalName;
return originalName
.split("-")
.map((item) => item.trim())
.filter((item) => item.length > 0)
Expand All @@ -80,13 +97,13 @@ created: ${time}
}

export class NoteTree {
root: Note = new Note("root");
root: Note = new Note("root", true);

sort() {
this.root.sortChildren(true);
}

private static getPathFromFileName(name: string) {
public static getPathFromFileName(name: string) {
return name.split(".");
}

Expand All @@ -95,6 +112,7 @@ export class NoteTree {
}

addFile(file: TFile, metadataCache: MetadataCache, sort: boolean) {
const titlecase = isUseTitleCase(file.basename);
const path = NoteTree.getPathFromFileName(file.basename);

let currentNote: Note = this.root;
Expand All @@ -105,7 +123,7 @@ export class NoteTree {
let note: Note | undefined = currentNote.findChildren(name);

if (!note) {
note = new Note(name);
note = new Note(name, titlecase);
currentNote.appendChild(note);
if (sort) currentNote.sortChildren(false);
}
Expand Down

0 comments on commit 7c9746a

Please sign in to comment.