All files / src/views wiki-view.ts

100% Statements 109/109
100% Branches 37/37
100% Functions 20/20
100% Lines 100/100

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210                2x       59x 59x 59x 59x 59x     59x 59x       1x       1x       1x       56x 56x 56x     56x 56x   56x   56x 56x 56x 56x   56x 56x 56x 56x 1x       56x         56x     56x     56x   56x       62x 62x   1x   62x 62x 1x         91x 90x   90x 91x   91x 62x       62x     30x 30x   28x 26x   28x 3x         29x 29x 30x         30x       30x   30x 30x 30x   30x   30x 30x   30x 29x 29x     30x 2x 2x 2x         10x 9x   9x   9x 9x 7x 7x   2x 2x               20x     19x 19x 19x 13x       19x 19x     19x 9x 9x 9x 5x 5x   5x 5x 2x 2x 2x     3x         9x 9x 2x 2x          
import { ItemView, MarkdownRenderer, setIcon, WorkspaceLeaf } from "obsidian";
import type LilbeePlugin from "../main";
import type { WikiPage, WikiPageDetail } from "../types";
import { CONCEPT_WIKI_PAGE_TYPES, WIKI_PAGE_TYPE } from "../types";
import { MESSAGES } from "../locales/en";
import { relativeTime } from "../utils";
import { CitationModal } from "./citation-modal";
 
export const VIEW_TYPE_WIKI = "lilbee-wiki";
 
export class WikiView extends ItemView {
    private plugin: LilbeePlugin;
    private pages: WikiPage[] = [];
    private selectedSlug: string | null = null;
    private listEl: HTMLElement | null = null;
    private detailEl: HTMLElement | null = null;
    private filterInput: HTMLInputElement | null = null;
 
    constructor(leaf: WorkspaceLeaf, plugin: LilbeePlugin) {
        super(leaf);
        this.plugin = plugin;
    }
 
    getViewType(): string {
        return VIEW_TYPE_WIKI;
    }
 
    getDisplayText(): string {
        return MESSAGES.LABEL_WIKI_VIEW;
    }
 
    getIcon(): string {
        return "book-open";
    }
 
    async onOpen(): Promise<void> {
        const { contentEl } = this;
        contentEl.empty();
        contentEl.addClass("lilbee-wiki-container");
 
        // Header
        const header = contentEl.createDiv({ cls: "lilbee-wiki-header" });
        header.createEl("h2", { text: MESSAGES.LABEL_WIKI });
 
        const actions = header.createDiv({ cls: "lilbee-toolbar-group" });
 
        const refreshBtn = actions.createEl("button", { cls: "lilbee-tasks-clear" });
        setIcon(refreshBtn, "refresh-cw");
        refreshBtn.setAttribute("aria-label", MESSAGES.BUTTON_REFRESH);
        refreshBtn.addEventListener("click", () => void this.refresh());
 
        const lintBtn = actions.createEl("button", { cls: "lilbee-tasks-clear" });
        setIcon(lintBtn, "check-circle");
        lintBtn.setAttribute("aria-label", MESSAGES.LABEL_WIKI_RUN_LINT);
        lintBtn.addEventListener("click", () => {
            void this.plugin.runWikiLint();
        });
 
        // Filter
        this.filterInput = contentEl.createEl("input", {
            type: "text",
            cls: "lilbee-wiki-search",
            placeholder: MESSAGES.PLACEHOLDER_TYPE_SEARCH,
        });
        this.filterInput.addEventListener("input", () => this.renderList());
 
        // Page list
        this.listEl = contentEl.createDiv({ cls: "lilbee-wiki-list" });
 
        // Detail area
        this.detailEl = contentEl.createDiv({ cls: "lilbee-wiki-detail" });
 
        void this.refresh();
    }
 
    async refresh(): Promise<void> {
        try {
            this.pages = await this.plugin.api.wikiList();
        } catch {
            this.pages = [];
        }
        this.renderList();
        if (this.selectedSlug) {
            void this.showPage(this.selectedSlug);
        }
    }
 
    private renderList(): void {
        if (!this.listEl) return;
        this.listEl.empty();
 
        const filter = this.filterInput?.value.toLowerCase() ?? "";
        const filtered = filter ? this.pages.filter((p) => p.title.toLowerCase().includes(filter)) : this.pages;
 
        if (filtered.length === 0) {
            this.listEl.createEl("p", {
                text: MESSAGES.LABEL_WIKI_NO_PAGES,
                cls: "lilbee-empty-state",
            });
            return;
        }
 
        const summaries = filtered.filter((p) => p.page_type === WIKI_PAGE_TYPE.SUMMARY);
        const concepts = filtered.filter((p) => CONCEPT_WIKI_PAGE_TYPES.has(p.page_type));
 
        if (summaries.length > 0) {
            this.renderGroup(this.listEl, MESSAGES.LABEL_WIKI_SUMMARIES, summaries);
        }
        if (concepts.length > 0) {
            this.renderGroup(this.listEl, MESSAGES.LABEL_WIKI_CONCEPTS, concepts);
        }
    }
 
    private renderGroup(container: HTMLElement, label: string, pages: WikiPage[]): void {
        container.createEl("h3", { text: label, cls: "lilbee-tasks-section-header" });
        for (const page of pages) {
            this.renderPageItem(container, page);
        }
    }
 
    private renderPageItem(container: HTMLElement, page: WikiPage): void {
        const item = container.createDiv({
            cls: `lilbee-wiki-page-item${page.slug === this.selectedSlug ? " active" : ""}`,
        });
 
        const info = item.createDiv({ cls: "lilbee-task-info" });
 
        const typeBadge = info.createSpan({ cls: "lilbee-wiki-type-badge" });
        typeBadge.addClass(`lilbee-wiki-type-${page.page_type}`);
        typeBadge.textContent = page.page_type;
 
        info.createSpan({ cls: "lilbee-task-name", text: page.title });
 
        const meta = item.createDiv({ cls: "lilbee-wiki-meta" });
        meta.createSpan({ text: MESSAGES.LABEL_WIKI_SOURCES_COUNT(page.source_count) });
 
        if (page.created_at) {
            const ts = new Date(page.created_at).getTime();
            meta.createSpan({ text: relativeTime(ts), cls: "lilbee-task-time" });
        }
 
        item.addEventListener("click", () => {
            this.selectedSlug = page.slug;
            this.renderList();
            void this.showPage(page.slug);
        });
    }
 
    private async showPage(slug: string): Promise<void> {
        if (!this.detailEl) return;
        this.detailEl.empty();
 
        const loading = this.detailEl.createDiv({ cls: "lilbee-loading" });
 
        try {
            const page = await this.plugin.api.wikiPage(slug);
            loading.remove();
            this.renderDetail(page);
        } catch {
            loading.remove();
            this.detailEl.createEl("p", {
                text: MESSAGES.ERROR_LOAD_PAGE,
                cls: "lilbee-empty-state",
            });
        }
    }
 
    private renderDetail(page: WikiPageDetail): void {
        if (!this.detailEl) return;
 
        // Metadata header
        const meta = this.detailEl.createDiv({ cls: "lilbee-wiki-meta" });
        meta.createEl("strong", { text: page.title });
        if (page.created_at) {
            meta.createSpan({ text: new Date(page.created_at).toLocaleString() });
        }
 
        // Markdown body
        const content = this.detailEl.createDiv({ cls: "lilbee-wiki-content" });
        void MarkdownRenderer.render(this.app, page.content, content, "", this);
 
        // Handle wikilink clicks within rendered content
        content.addEventListener("click", (e) => {
            const target = e.target as HTMLElement;
            const link = target.closest("a.internal-link");
            if (link) {
                e.preventDefault();
                const href = link.getAttribute("data-href") ?? link.textContent ?? "";
                // Check if it's a wiki page slug
                const matchingPage = this.pages.find((p) => p.slug === href || p.title === href);
                if (matchingPage) {
                    this.selectedSlug = matchingPage.slug;
                    this.renderList();
                    void this.showPage(matchingPage.slug);
                } else {
                    // Fall back to opening as a vault file
                    void this.app.workspace.openLinkText(href, "");
                }
            }
 
            // Handle citation footnote clicks
            const footnoteRef = target.closest("a[href^='#^src'], a[href^='#fn']");
            if (footnoteRef && this.selectedSlug) {
                e.preventDefault();
                new CitationModal(this.app, this.plugin, this.selectedSlug).open();
            }
        });
    }
}