All files / src/libs/KanbanSync KanbanParser.ts

0% Statements 0/53
0% Branches 0/35
0% Functions 0/6
0% Lines 0/53

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                                                                                                                                                                                                                                                                                                                         
import { TFile } from 'obsidian';
import type { IApp } from 'src/interfaces/IApp';
import type { ILogger_, ILogger } from 'src/interfaces/ILogger';
import { KanbanBoard, KanbanList, KanbanCard } from './KanbanModels';
import { Inject } from '../DependencyInjection/decorators/Inject';
import type { IStatusType_ } from '../StatusType/interfaces/IStatusType';
 
/**
 * The KanbanParser class is responsible for parsing a kanban board from a Kanban Markdown file.
 */
export default class KanbanParser {
    @Inject('IStatusType_')
    private readonly _IStatusType: IStatusType_;
    @Inject('IApp')
    private readonly _IApp: IApp;
    @Inject('ILogger_', (x: ILogger_) => x.getLogger('KanbanParser'))
    private readonly _logger?: ILogger;
 
    private readonly _file: TFile;
    private _isFileLoaded = false;
    private _contentFrontmatter: string;
    private _contentMarkdown: string;
    private _contentKanbanSettings: string;
 
    private _board: KanbanBoard;
 
    /**
     * Creates an instance of the KanbanParser class.
     * @param file The file to parse the kanban board from.
     */
    constructor(file: TFile) {
        this._file = file;
    }
 
    /**
     * Loads the file content and separates frontmatter, markdown content, and kanban settings.
     * @returns True if the file was loaded successfully, false otherwise.
     */
    private async loadFile(): Promise<boolean> {
        const content = await this._IApp.vault.read(this._file);
 
        const regex =
            /^---\n+([\s\S]*?)\n+---\n([\s\S]*)\n(%% kanban:settings[\s\S]*%%)$/m;
        const matches = content.match(regex);
 
        if (matches && matches.length === 4) {
            this._contentFrontmatter = matches[1];
            this._contentMarkdown = matches[2];
            this._contentKanbanSettings = matches[3];
 
            this._logger?.trace(
                `Separated frontmatter, markdown content and kanban settings for file ${this._file.path}`,
            );
            this._isFileLoaded = true;
 
            return true;
        } else {
            this._logger?.error(
                `Could not separate frontmatter,markdown content and kanban settings for file ${this._file.path}`,
            );
 
            return false;
        }
    }
 
    /**
     * Parses the kanban board from the file content.
     * @returns The parsed kanban board or undefined if the file could not be loaded.
     */
    public async parse(): Promise<KanbanBoard | undefined> {
        Iif (!this._isFileLoaded) {
            const loaded = await this.loadFile();
 
            Iif (!loaded) {
                return undefined;
            }
        }
 
        this._board = new KanbanBoard(
            this._contentFrontmatter,
            this._contentMarkdown,
            this._contentKanbanSettings,
        );
 
        return this.parseLists();
    }
 
    /**
     * Parses the cards and their items from the markdown content.
     * @returns The parsed kanban board.
     */
    private async parseLists(): Promise<KanbanBoard> {
        const regex = /^##\s(.+)((?:\n(?!##|\*\*\*|%%).*)*)/gm;
        const matches = this._contentMarkdown.matchAll(regex);
 
        for (const match of matches) {
            const title = match[1].trim();
            const content = match[2];
 
            const status =
                title === 'Archiv'
                    ? 'Archiv'
                    : this._IStatusType.getValidStatusFromTranslation(title);
 
            Iif (!status) {
                this._logger?.error(
                    `Skipping heading '${title}' as it is not a valid status or 'Archiv'`,
                );
                continue;
            }
 
            this._logger?.trace(
                `Found heading '${title}' as status '${status}'`,
            );
 
            const list = new KanbanList(title, status, content);
            await this.parseCards(list);
            this._board.addList(list);
        }
 
        return this._board;
    }
 
    /**
     * Parses the list items from the markdown content of a card.
     * @param list The card to parse the list items from.
     */
    private async parseCards(list: KanbanList): Promise<void> {
        const regex = /^- \[(x| )\]\s(\[\[(.+)\]\])(?:(?:\n(?!-).*)*)/gm;
        const matches = list.rawContent.matchAll(regex);
 
        for (const match of matches) {
            const checked = match[1] === 'x';
            const itemContent = match[2];
            const linkedFilePath = match[3];
 
            const linkedFile = this._IApp.metadataCache.getFirstLinkpathDest(
                linkedFilePath,
                this._file.path,
            );
 
            if (!linkedFile) {
                this._logger?.warn(
                    `No file found for list item '${itemContent}' linked to '${linkedFilePath}`,
                );
            } else {
                this._logger?.trace(
                    `Found list item '${itemContent}' linked to '${linkedFile.path}'`,
                );
            }
 
            const card = new KanbanCard(checked, linkedFile, itemContent);
            list.addCard(card);
        }
    }
}
 
Zur TypeDoc-Dokumentation