Skip to content

Commit

Permalink
fix: link in source and live mode target to title
Browse files Browse the repository at this point in the history
  • Loading branch information
levirs565 committed Aug 21, 2023
1 parent 9da4f4f commit a4ebd20
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/custom-resolver/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { createRefMarkdownProcessor } from "./ref-markdown-processor";
import { createLinkOpenHandler } from "./link-open";
import { LinkLivePlugin } from "./link-live";
import { createLinkMarkdownProcessor } from "./link-markdown-processor";
import { LinkRefClickbale } from "./link-ref-clickbale";

export class CustomResolver extends Component {
pagePreviewPlugin?: PagePreviewPlugin;
Expand All @@ -25,6 +26,9 @@ export class CustomResolver extends Component {
decorations: (value) => value.decorations,
}
);
linkRefClickbaleExtension = ViewPlugin.define((v) => {
return new LinkRefClickbale(v);
});

constructor(public plugin: Plugin, public workspace: DendronWorkspace) {
super();
Expand All @@ -34,6 +38,7 @@ export class CustomResolver extends Component {
this.plugin.app.workspace.onLayoutReady(() => {
this.plugin.app.workspace.registerEditorExtension(this.refEditorExtenstion);
this.plugin.app.workspace.registerEditorExtension(this.linkEditorExtenstion);
this.plugin.app.workspace.registerEditorExtension(this.linkRefClickbaleExtension);

this.pagePreviewPlugin = this.plugin.app.internalPlugins.getEnabledPluginById("page-preview");
if (!this.pagePreviewPlugin) return;
Expand All @@ -60,6 +65,7 @@ export class CustomResolver extends Component {
this.plugin.app.workspace.openLinkText = this.originalOpenLinkText;
MarkdownPreviewRenderer.unregisterPostProcessor(this.linkPostProcessor);
MarkdownPreviewRenderer.unregisterPostProcessor(this.refPostProcessor);
this.plugin.app.workspace.unregisterEditorExtension(this.linkRefClickbaleExtension);
this.plugin.app.workspace.unregisterEditorExtension(this.linkEditorExtenstion);
this.plugin.app.workspace.unregisterEditorExtension(this.refEditorExtenstion);

Expand Down
34 changes: 34 additions & 0 deletions src/custom-resolver/link-ref-clickbale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { EditorView, PluginValue } from "@codemirror/view";
import { Editor, editorInfoField } from "obsidian";

type GetClickableTokenType = Required<Editor>["getClickableTokenAt"];

export class LinkRefClickbale implements PluginValue {
static createClickableTokenAtWrapper(original: GetClickableTokenType): GetClickableTokenType {
return function (this: Editor, ...args) {
const result: ReturnType<GetClickableTokenType> = original.call(this, ...args);
if (result && result.type === "internal-link") {
const raw = this.getRange(result.start, result.end);
const split = raw.split("|", 2);
if (split.length === 2) {
result.text = split[1];
}
}
return result;
};
}

getClickableTokenAtOrig: Editor["getClickableTokenAt"];

constructor(private view: EditorView) {
const editor = view.state.field(editorInfoField).editor;
if (editor && editor.getClickableTokenAt) {
this.getClickableTokenAtOrig = editor.getClickableTokenAt;
editor.getClickableTokenAt = LinkRefClickbale.createClickableTokenAtWrapper(
this.getClickableTokenAtOrig
);
}
}

destroy(): void {}
}
13 changes: 12 additions & 1 deletion src/obsidian-ex.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,18 @@ declare module "obsidian" {
suggestions: {
setSuggestions(list: T[]);
};
setAutoDestroy(el: HTMLElement)
setAutoDestroy(el: HTMLElement);
reposition({ left: number, right: number, top: number, bottom: number });
}

interface ClickableToken {
type: string;
text: string;
start: EditorPosition;
end: EditorPosition;
}

interface Editor {
getClickableTokenAt?: (pos: EditorPosition) => ClickableToken | undefined;
}
}

0 comments on commit a4ebd20

Please sign in to comment.