All files / src/libs/KanbanSync KanbanMarkdownGenerator.ts

0% Statements 0/35
0% Branches 0/27
0% Functions 0/11
0% Lines 0/35

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                                                                                                                                                                                                                                                                           
import { TFile } from 'obsidian';
import type { IApp } from 'src/interfaces/IApp';
import type { ILogger, ILogger_ } from 'src/interfaces/ILogger';
import { KanbanBoard, KanbanList } from './KanbanModels';
import { ArchivedString, CompletedString } from './KanbanTypes';
import { Inject } from '../DependencyInjection/decorators/Inject';
 
/**
 * Represents a markdown generator for a kanban board.
 */
export default class KanbanMarkdownGenerator {
    @Inject('ILogger_', (x: ILogger_) => x.getLogger('KanbanMarkdownGenerator'))
    private readonly _logger?: ILogger;
    @Inject('IApp')
    private readonly _IApp: IApp;
    private readonly _kanbanBoard: KanbanBoard;
    private readonly _file: TFile;
    private readonly _path: string;
 
    /**
     * Creates a new instance of the markdown generator.
     * @param kanbanBoard The kanban board to generate the markdown for.
     * @param file The file to generate the markdown for.
     */
    constructor(kanbanBoard: KanbanBoard, file: TFile) {
        this._kanbanBoard = kanbanBoard;
        this._file = file;
        this._path = this._file.path;
    }
 
    /**
     * Saves the markdown file.
     * @param content The content to save.
     * @param onlyLog If true, the file is not saved but only logged.
     */
    public async saveFile(content: string, onlyLog = false): Promise<void> {
        if (onlyLog) {
            this._logger?.debug(
                `Would save file ${this._file.path} with content: \n${content}`,
            );
 
            return;
        } else {
            this._IApp.vault.modify(this._file, content);
 
            this._logger?.debug(
                `Saved file ${this._file.path} with content: \n${content}`,
            );
        }
    }
 
    /**
     * Generates the markdown file for the kanban board.
     * @returns The markdown file for the kanban board: frontmatter, markdown content, and kanban settings.
     */
    public generateMarkdownFile(): string {
        return (
            this.generateFrontmatter() +
            '\n' +
            this.generateMarkdownContent() +
            '\n' +
            this.generateKanbanSettings() +
            '\n'
        );
    }
 
    /**
     * Generates the frontmatter for the kanban board.
     * @returns The frontmatter for the kanban board.
     */
    private generateFrontmatter(): string {
        return '---\n' + this._kanbanBoard.contentFrontmatter + '\n---\n';
    }
 
    /**
     * Generates the markdown content for the kanban board.
     * @returns The markdown content for the kanban board.
     */
    private generateMarkdownContent(): string {
        const cards = this._kanbanBoard.lists.map((card) =>
            this.generateListMarkdown(card),
        );
 
        return cards.join('\n');
    }
 
    /**
     * Generates the markdown for a single List.
     * @param list The list to generate the markdown for.
     * @returns The markdown for the list.
     */
    private generateListMarkdown(list: KanbanList): string {
        const seperator = list.status === ArchivedString ? '\n***\n\n' : '\n';
 
        const heading = `## ${list.title} \n\n`;
 
        const completed = list.isCompleted ? CompletedString + '\n' : '';
 
        let cards: string[] = [];
 
        Iif (list.items && list.items.length > 0) {
            cards = list.items.map((item) => {
                let link = item.rawContent;
 
                Iif (item.linkedFile) {
                    const linktext = this._IApp.metadataCache.fileToLinktext(
                        item.linkedFile,
                        this._path,
                    );
                    link = `[[${linktext}]]`;
                }
 
                this._logger?.trace(
                    `Generating card: - [${item.isChecked ? 'x' : ' '}] ${link}`,
                );
 
                return `- [${item.isChecked ? 'x' : ' '}] ${link}`;
            });
        }
 
        const itemList = cards.join('\n') + '\n';
 
        return seperator + heading + completed + itemList;
    }
 
    /**
     * Generates the settings for the kanban board.
     * @returns The settings for the kanban board.
     */
    private generateKanbanSettings(): string {
        return this._kanbanBoard.contentKanbanSettings;
    }
}
 
Zur TypeDoc-Dokumentation