Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
8d968e1
Rename trang.JPG to trang.jpg
mdroidian Feb 13, 2025
d4c636b
remove trang.jpg
mdroidian Feb 13, 2025
95d426f
curr progress
trangdoan982 Feb 17, 2025
4a72b97
finished current settings
trangdoan982 Feb 22, 2025
c08d23d
small update
trangdoan982 Feb 24, 2025
34f6899
setting for hotkey
trangdoan982 Feb 24, 2025
f2de373
node instantiation finished
trangdoan982 Feb 25, 2025
833e246
address PR comments
trangdoan982 Feb 25, 2025
2ee0d28
add description
trangdoan982 Feb 25, 2025
9ae208c
address PR comments
trangdoan982 Feb 28, 2025
5eedbc4
fix the NodeType validation
trangdoan982 Feb 28, 2025
435233d
address PR review
trangdoan982 Mar 3, 2025
d9dc433
add Save button for new changes
trangdoan982 Mar 3, 2025
de5242e
types defined and basic settings up
trangdoan982 Mar 4, 2025
e00c251
fix the bug. now relationship is updated
trangdoan982 Mar 5, 2025
2c44ed9
change the style to show bidirectional relations visually
trangdoan982 Mar 5, 2025
4098e58
create plugin as context instead of passing in props
trangdoan982 Mar 5, 2025
9ff13f5
new type definitions + settings finished
trangdoan982 Mar 6, 2025
88466b5
rename
trangdoan982 Mar 6, 2025
3c8bdcd
Merge branch 'main' into trang/relationship-type-def
trangdoan982 Mar 6, 2025
0dbb188
check for duplicates
trangdoan982 Mar 6, 2025
c42d587
address PR comments
trangdoan982 Mar 6, 2025
bbc1871
Merge branch 'DiscourseGraphs:main' into trang/relationship-type-def
trangdoan982 Mar 17, 2025
585b631
current progress
trangdoan982 Mar 17, 2025
c4f591b
confirm before delete
trangdoan982 Mar 17, 2025
44e0c6b
first approach: registerMarkdownPostProcessor and eventListener on ac…
trangdoan982 Mar 18, 2025
957fd79
using ItemView approach
trangdoan982 Mar 19, 2025
a2c11f2
Merge branch 'DiscourseGraphs:main' into eng-40/identify-node-obsidian
trangdoan982 Mar 21, 2025
037d577
add some changes
trangdoan982 Mar 21, 2025
43d86f2
address PR comment
trangdoan982 Mar 21, 2025
4461c79
address PR comments
trangdoan982 Mar 24, 2025
3250a15
fix frontmatter issue
trangdoan982 Mar 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions apps/obsidian/src/components/DiscourseContextView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import { ItemView, TFile, WorkspaceLeaf } from "obsidian";
import { createRoot, Root } from "react-dom/client";
import DiscourseGraphPlugin from "~/index";
import { getDiscourseNodeFormatExpression } from "~/utils/getDiscourseNodeFormatExpression";
import { VIEW_TYPE_DISCOURSE_CONTEXT } from "~/types";

interface DiscourseContextProps {
activeFile: TFile | null;
plugin: DiscourseGraphPlugin;
}

const DiscourseContext = ({ activeFile, plugin }: DiscourseContextProps) => {
const extractContentFromTitle = (
format: string | undefined,
title: string,
): string => {
if (!format) return "";
const regex = getDiscourseNodeFormatExpression(format);
const match = title.match(regex);
return match?.[1] ?? title;
};

const renderContent = () => {
if (!activeFile) {
return <div>No file is open</div>;
}

const fileMetadata = plugin.app.metadataCache.getFileCache(activeFile);
if (!fileMetadata) {
return <div>File metadata not available</div>;
}

const frontmatter = fileMetadata.frontmatter;
if (!frontmatter) {
return <div>No discourse node data found</div>;
}

if (!frontmatter.nodeTypeId) {
return <div>Not a discourse node (no nodeTypeId)</div>;
}

const nodeType = plugin.settings.nodeTypes.find(
(type) => type.id === frontmatter.nodeTypeId,
);

if (!nodeType) {
return <div>Unknown node type: {frontmatter.nodeTypeId}</div>;
}
return (
<div>
<div
style={{
fontSize: "1.2em",
fontWeight: "bold",
marginBottom: "8px",
}}
>
{nodeType.name || "Unnamed Node Type"}
</div>

{nodeType.format && (
<div style={{ marginBottom: "4px" }}>
<span style={{ fontWeight: "bold" }}>Content: </span>
{extractContentFromTitle(nodeType.format, activeFile.basename)}
</div>
)}
</div>
);
};

return (
<div>
<h4 style={{ marginTop: 0 }}>Discourse Context</h4>
{renderContent()}
</div>
);
};

export class DiscourseContextView extends ItemView {
private plugin: DiscourseGraphPlugin;
private activeFile: TFile | null = null;
private root: Root | null = null;

constructor(leaf: WorkspaceLeaf, plugin: DiscourseGraphPlugin) {
super(leaf);
this.plugin = plugin;
}

setActiveFile(file: TFile | null): void {
this.activeFile = file;
this.updateView();
}

getViewType(): string {
return VIEW_TYPE_DISCOURSE_CONTEXT;
}

getDisplayText(): string {
return "Discourse Context";
}

getIcon(): string {
return "telescope";
}

async onOpen(): Promise<void> {
const container = this.containerEl.children[1];
if (container) {
container.empty();
container.addClass("discourse-context-container");

this.root = createRoot(container);

this.activeFile = this.app.workspace.getActiveFile();

this.updateView();

this.registerEvent(
this.app.workspace.on("file-open", (file) => {
this.activeFile = file;
this.updateView();
}),
);
}
}

updateView(): void {
if (this.root) {
this.root.render(
<DiscourseContext activeFile={this.activeFile} plugin={this.plugin} />,
);
}
}

async onClose(): Promise<void> {
if (this.root) {
this.root.unmount();
this.root = null;
}
}
}
39 changes: 36 additions & 3 deletions apps/obsidian/src/components/NodeTypeModal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { App, Editor, SuggestModal } from "obsidian";
import { App, Editor, SuggestModal, TFile, Notice } from "obsidian";
import { DiscourseNode } from "../types";
import { getDiscourseNodeFormatExpression } from "../utils/getDiscourseNodeFormatExpression";

Expand All @@ -25,8 +25,38 @@ export class NodeTypeModal extends SuggestModal<DiscourseNode> {
renderSuggestion(nodeType: DiscourseNode, el: HTMLElement) {
el.createEl("div", { text: nodeType.name });
}
async createDiscourseNode(
title: string,
nodeType: DiscourseNode,
): Promise<TFile | null> {
try {
const instanceId = `${nodeType.id}-${Date.now()}`;
const filename = `${title}.md`;

onChooseSuggestion(nodeType: DiscourseNode) {
await this.app.vault.create(filename, "");

const newFile = this.app.vault.getAbstractFileByPath(filename);
if (!(newFile instanceof TFile)) {
throw new Error("Failed to create new file");
}

await this.app.fileManager.processFrontMatter(newFile, (fm) => {
fm.nodeTypeId = nodeType.id;
fm.nodeInstanceId = instanceId;
});

new Notice(`Created discourse node: ${title}`);
return newFile;
} catch (error: unknown) {
const errorMessage =
error instanceof Error ? error.message : "Unknown error";
new Notice(`Error creating discourse node: ${errorMessage}`, 5000);
console.error("Failed to create discourse node:", error);
return null;
}
}

async onChooseSuggestion(nodeType: DiscourseNode) {
const selectedText = this.editor.getSelection();
const regex = getDiscourseNodeFormatExpression(nodeType.format);

Expand All @@ -39,6 +69,9 @@ export class NodeTypeModal extends SuggestModal<DiscourseNode> {
nodeFormat[2]?.replace(/\\/g, "");
if (!nodeFormat) return;

this.editor.replaceSelection(`[[${formattedNodeName}]]`);
const newFile = await this.createDiscourseNode(formattedNodeName, nodeType);
if (newFile) {
this.editor.replaceSelection(`[[${formattedNodeName}]]`);
}
}
}
46 changes: 43 additions & 3 deletions apps/obsidian/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Plugin } from "obsidian";
import { SettingsTab } from "~/components/Settings";
import { Settings } from "./types";
import { registerCommands } from "./utils/registerCommands";
import { Settings } from "~/types";
import { registerCommands } from "~/utils/registerCommands";
import { DiscourseContextView } from "~/components/DiscourseContextView";
import { VIEW_TYPE_DISCOURSE_CONTEXT } from "~/types";

const DEFAULT_SETTINGS: Settings = {
nodeTypes: [],
Expand All @@ -16,9 +18,47 @@ export default class DiscourseGraphPlugin extends Plugin {
await this.loadSettings();
registerCommands(this);
this.addSettingTab(new SettingsTab(this.app, this));

this.registerView(
VIEW_TYPE_DISCOURSE_CONTEXT,
(leaf) => new DiscourseContextView(leaf, this),
);

this.addRibbonIcon("telescope", "Toggle Discourse Context", () => {
this.toggleDiscourseContextView();
});
}

onunload() {}
toggleDiscourseContextView() {
const { workspace } = this.app;
const existingLeaf = workspace.getLeavesOfType(
VIEW_TYPE_DISCOURSE_CONTEXT,
)[0];

if (existingLeaf) {
existingLeaf.detach();
} else {
const activeFile = workspace.getActiveFile();
const leaf = workspace.getRightLeaf(false);
if (leaf) {
const layoutChangeHandler = () => {
const view = leaf.view;
if (view instanceof DiscourseContextView) {
view.setActiveFile(activeFile);
workspace.off("layout-change", layoutChangeHandler);
}
};

workspace.on("layout-change", layoutChangeHandler);

leaf.setViewState({
type: VIEW_TYPE_DISCOURSE_CONTEXT,
active: true,
});
workspace.revealLeaf(leaf);
}
}
}

async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
Expand Down
2 changes: 2 additions & 0 deletions apps/obsidian/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ export type Settings = {
discourseRelations: DiscourseRelation[];
relationTypes: DiscourseRelationType[];
};

export const VIEW_TYPE_DISCOURSE_CONTEXT = "discourse-context-view";
8 changes: 8 additions & 0 deletions apps/obsidian/src/utils/registerCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,12 @@ export const registerCommands = (plugin: DiscourseGraphPlugin) => {
new NodeTypeModal(plugin.app, editor, plugin.settings.nodeTypes).open();
},
});

plugin.addCommand({
id: "toggle-discourse-context",
name: "Toggle Discourse Context",
callback: () => {
plugin.toggleDiscourseContextView();
},
});
};