Skip to content

Commit 33f2f54

Browse files
committed
Refactor code to use Obsidian’s createEl API instead of native createElement
1 parent d54005f commit 33f2f54

File tree

3 files changed

+11
-14
lines changed

3 files changed

+11
-14
lines changed

src/frontmatterImageEditorExtension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
} from "@codemirror/view";
1313
import FrontmatterImagePlugin from "./main";
1414
import { Pos, TFile } from "obsidian";
15-
import { getImageSrc, renderFrontmatterImage } from "./utils";
15+
import { getImageSrc, appendFrontmatterImage } from "./utils";
1616

1717
export interface FrontmatterImageStateFieldValue {
1818
readonly activeFile: TFile;
@@ -40,7 +40,7 @@ export const frontmatterImageEditorExtension = (
4040
Decoration.widget({
4141
widget: new (class extends WidgetType {
4242
toDOM(view: EditorView): HTMLElement {
43-
return renderFrontmatterImage(resolvedImageSrc);
43+
return appendFrontmatterImage(view.contentDOM, resolvedImageSrc);
4444
}
4545
})(),
4646
}),

src/main.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
Setting
88
} from "obsidian";
99
import { frontmatterImageEditorExtension, } from "src/frontmatterImageEditorExtension";
10-
import { getImageSrc, renderFrontmatterImage } from "./utils";
10+
import { getImageSrc, appendFrontmatterImage } from "./utils";
1111

1212
interface FrontmatterImagePluginSettings {
1313
imageKeys: string[];
@@ -36,12 +36,9 @@ export default class FrontmatterImagePlugin extends Plugin {
3636
const imageSrc = getImageSrc(context.sourcePath, this);
3737
if (!imageSrc) return;
3838

39-
const div = document.createElement("div");
40-
const img = renderFrontmatterImage(imageSrc);
41-
div.appendChild(img);
42-
const br = document.createElement("br");
43-
div.appendChild(br);
44-
element.appendChild(div);
39+
const div = element.createDiv();
40+
appendFrontmatterImage(div, imageSrc);
41+
div.createEl("br");
4542
},
4643
);
4744
}

src/utils.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,23 @@ export const getImageSrc = (activeFilePath: string, plugin: FrontmatterImagePlug
1717
rawValue = wikilinkMatch[1];
1818
}
1919

20-
// Check if it's already a full URL or path
20+
// Check if it's already a full URL or path
2121
if (rawValue.startsWith('http') || rawValue.startsWith('/') || rawValue.startsWith('data:')) {
2222
return rawValue;
2323
}
24-
24+
2525
// Try to resolve as an Obsidian file
2626
const file = plugin.app.metadataCache.getFirstLinkpathDest(rawValue, activeFilePath);
2727
if (file) {
2828
return plugin.app.vault.getResourcePath(file);
2929
}
30-
30+
3131
// If we can't resolve it, return the original value
3232
return rawValue;
3333
};
3434

35-
export const renderFrontmatterImage = (src: string): HTMLElement => {
36-
const img = document.createElement("img");
35+
export const appendFrontmatterImage = (containerEl: HTMLElement, src: string): HTMLElement => {
36+
const img = containerEl.createEl("img");
3737
img.src = src;
3838
img.classList.add("frontmatter-image");
3939
return img;

0 commit comments

Comments
 (0)