-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create block reference via command
close #89
- Loading branch information
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters