Skip to content

Commit

Permalink
feat: create block reference via command
Browse files Browse the repository at this point in the history
close #89
  • Loading branch information
Vinzent03 committed Nov 22, 2022
1 parent 8b8b887 commit f7f37cb
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/block_utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Editor, EditorPosition, MarkdownView, SectionCache, TFile } from "obsidian";

export abstract class BlockUtils {
private static getBlock(editor: Editor, file: TFile): SectionCache | undefined {
const cursor = editor.getCursor("to");
const fileCache = app.metadataCache.getFileCache(file);

const currentBlock =
fileCache?.sections?.find((section) =>
section.position.start.line <= cursor.line &&
section.position.end.line >= cursor.line
);
return currentBlock;
}

private static getIdOfBlock(
editor: Editor,
block: SectionCache,
): string {
const blockId = block.id;

if (blockId) {
return blockId;
}

// Add a block id
const sectionEnd = block.position.end;
const end: EditorPosition = {
ch: sectionEnd.col,
line: sectionEnd.line,
};

const newId = Math.random().toString(36).substring(2, 8);
const spacer = BlockUtils.shouldInsertAfter(block) ? "\n\n" : " ";

editor.replaceRange(`${spacer}^${newId}`, end);
return newId;
}

private static shouldInsertAfter(block: SectionCache): boolean {
if (block.type) {
return [
"blockquote",
"code",
"table",
"heading",
"comment",
"footnoteDefinition",
].includes(block.type);
}
}

public static getBlockId(): string | undefined {
const view = app.workspace.getActiveViewOfType(MarkdownView);
if (view) {
const editor = view.editor;
const file = view.file;
const block = this.getBlock(editor, file);
if (block)
return this.getIdOfBlock(editor, block);
}
}

}
16 changes: 16 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { base64ToArrayBuffer, CachedMetadata, MarkdownView, normalizePath, Notic
import { stripMD } from "obsidian-community-lib";
import { appHasDailyNotesPluginLoaded, createDailyNote, getAllDailyNotes, getDailyNote } from "obsidian-daily-notes-interface";
import { v4 as uuidv4 } from 'uuid';
import { BlockUtils } from "./block_utils";
import { getDailyNotePath } from "./daily_note_utils";
import { CommandModal } from "./modals/command_modal";
import { EnterDataModal } from "./modals/enter_data_modal";
Expand Down Expand Up @@ -75,6 +76,21 @@ export default class AdvancedURI extends Plugin {
}
});

this.addCommand({
id: "copy-uri-block",
name: "copy URI for current block",
checkCallback: (checking) => {
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (checking) return view != undefined;
const id = BlockUtils.getBlockId();
if (id) {
this.copyURI({
filepath: view.file.path,
block: id
});
}
}
});

this.registerObsidianProtocolHandler("advanced-uri", async (e) => {
const parameters = e as unknown as Parameters;
Expand Down

0 comments on commit f7f37cb

Please sign in to comment.