All files / src parser.ts

93.02% Statements 120/129
83.33% Branches 20/24
100% Functions 16/16
92.91% Lines 118/127

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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 3091x 1x     1x   1x 1x   1x 1x       1x           1x                                 1x 1x 1x 1x   1x       28x     28x 28x 28x 28x       56x                   21x 21x 39x   21x       23x 41x 41x 41x   1x           31x 8x     23x   22x 22x 40x     22x 22x   22x 1x                 21x 21x                     21x       29x 29x         29x 36x       36x 36x     29x       29x             29x 1x     29x 5x     29x 2x 2x 1x   1x       28x 2x 2x     27x                 32x   32x 32x   32x 6x     26x 248x 26x   222x       26x       26x 26x     32x 26x   104x 104x 104x 96x     8x     8x       8x 8x   8x     26x     32x 32x 6x     26x 26x   26x       32x       32x   32x 32x 31x   31x 31x       31x 48x 7x   41x       31x 30x 1x     29x 29x     27x   27x           27x 27x             27x         27x         15x 15x 15x        
import joplin from "api";
import * as Handlebars from "handlebars/dist/handlebars";
import { Logger } from "./logger";
import { DateAndTimeUtils } from "./utils/dateAndTime";
import { doesFolderExist } from "./utils/folders";
import { Note } from "./utils/templates";
import { notEmpty } from "./utils/typescript";
import { getVariableFromDefinition } from "./variables/parser";
import { CustomVariable } from "./variables/types/base";
import { setTemplateVariablesView } from "./views/templateVariables";
import { HelperFactory } from "./helpers";
 
// Unique placeholder that Handlebars will output for {{ note_id }}.
// actions.ts replaces this with the real note ID after the note is created.
export const NOTE_ID_PLACEHOLDER = "__JOPLIN_TEMPLATE_NOTE_ID__";
 
// Can't use import for this library because the types in the library
// are declared incorrectly which result in typescript errors.
// Reference -> https://github.com/jxson/front-matter/issues/76
// eslint-disable-next-line @typescript-eslint/no-var-requires
const frontmatter = require("front-matter");
 
export interface NewNote {
    title: string;
    tags: string[];
    body: string;
    folder: string | null;
    todo_due: number | null;
}
 
interface NoteMetadata {
    title: string;
    tags: string[];
    folder: string | null;
    todo_due: number | null;
}
 
const NOTE_TITLE_VARIABLE_NAME = "template_title";
const NOTE_TAGS_VARIABLE_NAME = "template_tags";
const NOTE_FOLDER_VARIABLE_NAME = "template_notebook";
const TODO_DUE_VARIABLE_NAME = "template_todo_alarm";
 
export class Parser {
    private utils: DateAndTimeUtils;
    private dialog: string;
    private logger: Logger;
    private specialVariableNames = [NOTE_TITLE_VARIABLE_NAME, NOTE_TAGS_VARIABLE_NAME, NOTE_FOLDER_VARIABLE_NAME, TODO_DUE_VARIABLE_NAME];
 
    constructor(dateAndTimeUtils: DateAndTimeUtils, dialogViewHandle: string, logger: Logger) {
        this.utils = dateAndTimeUtils;
        this.dialog = dialogViewHandle;
        this.logger = logger;
        HelperFactory.registerHelpers(this.utils);
    }
 
    private getDefaultContext() {
        return {
            date: this.utils.getCurrentTime(this.utils.getDateFormat()),
            time: this.utils.getCurrentTime(this.utils.getTimeFormat()),
            datetime: this.utils.getCurrentTime(),
            bowm: this.utils.formatMsToLocal(this.utils.getBeginningOfWeek(1), this.utils.getDateFormat()),
            bows: this.utils.formatMsToLocal(this.utils.getBeginningOfWeek(0), this.utils.getDateFormat())
        }
    }
 
    private mapUserResponseToVariables(variableObjects: Record<string, CustomVariable>, response: Record<string, string>) {
        const variableValues = {};
        Object.keys(response).map(variableName => {
            variableValues[variableName] = variableObjects[variableName].processInput(response[variableName], this.utils);
        });
        return variableValues;
    }
 
    private checkVariableNames(variableNames: string[]) {
        for (const variable of variableNames) {
            try {
                const compiledBody = Handlebars.compile(`{{ ${variable} }}`);
                compiledBody({});
            } catch {
                throw new Error(`Variable name "${variable}" is invalid.\n\nPlease avoid using special characters ("@", ",", "#", "+", "(", etc.) or spaces in variable names. However, you can use "_" in variable names.`);
            }
        }
    }
 
    private async getVariableInputs(title: string, variables: Record<string, unknown>) {
        if (Object.keys(variables).length == 0) {
            return {};
        }
 
        this.checkVariableNames(Object.keys(variables));
 
        const variableObjects: Record<string, CustomVariable> = {};
        Object.keys(variables).map(variableName => {
            variableObjects[variableName] = getVariableFromDefinition(variableName, variables[variableName]);
        });
 
        await setTemplateVariablesView(this.dialog, title, variableObjects);
        const dialogResponse = (await joplin.views.dialogs.open(this.dialog));
 
        if (dialogResponse.id === "cancel") {
            return null;
        }
 
        let userResponse;
 
        // There's a try catch block here because a user experienced an error
        // due to the following line. I've added a try catch block to log the
        // necessary info to find the root cause of the error in case it happens again.
        // Reference -> https://github.com/joplin/plugin-templates/issues/6
        try {
            userResponse = dialogResponse.formData.variables;
        } catch (err) {
            console.error("Template variables form was not able to load properly.", err);
            console.error("DEBUG INFO", variables, dialogResponse, title);
 
            const message = (`Template variables form was not able to load properly.\n${err}\nDEBUG INFO\nvariables: ${JSON.stringify(variables)}\ndialogResponse: ${JSON.stringify(dialogResponse)}\ntitle: ${JSON.stringify(title)}`);
            this.logger.log(message);
 
            throw new Error(err);
        }
 
        return this.mapUserResponseToVariables(variableObjects, userResponse);
    }
 
    private parseSpecialVariables(specialVariables: Record<string, unknown>, customVariableInputs: Record<string, string>) {
        const res: Record<string, string> = {};
        const context = {
            ...this.getDefaultContext(),
            ...customVariableInputs
        };
 
        for (const variable of Object.keys(specialVariables)) {
            Iif (typeof specialVariables[variable] !== "string") {
                throw new Error(`${variable} should be a string, found ${typeof specialVariables[variable]}.`);
            }
 
            const compiledText = Handlebars.compile(specialVariables[variable]);
            res[variable] = compiledText(context);
        }
 
        return res;
    }
 
    private async getNoteMetadata(parsedSpecialVariables: Record<string, string>): Promise<NoteMetadata> {
        const meta: NoteMetadata = {
            title: parsedSpecialVariables.fallback_note_title,
            tags: [],
            folder: null,
            todo_due: null,
        };
 
        if (NOTE_TITLE_VARIABLE_NAME in parsedSpecialVariables) {
            meta.title = parsedSpecialVariables[NOTE_TITLE_VARIABLE_NAME];
        }
 
        if (NOTE_TAGS_VARIABLE_NAME in parsedSpecialVariables) {
            meta.tags = parsedSpecialVariables[NOTE_TAGS_VARIABLE_NAME].split(",").map(t => t.trim()).filter(t => t !== "");
        }
 
        if (NOTE_FOLDER_VARIABLE_NAME in parsedSpecialVariables) {
            const folderId = parsedSpecialVariables[NOTE_FOLDER_VARIABLE_NAME].trim();
            if (await doesFolderExist(folderId)) {
                meta.folder = folderId;
            } else {
                throw new Error(`There is no notebook with ID: ${folderId}`);
            }
        }
 
        if (TODO_DUE_VARIABLE_NAME in parsedSpecialVariables) {
            const rawTodoDue = parsedSpecialVariables[TODO_DUE_VARIABLE_NAME].trim()
            meta.todo_due = this.utils.formatLocalToJoplinCompatibleUnixTime(rawTodoDue, this.utils.getDateTimeFormat());
        }
 
        return meta;
    }
 
    private preProcessTemplateBody(templateBody: string) {
        /**
         * This function is supposed to do the following preprocessing.
         * 1. Wrap the value of template_title and template_tags with double quotes if it isn't already.
         */
 
        templateBody = templateBody.trimStart();
 
        const getVariableDefinitionsBlock = () => {
            const allLines = templateBody.split("\n");
 
            if (allLines.length === 0 || allLines[0] !== "---") {
                return null;
            }
 
            const allMatchesAfterFirstMatch = allLines.map((val, index) => {
                if (index && val === "---") {
                    return index;
                } else {
                    return null;
                }
            }).filter(notEmpty);
 
            Iif (!allMatchesAfterFirstMatch.length) {
                return null;
            }
 
            const blockEndIndex = allMatchesAfterFirstMatch[0];
            return allLines.slice(0, blockEndIndex + 1).join("\n");
        }
 
        const wrapInQuotes = (definitionsBlock: string, properties: string[]) => {
            for (const prop of properties) {
                // eslint-disable-next-line no-useless-escape
                const pattern = new RegExp(`^[^\S\n]*${prop}[^\S\n]*:.*`, "gm");
                const matches = definitionsBlock.match(pattern);
                if (!matches) {
                    continue;
                }
 
                const firstMatch = matches[0];
 
                // Don't do anything if it already contains a double quote
                Iif (firstMatch.indexOf("\"") !== -1) {
                    continue;
                }
 
                const splitVal = firstMatch.split(":");
                const wrappedString = `${splitVal[0]}: "${splitVal.slice(1).join(":").trim()}"`;
 
                definitionsBlock = definitionsBlock.replace(firstMatch, wrappedString);
            }
 
            return definitionsBlock;
        }
 
        let variableDefinitionsBlock = getVariableDefinitionsBlock();
        if (!variableDefinitionsBlock) {
            return templateBody;
        }
 
        const templateContentBlock = templateBody.substr(variableDefinitionsBlock.length, templateBody.length - variableDefinitionsBlock.length);
        variableDefinitionsBlock = wrapInQuotes(variableDefinitionsBlock, this.specialVariableNames);
 
        return `${variableDefinitionsBlock}${templateContentBlock}`;
    }
 
    public async parseTemplate(template: Note | null): Promise<NewNote | null> {
        Iif (!template) {
            return null;
        }
 
        template.body = this.preProcessTemplateBody(template.body);
 
        try {
            const processedTemplate = frontmatter(template.body);
            const templateVariables = processedTemplate.attributes;
 
            const customVariables = {};
            const specialVariables = {
                fallback_note_title: template.title
            };
 
            for (const variable of Object.keys(templateVariables)) {
                if (this.specialVariableNames.includes(variable)) {
                    specialVariables[variable] = templateVariables[variable];
                } else {
                    customVariables[variable] = templateVariables[variable];
                }
            }
 
            const variableInputs = await this.getVariableInputs(template.title, customVariables);
            if (variableInputs === null) {
                return null;
            }
 
            const parsedSpecialVariables = this.parseSpecialVariables(specialVariables, variableInputs);
            const newNoteMeta = await this.getNoteMetadata(parsedSpecialVariables);
 
            // Remove the fallback property because it's not actually a variable defined by the user.
            delete parsedSpecialVariables.fallback_note_title;
 
            const context = {
                ...this.getDefaultContext(),
                ...variableInputs,
                ...parsedSpecialVariables
            };
 
            const templateBody = processedTemplate.body;
            const compiledTemplate = Handlebars.compile(templateBody);
 
            // note_id is intentionally excluded from the shared context above
            // because it is only meaningful in the note body (it resolves to a
            // placeholder that actions.ts swaps for the real ID after creation).
            // Including it in the title/tags context would leave the placeholder
            // string unreplaced there.
            const bodyContext = {
                ...context,
                note_id: NOTE_ID_PLACEHOLDER
            };
 
            return {
                ...newNoteMeta,
                body: compiledTemplate(bodyContext)
            };
        } catch (err) {
            console.error("Error in parsing template.", err);
            await joplin.views.dialogs.showMessageBox(`There was an error parsing this template, please review it and try again.\n\n${err}`);
            return null;
        }
    }
}