Skip to content

Commit 9ed0307

Browse files
committed
refactor: Inline html/htm checks instead of const+type guard
Three-similar-lines beats a premature abstraction here — the set of HTML extension aliases is closed at html/htm.
1 parent 91483a1 commit 9ed0307

1 file changed

Lines changed: 9 additions & 11 deletions

File tree

main.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ import {
1111
} from "obsidian";
1212

1313
const VIEW_TYPE_HTML = "html-docs";
14-
const HTML_EXTENSIONS = ["html", "htm"] as const;
15-
type HtmlExtension = (typeof HTML_EXTENSIONS)[number];
16-
const isHtmlExtension = (ext: string): ext is HtmlExtension =>
17-
(HTML_EXTENSIONS as readonly string[]).includes(ext);
1814

1915
interface EmbedContext {
2016
containerEl: HTMLElement;
@@ -181,7 +177,7 @@ class HtmlView extends FileView {
181177
}
182178

183179
canAcceptExtension(extension: string): boolean {
184-
return isHtmlExtension(extension);
180+
return extension === "html" || extension === "htm";
185181
}
186182

187183
async onLoadFile(file: TFile): Promise<void> {
@@ -262,18 +258,20 @@ export default class HtmlDocsPlugin extends Plugin {
262258
VIEW_TYPE_HTML,
263259
(leaf: WorkspaceLeaf) => new HtmlView(leaf),
264260
);
265-
this.registerExtensions([...HTML_EXTENSIONS], VIEW_TYPE_HTML);
261+
this.registerExtensions(["html", "htm"], VIEW_TYPE_HTML);
266262
this.registerExistingHtmlTabNavigation();
267263
this.registerThemeRefresh();
268264

269265
const embedRegistry = (this.app as unknown as AppWithEmbedRegistry).embedRegistry;
270266
if (!embedRegistry) {
271267
throw new Error("HTML Docs: app.embedRegistry is unavailable; cannot register HTML embeds.");
272268
}
273-
for (const ext of HTML_EXTENSIONS) {
274-
embedRegistry.registerExtension(ext, (context, file) => new HtmlEmbed(context.containerEl, this, file));
275-
this.register(() => embedRegistry.unregisterExtension(ext));
276-
}
269+
embedRegistry.registerExtension("html", (context, file) => new HtmlEmbed(context.containerEl, this, file));
270+
embedRegistry.registerExtension("htm", (context, file) => new HtmlEmbed(context.containerEl, this, file));
271+
this.register(() => {
272+
embedRegistry.unregisterExtension("html");
273+
embedRegistry.unregisterExtension("htm");
274+
});
277275

278276
// Obsidian hides files with unrecognized extensions in the file
279277
// explorer unless "Show all file types" is on; registering the
@@ -365,7 +363,7 @@ export default class HtmlDocsPlugin extends Plugin {
365363
const file =
366364
this.app.metadataCache.getFirstLinkpathDest(linkpath, sourcePath) ??
367365
this.app.vault.getAbstractFileByPath(linkpath);
368-
return file instanceof TFile && isHtmlExtension(file.extension) ? file : null;
366+
return file instanceof TFile && (file.extension === "html" || file.extension === "htm") ? file : null;
369367
}
370368

371369
private findOpenHtmlLeaf(file: TFile): WorkspaceLeaf | null {

0 commit comments

Comments
 (0)