All files / src/libs/Wikilink Wikilink.ts

100% Statements 26/26
94.44% Branches 17/18
100% Functions 5/5
100% Lines 24/24

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 1151x   1x             1x                                                               12x               12x 1x   11x   11x 1x   10x 10x 10x 10x 10x                     11x   11x 1x   10x   10x                             10x       10x 6x   4x                   10x      
import { ImplementsStatic } from 'src/classes/decorators/ImplementsStatic';
import { IWikilink, IWikilink_, IWikilinkMatch } from './IWikilink';
import { Register } from '../DependencyInjection/decorators/Register';
 
/**
 * Represents a Wikilink and its parsed components.
 */
@ImplementsStatic<IWikilink_>()
@Register('IWikilink_')
export class Wikilink implements IWikilink {
    /**
     * The date of the wikilink.
     * @remarks [[2021.01.01 - file.txt|Display text]] => 2021-01-01
     */
    date: Date | undefined;
    /**
     * The basename of the wikilink.
     * @remarks [[2021.01.01 - file.txt|Display text]] => 2021.01.01 - file
     */
    basename: string | undefined;
    /**
     * The extension of the wikilink. Default is 'md'.
     * @remarks [[2021.01.01 - file.txt|Display text]] => txt
     */
    extension: string | undefined;
    /**
     * The filename of the wikilink.
     * @remarks [[2021.01.01 - file.txt|Display text]] => 2021.01.01 - file.txt
     */
    filename: string | undefined;
    /**
     * The display text of the wikilink.
     * @remarks [[2021.01.01 - file.txt|Display text]] => Display text
     */
    displayText: string | undefined;
 
    /**
     * Creates an instance of Wikilink and extracts data from the provided wikilink string.
     * @param wikilink - The wikilink string to be parsed. It can be null or undefined.
     */
    constructor(wikilink: string | null | undefined) {
        this.extractDataFromLink(wikilink);
    }
 
    /**
     * Extracts data from the provided wikilink string and populates the instance properties.
     * @param wikilink - The wikilink string to be parsed. It can be null or undefined.
     */
    private extractDataFromLink(wikilink: string | null | undefined): void {
        if (!wikilink || typeof wikilink !== 'string') {
            return;
        } else {
            const dismantledWikilink = this.dismantleWikilink(wikilink);
 
            if (!dismantledWikilink) {
                return;
            } else {
                this.date = this.parseDate(dismantledWikilink);
                this.basename = dismantledWikilink.filename;
                this.extension = this.parseFileExtension(dismantledWikilink);
                this.filename = `${this.basename}.${this.extension}`;
                this.displayText = dismantledWikilink.text;
            }
        }
    }
 
    /**
     * Dismantles the wikilink string into its components using a regular expression.
     * @param wikilink - The wikilink string to be dismantled.
     * @returns An object representing the dismantled components of the wikilink or undefined if the wikilink does not match the expected format.
     */
    private dismantleWikilink(wikilink: string): IWikilinkMatch | undefined {
        const match = wikilink.match(/\[\[(.+?)(?:\.(\w+))?(?:\|(.*))?\]\]/);
 
        if (!match) {
            return undefined;
        } else {
            const [, filename, fileExtension, text] = match;
 
            return {
                filename,
                fileExtension,
                text,
            };
        }
    }
 
    /**
     * Parses the date from the dismantled wikilink components.
     * @param wikilinkMatch - The object containing dismantled wikilink components.
     * @returns A Date object if a valid date is found, otherwise undefined.
     */
    private parseDate(wikilinkMatch: IWikilinkMatch): Date | undefined {
        // Expected date format is YYYY.MM.DD
        const dateMatch = wikilinkMatch.filename?.match(
            /(\d{4})\.(\d{2})\.(\d{2})/,
        );
 
        if (dateMatch) {
            return new Date(`${dateMatch[1]}-${dateMatch[2]}-${dateMatch[3]}`);
        } else {
            return undefined;
        }
    }
 
    /**
     * Parses the file extension from the dismantled wikilink components.
     * @param wikilinkMatch - The object containing dismantled wikilink components.
     * @returns The file extension if found, otherwise 'md' as the default extension.
     */
    parseFileExtension(wikilinkMatch: IWikilinkMatch): string | undefined {
        return wikilinkMatch.fileExtension ?? 'md';
    }
}
 
Zur TypeDoc-Dokumentation