Skip to content

Commit 91483a1

Browse files
committed
feat: Support .htm extension alongside .html
Closes #9
1 parent f971d8b commit 91483a1

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

main.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ 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);
1418

1519
interface EmbedContext {
1620
containerEl: HTMLElement;
@@ -177,7 +181,7 @@ class HtmlView extends FileView {
177181
}
178182

179183
canAcceptExtension(extension: string): boolean {
180-
return extension === "html";
184+
return isHtmlExtension(extension);
181185
}
182186

183187
async onLoadFile(file: TFile): Promise<void> {
@@ -258,16 +262,18 @@ export default class HtmlDocsPlugin extends Plugin {
258262
VIEW_TYPE_HTML,
259263
(leaf: WorkspaceLeaf) => new HtmlView(leaf),
260264
);
261-
this.registerExtensions(["html"], VIEW_TYPE_HTML);
265+
this.registerExtensions([...HTML_EXTENSIONS], VIEW_TYPE_HTML);
262266
this.registerExistingHtmlTabNavigation();
263267
this.registerThemeRefresh();
264268

265269
const embedRegistry = (this.app as unknown as AppWithEmbedRegistry).embedRegistry;
266270
if (!embedRegistry) {
267271
throw new Error("HTML Docs: app.embedRegistry is unavailable; cannot register HTML embeds.");
268272
}
269-
embedRegistry.registerExtension("html", (context, file) => new HtmlEmbed(context.containerEl, this, file));
270-
this.register(() => embedRegistry.unregisterExtension("html"));
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+
}
271277

272278
// Obsidian hides files with unrecognized extensions in the file
273279
// explorer unless "Show all file types" is on; registering the
@@ -359,7 +365,7 @@ export default class HtmlDocsPlugin extends Plugin {
359365
const file =
360366
this.app.metadataCache.getFirstLinkpathDest(linkpath, sourcePath) ??
361367
this.app.vault.getAbstractFileByPath(linkpath);
362-
return file instanceof TFile && file.extension === "html" ? file : null;
368+
return file instanceof TFile && isHtmlExtension(file.extension) ? file : null;
363369
}
364370

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

test/test.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,9 +447,9 @@ check "theme class re-render flips scheme" "$(echo "$THEME_RERENDER" | jq -r '.a
447447
check "theme class re-render swaps blob" "$(echo "$THEME_RERENDER" | jq -r .rerendered)" "true"
448448
check "theme class test restores classes" "$(echo "$THEME_RERENDER" | jq -r .restored)" "true"
449449
check ".html routes to html-docs view" "$(echo "$REGISTRY" | jq -r .htmlViewType)" "html-docs"
450-
check ".htm is not registered" "$(echo "$REGISTRY" | jq -r .htmViewType)" "null"
450+
check ".htm routes to html-docs view" "$(echo "$REGISTRY" | jq -r .htmViewType)" "html-docs"
451451
check ".html embed registered" "$(echo "$REGISTRY" | jq -r .htmlEmbedRegistered)" "true"
452-
check ".htm embed not registered" "$(echo "$REGISTRY" | jq -r .htmEmbedRegistered)" "false"
452+
check ".htm embed registered" "$(echo "$REGISTRY" | jq -r .htmEmbedRegistered)" "true"
453453
check "open existing html tab from link" "$(echo "$DEDUPED_NAVIGATION" | jq -r .htmlLeafCount)" "1"
454454
check "existing html tab is focused" "$(echo "$DEDUPED_NAVIGATION" | jq -r .activeFile)" "$FIXTURE_REL"
455455
check "deferred html state is reused" "$(echo "$DEFERRED_STATE_NAVIGATION" | jq -r .activeIsFakeDeferredLeaf)" "true"

0 commit comments

Comments
 (0)